<?php 
if(isset($_GET['id']) && $_GET['id'] > 0){
    $qry = $conn->query("SELECT e.*, CONCAT(e.lastname,', ', e.firstname,COALESCE(concat(' ',e.middlename),'')) as `name`, c.name as class from `enroll_list` e inner join class_list c on e.class_id = c.id where e.id = '{$_GET['id']}'");
    if($qry->num_rows > 0){
        foreach($qry->fetch_array() as $k => $v){
            if(!is_numeric($k))
                $$k = $v;
        }
    }
}
?>
<div class="content bg-gradient-primary py-5 px-4">
    <h3 class="font-weight-bolder">Enrollee Details</h3>
</div>
<div class="row mt-n5 justify-content-center">
    <div class="col-lg-8 col-md-10 col-sm-12 col-xs-12">
        <div class="card card-outline card-primary rounded-0 shadow">
            <div class="card-body">
                <div class="container-fluid" id="printout">
                    <fieldset>
                        <div class="row mb-3">
                            <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
                                <label for="" class="control-label text-muted">Enrollee Name</label>
                                <div class="pl-4 font-weight-bolder"><?= isset($name) ? $name : '' ?></div>
                            </div>
                        </div>
                        <div class="row mb-3">
                            <div class="col-lg-4 col-md-4 col-sm-12 col-xs-12">
                                <label for="" class="control-label text-muted">Gender</label>
                                <div class="pl-4 font-weight-bolder"><?= isset($gender) ? $gender : '' ?></div>
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-12 col-xs-12">
                                <label for="" class="control-label text-muted">Birthday</label>
                                <div class="pl-4 font-weight-bolder"><?= isset($dob) && !empty($dob) ? date("F d, Y", strtotime($dob)) : 'N/A' ?></div>
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-12 col-xs-12">
                                <label for="" class="control-label text-muted">Age</label>
                                <div class="pl-4 font-weight-bolder"><?= isset($dob) && !empty($dob) ? (floor((strtotime(date('Y-m-d')) - strtotime($dob)) / (24*60*60*365.25))) : 'N/A' ?></div>
                            </div>
                        </div>
                        <div class="row mb-3">
                            <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
                                <label for="" class="control-label text-muted">Contact #</label>
                                <div class="pl-4 font-weight-bolder"><?= isset($contact) ? $contact : '' ?></div>
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
                                <label for="" class="control-label text-muted">Email</label>
                                <div class="pl-4 font-weight-bolder"><?= isset($email) && !empty($email) ? $email : 'N/A' ?></div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
                                <label for="" class="control-label text-muted">Class Wish to Enroll</label>
                                <div class="pl-4"><?= isset($class) && !empty($class) ? $class : '' ?></div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
                                <label for="" class="control-label text-muted">Address</label>
                                <div class="pl-4"><?= isset($address) ? (str_replace(["\n\r", "\n", "\r"], '<br>', htmlspecialchars_decode($address))) : '' ?></div>
                            </div>
                        </div>
                        <?php if(isset($guardian_name) && !empty($guardian_name)): ?>
                        <fieldset class="mt-3">
                            <div class="row mb-3">
                                <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
                                    <label for="" class="control-label text-muted">Guardian/Parent Name</label>
                                    <div class="pl-4 font-weight-bolder"><?= isset($guardian_name) ? $guardian_name : '' ?></div>
                                </div>
                                <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
                                    <label for="" class="control-label text-muted">Guardian Contact #</label>
                                    <div class="pl-4 font-weight-bolder"><?= isset($guardian_contact) && !empty($guardian_contact) ? $guardian_contact : 'N/A' ?></div>
                                </div>
                                <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
                                    <label for="" class="control-label text-muted">Enrollee Relation with the Guardian</label>
                                    <div class="pl-4 font-weight-bolder"><?= isset($relation) && !empty($relation) ? $relation : 'N/A' ?></div>
                                </div>
                            </div>
                        </fieldset>
                        <?php endif; ?>
                        <div class="row mb-3">
                            <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
                                <label for="" class="control-label text-muted">Status</label>
                                <div class="pl-4 font-weight-bolder">
                                    <?php 
                                        $status = isset($status) ? $status : '';
                                        switch($status){
                                            case 0:
                                                echo '<span class="badge badge-light bg-gradient-light border px-3 rounded-pill">Pending</span>';
                                                break;
                                            case 1:
                                                echo '<span class="badge badge-primary bg-gradient-primary px-3 rounded-pill">Confirmed</span>';
                                                break;
                                            case 2:
                                                echo '<span class="badge badge-danger bg-gradient-danger px-3 rounded-pill">Cancelled</span>';
                                                break;
                                            default:
                                                echo '<span class="badge badge-secondary bg-gradient-secondary px-3 rounded-pill">N/A</span>';
                                                break;
                                        }
                                    ?>
                                </div>
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
                                <label for="" class="control-label text-muted">Date Submitted</label>
                                <div class="pl-4 font-weight-bolder"><?= isset($date_created) ? date("F d, Y", strtotime($date_created)) : '' ?></div>
                            </div>
                        </div>
                    </fieldset>
                </div>
            </div>
            <div class="card-footer py-1 text-center">
                <button id="delete_enroll" class="btn btn-danger btn-flat bg-gradient-danger btn-sm" type="button"><i class="fa fa-trash"></i> Delete</button>
                <button id="update_status" class="btn btn-primary btn-flat bg-gradient-primary btn-sm" type="button"><i class="fa fa-pen"></i> Updated Status</button>
                <button id="print" class="btn btn-success btn-flat bg-gradient-success btn-sm" type="button"><i class="fa fa-print"></i> Print</button>
                <a class="btn btn-light btn-flat bg-gradient-light border btn-sm text-dark" href="./?page=enrollees"><i class="fa fa-angle-left"></i> Back to List</a>
            </div>
        </div>
    </div>
</div>
<noscript id="print-header">
    <div>
        <div class="d-flex w-100 align-items-center">
            <div class="col-2 text-center">
                <img src="<?= validate_image($_settings->info('logo')) ?>" alt="" class="rounded-circle border" style="width: 5em;height: 5em;object-fit:cover;object-position:center center">
            </div>
            <div class="col-8">
                <div style="line-height:1em">
                    <h3 class="text-center font-weight-bold mb-0"><large><?= $_settings->info('name') ?></large></h3>
                    <h3 class="text-center font-weight-bold mb-0"><large>Enrollee Details</large></h3>
                </div>
            </div>
        </div>
       
        <hr>
    </div>
</noscript>
<script>
     function print_t(){
        var h = $('head').clone()
        var p = $('#printout').clone()
        var ph = $($('noscript#print-header').html()).clone()
        h.find('title').text("order Details - Print View")
        var nw = window.open("", "_blank", "width="+($(window).width() * .8)+",left="+($(window).width() * .1)+",height="+($(window).height() * .8)+",top="+($(window).height() * .1))
            nw.document.querySelector('head').innerHTML = h.html()
            nw.document.querySelector('body').innerHTML = ph[0].outerHTML
            nw.document.querySelector('body').innerHTML += p[0].outerHTML
            nw.document.close()
            start_loader()
            setTimeout(() => {
                nw.print()
                setTimeout(() => {
                    nw.close()
                    end_loader()
                }, 200);
            }, 300);
    }
    $(function(){
        $('#print').click(function(){
            print_t()
        })
        $('#delete_enroll').click(function(){
			_conf("Are you sure to delete this enroll permanently?","delete_enroll",['<?= isset($id) ? $id : '' ?>'])
		})
        $('#update_status').click(function(){
			uni_modal("Update Status","enrollees/update_status.php?id=<?= isset($id) ? $id : '' ?>")
		})
    })
    function delete_enroll($id){
		start_loader();
		$.ajax({
			url:_base_url_+"classes/Master.php?f=delete_enroll",
			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=enrollees");
				}else{
					alert_toast("An error occured.",'error');
					end_loader();
				}
			}
		})
	}
</script>