<?php

include("../../db.php");

$uid = $_REQUEST['idno'];
$class = $_REQUEST['class'];
$school_id = $_REQUEST['school_id'];
$session = $_REQUEST['session'];
$term_id = $_REQUEST['term_id'];
$term_name = $_REQUEST['term_name'];
$term_type = $_REQUEST['term_type'];

$permission = $db->getRow("select * from examname where name='$term_name' ");



if($permission['app_visibilty']===1){
redirectToResult();
} else {
    echo "Not Available";
}
/*
$resultFeeCheck = $db->getVal("SELECT result_fee_check FROM tbl_info WHERE id = '1'");

$resultPublishCheck = $db->getVal("SELECT result_publish FROM tbl_info WHERE id = '1'");

if ($resultFeeCheck == 0 && $resultPublishCheck == 0){

    redirectToResult();
 
}else{

    if ($resultPublishCheck == 1){
        //$responseQry = $db->getRow("SELECT * FROM exam_publish WHERE name = '$term_name' AND session = '$session'");

        $responseQry = $db->getRow("SELECT * FROM examterm WHERE name = '$term_name' AND class = '$class' AND session = '$session'");

        //print_r($responseQry);
        //return;

        if ($responseQry && count($responseQry) > 0){

            $currentDate = strtotime(date("Y-m-d H:i:s"));
            $publishDateBegin = strtotime($responseQry['start_date']);
            $publishDateEnd = strtotime($responseQry['end_date']);

            if($currentDate > $publishDateBegin && $currentDate < $publishDateEnd) {
                echo "Result Published";
                checkFee();
            } else {
                echo "Result Awaited..!";
            }

        }else{
            echo "fail";
        }
    }else{
        checkFee();
    }

}

function checkFee(){
    global $resultFeeCheck;
    if ($resultFeeCheck == 1){

    }else{
        redirectToResult();
    }
}*/


function redirectToResult(){

    global $db, $LinksDetails, $term_name,$uid,$class,$school_id,$session,$permission;

    $aryForm=$db->getRow("SELECT * FROM student WHERE school_id='".$school_id."' and idno='".$uid."' and class='$class' and session='$session' ");
    ?>

    <div style="height:1195px;margin-bottom:20px;"  class="mydiv">
	  <div style="height:1290px;border:8px solid #D5DA13;">
		<?php

		error_reporting(55);

		$termWhere=" and name='".$term_name."' ";

        $checksection = $db->getVal("select count(section) from subject where class='$class' and section!='' and session='".$session."' ");
        $wheresection = '';
        if($checksection > 0){
            $wheresection = " and section='$aryForm[section]' ";
        }

		$examtype=$db->getRow("select * from examterm  where session='".$session."' and school_id='".$school_id."' $termWhere and class='".$class."' $wheresection ");

		?>
		  <div class="row-fluid">
			<div class="span12">
			  <div class="well" style="min-height:800px;">
			  <div class="headPrint hide" style="border: none;">
			 
    			 <table style="width:100%;">
    			     <tr>
    			         <td><img src="<?php echo URL_ROOT ?>uploads/cbselogo.png" height="90px" width="110px" style="margin:0px 0px 3px 10px;"></td>
    			         <td style="text-align:center;margin-bottom:18px;"><h1 style="font-size:30px;margin:5px 0px 0px 0px;color: #EC1E1E;"><?php echo ucfirst($LinksDetails["site_name"]) ?></h1>
        			         <span style="font-size:16px;font-weight:800;  color: #186916;"> <?php echo ucfirst(nl2br($LinksDetails["contact_content"])) ?></span>
    			         </td>
    			         <td><img src="<?php echo $LinksDetails["logo"]; ?>" height="90px" width="110px" style="margin:0px 20px 3px 10px;float:right;"></td>
    			     </tr>
    			 </table>
			  
		  </div>
				<br>
				
				<div style="font-size: 16px;clear: both;font-weight: bold;padding: 5px;color: #000;text-align: center;border-bottom: 2px solid #000;;"> Report Card for Academic Session: <?php echo $session;?>  (<?php echo ucwords($term_name); ?>)</div>

                <!--<h4 style="text-align:center;color:#fa0606;padding-left:50px;"><?php echo strtoupper($aryForm['name']); ?></h4>-->
                <h4 style="text-align:center;color:#fa0606;padding-top:5px;margin:auto;padding-left:0px;"><?php echo strtoupper($aryForm['name']); ?></h4>
                <h6 style="text-align:center;">
                 <?php
                            if($aryForm['photo']!=""){
                            ?>
                                <img src="<?php echo URL_ROOT; ?>media/photo/<?php echo $aryForm['photo']; ?>" style="margin-top:-15px;position:absolute;height:110px;width:110px;border-radius:50%;border:solid 2px #d5da13;margin-left:-50px;" />
                            <?php
                            } ?>
                </h6>

                <table style="width:100%;font-size:13px;font-weight: bold;border: 2px solid #D5DA13;height:90px;" cellspacing="0">
                    <tr>
                        <td style="color:#000;">Class : <span style="color:#FA0606;"> <?php echo $aryForm["class"] ?>-<?php echo $aryForm["section"] ?></span></td>
                        <td style="color:#000;text-align:right;">Father's Name : <span style="color:#FA0606;"> <?php echo $aryForm["fname"] ?></span></td>
                    </tr>
                    <tr>
                        <td style="color:#000;">Roll No. : <span style="color:#FA0606;"> <?php echo $aryForm["fincome"] ?></span></td>
                        <td style="color:#000;text-align:right;">Mother's Name : <span style="color:#FA0606;"> <?php echo $aryForm["mname"] ?></span></td>
                    </tr>
                    <tr>
                        <td style="color:#000;">Admission No : <span style="color:#FA0606;"> <?php echo $aryForm["admissionno"] ?></span></td>
                        <td style="color:#000;text-align:right;">Date of Birth : <span style="color:#FA0606;"> <?php echo $aryForm["dob"] ?></span></td>
                    </tr>
                </table>

				<h4 style="text-align:center;color:#fa0606;padding-left:50px;">SCHOLASTIC AREAS</h4>
				
				<table style="width:100%; font-size:14px;border:2px solid #D5DA13;text-align: center;color: #000;font-weight: 800;" cellpadding="3" cellspacing="0">

					<tr style="font-weight: 800;padding: 5px;color: #000;">
						<td style="border-right:2px solid #D5DA13;border-bottom: 1px solid #D5DA13;padding:5px;color:#000;">SUBJECTS</td>
						<?php

						if($examtype['percent']==""){
						    ?>
						<td style="border-right:2px solid #D5DA13;border-bottom: 2px solid #D5DA13;padding:5px;color:#000;">TOTAL </td>    
						    <?php
						}

						 $finalObtain=0;$finalTotal=0;

							?>
						<td style="border-right:2px solid #D5DA13;border-bottom: 2px solid #D5DA13;padding:5px;color:#000;">
                        <?php
                            echo $examtype["full_name"];
							if($examtype['percent']!=""){
							    echo "<br>(".$examtype['percent'].")";
							}

							 ?>
                       </td>
                       
                       <td style="border-bottom: 2px solid #D5DA13;padding:5px;color:#000;">GRADE</td>

					</tr>
 
					<?php $xvalues = []; $yvalues = []; $colour = []; $maxmarks = 0;
                        
                        $chkclassoptional = $db->getVal("select id from optional_class where class='$_REQUEST[class]' and section='$_GET[section]' ");
						
						$detail=$db->getRows("select * from subject where school_id='".$school_id."' and class='".$class."' $wheresection  and session='".$session."' order by id");

						foreach($detail as $d){ $fsAry=array();$final="";

						$chksubjectoptional = 0;
						if($chkclassoptional > 0){
						    $chksubjectoptional = $db->getVal("select id from optional_subject where idno='$aryForm[idno]' and subject='$d[subject]' and session='".$session."' ");
						}
						
						if($chksubjectoptional == 0){
						$examResultData=$db->getRow("select * from exammarks where idno='".$aryForm["idno"]."' and type='".$examtype['name']."' and subject='".$d["subject"]."'");
                        // if($examResultData["obtain"]!="NA"){
        				array_push($xvalues, $d['subject']);
						?><tr>
								<td style="border-bottom: 2px solid #D5DA13;border-right:2px solid #D5DA13;color:#000;"><?php echo ucfirst($d["subject"]) ?></td>
								<?php
								
    								if($examtype['percent']==""){
    								    array_push($yvalues, (int)$examResultData['obtain']);
    								    
    								    if($maxmarks < $examResultData['total']){
    								        $maxmarks = $examResultData['total'];
    								    }
							    ?>
        							<td style="border-bottom: 2px solid #D5DA13;border-right:2px solid #D5DA13;color:#000;"><?php echo $examResultData['total']; $finalTotal+= $examResultData["total"]; ?> </td>
        							<td style="border-bottom: 2px solid #D5DA13;border-right:2px solid #D5DA13;color:#000;"><?php echo $examResultData['obtain']; $finalObtain+=  $examResultData["obtain"]; ?> </td>
									<td style="border-bottom: 2px solid #D5DA13;color:#000;">
    								    <?php
    								        echo FindGrades($examResultData["obtain"],$examResultData["total"]);

    								        $color = FindColour($examResultData["obtain"],$examResultData["total"]);
    								        array_push($colour, $color);
    								    ?>
    								</td>
							    <?php
    							} else {
								?>
								<td style="border-bottom: 2px solid #D5DA13;border-right:2px solid #D5DA13;color:#000;">

								 <?php
								  $obtain = number_format($examResultData["obtain"]*$examtype['percent']/$examResultData["total"],1)+0;
                                 if($examResultData["obtain"]=="AB"){
                                    echo $examResultData["obtain"];
								 } else {
								     echo round($obtain);
								 }

								$finalTotal+=   $examtype['percent'];
                                $finalObtain+=  round($obtain);

                                ?>
								</td>
								<td style="border-bottom: 2px solid #D5DA13;text-align:center;color:#000;">
								    <?php
								        echo FindGrades($obtain,$examtype['percent']);
								    ?>
								</td>
								<?php
    								array_push($yvalues, (int) round($obtain));

							        $color = FindColour((int) round($obtain),$examtype['percent']);
							        array_push($colour, $color);
							        
							        $maxmarks = $examtype['percent'];
    								
        							}
								?>
							</tr>
						<?php
				// 		}
						}
                        }
						?>

				</table>
                <div style="width:900px;float:left;color:transparent;">.</div>
				
				<br><br>

                <?php 
                    $data = $db->getRow("select * from attendance_remark where idno='$uid' and class='$class' and type='$term_name' and session='$session' ");
                ?>

                <div style="width:100%;float:left;">
    				<table class="table" style="width:100%;" cellspacing="0">
    				    <tr>
    				        <td style="text-align:center;color:#000;border-left: 2px solid #D5DA13;border-top: 2px solid #D5DA13;border-right: 2px solid #D5DA13;border-bottom: 2px solid #D5DA13;">TOTAL MARKS</td>
    				        <td style="text-align:center;color:#000;border-top: 2px solid #D5DA13;border-right: 2px solid #D5DA13;border-bottom: 2px solid #D5DA13;"><?PHP echo $finalObtain." / ".$finalTotal; ?></td>
    				        <td style="text-align:center;color:#000;border-top: 2px solid #D5DA13;border-right: 2px solid #D5DA13;border-bottom: 2px solid #D5DA13;">PERCENTAGE</td>
    				        <td style="text-align:center;color:#000;border-top: 2px solid #D5DA13;border-right: 2px solid #D5DA13;border-bottom: 2px solid #D5DA13;">
    				            <?php 
    				            $percentage = number_format(($finalObtain*100/ $finalTotal),2);
                				// $percentArray[$aryForm[idno]]= $percentage;
    				            echo number_format(($finalObtain*100/ $finalTotal),2)."%"; ?></td>

    				        <?php if($data['view_attendance']==1){ ?>
    				        <td style="text-align:center;color:#000;border-top: 2px solid #D5DA13;border-right: 2px solid #D5DA13;border-bottom: 2px solid #D5DA13;">Attendance</td>
    				        <td style="text-align:center;color:#000;border-top: 2px solid #D5DA13;border-right: 2px solid #D5DA13;border-bottom: 2px solid #D5DA13;"><?php echo $data['present']."/".$data['working_days']; ?></td>
    				        <?php } ?>
    				    </tr>
    				</table>
                </div>

				<br>
				<br><br>
                <?php if($permission["co_scholastic_view"] == 1) { ?>
                <div style="width:37%;float:left;">
                    <?php 
                        $record = $db->getRows("select type from criteria where class='$class' and session='$session' group by type ");
                        foreach($record as $row){ 
                    ?>
                        <table style="width:100%;border:solid 2px #D5DA13;" cellpadding="5">
                            <tr>
                                <th style="text-align:left;border-bottom:solid 2px #D5DA13;font-size:15px;"><?php echo $row['type']; ?></th>
                                <th style="text-align:center;border-bottom:solid 2px #D5DA13;border-left:solid 2px #D5DA13;font-size:15px;width:80px;">Grade</th>
                            </tr>
                            <?php $names = $db->getRows("select * from criteria where type='$row[type]' and class='$class' and session='$session' ");
                                foreach($names as $name){
                                    $grade=$db->getVal("select grade from examcriteriarecord where school_id='1' and idno='$uid' and subcriteria='".$name["name"]."' and criteria='".$row['type']."' and session='$session' and term='$term_name' ");
                                    $gradecolor = $db->getVal("select colour from grade where grade='$grade' and session='$session' ");
                                ?>
                                <tr>
                                    <td style="text-align:left;border-bottom:dashed 1px #000;color:#000;"><?php echo $name['name']; ?></td>
                                    <td style="text-align:center;border-bottom:dashed 1px #000;border-left:solid 2px #D5DA13;color:#fff;width:80px;">
                                        <label style="display:inline-block;width:20px;text-align:center;height:20px;color:#fff;background:<?php echo $gradecolor; ?>"><?php echo $grade; ?></label>
                                    </td>
                                </tr>
                            <?php } ?>
                            
                        </table>
                    <?php } 
                    
                    if($data['view_remark']==1){ ?>
                    <br>    <label style="padding-left:10px;font-weight:800;color:#000;">Remark: <?php echo $data['remark']; ?></label>
                    <?php } ?>
                </div>
                <?php } ?>
				<div style="width:61%;float:right;overflow:visible;">

				    <?php
				        for($j = 0; $j <= $maxmarks; $j++){
    				        array_push($yvalues,$j);
    				        $j=$j+10;
				        }
                    ?>
				    <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js"></script>
				    <canvas id="myChart<?php echo $he; ?>" class="chartdiv" style="width:100%;max-width:600px;z-index:999999;height:250px;"></canvas>

				    <script>
                        var xValues = <?php echo json_encode($xvalues); ?>;
                        var yValues = <?php echo json_encode($yvalues); ?>;
                        var barColors = <?php echo json_encode($colour); ?>;
                        
                        new Chart("myChart<?php echo $he; ?>", { 
                          type: "bar",
                          data: {
                            labels: xValues,
                            datasets: [{
                              backgroundColor: barColors,
                              data: yValues
                            }]
                          },
                          options: {
                            legend: {display: false},
                            title: {
                              display: true,
                              text: "Graphical Result"
                            },
                            scales: {
                                xAxes: [{
                                    barPercentage: 0.4,
                                }]
                            }
                          }
                        });
                    </script>
                    <br><br>				    
                    <?php if($permission["co_scholastic_view"] == 1) { ?>
				    <?php $allgrades = $db->getRows("select * from grade");
                        foreach($allgrades as $gd){ ?>
                            <div class="icardbg" style="padding:10px;width:25%;display:inline-block;margin:0px;">
                                <label style="display:inline-block;width:20px;text-align:center;height:20px;color:#fff;background:<?php echo $gd['colour']; ?>"><?php echo $gd['grade']; ?></label> 
                                <label style="display:inline-block;"><?php echo $gd['detail']; ?></label>
                            </div>
                    <?php } }?>
				    
				</div>

                <br>
				 <div style="position:absolute;width:98%;margin-top:400px;float:left;">
				     <?php
				        $wheresign = "";
                            if(isset($_REQUEST["section"]) && $_REQUEST["section"]!=""){
                    			$wheresign=" AND section='".$_REQUEST["section"]."' ";
                            }
				           $principal = $db->getVal("select sign from sign where type='Principal' "); 
    				       $teacher = $db->getVal("select sign from sign where type='Class Teacher' and class='$_REQUEST[class]' $wheresign "); 
				     ?>
					   <table style="width:100%; text-align:center;font-size:14px;font-weight: 800;" cellpadding="0" cellspacing="2" >
						<tr style="vertical-align: bottom;">
						  <td style="color:#000;text-align:left;padding-left:20px;padding-top:50px;">
						       <?php if($teacher!=""){ ?>
						          <img src="<?php echo URL_ROOT."uploads/sign/$teacher" ;?>" style="display:bloc;height:50px;padding-bottom:20px;" /><br>
						      <?php } ?>
						      <span style="border-top:solid 1px;padding-top:10px;">Class Teacher's Signature</span></td>
						  <td style="color:#000;text-align:right;padding-right:20px;padding-top:50px;">
						      <?php if($principal!=""){ ?>
						          <img src="<?php echo URL_ROOT."uploads/sign/$principal" ;?>" style="display:bloc;height:50px;padding-bottom:20px;" /><br>
						      <?php } ?>
						      <span style="border-top:solid 1px;padding-top:10px;">Principal's Signature</span></td>
						</tr>
					  </table>
				</div>

			  </div>
			</div>
		  </div>

	   <style>
@media print {
.mydiv {page-break-after: always;}

.chartdiv {display:block; z-index:99999;}
}
</style>
	  </div>
	  </div>

<?php

    // $page_url = "";

    // if ($term_name == 'Term 1'){
    //     $page_url = 'https://'.$_SERVER['HTTP_HOST']."/api_new/student/result_term1.php?idno=$uid&class=$class&school_id=$school_id&session=$session";
    // }elseif ($term_name == 'Term 2'){
    //     $page_url = 'https://'.$_SERVER['HTTP_HOST']."/api_new/student/result_term2.php?idno=$uid&class=$class&school_id=$school_id&session=$session";
    // }elseif ($term_name == 'Final'){
    //     $page_url = 'https://'.$_SERVER['HTTP_HOST']."/api_new/student/result_final.php?idno=$uid&class=$class&school_id=$school_id&session=$session";
    // }else{
    //     $page_url = 'https://'.$_SERVER['HTTP_HOST']."/api_new/student/result_final.php?idno=$uid&class=$class&school_id=$school_id&session=$session";
    // }

    // header("Location: $page_url");
    // exit();

}

function FindGrades($obtain,$total){
	$per=round(($obtain*100)/$total);
	global $db;
	$data=$db->getRows("select * from examgrade  where school_id='1' ");
	if(is_array($data) && count($data)>0){
		foreach($data as $d){
			if($per>=$d["minrange"] && $per<=$d["maxrange"]){
				return "<div style='background:$d[colour];width:25px;height:20px;color:white;padding-top:5px;text-align:center;margin:auto;'>$d[grade]</div>"; 
			}
		}
	}
	return $per;
}


function FindColour($obtain,$total){
	$per=round(($obtain*100)/$total);
	global $db;
	$data=$db->getRows("select * from examgrade  where school_id='1' ");
	if(is_array($data) && count($data)>0){
		foreach($data as $d){
			if($per>=$d["minrange"] && $per<=$d["maxrange"]){
				return $d['colour'];
			}
		}
	}
	return $per;
}





?>


<script>
window.print();
</script>