<?php
$sid=$this->uri->segment('3');
$acedemic_session=$this->session->userdata('acedemic_session');
$acedemic_session_table='academic_session_details';
$acedemic_session_data=$this->SchoolModel->get_item_data($acedemic_session,$acedemic_session_table);
$course_id='';
$course='';
$subject_stream='';

$student_subjects='student_subjects';

$get_students_course_info=$this->NewExamModel->student_course_info($sid,$acedemic_session);
if($get_students_course_info){
    foreach($get_students_course_info as $course_info){
        $course_id=$course_info['course_id'];
        
    }
}
$course_table='courses';
$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'];
    }
}

$exam_subjects_table='exam_subjects_'.$acedemic_session;
$get_course_subjects=$this->NewExamModel->get_course_active_subjects($course_id,$exam_subjects_table);

$get_student_subjects=$this->NewExamModel->get_student_subjects($sid,$course_id,$acedemic_session);
?>

<div class="container  pt-3 pb-3 bg-white mt-3 mb-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();?>Examination/course_students/<?php echo $course_id;?>">Exam</a></li>
                <li class="breadcrumb-item">Subject</li>
                <li class="breadcrumb-item">Allocate</li>
                
            </ul>
<?php            
$table='student';
$profile_pic='';
$student_data_table='student_data';
$get_student_profile_pic_data=$this->SchoolModel->get_student_data($sid,$student_data_table);
if($get_student_profile_pic_data){
    foreach($get_student_profile_pic_data as $student_profile_pic_data){
        $profile_pic=$student_profile_pic_data['profile_pic'];
    }
}else{
    
}
$student_information=$this->SchoolModel->get_item_data($sid,$table);
if($student_information){
    foreach($student_information as $data){
?>
            <div class="profile-div text-center pb-3">
                <?php
                if($profile_pic !=''){
                ?>
                <img src="<?php echo base_url();?>student-data/profile-img/<?php echo $profile_pic;?>" style="border-radius:50%;width:200px;">
                <?php
                }else{
                ?>
                <img src="<?php echo base_url();?>assets/student-profile-pic.jpg" style="border-radius:50%;">
                <?php
                }
                ?>
            </div>
            
            <div class="table-responsive">
                <table class="table table-bordered table-striped">
                    <thead>
                        <tr>
                            <?php
                            if($acedemic_session_data){
                                foreach($acedemic_session_data as $session_data){
                            ?>
                            <th colspan="2" class="text-center">Personal Information (<?php echo $session_data['year_long']; ?>)</th>
                            <?php
                                }
                            }
                            ?>
                            
                        </tr>
                        <tr>
                            <th>Name</th>
                            <td><?php echo $data['student_name'];?></td>
                        </tr>
                        <tr>
                            <th>Father's Name</th>
                            <td><?php echo $data['father_name'];?></td>
                        </tr>
                        <tr>
                            <th>D.O.B.</th>
                            <td><?php if($data['dob']){echo date('d-M-Y',strtotime($data['dob']));}else{ echo "----";}?></td>
                        </tr>
                        <tr>
                            <th>Contact No.:</th>
                            <td><?php echo $data['contact_no']; echo ","; echo $data['alternate_no'];?></td>
                        </tr>
                        <tr>
                            <th>Address : </th>
                            <td><?php echo $data['address'];?></td>
                        </tr>
                        <tr>
                            <th>Class : </th>
                            <?php 
                            $courses_table='courses';
                            if($get_students_course_info){
                                foreach($get_students_course_info as $course_data){
                                    $course_id=$course_data['course_id'];
                                    $get_course_info=$this->SchoolModel->get_item_data($course_id,$courses_table);
                                    if($get_course_info){
                                        foreach($get_course_info as $course_info){
                            ?>
                            <td><?php echo $course_info['course']; echo " "; echo $course_info['subject_stream']; ?></td>
                            <?php
                                        }
                                    }
                                }
                            }else{
                            ?>
                            <td></td>
                            <?php
                            }
                            ?>
                        </tr>
                    </thead>
                </table>
            </div>
  
<?php
    }   
}
?>
            
            
            <form method="post" action="#" class="mt-3">
                
                <div class="form-group">
                    <table class="table table-bordered">
                        <tr>
                            
                            <th><?php echo $course; echo " ";  echo $subject_stream;?></th>
                        </tr>
                        <?php
                        if($get_course_subjects){
                            foreach($get_course_subjects as $subjects){
                                $subject_id=$subjects['id'];
                                $student_sub_count=0;
                                $get_subject_info=$this->NewExamModel->student_subject_info($sid,$course_id,$acedemic_session,$subject_id);
                                if($get_subject_info){
                                    $student_sub_count=$student_sub_count + 1;
                                }
                                
                            ?>
                        
                        <tr>
                            
                            <td style="width:100%;">
                                <input type="hidden" name="sid" value="<?php echo $sid; ?>">
                                <input type="hidden" name="course_id" value="<?php echo $course_id; ?>">
                                <input type="hidden" name="acedemic_session" value="<?php echo $acedemic_session; ?>">
                                <input type="hidden" name="" value="">
                                <input type="checkbox" value="<?php echo $subjects['id'];?>" name="subject_item[]" id="subject_item" <?php if($student_sub_count == 1){ echo "checked";} ?>>&nbsp;&nbsp;<?php echo $subjects['subject']; ?>
                                </td>
                        </tr>
                        <?php
                            }
                        }
                        ?>
                        
                    </table>
                </div>
                <div class="form-group">
                    <label></label>
                    <button class="btn btn-sm btn-success" name="allot_subjects" type="submit">Allot Subjects</button>
                </div>
            </form>
        </div>
    </div>
</div>


<script>
    $(document).ready(function(){
       $("#allot_subjects").click(function(){
           var sid='<?php echo $sid;?>';
           var course_id='<?php echo $course_id;?>';
           var acedemic_session='<?php echo $acedemic_session;?>';
        //   alert(sid);
            $.each($("input[class='subject_item']:checked"), function(){
                var subjectid=$(this).val();
                // alert(subjectid);
                $.ajax({
                        url: '<?php echo base_url() ?>Examination/update_student_subjects',
                        type: 'POST', 
                        data: { sid:sid,course_id:course_id,acedemic_session:acedemic_session,subjectid:subjectid},
                        success: function(data){
                            swal(data);
                        }
                });
                
            });
                
       });
    });
</script>