<?php
$class=$this->uri->segment("3");
$section=$this->uri->segment("4");
$exam_id=$this->uri->segment("5");
$table="all_exams";
$get_exam_data=$this->ExamManagementModel->get_item_data($exam_id,$table);
$exam_title="";
if($get_exam_data){
    foreach($get_exam_data as $exam_data){
        $exam_title=$exam_data['exam_title'];
    }
}
$acedemic_session=$this->session->userdata("acedemic_session");
$exam_subjects_table='exam_subject_master_'.$acedemic_session;
$get_course_subjects=$this->ExamManagementModel->get_class_active_subjects($class,$exam_subjects_table);

$student_subjects_table="student_subjects_".$acedemic_session;


$eh_count=0;
$exam_head_table='exam_head_master_'.$acedemic_session.'';
// $all_exam_heads=$this->ExamManagementModel->get_course_exam_heads($class,$exam_head_table);

$all_exam_heads=$this->ExamManagementModel->get_course_exam_wise_heads($class,$exam_id,$exam_head_table);

$exam_heads_count=count($all_exam_heads);
?>
<style>
    input[type=text],input[type=number]{
        min-width:100px;
        padding:0px !important;
        text-align:center;
    }
</style>
<div class="container-fluid mt-3 mb-3 bg-white">
    <div class="row">
        <div class="col-sm-12 pt-3 pb-3">
            <ul class="breadcrumb">
                <li class="breadcrumb-item">
                    <a href="<?php echo base_url(); ?>Welcome">Home</a>
                </li>
                <li class="breadcrumb-item">
                    <a href="<?php echo base_url()."ExamManagement"; ?>">All Exams</a>
                </li>
                <li class="breadcrumb-item"><a href="<?php echo base_url()."ExamManagement/class_structure/".$exam_id; ?>"><?php echo $exam_title; ?></a></li>
                <li class="breadcrumb-item"><?php echo $class."-".$section?></li>
            </ul>
            <form method="post" action="#">
                
            <input type="hidden" name="class" value="<?php echo $class; ?>">
            <input type="hidden" name="section" value="<?php echo $section; ?>">
            <input type="hidden" name="exam_id" value="<?php echo $exam_id; ?>">
            
                <div class="table-responsive">
                <table class="table table-bordered">
                    <thead>
                        <tr>
                            <th rowspan="2" class="sl_no">Sl.No</th>
                            <th rowspan="2" class="sl_no">Roll No</th>
                            <th rowspan="2">Student Name</th>
                            <th rowspan="2">Father's Name</th>
                            <th rowspan="2">Mother's Name</th>
                            <?php
                            if($get_course_subjects){
                                foreach($get_course_subjects as $subjects){
                            ?>
                            <th colspan="<?php echo $exam_heads_count; ?>" class="text-center"><?php echo $subjects['subject']; ?></th>
                            <?php
                                }
                            }
                            ?>
                        </tr>
                        <tr>
                            <?php 
                            
                            if($get_course_subjects){
                                foreach($get_course_subjects as $subjects){
                            
                                    if($all_exam_heads){
                                        foreach($all_exam_heads as $exam_heads){
                                            $eh_count=$eh_count + 1;
                            ?>
                            <th><?php echo $exam_heads['exam_head']; echo "[".$exam_heads['mm']."]";?></th>
                            <?php
                                        }
                                    }
                                }
                            }
                            ?>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $i=0;
                        $academic_session=$this->session->userdata("acedemic_session");
                        // $all_class_and_sections=$this->ExamManagementModel->get_class_section_structure($academic_session);
                        $all_students=$this->ExamManagementModel->get_students_ac_session_by_course_and_class($academic_session,$class,$section);
                        if($all_students){
                            foreach($all_students as $student){
                                $i=$i + 1;
                                $sid=$student['id'];
                        ?>
                        <tr>
                            <td><?php echo $i; ?></td>
                            <td><?php echo $student['roll_no']; ?></td>
                            <td><?php echo $student['student_name']; ?></td>
                            <td><?php echo $student['father_name']; ?></td>
                            <td><?php echo $student['mother_name']; ?></td>
                            <?php
                            if($get_course_subjects){
                                foreach($get_course_subjects as $subjects){
                                    $subject_id=$subjects['id'];
                                    $student_sub_count=0;
                                    $get_subject_info=$this->ExamManagementModel->student_subject_info($sid,$acedemic_session,$subject_id,$student_subjects_table);
                                    if($get_subject_info){
                                        $student_sub_count=$student_sub_count + 1;
                                        
                                    }
                            
                                    if($all_exam_heads){
                                        foreach($all_exam_heads as $exam_heads){
                                            $eh_count=$eh_count + 1;
                                            $ehid=$exam_heads['id'];
                            $elog_table='exam_log_'.$acedemic_session;
                                            $field_name="student_e_log[".$sid."][".$subject_id."][".$ehid."]";
                            ?>
                            <td>
                            <?php
                                            
                            $get_log_item_data=$this->ExamManagementModel->get_log_item_data($sid,$ehid,$subject_id,$elog_table);
                            if($get_log_item_data){
                                foreach($get_log_item_data as $elog_data){
                            ?>
                            
                                <?php
                                if($elog_data['attendance'] == "A" || $elog_data['attendance'] == "ML"){
                                ?>
                                <span class="text-danger"><?php echo $elog_data['attendance']; ?></span>
                                <?php
                                }else{
                                if($subjects['exam_type'] == 'Marks'){
                                ?>
                                <input type="number" name="<?php echo  $field_name; ?>" value="<?php echo $elog_data['obtained_marks']; ?>" class="form-control"  <?php if($exam_heads['freeze'] == 1){ echo "readonly"; } ?> max="<?php echo $exam_heads['mm']; ?>" min="0">
                                <?php                                        }else if($subjects['exam_type'] == 'Grade'){
                                ?>
                                <input type="text" name="<?php echo  $field_name; ?>" value="<?php echo $elog_data['grade']; ?>" class="form-control" <?php if($exam_heads['freeze'] == 1){ echo "readonly"; } ?> >
                                <?php
                                        // echo $elog_data['grade'];
                                }
                                
                                }
                                        }
                                    }else{
                            
                                if($subjects['exam_type'] == 'Marks'){
                                ?>
                                <input type="number" name="<?php echo  $field_name; ?>" value="" class="form-control" <?php if($exam_heads['freeze'] == 1){ echo "readonly"; } ?> max="<?php echo $exam_heads['mm']; ?>" min="0">
                                <?php                                        }else if($subjects['exam_type'] == 'Grade'){
                                ?>
                                <input type="text" name="<?php echo  $field_name; ?>" value="" class="form-control" <?php if($exam_heads['freeze'] == 1){ echo "readonly"; } ?>>
                                <?php
                                        // echo $elog_data['grade'];
                                }
                                    }
                            ?>
                        </td>
                            <?php
                                        }
                                    }
                                    
                                }
                            }
                            ?>
                        
                                
                             
                        </tr>        
                            <?php
                                }
                            }
                           
                        ?>
                    </tbody>
                </table>
                </div>
                
                <div class="form-group mt-3">
                    <button class="btn btn-sm btn-success" type="submit" name="update_exam_log">Submit</button>
                </div>
            </form>
        </div>
    </div>
</div>