<?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='';
$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'];
        $subject_stream=$course_data['subject_stream'];
    }
}
?>
<div class="container mt-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();?>Examination">Exam</a></li>
                <li class="breadcrumb-item"><a href="<?php echo base_url();?>Examination/dashboard">All Courses</a></li>
                <li class="breadcrumb-item"><a href="<?php echo base_url();?>Examination/course_students/<?php echo $course_id;?>"><?php echo $course; echo " "; echo $subject_stream;?></a></li>
                <li class="breadcrumb-item">Log Sheet</li>
            </ul>
        </div>
    </div>
</div>
<?php
$this->load->view('erp/exam/new/student-profile');
?>
<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">
                <table class="table table-bordered table-striped">
                    <thead>
                        <tr>
                            <th>S.No.</th>
                            <th>Subject</th>
                            <?php 
                            $eh_count=0;
                            $exam_head_table='exam_heads_'.$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){
                                    $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_subjects_'.$acedemic_session.'';
                            $all_active_subjects=$this->NewExamModel->get_course_active_subjects($course_id,$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_heads_'.$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="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>
                                <?php
                                if($subjects['exam_type'] == 'Marks'){
                                ?>
                                <input type="number" name="<?php echo  "s_".$subject_id."_h_".$ehid; ?>" value="" class="form-control">
                                <?php
                                }else if($subjects['exam_type'] == 'Grade'){
                                ?>
                                <input type="text" name="<?php echo  "s_".$subject_id."_h_".$ehid; ?>" value="" class="form-control">
                                <?php
                                }
                                ?>
                                </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;?>" >
                <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>