<?php
	ob_start();
    session_start();
?>
<?php
	require_once("../../public/connection.php");
	require_once("../../public/functions.php");
?>
<?php
	//////////////// DEFAULT NULL VALUES ///////////////
	$error = array();
	$date = date('d/M/Y');
	$select = "--select--";
	$time = date('h:m:s');
?>
<?php
	$query = " SELECT * FROM `results1` WHERE `id` = '{$_SESSION['result']}' ";
	$run_query = mysqli_query($connection, $query);
	
	if(mysqli_num_rows($run_query) == 1){
		while($result = mysqli_fetch_assoc($run_query)){
			$result_name = $result['name'];
			$result_reg_number = $result['reg_number'];
			$result_class = $result['class'];
			$result_term = $result['term'];
			$result_session = $result['session'];
		}
	}
?>
<?php
	$query = " SELECT * FROM `students` WHERE `reg_number` = '{$result_reg_number}' ";
	$run_query = mysqli_query($connection, $query);
	
	if(mysqli_num_rows($run_query) == 1){
		while($result = mysqli_fetch_assoc($run_query)){
			$result_passport = $result['passport'];
            $result_sex = $result['gender'];
			$result_height = $result['height'];
			$result_weight = $result['weight'];
			$result_club = $result['club'];
		}
	}
?>
<?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_motto'];
			$school_stamp = $result['school_stamp'];
		}
	}
?>
<?php
	$query = " SELECT * FROM `dates` ";
	$run_query = mysqli_query($connection, $query);
	
	if(mysqli_num_rows($run_query) == 1){
		while($result = mysqli_fetch_assoc($run_query)){
			$term_ended = $result['end_of_term'];
			$next_term_starts = $result['next_term_begins'];
		}
	}
?>
<?php
    ///POST ACTION TO GET BEHAVIORAL ANALYSIS
    $query = " SELECT * FROM `behavioral` WHERE `name` = '{$result_name}' ";
    $run_query = mysqli_query($connection, $query);

	if (mysqli_num_rows($run_query) > 0) {
		while ($row = mysqli_fetch_assoc($run_query)) {
			$puntuality = $row['puntuality'];
			$neatness = $row['neatness'];
			$relationship_with_others = $row['relationship_with_others'];
			$regards_for_authority = $row['regards_for_authority'];
			$attitude_towards_academics = $row['attitude_towards_academics'];
			$honesty = $row['honesty'];
			$initiative = $row['initiative'];
			$form_master_or_mistress = $row['form_master_or_mistress'];
		}
	} else {
		// Optionally set default values or handle the case where no results are returned
		$puntuality = $neatness = $relationship_with_others = $regards_for_authority = $attitude_towards_academics = $honesty = $initiative = $form_master_or_mistress = "N/A";
	}

?>

<?php
    // Query to get the 'present' and 'absent' values for the student
    $query = "SELECT 
                COALESCE(SUM(CASE WHEN roll_call = 'Present' THEN 1 ELSE 0 END), 0) AS present,
                COALESCE(SUM(CASE WHEN roll_call = 'Absent' THEN 1 ELSE 0 END), 0) AS absent
              FROM `student_attendance`
              WHERE `reg_number` = '{$result_reg_number}' 
              AND `term` = '{$result_term}' 
              AND `session` = '{$result_session}' 
              AND `class` = '{$result_class}'";
              
    $run_query = mysqli_query($connection, $query);
    
    // Check if the query returned any rows
    if (mysqli_num_rows($run_query) > 0) {
        // Fetch the row
        $row = mysqli_fetch_assoc($run_query);
        
        // Set the values
        $times_present = $row['present'];
        $times_absent = $row['absent'];
    } else {
        // Default to 0 if no record is found
        $times_present = 0;
        $times_absent = 0;
    }
?>


<?php
    $query = " SELECT school_stamp FROM `administratives` ";
    $run_query = mysqli_query($connection, $query);

    if(mysqli_num_rows($run_query) == 1){
        while($result = mysqli_fetch_assoc($run_query)){
            $signature = $result['school_stamp'];
        }
    }
?>

<?php
	// Define the class for which you want to retrieve the next term fee
	$allocate_class = $result_class;  // Assuming $result_class is already set

	// Query to fetch the next term fee based on the allocated class
	$query = "SELECT fee_amount FROM fee_allocation WHERE allocate_class = '{$allocate_class}' LIMIT 1";
	$run_query = mysqli_query($connection, $query);

	if (mysqli_num_rows($run_query) == 1) {
		while($result = mysqli_fetch_assoc($run_query)){
			$next_term_fee = $result['fee_amount'];
		}
	}
?>



<!DOCTYPE>
<html>
	<head>
		<title>HIS Result - <?php echo $result_name; ?></title>
		<meta charset='utf-8'>
        <meta charset='utf-8'>
        <meta http-equiv='X-UA-Compatible' content='IE=edge'>
		<meta name="viewport" content="width=1024, initial-scale=1.0">
        <!-- CSS-->
        <link rel='stylesheet' type='text/css' href='../../assets/css/main.css'>
		<link rel='stylesheet' type='text/css' href='../../assets/css/defined.css'>
		<link rel="shortcut icon" href="../../img/favicon.png">
		<!-- Font-icon css-->
		<link rel="stylesheet" href="../../assets/css/font-awesome-4.7.0/css/font-awesome.css">
		<script src="../../assets/js/jquery-2.1.4.min.js"></script>
		<script src='../../assets/js/bootstrap.js'></script>
		<script src='../../assets/js//blink.js'></script>
		<style type="text/css">
		<style type="text/css">
			body {
				font-family: Arial, sans-serif;
				width: 1024px;
				margin: 0 auto;
				overflow-x: hidden;
				touch-action: none;				
				padding: 8px;
				background-color: #f9f9f9;
			}
			.container {
				font-family: Arial, sans-serif;
				max-width: 1000px;
				margin: 0;
				background: white;
				padding: 8px;
				box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
			}
			.header-row {
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin-bottom: 20px;
			}
			.logo, .pic {
				width: 100px;
				height: 100px;
				border-radius: 20%;
			}
			.logo img, .pic img {
				width: 100%;
				height: 100%;
				border-radius: 20%;
			}
			.school-info {
				text-align: center;
				flex: 1;
				margin-left: 20px;
				margin-right: 20px;
			}
			.school-info h1 {
				color: #0066cc;
				font-size: 18px;
				margin: 0;
			}
			.school-info p {
				margin: 5px 0;
				font-size: 12px;
			}

			.left-align {
				text-align: left;
			}

			.right-align {
				text-align: right;
			}

			.center-align {
				text-align: center;
			}

			.justify-align {
				text-align: justify;
			}
			p {
				font-size: 14px;
			}
			#result td {
				border: 1px solid black;
				padding: 8px;
				font-size: 12px;
				border-style:solid;
				border-color: black;
                text-align: left;
                white-space: nowrap; /* Prevents line breaks */                
			}
			.perf {
				float: left;
				width: 70%;
			}
			#performance th, #performance td {
				border: 1px solid black;
				text-align: left;
				font-size: 11px;
				padding: 3px;
			}
			#result th {
				border-style:solid black;
				border-color: black;
				border: 1px solid ;
                text-align: left;
                white-space: nowrap; /* Prevents line breaks */                
				padding: 2px;
				font-size: 12px;
			}
			h5 {
				font-size: 8px;
			}

			.row {
			  display: flex;
			  margin-left:-5px;
			  margin-right:-5px;
			}

			.column {
			  flex: 100%;
			  padding: 1px;
			}

			table {
			  border-collapse: collapse;
			  border-style: solid black;			  
			  border-spacing: 0;
			  width: 100%;
			  border: 3px solid;
			}

			#attendance th, #attendance td, #affective th, #affective td {
			border: 2px solid black;	
			  text-align: left;
			  font-size: 10px;
			  padding: 9px;
			}
			/* Left column */
			.leftcolumn {
			  float: left;
			  width: 70%;
			}

			/* Right column */
			.rightcolumn {
			  float: left;
			  width: 30%;
			  padding-left: 5px;
			}
			tr:nth-child(even) {
			  background-color: solid black;
			}
		   @media print {
				@page {
					size: A4; /* Set the paper size to A4 */
					margin: 10mm; /* Adjust margins if necessary */
				}

				body {
					font-family: Arial, sans-serif; /* Example font */
					margin: 0; /* Remove default body margin */
				}
			}

			.grade-details, .remarks, .admin-signature {
				font-size: 12px;
				margin-top: 5px;
			}
			.remarks, .admin-signature {
                border: 3px solid black;
                font-size: 10px;
                text-align: left;
                font-size: 10px;
                padding: 2px;
			}
			.clear-fix::after {
				content: "";
				display: block;
				clear: both;
			}
			.left {
				float: left;
				width: 46%;
				text-align:left;
			}
			.center {
				float: left;
				width: 27%;
				text-align:center;
			}			
			.right {
				float: left;
				width: 27%;				
				text-align:right;
			}
			.stampC {
				position: relative;
				text-align: center;
				max-width: 300px;
				width: 300%;                
			}
			.stamp {
                position: center;
				text-align: center;
				max-width: 300px;
				width: 300%;
			}
            		
		}
		</style>
		<head>
        <div class='panel panel-primary'>
            <div class='panel-body'>
			<div class="container">
				<div class="header-row">
					<div class="logo">
						<img src="<?php echo $school_logo; ?>" class="img-responsive" alt="School Logo" />
					</div>
					<div class="school-info">
						<h1><?php echo $school_name; ?></h1>
						<p><b>Behind Abanurat Filling Station,</b></p>
						<p><b>Kwanawwa Area, Sokoto State</b></p>
						<p><b>Tel: 07039354339</b></p>
						<p><b>E-mail: hikimahschools@gmail.com</b></p>
					</div>
					<div class="pic">
						<img src="<?php echo $result_passport; ?>" class="img-responsive" alt="Student Passport" />
					</div>
				</div>
				<div class="clear-fix">
					<div class="left">
						<b>TERM:</b> <u><?php echo $result_term; ?></u>
					</div>
					<div class="center">
						<b>SESSION:</b> <u><?php echo $result_session; ?></u>
					</div>
					<div class="right">
						<b>CLASS:</b> <u><?php echo $result_class; ?></u>
					</div>
				</div>				

				<div class="clear-fix">
					<div class="left">
						<b>NAME:</b> <u><?php echo strtoupper($result_name); ?></u>
					</div>
					<div class="center">
						<b>ADM NO:</b> <u><?php echo $result_reg_number; ?></u>
					</div>
					<div class="right">
						<b>GENDER:</b> <u><?php echo $result_sex; ?></u>
					</div>
				</div>
				<div class="clear-fix">
					<div class="left">
						<b>WEIGHT:</b> <u><?php echo $result_weight; ?> kg</u>
					</div>
					<div class="center">
						<b>HEIGHT:</b> <u><?php echo $result_height; ?> ft</u>
					</div>
					<div class="right">
						<b>CLUB:</b> <u><?php echo $result_club; ?></u>
					</div>
				</div>
            <?php
                $query = " SELECT * FROM `results1` WHERE `reg_number` = '{$result_reg_number}' AND `class` = '{$result_class}' AND `term` ='{$result_term}' AND `session` = '{$result_session}' ";
                $run_query = mysqli_query($connection, $query);

                if(mysqli_num_rows($run_query) > 0){
                    $number_of_subject = mysqli_num_rows($run_query);
                    $marks_obtainable = $number_of_subject * 100;
					$i = 0;
                    echo"
					<div class='row'>
						<div class='leftcolumn'>
                            <table class='text-info' id='result'>
                                <thead>
									<th style='text-align:center' colspan='7'><b>COGNITIVE DOMAIN</b></th>
                                    <tr class='info'>
										<th>S/N</th>
                                        <th>SUBJECTS</th>
                                        <th style='writing-mode:vertical-rl; text-align:center;'>CA (40)</th>
                                        <th style='writing-mode:vertical-rl; font-size: 9; text-align:center;'>EXAM (60)</th>
                                        <th style='writing-mode:vertical-rl; font-size: 9; text-align:center;'>TOTAL (100)</th>
										<th style='writing-mode:vertical-rl; text-align:center;'>POSITION</th>
                                        <th style='text-align:center;'>GRADE</th>
                                    </tr>
                                </thead>
                    ";

                    while($result = mysqli_fetch_assoc($run_query)){
						$i++;
                        $result_regno = $result['reg_number'];
                        $result_subject = $result['subjects'];
                        $result_ca = $result['ca1'] + $result['ca2'];
                        $result_exam = $result['exam'];
                        $result_score = $result['subject_total'];
                        $result_rank = subject_position($result['subject_rank']);
                        $a = 80;
                        $b = 60;
                        $c = 50;
                        $d = 40;
                        $f = 0;
                        if($result_score >= $a){
                            $result_grade = "A";
                            $remark = "Excellent";
                        }
                        elseif($result_score >= $b){
                            $result_grade = "B";
                            $remark = "Good";
                        }
                        elseif($result_score >= $c){
                            $result_grade = "C";
                            $remark = "Fair";
                        }
                        elseif($result_score >= $d){
                            $remark = "Below Average";
                        }
                        else{
                            $result_grade = "F";
                            $remark = "Failed";
                        }

                        echo"
                            <tr>
							    <td>{$i}</td>
                                <td>{$result_subject}</td>
                                <td style='text-align: center'>{$result_ca}</td>
                                <td style='text-align: center'>{$result_exam}</td>
                                <td style='text-align: center'>{$result_score}</td>
                                <td style='text-align: center'>{$result_rank}</td>
                                <td style='text-align: center'>{$remark}</td>
                            </tr>
                        ";
                    }
                }echo"
                        </table>
					

                ";
				?>
                
                    <h6></h6>
                    <table class='text-info' id='result'>
                            <th style='text-align:center' colspan="5"><b>GRADE SCALE </b></th>					   
                            <tr>
                                <td style='text-align:center' ><b>A (80-100)</b></br> <b>EXCELLENT</b></td>
                                <td style='text-align:center' ><b>B (60-79)</b></br> <b>GOOD</b></td>
                                <td style='text-align:center' ><b>C (50-59)</b></br> <b>FAIR</b></td>
                                <td style='text-align:center' ><b>D (40-49)</b></br> <b>BELOW AVERAGE</b></td>
                                <td style='text-align:center' ><b>F (0-39)</b></br> <b>FAILED</b></td>			
                            </tr>				
                    </table>

                    <h6></h6>
                <?php
					function getOrdinal($number) {
						$suffix = ['th', 'st', 'nd', 'rd'];
						$value = $number % 100;
						return $number . '<sup>' .($suffix[($value % 10 > 3 || $value % 100 > 13) ? 0 : ($value % 10)] ?? $suffix[0] . '</sup>');
					}

					$query = "SELECT * FROM `positions` WHERE `class` = '{$result_class}' AND `term` = '{$result_term}' AND `session` = '{$result_session}'";
					$run_query = mysqli_query($connection, $query);
					$number_in_class = mysqli_num_rows($run_query);

					$query = "SELECT * FROM `positions` WHERE `reg_number` = '{$result_reg_number}' AND `class` = '{$result_class}' AND `term` = '{$result_term}' AND `session` = '{$result_session}'";
					$run_query = mysqli_query($connection, $query);

					if (mysqli_num_rows($run_query) == 1) {
						while ($result = mysqli_fetch_assoc($run_query)) {
							$student_total = $result['students_sub_total'];
							$student_average = round($student_total / $number_of_subject, 2);
							$position_in_class = $result['class_position'];
							$ordinal_position = getOrdinal($position_in_class); // Convert to ordinal
						}
					}
				?>                    
                    <table class='text-info' id='result'>
                        <th style='text-align:center' colspan="5"><b>PERFORMANCE SUMMARY </b></th>					   
                        <tr>
                            <td style="text-align:center; font-size:10px;"><b>MARKS OBTAINABLE</b></td>
                            <td style="text-align:center; font-size:10px;"><b>MARKS OBTAINED</b></td>
                            <td style="text-align:center; font-size:10px;"><b>AVERAGE</b></td>
                            <td style="text-align:center; font-size:10px;"><b>NO IN CLASS</b></td>
                            <td style="text-align:center; font-size:10px;"><b>POSITION</b></td>
                        </tr>
                        <tr>
                            <td style='text-align:center' ><b><?php echo $marks_obtainable; ?></b></td>
                            <td style='text-align:center' ><b><?php echo $student_total; ?></b></td>
                            <td style='text-align:center' ><b><?php echo $student_average; ?>%</b></td>
                            <td style='text-align:center' ><b><?php echo $number_in_class; ?></b></td>
                            <td style='text-align:center' ><b><?php echo $ordinal_position; ?></b></td>
                        </tr>
                    </table>
                <?php
                    if($student_average >= 40){
                        $teacher_remark = "Good result, keep it up.";
                    }else{
                        $teacher_remark = "";
                    }
                ?>
                <?php
                    if($student_average >= 40){
                        $result_status = "Pass";
                    }
                    elseif($student_average < 40){
                        $result_status = "Fail";
                    }
                ?>

                <?php
                    if($result_term === "Third Term" && $result_status === "Pass"){
                        $third_term_promotion = "Promoted";
                    }
                    elseif($result_term === "Third Term" && $result_status === "Fail"){
                        $third_term_promotion = "Repeat";
                    }else{
                        $third_term_promotion = $result_status;
                    }
                ?>
					    <div class="remarks">
							<p><b>Form Master/Mistress Remarks:</b> <?php echo $teacher_remark; ?>
                            <?php
                                $query = " SELECT subjects FROM `results1` WHERE `reg_number` = '{$result_reg_number}' AND `class` = '{$result_class}' AND `term` ='{$result_term}' AND `session` = '{$result_session}' AND subject_total < 40 ";
                                $run_query = mysqli_query($connection, $query);

                                if(mysqli_num_rows($run_query) > 0){
                                    echo "Put more effort in ";
                                    while($result = mysqli_fetch_assoc($run_query)){
                                        $result_subject = $result['subjects'];
                                        echo "<b>{$result_subject},</b>";
                                    }
                                }
                            ?>
							</p>
						</div>
						<div class="admin-signature">
							<p><b>VP ACAD/ADMIN:</b> <?php echo $teacher_remark; ?>
                            <?php
                                $query = " SELECT subjects FROM `results1` WHERE `reg_number` = '{$result_reg_number}' AND `class` = '{$result_class}' AND `term` ='{$result_term}' AND `session` = '{$result_session}' AND subject_total < 40 ";
                                $run_query = mysqli_query($connection, $query);

                                if(mysqli_num_rows($run_query) > 0){
                                    echo "Put more effort in ";
                                    while($result = mysqli_fetch_assoc($run_query)){
                                        $result_subject = $result['subjects'];
                                        echo "<b>{$result_subject},</b>";
                                    }
                                }
                            ?>
							</p>
						</div>                                  
				</div>
				<div class="rightcolumn">
					<table id="affective">
					<thead>
						<th style='text-align:center' colspan="2"><b>AFFECTIVE/PSYCHOMOTOR DOMAIN</b></th>
						<tr>
							<td>PUNCTUALITY</td>
							<td><?php echo $puntuality; ?></td>
						</tr>
						<tr>
							<td>NEATNESS</td>
							<td><?php echo $neatness; ?></td>
						</tr>
						<tr>
							<td>RELATIONSHIP WITH OTHERS</td>
							<td><?php echo $relationship_with_others; ?></td>
						</tr>
						<tr>
							<td>REGARDS FOR AUTHORITY</td>
							<td><?php echo $regards_for_authority; ?></td>
						</tr>
						<tr>
							<td>ATTITUDE TOWARDS ACADEMIC</td>
							<td><?php echo $attitude_towards_academics; ?></td>
						</tr>
						<tr>
							<td>HONESTY</td>
							<td><?php echo $honesty; ?></td>
						</tr>
												<tr>
							<td>INITIATIVE</td>
							<td><?php echo $initiative; ?></td>
						</tr>
						<tr>
							<td>FORM MASTER/MISTRESS</td>
							<td><?php echo $form_master_or_mistress; ?></td>
						</tr>
						<th style='text-align:center;' colspan="2">
						  <b>GRADE CODE</br>(A=5, B=4, C=3, D=2, F=1)</b>
						</th>						
					</table>

					<h6></h6>
					<table id="attendance">
					<th style='text-align:center' colspan="2"><b>ATTENDANCE SUMMARY</b></th>
						<tr>
							<td>NO OF DAYS SCHOOL OPENED</td>
							<td><?php echo $times_absent + $times_present; ?></td>
						</tr>
						<tr>
							<td>NO OF TIME PRESENT</td>
							<td><?php echo $times_present; ?></td>
						</tr>
						<tr>
							<td>NO OF TIME ABSENT</td>
							<td><?php echo $times_absent ?></td>
						</tr>
					</table>

                    <h6></h6>
					<table id="attendance">
                        <th style='text-align:center' colspan="2"><b>TERM SCHEDULE AND FEES</b></th>					   
						<tr>
						    <td><b>SCHOOL CLOSE</b></td>
                            <td style="text-align:center; font-size:12px;"><?php echo $term_ended; ?></b></td>
                        </tr>
                        <tr>
							<td><b>NEXT TERM RESUMES</b></td>
                            <td style="text-align:center; font-size:12px;"><b><?php echo $next_term_starts; ?></b></td>
                        </tr>
							<td><b>NEXT TERM FEES</b></td>
                            <td style="text-align:center; font-size:12px;"><b>₦<?php echo $next_term_fee; ?></b></td>
						</tr>
					</table>
                    <h5></h5>
				    <div style="display: flex; justify-content: center;">
                        <img src="<?php echo $signature;?>" alt="Signature" id="signature" class="stamp" style="width: 200px; height: auto;" />
                    </div>

				</div>
			</div>	

					</div>
                </div>
            </div>
			
            <button type='button' class='btn btn-primary btn-sm noprint' onclick='window.print()' value='print a div!'><i class='fa fa-print' style='text-align:center' ></i> Print </button>
            <a href='teacher_dashboard.php?teacher_check_result' class='btn btn-danger btn-sm noprint'><i class='fa fa-close'> Cancel</i></a>
            <br />
            <br />
        </div>
        </div>
        </div>
	</body>
</html>