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

$all_students=$this->NewExamModel->get_students_ac_session_by_course_and_class_new($acedemic_session,$class,$section);
$exam_head_table='exam_head_master_'.$acedemic_session;
$get_course_exam_heads=$this->ExamManagementModel->get_course_exam_wise_heads($class,$exam_id,$exam_head_table);

$ehcount=count($get_course_exam_heads);
                            
$subject_table='exam_subject_master_'.$acedemic_session;
$get_course_exam_subjects=$this->ExamManagementModel->get_course_active_subjects($class,$subject_table);

?>

<!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>First 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 school_logo_full_bageshwar; ?>");
                opacity: 0.1;
                background-repeat: no-repeat;
                background-size:auto 40%;
                background-position: center;
            }
        </style>
    </head>
    <body>
        
<?php

// var_dump($get_course_exam_heads);
$count=0;


if($all_students){

    foreach($all_students as $students){
        $sid=$students['sid'];
        $student_table='student';
        $course_id=$students['course_id'];
        $roll_no=$students['roll_no'];
        $height=$students['height'];
        $weight=$students['weight'];
        // $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){
                        
                        $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;
                        
                        $rank_in_class="";
                        $result_status="";
                        $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'];
                                    $result_status=trim($exam_data["result_status"]);
                                }
                            }
                        
// $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_".$acedemic_session;
$get_student_subjects=$this->ExamManagementModel->get_student_subjects($sid,$course_id,$acedemic_session,$student_subjects_table);



$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>-->
                <div>
                    <table class="table" style="width:100%;border:1px solid #4F3F83;width:100%;">
                        <tr>
                            <th style="width:15%;border:none !important;">
                                <img src='<?php echo school_logo_full; ?>' style="width:auto;height:60px;" />
                            </th>
                            <th style="width:85%;border:none !important;">
                                <p style="padding:0px; margin:0px;font-size:26px;text-align:center;font-weight:900;color:#29166f;"><?php echo school_full_name_bageshwar; ?></p>
                                <p style='padding:0px; margin:0px;text-align:center;color:#29166f;font-size:18px;'><b><?php echo school_address_bageshwar; ?></b></p>
                                
                                <p style='padding:0px; margin:0px;text-align:center;color:#29166f;font-size:18px;'><b> HALF YEARLY PROGRESS REPORT - SESSION 2023-2024</b></p>
                            </th>
                        </tr>
                    </table>
                </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="<?php echo $ehcount + 5; ?>"><b>ACADEMIC PERFORMANCE</b></th>
                            </tr>
                            <tr>
                                <th rowspan="2" colspan="2" style="max-width:250px !important;margin:0px;height:20px;">&nbsp;SUBJECTS</th>
                                <th rowspan="2" style="text-align:center;margin:0px;height:20px;font-size:10px;">MM</th>
                                <?php 
                                // var_dump($get_course_exam_heads);
                                if($get_course_exam_heads){
                                    foreach($get_course_exam_heads as $exam_heads){
                                ?>
                                <th style="text-align:center;height:20px;font-size:10px;text-transform:uppercase;" rowspan="2"><?php echo $exam_heads['exam_head'];?>
                                </th>
                                <?php
                                        
                                    }
                                }
                                ?>
                                <th style="margin:0px;text-align:center;height:20px;font-size:10px;">Total</th>
                                <th style="margin:0px;text-align:center;height:20px;font-size:10px;">Grade</th>
                            </tr>
                            
                            <tr>
                                
                                <?php 
                                $mm_eh_total=0;
                                if($get_course_exam_heads){
                                    foreach($get_course_exam_heads as $exam_heads){
                                        $mm_eh_total=$mm_eh_total + $exam_heads['mm'];
                                        
                                        
                                ?>
                                <!--<th style="text-align:center;margin:2px;">-->
                                <!--    <?php echo $exam_heads['mm']; ?>-->
                                <!--</th>-->
                                <?php
                                        
                                    }
                                }
                                ?>
                                <th style="text-align:center;margin:2px;">100<?php // echo $mm_eh_total; ?></th>
                                <th style="text-align:center;margin:2px;"></th>
                                
                            </tr>
                        </thead>
                        <tbody>
                            
                            <?php
                            $mm_total=0;
                            $grand_total=0;
                            $main_subject_count=0;
                            $annual_grand_total=0;
                            if($get_course_exam_subjects){
                                foreach($get_course_exam_subjects as $subjects){
                                    $sub_type=$subjects['subject_type'];
                                    $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 && $sub_type == "Main Subject"){
                                        $main_subject_count=$main_subject_count + 1;
                                        $subject_mm=$subjects['MM'];
                                        $mm_total=$mm_total + $subjects['MM'];
                                   
                                        $sub_total=0;
                               
                                        
                                        
                            ?>                    
                            <tr style="padding:0px;margin:0px;">
                                <th style="margin:0px;text-align:left;font-size:12px;width:250px !important" colspan="2">&nbsp;<?php echo $subjects['subject']; ?>
                                </th>
                                <th><?php 
                                if($exam_type == "Marks"){ echo $subject_mm; }else{ echo "Grade"; }
                                ?></th>
                                <?php 
                                $grade="";
                                if($get_course_exam_heads){
                                foreach($get_course_exam_heads as $exam_heads){
                            ?>         
                                <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" || $e_log_attendance == "ML"){
                                            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;
                            
                                                
                                        if($obtained_marks > $subject_mm){
                                            echo "<span style='background:red;'>".$elog['obtained_marks']."</span>";
                                        }else{
                                            echo $elog['obtained_marks'];
                                        }
                                            }else if($exam_type == "Grade"){
                                                    $grade=$elog['grade'];
                                                // echo $elog['grade'];
                                            }
                                        }    
                                        }
                                        
                                    
                                            }
                                            
                                            
                                            
                                            
                                            
                                        }else{
                                        
                                        echo "--";
                                        
                                        }
                                        ?>    
                                        </th>
                                <?php        
                                        
                                    }
                                }
                                ?>
                                <th style="margin:0px;text-align:center;">
                                    <?php 
                                    if($exam_type == "Marks"){ 
                                        $sub_pc=($sub_total*100)/$subject_mm;
                                        
                                        if($sub_pc > 33){
                                            echo $sub_total;    
                                        }else{
                                            echo "<u> ".$sub_total." </u>";
                                        }
                                    }else if($exam_type == "Grade"){
                                            // echo $grade;
                                    }
                                    ?>
                                </th>
                                <th style="margin:0px;text-align:center;">
                                    <?php 
                                    if($exam_type == "Marks"){
                                    if($sub_total >=91 && $sub_total<=100 ){
                                        echo "A1";
                                    }else if($sub_total >=81 && $sub_total<=90 ){
                                        echo "A2";
                                    }else if($sub_total >=71 && $sub_total<=80 ){
                                        echo "B1";
                                    }else if($sub_total >=61 && $sub_total<=70 ){
                                        echo "B2";
                                    }else if($sub_total >=51 && $sub_total<=60 ){
                                        echo "C1";
                                    }else if($sub_total >=41 && $sub_total<=50 ){
                                        echo "C2";
                                    }else if($sub_total >=33 && $sub_total<=40 ){
                                        echo "D";
                                    }else if($sub_total <=32){
                                        echo "E";
                                    }
                                    }else{
                                        echo $grade;
                                    }
                                    
                                    ?>
                                    
                                </th>
                            </tr>                    
                                    <?php           
                                    $grand_total= $grand_total + $sub_total;
                                    }
                                }
                            }
                            ?>
                            
                            
                            
                            <?php
                            // $mm_total=0;
                            // $grand_total=0;
                            // $hy_grand_total=0;
                            // $annual_grand_total=0;
                            
                            
                            // $main_subject_count
                            $additional_subject_count=0;
                            $total_subjects=$main_subject_count;
                            // Additional Subjects
                            if($get_course_exam_subjects){
                                foreach($get_course_exam_subjects as $subjects){
                                    $sub_type=$subjects['subject_type'];
                                    
                                    $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 && $sub_type != "Main Subject"){
                                        $additional_subject_count=$additional_subject_count + 1;
                                        
                                        $subject_mm=$subjects['MM'];
                                        
                                        
                                        $sub_total=0;
                                        
                                    
                                        
                                        
                            ?>                    
                            <tr style="padding:0px;margin:0px;">
                                <th style="margin:0px;text-align:left;font-size:12px;width:200px !important" colspan="2">&nbsp;<?php echo $subjects['subject']; ?>
                                </th>
                                <th><?php 
                                if($exam_type == "Marks"){ echo $subject_mm; }else{ echo "Grade"; }
                                ?></th>
                                <?php 
                                $grade="";
                                if($get_course_exam_heads){
                                foreach($get_course_exam_heads as $exam_heads){
                            ?>         
                                <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;
                                                if($obtained_marks > $subject_mm){
                                            echo "<span style='background:red;'>".$elog['obtained_marks']."</span>";
                                        }else{
                                            echo $elog['obtained_marks'];
                                        }
                                            }else if($exam_type == "Grade"){
                                                    $grade=$elog['grade'];
                                                // echo $elog['grade'];
                                            }
                                        }    
                                        }
                                            }
                                            
                                        }else{
                                            echo "--";
                                        }
                                        ?>    
                                        </th>
                                <?php        
                                    }
                                }
                                ?>
                                <th style="margin:0px;text-align:center;">
                                    <?php 
                                    if($exam_type == "Marks"){ 
                                        $sub_pc=($sub_total*100)/$subject_mm;
                                        
                                        if($sub_pc > 33){
                                            echo $sub_total;    
                                        }else{
                                            echo "<u> ".$sub_total." </u>";
                                        }
                                    }else if($exam_type == "Grade"){
                                            // echo $grade;
                                    }
                                    ?>
                                </th>
                                <th style="margin:0px;text-align:center;">
                                    <?php 
                                    if($exam_type == "Marks"){
                                    if($sub_total >=91 && $sub_total<=100 ){
                                        echo "A1";
                                    }else if($sub_total >=81 && $sub_total<=90 ){
                                        echo "A2";
                                    }else if($sub_total >=71 && $sub_total<=80 ){
                                        echo "B1";
                                    }else if($sub_total >=61 && $sub_total<=70 ){
                                        echo "B2";
                                    }else if($sub_total >=51 && $sub_total<=60 ){
                                        echo "C1";
                                    }else if($sub_total >=41 && $sub_total<=50 ){
                                        echo "C2";
                                    }else if($sub_total >=33 && $sub_total<=40 ){
                                        echo "D";
                                    }else if($sub_total <=32){
                                        echo "E";
                                    }
                                    }else{
                                        echo $grade;
                                    }
                                    ?>
                                </th>
                            </tr>                    
                                    <?php      
                                    
                                    
                                    if($total_subjects < 5){
                                        $mm_total=$mm_total + $subjects['MM'];
                                        $grand_total= $grand_total + $sub_total;
                                        $total_subjects=$total_subjects + 1;
                                    }
                                    
                                    }
                                }
                            }
                            ?>
                            <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 colspan="<?php echo $ehcount; ?>"></th>
                                <!--<th></th>-->
                                <!--<th></th>-->
                                <!--<th></th>-->
                                <!--<th></th>-->
                                
                                <th style="text-align:center;margin:0px;"><?php echo $grand_total; ?></th>
                                <th></th>
                            </tr>
                        </tbody>
                    </table>
                    <table style="width:100%;margin-top:20px;">
                            <?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;width:25%;"]> Rank in class : <?php
                                    if($rank_in_class <= 10){
                                        echo $rank_in_class;        
                                    }
                                ?></th>
                                <th style="margin:0px;text-align:left;padding-left:5px;width:25%;"> PERCENTAGE : <b>&nbsp; <?php echo number_format((float)$percentage, 2, '.', ''); ?> % </b></th>
                                
                                <th style="margin:0px;text-align:left;padding-left:5px;width:25%;">Height : <?php echo $height; ?> cm</th>
                                <th style="margin:0px;text-align:left;padding-left:5px;width:25%;">Weight : <?php echo $weight; ?> kg</th>
                            </tr>
                            
                        
                    </table>
                </div>
      
                <table class="table table-bordered">
                    <tbody>
                        <tr>
                            <th colspan="3" style="text-align:left;">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><br>-->
                                
                                <br>
                                <?php 
                                if(PrincipalSign !=""){
                                ?>
                                    <img src="<?php echo base_url().PrincipalSign; ?>" style="height:40px;width:auto;">
                                <?php
                                } 
                                ?>    
                                    <br>
                                    PRINCIPAL
                                </th>
                            </tr>
                        </tbody>
                </table>
                </div>
            </div>
                
        </div>
        
        
        <?php
                            }
                        // }
                //     }
                // }
            }
        }
        ?>
    </body>
</html>