<?php
$acedemic_session=$this->session->userdata('acedemic_session');
$course_id=$this->uri->segment('3');
$course_table='courses';
$course='';
$subject_stream='';
$get_course_info=$this->WelcomeModel->get_item_data($course_id,$course_table);
if($get_course_info){
    foreach($get_course_info as $info){
        $course=$info['course'];
        $subject_stream=$info['subject_stream'];
    }
}
?>

<div class="container bg-white mt-3 mb-3 pt-3 pb-3">
    <div class="row">
        <div class="col-sm-12">
            <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();?>School/courses">Courses</a></li>
                <li class="breadcrumb-item"><?php echo $course; echo " "; echo $subject_stream;?></li>
            </ul>
            <div class="table-responsive">
                <table class="table table-bordered table-striped">
                    <thead>
                        <tr>
                            
                            <th colspan="4">Acedemic Session : <?php echo $this->session->userdata('ac_year');?></th>
                            <th colspan="3">Class: <?php echo $course; echo " "; echo $subject_stream;?></th>
                        </tr>
                        <tr>
                            <th></th>
                            <th>#</th>
                            <th>S. Id.</th>
                            <th>Name</th>
                            <th>Father Name</th>
                            <th>D.O.B.</th>
                            <th>Class / Section</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $count=0;
                        $student_table='student';
                        $get_all_students=$this->WelcomeModel->get_data_in_alp_order($student_table);
                        if($get_all_students){
                            foreach($get_all_students as $students){
                                $sid=$students['id'];
                                $check_student_in_acedemic_session=$this->WelcomeModel->get_student_in_acedemic_session($sid,$acedemic_session);
                                if($check_student_in_acedemic_session){
                                    foreach($check_student_in_acedemic_session as $ac_exist){
                                        
                                        if($ac_exist['class'] == $course){
                                            $count = $count + 1;
                                     ?>
                        <tr>
                            <td><input type="checkbox" name="student_id" id="student_item" class="student_item" value="<?php echo $sid;?>"></td>
                            <td><?php echo $count;?></td>
                            <td><?php echo $sid;?></td>
                            <td><?php echo $students['student_name'];?></td>
                            <td><?php echo $students['father_name'];?></td>
                            <td><?php if($students['dob']){ echo date('d-m-Y',strtotime($students['dob']));}?></td>
                            <td><?php echo $course; echo " "; echo $subject_stream;?> / <?php echo $ac_exist['section']; ?></td>
                        </tr>                            
                                                                 
                                     
                                     <?php
                                        }
                                    }
                                }
                                
                            }
                        }
             ?>
                        
                        

                    </tbody>
                </table>
            </div>
            <form method="post" action="#" class="mt-3 mb-3">
                <div class="form-group">
                    <select class="form-control" name="" id="course_section">
                        <option value="">-- select section --</option>
                        <option value="A">A</option>
                        <option value="B">B</option>
                        <option value="C">C</option>
                        <option value="D">D</option>
                    </select>
                </div>
                <div class="form-group">
                    <button type="button" id="update_course_sestion" class="btn btn-sm btn-success">Update Section</button>
                </div>
                
            </form>
        </div>
    </div>
</div>

<script>
    $(document).ready(function(){
       $("#update_course_sestion").click(function(){
           
            var course_id='<?php echo $course_id;?>';
            var acedemic_session='<?php echo $acedemic_session;?>';
            var course_section=$("#course_section").val();
            
            if(course_section != ""){
            $.each($("input[class='student_item']:checked"), function(){
                var sid=$(this).val();
                $.ajax({
                        url: '<?php echo base_url() ?>Settings/update_student_course_sestion',
                        type: 'POST', 
                        data: { sid:sid,course_id:course_id,acedemic_session:acedemic_session,course_section:course_section},
                        success: function(data){
                            swal(data);
                            location.reload();
                        }
                });
                
            });
            }else{
                alert("Select Course Section");
            }    
       });
    });
</script>