<?php
$class=$this->uri->segment('3');
$section=$this->uri->segment('4');
$acedemic_session=$this->session->userdata('acedemic_session');
$all_students=$this->NewExamModel->get_students_ac_session_by_course_and_class($acedemic_session,$class,$section);
$exam_id=3;
?>

<!DOCTYPE html>
<html>
    <head>
        <link rel="preconnect" href="https://fonts.googleapis.com">
        <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
        <link href="https://fonts.googleapis.com/css2?family=Inconsolata:wght@200;300;400;500;600;700&display=swap" rel="stylesheet"> 
        <title>Mid Term Report Card</title>
        <style>
            html{padding:0px;margin:0px;}
            body{
                padding:25px;margin:0px;vertical-align:middle;
                font-family: 'Inconsolata', monospace;
            }
            .report-card{
                border : 2px solid #4F3F83;
                height:96%;
                border-radius:5px;
                margin:0px;
                padding:5px;
            }
            .report-2{
                border:1px solid #4F3F83 !important;
                height:97%;
                border-radius:5px;
                /*background-image: url("<?php echo logo_png; ?>");*/
                /*opacity: 0.1;*/
                /*background-repeat: no-repeat;*/
                /*background-size:auto 40%;*/
                /*background-position: center;*/
            }
            table{border-collapse:collapse;width:100%;}
            table tr{
                border :1px solid !important;
                border-collapse:collapse;
            }
            tr th{
                margin:0px;
                padding:2px;
                font-size:14px;
                font-weight:700;
                border :1px solid !important;
                border-collapse:collapse;
            }
            .report-3{
                height:97%;
                background-image: url("<?php echo logo_png; ?>");
                opacity: 0.1;
                background-repeat: no-repeat;
                background-size:auto 40%;
                background-position: center;
            }
        </style>
    </head>
    <body>
        
<?php
$count=0;
if($all_students){
    foreach($all_students as $data){
        $sid=$data['sid'];
        $student_table='student';
        $course_id=$data['course_id'];
        $get_student_data=$this->NewExamModel->get_item_data($sid,$student_table);
        if($get_student_data ){
            foreach($get_student_data as $students){
                if($students['status'] ==1){
                    $count=$count + 1;
                    if($count <=100){
                        $roll_no="";
                        $remark="";
                        $student_name=$students['student_name'];
                        $father_name=$students['father_name'];
                        $dob=date('d-m-Y',strtotime($students['dob']));
                        $sch_no=$students['sch_no'];
                            
                        $attendance_days=0;
                            
                        $attendance=0;
                        $remark='';
                        $result_table='exam_result';
                        $exam_id=3;
                        $roll_no='';
                        $rank_in_class="";
                        $check_student_exam_data=$this->NewExamModel->get_student_exam_data($sid,$exam_id,$course_id,$result_table);
                        if($check_student_exam_data){
                            foreach($check_student_exam_data as $exam_data){
                                $attendance_days=$exam_data['attendance_days'];
                                    $remark=$exam_data['remark'];
                                    $attendance=$exam_data['attendance'];
                                    $roll_no=$exam_data['roll_no'];
                                    $rank_in_class=$exam_data['rank_in_class'];
                                }
                            }
                        
$exam_head_table='exam_heads_3';
$get_course_exam_heads=$this->ReportCardModel->get_course_active_exam_heads($course_id,$exam_head_table);
                            
$course_subjects_table='exam_subjects_3';
$get_course_exam_subjects=$this->ReportCardModel->get_course_active_subjects($course_id,$course_subjects_table);


$student_subjects_table="student_subjects";
$get_student_subjects=$this->NewExamModel->get_student_subjects($sid,$course_id,$acedemic_session);



$elog_table="exam_log_".$acedemic_session;
$get_exam_log=$this->NewExamModel->students_exam_log($sid,$acedemic_session,$elog_table);
?>
        <div class="report-card p-1">
            <div class="report-2" >
                <div class="report-3">
                <div style="height:100px;">
                </div>
                <table style="text-align:left;">
                    <tr>
                        <th style="text-align:left;">
                            
                <div style="width:100%;height:25px;">
                    <div style='width:40%;float:left;margin:0px;padding:0px;'>
                        <p class='pl-1' style='margin:0px;padding:0px;padding-left:5px;text-transform:uppercase;'><b>NAME: </b><?php echo $student_name;?></p>
                    </div>

                    <div style='width:20%;float:left;'>
                        <p style='margin:0px;padding:0px;'><b>SCHOLAR NO: </b><?php echo $sch_no; ?></p>

                    </div>

                    <div style='width:20%;float:left;'>
                        <p style='margin:0px;padding:0px;'><b>ROLL NO: <?php echo $roll_no; ?></b></p>

                    </div>

                    <div style='width:20%;float:left;'>
                        <p style='margin:0px;padding:0px;'><b>ATTENDANCE: </b><?php echo round($attendance,2); ?> %</p>

                    </div>
                </div>
                <div style="width:100%;height:25px;">
                    <div style='width:80%;float:left;margin:0px;padding:0px;'>
                        
                        <p class='pl-1' style='margin:0px;padding:0px;padding-left:5px;text-transform:uppercase;'><b>FATHER'S NAME: </b><?php echo $father_name; ?></p>
                    </div>

                    <div style='width:20%;float:left;'>
                        <p style='margin:0px;padding:0px;'><b>CLASS: </b><?php echo $class; echo " "; echo $section; ?></p>

                    </div>

                    
                </div>

                        </th>
                    </tr>
                </table>
                
                <div style="padding:0px;" class="report-card-body">
                    <table class="table table-bordered" style="width:100%;">
                        <thead>
                            <tr style="margin:0px;padding:0px;background:#DBF7F3;">
                                <th style="text-align:center;margin:0px;font-size:16px;font-weight:700;" colspan="7"><b>ACADEMIC PERFORMANCE</b></th>
                            </tr>
                            <tr>
                                <th rowspan="2" colspan="2" style="width:100px;margin:0px;height:20px;">&nbsp;SUBJECTS</th>
                                <th rowspan="2" style="text-align:center;margin:0px;height:20px;">MM</th>
                                <?php 
                                
                                if($get_course_exam_heads){
                                    foreach($get_course_exam_heads as $exam_heads){
                                        if($exam_heads['exam_mode'] =='FirstTerm'){
                                ?>
                                <th style="text-align:center;height:20px;"><?php echo $exam_heads['exam_head'];?>
                                </th>
                                <?php
                                        }
                                    }
                                }
                                ?>
                                <th style="margin:0px;text-align:center;height:20px;">Total</th>
                                <th style="margin:0px;text-align:center;height:20px;">HIC</th>
                            </tr>
                            
                            <tr>
                                
                                <?php 
                                if($get_course_exam_heads){
                                    foreach($get_course_exam_heads as $exam_heads){
                                        if($exam_heads['exam_mode'] =='FirstTerm'){
                                ?>
                                <th style="text-align:center;margin:2px;">
                                    <?php echo $exam_heads['mm']; ?>
                                </th>
                                <?php
                                        }
                                    }
                                }
                                ?>
                                <th style="text-align:center;margin:2px;">100</th>
                                <th style="text-align:center;margin:2px;">100</th>
                                
                            </tr>
                        </thead>
                        <tbody>
                            
                            <?php
                            $mm_total=0;
                            $grand_total=0;
                            $hy_grand_total=0;
                            $annual_grand_total=0;
                            if($get_course_exam_subjects){
                                foreach($get_course_exam_subjects as $subjects){
                                    
                                    
                                    $subject_id=$subjects['id'];
                                    $exam_type=$subjects['exam_type'];
                                    $student_subject=0;
                                    
                                    
                                    
                                    
                                    
                                    
                                    if($get_student_subjects){
                                        foreach($get_student_subjects as $subjectss){
                                            if($subjectss['subject_id'] == $subject_id){
                                                $student_subject=$student_subject + 1;    
                                            }
                                            
                                        }
                                    }    
                                    // if($student_subject >=1){
                                        $subject_mm=$subjects['MM'];
                                    
                                        $hy_total=0;
                                        // $hic=$subjects['hy_hic'];
                                        $hic=0;
                                        $get_hic_data=$this->NewExamModel->get_hic_data_manual($subject_id,$class,$section,$acedemic_session);
                                        if($get_hic_data){
                                            foreach($get_hic_data as $hic_data){
                                            $hic=$hic_data['hy_total_marks'];
                                            }
                                        }
                                        
                                        $mm_total=$mm_total + $subjects['MM'];
                                        $sub_total=0;
                                        $sub_type=$subjects['subject_type'];
                            ?>                    
                            <tr style="padding:0px;margin:0px;">
                                <th style="margin:0px;text-align:left;" colspan="2">&nbsp;<?php echo $subjects['subject']; ?>
                                </th>
                                <th><?php echo $subject_mm; ?></th>
                                <?php 
                            if($get_course_exam_heads){
                                foreach($get_course_exam_heads as $exam_heads){
                                    if($exam_heads['exam_mode'] =='FirstTerm'){
                            ?>         
                            <th style="text-align:center;margin:0px;">
                               <?php
                               $ehid=$exam_heads['id'];
                                    if($get_exam_log){
                                    foreach($get_exam_log as $elog){
                                        
                                        if($elog['exam_head'] == $ehid && $elog['subject_id'] == $subject_id){
                                            
                                        $e_log_attendance=$elog['attendance'];
                                        if($e_log_attendance == "A"){
                                            echo "<span style='color:red;'>".$e_log_attendance."</span>";
                                        }else{
                                            if($exam_type == "Marks"){
                                                $obtained_marks=$elog['obtained_marks'];
                                                    $sub_total=$sub_total + $obtained_marks;
                                        $hy_total=$hy_total + $obtained_marks;
                                        $hy_grand_total=$hy_grand_total + $obtained_marks;
                                                
                                                if($obtained_marks > $subject_mm){
                                            echo "<span style='background:red;'>".$elog['obtained_marks']."</span>";
                                        }else{
                                            echo $elog['obtained_marks'];
                                        }
                                        
                                            }else if($exam_type == "Grade"){
                                                echo $elog['grade'];
                                            }
                                        }    
                                        }
                                        
                                    
                                            }
                                            
                                            
                                            
                                            
                                            
                                        }else{
                                        
                                        echo "--";
                                        
                                        }
                                        ?>    
                                        </th>
                                <?php        
                                        }
                                    }
                                }
                                ?>
                                <th style="margin:0px;text-align:center;">
                                    <?php 
                                    if($exam_type == "Marks"){ 
                                        echo $hy_total;
                                    }else{
                                        echo "--";
                                    }
                                    ?>
                                </th>
                                <th>
                                    <?php
                                    if($exam_type == "Marks"){
                                        echo $hic;
                                    }else{
                                        echo "--";
                                    }
                                    ?>
                                </th>
                            </tr>                    
                                                
                                                
                                                
                            <?php           
                            if($sub_type == "Main Subject"){
                                $grand_total= $grand_total + $sub_total;    
                            }
                            
                            
                                    // }
                                }
                            }
                            ?>
                            <tr style="background:#DBF7F3;border-top:1px solid #4F3F83 !important;padding:0px;margin:0px;">
                                <th colspan="2" style="margin:0px;">GRAND TOTAL</th>

                                
                                <th style="text-align:center;margin:0px;"><?php echo $mm_total; ?></th>
                                
                                <th></th>
                                <th></th>
                                
                                <th style="text-align:center;margin:0px;"><?php echo $hy_grand_total; ?></th>
                                <th></th>
                            </tr>
                            
                            <?php 
                            $avg_grand_total=$grand_total/2;
                            $percentage="";
                            if($mm_total != 0){
                                $percentage=($grand_total * 100)/$mm_total;    
                            }else{
                                $percentage=0;
                            }
                            
                            ?>
                            <tr style="background:#DBF7F3;border:1px solid #4F3F83;padding:0px;margin:0px;">
                                
                                <th style="margin:0px;text-align:left;padding-left:5px;" colspan="2"> Rank in class : <?php echo $rank_in_class;?></th>
                                <th style="margin:0px;text-align:left;padding-left:5px;" colspan="5"> PERCENTAGE : <b>&nbsp; <?php echo number_format((float)$percentage, 2, '.', ''); ?> % </b></th>
                            </tr>
                            
                        </tbody>
                    </table>
                </div>
                <div style="height:275px;width:100%;padding:5px;border:1px solid #4F3F83;">
	                    <table style="height:265px; width: 100%;">
	                        <thead style="border-bottom:2px solid #000;width:100%;">
		                    <tr style="">
		                        <td style="height:200px;vertical-align:bottom;">
				                    <div style="width:45px;border-right:2px solid;height:200px;">
					                    <div style="height: 18px;margin:0px;width: 100%;border-top:2px solid;">100%</div>
					                    <div style="height: 18px;margin:0px;width: 100%;border-top:2px solid;">90%</div>
					                    <div style="height: 18px;margin:0px;width: 100%;border-top:2px solid;">80%</div>
					                    <div style="height: 18px;margin:0px;width: 100%;border-top:2px solid;">70%</div>
					                    <div style="height: 18px;margin:0px;width: 100%;border-top:2px solid;">60%</div>
					                    <div style="height: 18px;margin:0px;width: 100%;border-top:2px solid;">50%</div>
					                    <div style="height: 18px;margin:0px;width: 100%;border-top:2px solid;">40%</div>
					                    <div style="height: 18px;margin:0px;width: 100%;border-top:2px solid;">30%</div>
					                    <div style="height: 18px;margin:0px;width: 100%;border-top:2px solid;">20%</div>
					                    <div style="height: 18px;margin:0px;width: 100%;border-top:2px solid;">10%</div>
				                    </div>
			                    </td>
                        <?php

                        if($get_course_exam_subjects){
                            foreach($get_course_exam_subjects as $exam_subjects){
                                $exam_type=$exam_subjects['exam_type'];
                                if($exam_type == "Marks"){   
                                    $subject_id=$exam_subjects['id'];
                                    $student_subject=0;
                                    if($get_student_subjects){
                                        foreach($get_student_subjects as $subjectss){
                                            if($subjectss['subject_id'] == $subject_id){
                                                $student_subject=$student_subject + 1;    
                                            }
                                            
                                        }
                                    }    
                                    if($student_subject >=1){
                                    $sub_mm=$exam_subjects['MM'];
                                    $sub_total=0;
                                    $subject_id=$exam_subjects['id'];
                                    $sub_type=$exam_subjects['subject_type'];
                                    $subject =$exam_subjects['subject'];    
                                    // $hic1=$exam_subjects['hy_hic'];      
                                    
                                    
                                    $hic1=0;
                                        $get_hic_data=$this->NewExamModel->get_hic_data_manual($subject_id,$class,$section,$acedemic_session);
                                        if($get_hic_data){
                                            foreach($get_hic_data as $hic_data){
                                            $hic1=$hic_data['hy_total_marks'];
                                            }
                                        }
                                    
                                    if($get_course_exam_heads){
                                        foreach($get_course_exam_heads as $exam_heads){
                                            if($exam_heads['exam_mode'] =='FirstTerm'){
                                                $ehid=$exam_heads['id'];
                                    if($get_exam_log){
                                        foreach($get_exam_log as $elog){
                                            if($elog['exam_head'] == $ehid && $elog['subject_id'] == $subject_id){
                                                
                                            
                                            $obtained_marks=$elog['obtained_marks'];
                                            $sub_total=$sub_total + $obtained_marks;
                                            }
                                                    }
                                                }
                                            }
                                        }
                                    }
                        $chart_h=200;
                        $perr=0;
                        if($sub_mm == 0){
                            $perr=0;
                        }else{
                            $perr=($sub_total*100)/$sub_mm;    
                        }
                        
                        $height=$sub_total * 2;
                        // $height=50;
                        $hic=$hic1 * 2;
                        $margin_top=0;
                        
                        
                        $margin_top=$hic - $height;
                        
                        ?>
                        
                        <td style="padding: 5px; padding-bottom: 0px !important;vertical-align: bottom;width: 40px;">
                            <div style="bottom: 0px !important;width:30px;border:1px solid black;height:<?php echo $hic; ?>px;background:black;vertical-align: bottom !important;display: block;">
                                <div style="display: block;width:22px;background:silver;height:<?php echo $height; ?>px;margin-top:<?php echo $margin_top; ?>px;font-weight: 600;text-transform: uppercase;text-align:center;font-size:13px;">
                                    <?php if($sub_total >0){ echo $sub_total; } ?>
                                </div>
                            </div>
                        </td>
                        <?php
                        
                                    }
                                }
                            }
                        }
                        ?>
                        <td></td>
                            </tr>
                        </thead>    
                        <tr>    
                        <td></td>
                        <?php
                        if($get_course_exam_subjects){
                            foreach($get_course_exam_subjects as $exam_subjects){
                                $exam_type=$exam_subjects['exam_type'];
                                if($exam_type == "Marks"){
                                    $student_subject=0;
                                    $subject_id=$exam_subjects['id'];
                                    if($get_student_subjects){
                                        foreach($get_student_subjects as $subjectss){
                                            if($subjectss['subject_id'] == $subject_id){
                                                $student_subject=$student_subject + 1;    
                                            }
                                            
                                        }
                                    }    
                                    if($student_subject >=1){
                                        
                                    $sub_mm=$exam_subjects['MM'];
                                    $sub_total=0;
                                    $subject_id=$exam_subjects['id'];
                                    $sub_type=$exam_subjects['subject_type'];
                                    $subject =$exam_subjects['subject'];  
                        ?>            
                            <td style="vertical-align: top;width: 40px;font-size:11px;height:65px;">
                                   <div style="position:relative;transform: rotate(-75deg); transform-origin: 90% ;text-align:right;left:-20px;"><?php echo $subject; ?></div> 
                                </td>
                            
                        <?php
                                    }
                                }
                            }
                        }
                        ?>
                        <td></td>
                        </tr>
                        </table>
                    </div>
                
                
                <table class="table table-bordered">
                    <tbody>
                        <tr>
                            <th colspan="3" style="text-align:left;height:auto;">REMARK : <?php echo $remark; ?></td>
                            </tr>
                            <tr>
                                <th class="text-center" style="vertical-align:bottom;">
                                    <br>
                                    Parent
                                </th>
                                <th colspan="" rowspan="" class="text-center" style="vertical-align:bottom;"> 
                                    
                                    <br>
                            CLASS TEACHER
                                </th>
                                <th colspan="" rowspan="" class="text-center" style="vertical-align:bottom;"> 
                                <br><br>
                                    <!--<img src="<?php echo base_url(); ?>assets/sjec-principal-2.jpeg" style="height:40px;width:auto;"><br>-->
                            Coordinator/PRINCIPAL
                                </th>
                            </tr>
                        </tbody>
                </table>
                </div>
            </div>
                
        </div>
        
        
        <?php
                            }
                        }
                    }
                }
            }
        }
        ?>
    </body>
</html>