<?php 

?>
<style>
    form{
        box-shadow:4px 4px 8px silver;
        width:100%;
        display:inline-block;
        padding:20px;
        margin-top:20px;
        margin-bottom:20px;
    }
    
    form h3{
        text-align:center;
        font-size:18px;
        font-size:600;
    }
    
    .w-25{
        float:left;
        padding:5px 10px;
    }
    .w-50{
        float:left;
        padding:5px 10px;
    }
    
    .w-100{
        
        padding:5px 10px;
    }
</style>

<?php 
$roll_no="";
$sid=$this->uri->segment('3');
$exam_id=$this->uri->segment('4');
$scd_table="student_course_data";
$acedemic_session=$this->session->userdata('acedemic_session');
// $student_course_info=$this->NewExamModel->student_course_info($sid,$acedemic_session);
$student_course_info=$this->ExamManagementModel->get_student_combined_data($sid,$acedemic_session,$scd_table);

$course_id='';
$class="";
$subject_stream='';
if($student_course_info){
    foreach($student_course_info as $course_info){
        $course_id=$course_info['course_id'];
        $class=$course_info["scd_class"];
        $subject_stream=$course_info['subject_stream'];
        $roll_no=$course_info['roll_no'];
    }
}

?>
<div class="container-fluid bg-white ">
    <div class="row">
        <div class="col-sm-12 pt-3 pb-3">
            
            <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_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>
                                <a href="<?php echo base_url();?>ExamManagement/update_exam_log/<?php echo $sid;?>" class="btn btn-sm btn-warning">
                                    <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-pencil-square" viewBox="0 0 16 16">
                                        <path d="M15.502 1.94a.5.5 0 0 1 0 .706L14.459 3.69l-2-2L13.502.646a.5.5 0 0 1 .707 0l1.293 1.293zm-1.75 2.456-2-2L4.939 9.21a.5.5 0 0 0-.121.196l-.805 2.414a.25.25 0 0 0 .316.316l2.414-.805a.5.5 0 0 0 .196-.12l6.813-6.814z"/>
                                        <path fill-rule="evenodd" d="M1 13.5A1.5 1.5 0 0 0 2.5 15h11a1.5 1.5 0 0 0 1.5-1.5v-6a.5.5 0 0 0-1 0v6a.5.5 0 0 1-.5.5h-11a.5.5 0 0 1-.5-.5v-11a.5.5 0 0 1 .5-.5H9a.5.5 0 0 0 0-1H2.5A1.5 1.5 0 0 0 1 2.5v11z"/>
                                    </svg>
                                </a>
                                
                                <!--<a href="#" class="btn btn-sm btn-warning">-->
                                <!--    <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-pencil-square" viewBox="0 0 16 16">-->
                                <!--        <path d="M15.502 1.94a.5.5 0 0 1 0 .706L14.459 3.69l-2-2L13.502.646a.5.5 0 0 1 .707 0l1.293 1.293zm-1.75 2.456-2-2L4.939 9.21a.5.5 0 0 0-.121.196l-.805 2.414a.25.25 0 0 0 .316.316l2.414-.805a.5.5 0 0 0 .196-.12l6.813-6.814z"/>-->
                                <!--        <path fill-rule="evenodd" d="M1 13.5A1.5 1.5 0 0 0 2.5 15h11a1.5 1.5 0 0 0 1.5-1.5v-6a.5.5 0 0 0-1 0v6a.5.5 0 0 1-.5.5h-11a.5.5 0 0 1-.5-.5v-11a.5.5 0 0 1 .5-.5H9a.5.5 0 0 0 0-1H2.5A1.5 1.5 0 0 0 1 2.5v11z"/>-->
                                <!--    </svg>-->
                                <!--</a>-->
                                
                                <a href="<?php echo base_url(); ?>ExamManagement/exam_attendance_log/<?php echo $sid;?>" class="btn btn-sm btn-outline-info">Attendance Log</a>
                            </th>
                            
                            
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $sub_count=0;
                            $subject_table='exam_subject_master_'.$acedemic_session.'';
                            $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->ExamManagementModel->get_course_exam_heads($class,$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->ExamManagementModel->get_log_item_data($sid,$ehid,$subject_id,$elog_table);
                                    if($get_log_item_data){
                                        foreach($get_log_item_data as $elog_data){
                            ?>
                            <td>
                                <?php 
                                if($subjects['exam_type'] == 'Marks'){
                                    echo $elog_data['obtained_marks'];
                                }else if($subjects['exam_type'] == 'Grade'){
                                echo $elog_data['grade'];
                                }
                                ?>
                                
                            </td>
                            <?php
                                        }
                                    }else{
                            ?>
                            <td>---</td>                            
                            <?php
                                    }
                            ?>
                                                        
                            
                            <?php
                                }
                            }
                            ?>
                            <td>
                                --
                            </td>
                        </tr>    
                            <?php
                                }
                            }
                            ?>
                    </tbody>
                </table>
            </div>
                  
        </div>
    </div>
</div>
<?php 
// $exam_id='2';
$course_exam_data_table='exam_course_data';
$get_exam_course_data=$this->ExamManagementModel->check_course_exam_data($exam_id,$course_id,$course_exam_data_table);
$total_working_days=0;
if($get_exam_course_data){
    foreach($get_exam_course_data as $exam_data){
        $total_working_days=$exam_data['working_days'];
    }
}


$attendance_days=0;
$remark='';
$result_table='exam_result';
$check_student_exam_data=$this->ExamManagementModel->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'];
    }
}


$exam_title="";
$exams_table="all_exams";
$get_exam_data=$this->ExamManagementModel->get_item_data($exam_id,$exams_table);
if($get_exam_data){
    foreach($get_exam_data as $exam_data){
        $exam_title=$exam_data["exam_title"];        
    }
}
?>
<div class="container-fluid bg-white">
    <div class="row">
        <div class="col-sm-12 pt-3 pb-3">
            <form class="form-outer">
                <div style="width:100%;display:inline-block;">
                    <h3>Update <?php echo $exam_title; ?> Exam Data</h3><hr>
                <div class="form-group w-25">
                    <label>Total Working Days</label>
                    <input type="number" value="<?php echo $total_working_days; ?>" name="total_working_days" class="form-control total_working_days" readonly>
                    <input type="hidden" class="student_id" value="<?php echo $sid;?>">
                    <input type="hidden" class="exam_id" value="<?php echo $exam_id;?>">
                    <input type="hidden" class="course_id" value="<?php echo $course_id; ?>">
                    <input type="hidden" class="course" value="<?php echo $clas; ?>">
                    
                </div>
                <div class="form-group w-25">
                    <label>Attendance</label>
                    <input type="number" name="attendance" class="form-control attendance" placeholder="Enter Present Days" value="<?php echo $attendance_days; ?>">
                </div>
                
                <div class="form-group w-50">
                    <label>Roll Number</label>
                    <input type="number" name="roll_no" class="form-control roll_no" placeholder="Roll Number" value="<?php echo $roll_no; ?>">
                </div>
                
                <div class="form-group w-100">
                    <label>Select Remark Refrence</label>
                    <select class="form-control remark">
                        <option value="">-- Select Remark --</option>
                        <?php 
                        $remark_table='exam_quote_list';
                        $all_remarks=$this->NewExamModel->get_data($remark_table);
                        if($all_remarks){
                            foreach($all_remarks as $remark_data){
                        ?>
                        <option value="<?php echo $remark_data['quote']; ?>" <?php if($remark == $remark_data['quote']){ echo "selected"; } ?> ><?php echo $remark_data['quote']; ?></option>
                        <?php
                            }
                        }
                        ?>
                    </select>
                </div>
                
                <div class="form-group w-100">
                    <label>Remark</label>
                    <input type="text" name="remark" class="form-control remark-input" value="<?php if($remark !=''){ echo $remark; } ?>" placeholder="Enter Remark">
                </div>
                <div class="form-group w-100">
                    <button class="btn btn-sm btn-success add-remark" type="button">Submit</button>
                </div>
                </div>
            </form>

        </div>
    </div>
</div>

<script>
    $(document).ready(function(){
        $(".remark" ).change(function() {
            var remark = $(this).val();
            $(".remark-input").val(remark);
        });
        
        
        $(".annual_remark" ).change(function() {
            var remark = $(this).val();
            $(".annual-remark-input").val(remark);
        });
        
        
        $(".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_no=$(".roll_no").val();
            $.ajax({
                url: '<?php echo base_url(); ?>ExamManagement/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_no:roll_no},
                success: function(data){
                    alert(data);
                    // swal(data);
                    // alert("Updated Successfully");
                    location.reload(); 
                }
            });
        });
        
        $(".add-annual-remark").click(function(){
            var remark=$(".annual-remark-input").val();
            var total_working_days=$(".annual_total_working_days").val();
            var attendance=$(".annual_attendance").val();
            var sid=$(".student_id").val();
            var exam_id=$(".annual_exam_id").val();
            var course_id=$(".course_id").val();
            var course=$(".course").val();
            $.ajax({
                url: '<?php echo base_url(); ?>ExamManagement/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},
                success: function(data){
                    alert(data);
                    // swal(data);
                    // alert("Updated Successfully");
                    location.reload(); 
                }
            });
        });
    });
</script>