<?php 
$sid=$this->uri->segment('3');
$acedemic_session=$this->session->userdata('acedemic_session');
$student_course_info=$this->NewExamModel->student_course_info($sid,$acedemic_session);
$course_id='';
if($student_course_info){
    foreach($student_course_info as $course_info){
        $course_id=$course_info['course_id'];
    }
}

$course_table='courses';
$course='';
$subject_stream='';
$class="";
$get_course_data=$this->NewExamModel->get_item_data($course_id,$course_table);
if($get_course_data){
    foreach($get_course_data as $course_data){
        $course=$course_data['course'];
        $class=$course_data['course'];
        $subject_stream=$course_data['subject_stream'];
    }
}

?>

<div class="container-fluid bg-white ">
    <div class="row">
        <div class="col-sm-12 pt-3 pb-3">
            <form method="post" action="#">
                <?php 
                if($success){
                ?>
                <div class="alert alert-success"><?php echo $success; ?></div>
                <?php
                }
                if($error){
                ?>
                <div class="alert alert-danger"><?php echo $error; ?></div>
                <?php
                }
                ?>
                
            
            
                <div class="table-responsive form-outer">
                <table class="table table-bordered table-striped">
                    <thead>
                        <tr>
                            <th>S.No.</th>
                            <th>Subject</th>
                            <?php 
                            $eh_count=0;
                            $exam_head_table='exam_head_master_'.$acedemic_session.'';
                            // $all_exam_heads=$this->NewExamModel->get_course_exam_heads($course_id,$exam_head_table);
                            
                            $all_exam_heads=$this->ExamManagementModel->get_course_exam_heads($class,$exam_head_table);
                            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
                                }
                            }
                            ?>
                            <!--<th>Edit</th>-->
                            
                            
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $sub_count=0;
                            $subject_table='exam_subject_master_'.$acedemic_session.'';
                            // $all_active_subjects=$this->NewExamModel->get_course_active_subjects($course_id,$subject_table);
                            $all_active_subjects=$this->ExamManagementModel->get_course_active_subjects($class,$subject_table);
                            if($all_active_subjects){
                                foreach($all_active_subjects as $subjects){
                                    $sub_count=$sub_count + 1;
                                    $subject_id=$subjects['id'];
                            ?>
                        <tr>
                            <th><?php echo $sub_count;?></th>
                            <th><?php echo $subjects['subject'];?></th>
                            <?php 
                            $exam_head_table='exam_head_master_'.$acedemic_session.'';
                            // $all_exam_heads=$this->NewExamModel->get_course_exam_heads($course_id,$exam_head_table);
                            
                            
                            if($all_exam_heads){
                                foreach($all_exam_heads as $exam_heads){
                                    $ehid=$exam_heads['id'];
                                    $elog_table='exam_log_'.$acedemic_session.'';
                                    $get_log_item_data=$this->NewExamModel->get_log_item_data($sid,$ehid,$subject_id,$elog_table);
                                    if($get_log_item_data){
                                        foreach($get_log_item_data as $elog_data){
                            ?>
                            <td>
                                
                                <select class="form-control" name="<?php echo  "s_".$subject_id."_h_".$ehid; ?>" style="color:#000;">
                                    <option value="">-- Select Attendance --</option>
                                    <option value="P" class="text-success" <?php if($elog_data['attendance'] == "P"){ echo "selected"; } ?>>Present</option>
                                    <option value="A" class="text-danger" <?php if($elog_data['attendance'] == "A"){ echo "selected"; } ?>>Absent</option>
                                    <option value="ML" class="text-danger" <?php if($elog_data['attendance'] == "ML"){ echo "selected"; } ?>>ML</option>
                                </select>
                            </td>
                            <?php
                                        }
                                    }else{
                            ?>
                            <td>
                               <select class="form-control" name="<?php echo  "s_".$subject_id."_h_".$ehid; ?>" style="color:#000;">
                                    <option value="">-- Select Attendance --</option>
                                    <option value="P" class="text-success" selected>Present</option>
                                    <option value="A" class="text-danger">Absent</option>
                                    <option value="ML" class="text-danger">ML</option>
                                </select>
                                </td>                            
                            <?php
                                    }
                            ?>
                                                        
                            
                            <?php
                                }
                            }
                            ?>
                        </tr>    
                            <?php
                                }
                            }
                            ?>
                    </tbody>
                </table>
                </div>
                <input type="hidden" name="sid" value="<?php echo $sid;?>" >
                <input type="hidden" name="course_id" value="<?php echo $course_id;?>" >
                <input type="hidden" name="class" value="<?php echo $course;?>" >
                <div class="form-group">
                    <button type="submit" name="add_exam_attendance_log" class="btn btn-sm  btn-success mt-3">Submit</button>
                </div>
            </form>  
        </div>
    </div>
</div>

<script>
    $(document).ready(function(){
        // $(".remark" ).change(function() {
        //     var remark = $(this).val();
        
        //     $(".remark-input").val(remark);
            // alert(remark);
            // $.ajax({
            //     url: '<?php echo base_url() ?>Welcome/change_ac_session',
            //     type: 'POST', 
            //     data: {ac_session_id:ac_session_id},
            //     success: function(data){
            //         // swal("Changed Successfully");
            //         location.reload(); 
            //     }
            // });
        // });
        // $(".add-remark").click(function(){
        //     var remark=$(".remark-input").val();
        //     var total_working_days=$(".total_working_days").val();
        //     var attendance=$(".attendance").val();
        //     var sid=$(".student_id").val();
        //     var exam_id=$(".exam_id").val();
        //     var course_id=$(".course_id").val();
        //     var course=$(".course").val();
        //     var roll_number=$(".roll_number").val();
        //     $.ajax({
        //         url: '<?php echo base_url(); ?>Examination/student_exam_data',
        //         type: 'POST', 
        //         data: {sid:sid,remark:remark,total_working_days:total_working_days,attendance:attendance,exam_id:exam_id,course_id:course_id,course:course,roll_number:roll_number},
        //         success: function(data){
        //             swal(data);
        //             // alert("Updated Successfully");
        //             location.reload(); 
        //         }
        //     });
        // });
    });
</script>