<?php

if(isset($_GET['id']) && $_GET['id'] > 0){
    $qry = $conn->query("SELECT * FROM application_list where id = '{$_GET['id']}'");
    if($qry->num_rows > 0){
        foreach($qry->fetch_assoc() as $k => $v){
            $$k=$v;
        }
		if(isset($individual_id)){
			$user_qry = $conn->query("SELECT *, concat(lastname, ', ', firstname, ' ', coalesce(middlename,'')) as `name` from `individual_list` where id = '{$individual_id}' ");
			if($qry->num_rows > 0){
				foreach($user_qry->fetch_assoc() as $k => $v){
					$inv[$k]=$v;
				}
				$meta_qry = $conn->query("SELECT * FROM `individual_meta` where `individual_id` = '{$individual_id}' ");
				while($row = $meta_qry->fetch_assoc()){
					$inv[$row['meta_field']] = $row['meta_value'];
				}
			}
		}
    }else{
        echo '<script> alert("Application ID is invalid."); location.replace("./?page=applications");</script>';
    }
}else{
    echo '<script> alert("Application ID is required."); location.replace("./?page=applications");</script>';
}
?>
<div class="mx-0 py-5 px-3 mx-ns-4 bg-gradient-primary">
	<h3><b>Application Details</b></h3>
</div>
<style>
    #indi-img{
        width:6em;
        height:6em;
        object-fit:cover;
        object-position:center center;
    }
    #vaccine-img{
        width:100%;
        height:15em;
        object-fit:scale-down;
        object-position:center center;
    }
</style>
<div class="row justify-content-center" style="margin-top:-2em;">
	<div class="col-lg-10 col-md-11 col-sm-11 col-xs-11">
		<div class="card rounded-0 shadow">
			<div class="card-body">
				<div class="container-fluid">
					<div class="container-fluid">
						<div class="card roudned-0 shadow m-3">
							<div class="card-body">
								<div class="row align-items-center">
									<div class="col-lg-4 col-md-5 col-sm-12 col-xs-12">
										<center>
											<img src="<?= validate_image(isset($inv['avatar']) ? $inv['avatar'] : '') ?>" alt="" class="img-thumbnail rounded-circle" id="indi-img">
										</center>
									</div>
									<div class="col-lg-8 col-md-7 col-sm-12 col-xs-12">
										<div class="d-flex w-100">
											<div class="col-auto text-muted">Name:</div>
											<div class="col-auto flex-shrink-1 flex-grow-1 font-weight-bolder"><?= isset($inv['name']) ? $inv['name'] : '' ?></div>
										</div>
										<div class="d-flex w-100">
											<div class="col-auto text-muted">Gender:</div>
											<div class="col-auto flex-shrink-1 flex-grow-1 font-weight-bolder"><?= isset($inv['gender']) ? $inv['gender'] : '' ?></div>
										</div>
										<div class="d-flex w-100">
											<div class="col-auto text-muted">Email:</div>
											<div class="col-auto flex-shrink-1 flex-grow-1 font-weight-bolder"><?= isset($inv['email']) ? $inv['email'] : '' ?></div>
										</div>
										<div class="d-flex w-100">
											<div class="col-auto text-muted">Contact #:</div>
											<div class="col-auto flex-shrink-1 flex-grow-1 font-weight-bolder"><?= isset($inv['contact']) ? $inv['contact'] : '' ?></div>
										</div>
										<div class="d-flex w-100">
											<div class="col-auto text-muted">Address:</div>
											<div class="col-auto flex-shrink-1 flex-grow-1 font-weight-bolder"><?= isset($inv['address']) ? $inv['address'] : '' ?></div>
										</div>
										<div class="d-flex w-100">
											<div class="col-auto text-muted">Status:</div>
											<div class="col-auto flex-shrink-1 flex-grow-1 font-weight-bolder">
												<?php
													$inv['status'] = isset($inv['status']) ? $inv['status'] : 0;
													switch($inv['status']){
														case 0:
															echo '<span class="badge badge-secondary bg-gradient-secondary text-sm px-3 rounded-pill"><i class="fa fa-circle"></i> Waiting for Verification</span>';
															break;
														case 1:
															echo '<span class="badge badge-primary bg-gradient-primary text-sm px-3 rounded-pill"><i class="fa fa-check"></i> Verified</span>';
															break;
														case 2:
															echo '<span class="badge badge-danger bg-gradient-danger text-sm px-3 rounded-pill"><i class="fa fa-times"></i> Denied</span>';
															break;
														case 3:
															echo '<span class="badge badge-maroon bg-gradient-maroon text-sm px-3 rounded-pill"><i class="fa fa-exclamation-circle"></i> Blocked</span>';
															break;
													}
													?>
											</div>
										</div>
									</div>
								</div>
							</div>
						</div>
						<div class="row">
							<div class="col-lg-4 col-md-5 col-sm-12 col-xs-12 py-3 font-weight-bolder border">Pass Code</div>
							<div class="col-lg-8 col-md-7 col-sm-12 col-xs-12 py-3 border"><?= isset($code) ? $code : '' ?></div>
							<div class="col-lg-4 col-md-5 col-sm-12 col-xs-12 py-3 font-weight-bolder border">Location</div>
							<div class="col-lg-8 col-md-7 col-sm-12 col-xs-12 py-3 border"><?= isset($location) ? $location : '' ?></div>
							<div class="col-lg-4 col-md-5 col-sm-12 col-xs-12 py-3 font-weight-bolder border">Destination</div>
							<div class="col-lg-8 col-md-7 col-sm-12 col-xs-12 py-3 border"><?= isset($destination) ? $destination : '' ?></div>
							<div class="col-lg-4 col-md-5 col-sm-12 col-xs-12 py-3 font-weight-bolder border">Travelling Date From</div>
							<div class="col-lg-8 col-md-7 col-sm-12 col-xs-12 py-3 border"><?= isset($date_from) ? date("F d, Y", strtotime($date_from)) : '' ?></div>
							<div class="col-lg-4 col-md-5 col-sm-12 col-xs-12 py-3 font-weight-bolder border">Travelling Date To</div>
							<div class="col-lg-8 col-md-7 col-sm-12 col-xs-12 py-3 border"><?= isset($date_to) ? date("F d, Y", strtotime($date_to)) : '' ?></div>
							<div class="clear-fix my-1"></div>
							<div class="col-lg-4 col-md-5 col-sm-12 col-xs-12 py-3 font-weight-bolder border">Reason of Travelling</div>
							<div class="col-lg-8 col-md-7 col-sm-12 col-xs-12 py-3 border" ><?= isset($reason) ? $reason : '' ?></div>
							<div class="clear-fix my-1"></div>
							<div class="col-lg-4 col-md-5 col-sm-12 col-xs-12 py-3 font-weight-bolder border">Status</div>
							<div class="col-lg-8 col-md-7 col-sm-12 col-xs-12 py-3 border">
								<?php
									$status = isset($status) ? $status : 0 ;
									switch($status){
										case 0:
											echo '<span class="badge badge-secondary bg-gradient-secondary text-sm px-3 rounded-pill">Pending</span>';
											break;
										case 1:
											echo '<span class="badge badge-primary bg-gradient-primary text-sm px-3 rounded-pill">Approved</span>';
											break;
										case 2:
											echo '<span class="badge badge-danger bg-gradient-danger text-sm px-3 rounded-pill">Denied</span>';
											break;
									}
								?>
							</div>
						</div>
					</div>
				</div>
			</div>
			<div class="card-footer py-1 text-center">
				<a class="btn btn-primary btn-sm bg-gradient-primary rounded-0" href="javascript:void(0)" id="update_status"><i class="fa fa-edit"></i> Update Status</a>
				<button class="btn btn-danger btn-sm bg-gradient-danger rounded-0" type="button" id="delete-data"><i class="fa fa-trash"></i> Delete</button>
				<a class="btn btn-light btn-sm bg-gradient-light border rounded-0" href="./?page=applications"><i class="fa fa-angle-left"></i> Back to List</a>
			</div>
		</div>
	</div>
</div>
<script>
	
	$(document).ready(function(){
		$('#update_status').click(function(){
            uni_modal("<i class='fa fa-edit'></i> Update Status","applications/update_status.php?id=<?= isset($id) ? $id : '' ?>")
        })
        $('#delete-data').click(function(){
			_conf("Are you sure to delete this application permanently?","delete_application",['<?= isset($id) ? $id : '' ?>'])
		})
	})
    function delete_application($id){
		start_loader();
		$.ajax({
			url:_base_url_+"classes/Master.php?f=delete_application",
			method:"POST",
			data:{id: $id},
			dataType:"json",
			error:err=>{
				console.log(err)
				alert_toast("An error occured.",'error');
				end_loader();
			},
			success:function(resp){
				if(typeof resp== 'object' && resp.status == 'success'){
					location.replace("./?page=applications");
				}else{
					alert_toast("An error occured.",'error');
					end_loader();
				}
			}
		})
	}
</script>