<?php
$query = "SELECT * FROM students WHERE id = '{$_SESSION['slip_id']}'";
$run_query = mysqli_query($connection, $query);

if (mysqli_num_rows($run_query) == 1) {
    while ($result = mysqli_fetch_assoc($run_query)) {
        $student_id = $result['id'];
        $student_gender = ucfirst($result['gender']);
        $surname = ucfirst($result['firstname']);
        $lastname = ucfirst($result['lastname']);
        $othername = ucfirst($result['othername']);
        $student_class = $result['class'];
        $reg_number = $result['reg_number']; 
        $student_dob = $result['dob']; // e.g., "21/5/2011"
        $weight = $result['weight'];
        $height = $result['height'];
        $club = $result['club'];
        $level = $result['level'];
        $health_conditions = $result['health_conditions'];
        $student_address = ucwords($result['address']);
        $student_state = ucwords($result['state']);
        $student_lga = ucwords($result['lga']);
        $student_nationality = ucwords($result['nationality']);
        $guardian_name = $result['guardian_name'];
        $guardian_occupation = $result['guardian_occupation'];
        $guardian_phone = $result['guardian_phone'];
        $student_password = $result['gen_password'];
        $student_passport = "./" . $result['passport'];
        $student_reg_date = $result['date_of_reg'];

        // --- ✅ Calculate age safely ---
$dob_object = DateTime::createFromFormat('d/m/Y', $student_dob);
$today = new DateTime();

if ($dob_object && $dob_object->format('d/m/Y') === $student_dob) {
    $age_diff = $dob_object->diff($today);
    $student_age = $age_diff->y . " year" . ($age_diff->y !== 1 ? "s" : "") . ", " . 
                   $age_diff->m . " month" . ($age_diff->m !== 1 ? "s" : "") . ", " . 
                   $age_diff->d . " day" . ($age_diff->d !== 1 ? "s" : "");
} else {
    $student_age = "Invalid Date of Birth";
}

    }
}
?>

<?php
	$query = " SELECT * FROM `administratives` ";
	$run_query = mysqli_query($connection, $query);
	
	if(mysqli_num_rows($run_query) == 1){
		while($result = mysqli_fetch_assoc($run_query)){
			$school_logo = "./" . $result['school_logo'];
			$school_name = $result['school_name'];
			$school_address = $result['school_address'];
			$school_motto = $result['school_motto'];
		}
	}
?>

<div class='panel panel-primary'>
    <div class='panel-body'>
        <div class='row'>
            <div class='col-xs-2 col-sm-2 col-md-2 text-center'>
                <img class='img-rounded img-responsive' src='../<?php echo $school_logo; ?>' alt='logo'/>
            </div>

            <div class='col-xs-8 col-sm-8 col-md-8 text-center'>
                <h3><?php echo $school_name; ?></h3>
                <p class='text-danger'>
                <?php echo $school_motto; ?><br/>
                </p>
                <p class='text-danger'>
                <?php echo $school_address; ?><br/>
                </p>
            </div>

            <div class='col-xs-2 col-sm-2 col-md-2 text-center text-responsive'> 
                <img src='../<?php echo $student_passport; ?>' class='img-rounded img-responsive' alt='student passport' />
            </div>
        </div>
        <hr />

		<div class='row'>
            <div class='col-xs-4 col-md-4'>
                <legend>Bio-Data</legend>
                <p><b>First Name:</b> <?php echo $surname; ?></p>
                <p><b>Other Name:</b> <?php echo $othername; ?></p>
                <p><b>Last Name:</b> <?php echo $lastname; ?></p>
                <p><b>Sex:</b> <?php echo $student_gender; ?></p>
                <p><b>Height:</b> <?php echo $height; ?>ft</p>
				<p><b>Weight:</b> <?php echo $weight; ?>kg</p>
                <p><b>LGA:</b> <?php echo $student_lga; ?></p>
                <p><b>State:</b> <?php echo $student_state; ?></p>
                <p><b>Nationality:</b> <?php echo $student_nationality; ?></p>
				<p><b>Club:</b> <?php echo $club; ?></p>
				<p><b>Health Conditions:</b> <?php echo $health_conditions; ?></p>
            </div>
            <div class='col-xs-4 col-md-4'>
                <legend>Birthday</legend>
                <p><b>Day:</b> <?php echo $student_dob; ?></p>
                <p><b>Age:</b> <?php echo $student_age; ?></p>                
            </div>
            <div class='col-xs-4 col-md-4'>
                <legend>Guardian & Contact</legend>
		        <p><b>Name:</b> <?php echo $guardian_name; ?></p>
                <p><b>Address:</b> <?php echo $student_address; ?></p>
                <p><b>Phone No:</b> <?php echo $guardian_phone; ?></p>
                <p><b>Occupation:</b> <?php echo $guardian_occupation; ?></p>
            </div>
        </div>
        <br />

        <div class='row'>
            <div class='col-xs-4 col-md-4'>
                <legend>Academics</legend>
                <p><b>Registration No:</b> <?php echo $reg_number; ?></p>                
                <p><b>Class:</b> <?php echo $student_class; ?></p>
                <p><b>Level:</b> <?php echo $level; ?></p>
            </div>
            <div class='col-xs-4 col-md-4'>
                <legend>Registered On</legend>
                <p><b>Date:</b> <?php echo $student_reg_date; ?></p>

            </div>
        </div>
        <br/>
        <hr/>

        <div class='row'>
            <div class='col-xs-12 col-md-12 text-center'>
                <em>
                    <br/>
                    <button type='button' class='btn btn-primary noprint' onclick='window.print()' value='print a div!'><i class='fa fa-print'></i> Print</button>
                    <a class='noprint btn btn-danger' href='view_students'><i class='fa fa-remove'></i> Close Slip</a> 
                </em>
            </div>
        </div>

    </div>
</div>
