<style>
    .wf-l{
        float:left;
        width:50%;
        padding-right:10px;
    }
    .wf-r{
        float:left;
        width:50%;
        padding-left:10px;
    }
    
    /* Chrome, Safari, Edge, Opera */
    input::-webkit-outer-spin-button,
    input::-webkit-inner-spin-button {
        -webkit-appearance: none;
        margin: 0;
    }

    /* Firefox */
    input[type=number] {
        -moz-appearance: textfield;
    }
    
     .w-25{
        float:left;
        padding:5px 10px;
    }
    .w-50{
        float:left;
        padding:5px 10px;
    }
    
    .w-100{
        
        padding:5px 10px;
    }
    
    input[type=number],input[type=text] {
        min-width:100px;
        text-align:center;
        padding:0px !important;
        vertical-align:middle;
    }
    
    @media only screen and (max-width: 767px) {
        .w-25{
            width:100% !important;
        }
        
        .w-50{
            width:100% !important;
        }
        
        
    }
</style>

<?php 
$sid=$this->uri->segment('3');

$exam_id=$this->uri->segment('4');
$acedemic_session=$this->session->userdata('acedemic_session');
$course_id='';
$class="";
$subject_stream='';
$roll_no="";
$height="";
$weight="";

$scd_table="student_course_data";
$student_course_info=$this->ExamManagementModel->get_student_combined_data($sid,$acedemic_session,$scd_table);
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'];
        // $height=$course_info["height"];
        // $weight=$course_info["weight"];
    }
}

?>

<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
                }
                ?>
                
            
            
                <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->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);
                            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->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);
                            $all_exam_heads=$this->ExamManagementModel->get_course_exam_wise_heads($class,$exam_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->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($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  "s_".$subject_id."_h_".$ehid; ?>" 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  "s_".$subject_id."_h_".$ehid; ?>" value="<?php echo $elog_data['grade']; ?>" class="form-control" <?php if($exam_heads['freeze'] == 1){ echo "readonly"; } ?> >
                                <?php
                                        // echo $elog_data['grade'];
                                }
                                
                                }
                                ?>
                                
                            </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 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  "s_".$subject_id."_h_".$ehid; ?>" value="" class="form-control" <?php if($exam_heads['freeze'] == 1){ echo "readonly"; } ?>>
                                <?php
                                        // echo $elog_data['grade'];
                                }
                                ?>
                                
                            </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 $class; ?>" >
                <div class="form-group">
                    <button type="submit" name="add_exam_log" class="btn btn-sm  btn-success">Submit</button>
                </div>
            </form>  
        </div>
    </div>
</div>
<?php 
$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'];
        $height=$exam_data["height"];
        $weight=$exam_data["weight"];
    }
}


$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 $class; ?>">
                    
                </div>
                <div class="form-group w-25">
                    <label>Attendance(Present Days)</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-50">
                    <label>Height (in cm)</label>
                    <input type="number" name="height" class="form-control height" placeholder="Student Height" value="<?php echo $height; ?>">
                </div>
                
                <div class="form-group w-50">
                    <label>Weight (in kg)</label>
                    <input type="number" name="weight" class="form-control weight" placeholder="Roll Number" value="<?php echo $weight; ?>">
                </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();
            var height=$(".height").val();
            var weight=$(".weight").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,height:height,weight:weight},

                success: function(data){
                    alert(data);
                    // swal(data);
                    // alert("Updated Successfully");
                    location.reload(); 
                }
            });
        });
    });
</script>