<?php 
$acedemic_session=$this->session->userdata('acedemic_session');
$acedemic_session_table='academic_session_details';
$get_acedemic_session_data=$this->NewExamModel->get_item_data($acedemic_session,$acedemic_session_table);
$year_long='';
if($get_acedemic_session_data){
    foreach($get_acedemic_session_data as $acedemic_session_data){
        $year_long=$acedemic_session_data['year_long'];
    }
}
$courses_table='courses';

?>
<div class="container mt-3 mb-3 pt-3 pb-3 bg-white">
    <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">Exam</a></li>
                <li class="breadcrumb-item"><a href="<?php echo base_url();?>Examination/dashboard">Subjects</a></li>
                <li class="breadcrumb-item"><a href="#">Add Exam Subject</a></li>
            </ul>
            
            
            <form method="post" action="#">
                
                <div class="form-group">
                    <label>Course</label>
                    <select class="form-control" id="course_id">
                        <?php 
                        $all_courses=$this->NewExamModel->get_data($courses_table);
                        if($all_courses){
                            foreach($all_courses as $courses){
                        ?>
                        <option value="<?php echo $courses['id'];?>"><?php echo $courses['course']; echo " "; echo  $courses['subject_stream'];?></option>
                        <?php
                            }
                        }
                        ?>
                        
                    </select>
                </div>
                
                <div class="form-group">
                    <label>Acedemic Session</label>
                    <input type="hidden" name="acedemic_session" id="acedemic_session" value="<?php echo $acedemic_session;?>">
                    <input type="text" name="acedemic_session_info" id="acedemic_session_info" class="form-control" value="<?php echo $year_long; ?>" readonly>
                </div>
                
                <div class="form-group" id="subjects-sec">
                    <label>Subject</label>
                    <input type="text" id="subject" class="subject form-control mt-1 mb-1" value="" placeholder="Subject" required>
                </div>
                <button type="button" id="more_fields" onclick="add_fields();" class="btn btn-sm btn-info" >Add More Subject</button>
                <button type="button" id="add_subject" class="btn btn-sm btn-success">Add Subject</button>
            </form>
            
        </div>
    </div>
</div>

<script>

function add_fields() {    
    var input_element ='<input type="text" id="subject" class="subject form-control mt-1 mb-1" value="" placeholder="Subject" required>';
        $("#subjects-sec").append(input_element);
     
    }
    
    
$(document).ready(function(){
    
    $("#add_subject").click(function(){
        // var taskObj = [];
        var acedemic_session = $('#acedemic_session').val();
        var course_id = $('#course_id').val();
        var acedemic_session_info = $('#acedemic_session_info').val();
        // alert()
        // if(task_date){
        
        $("input[id='subject']").each(function() {
            var subject = $(this).val();
            
            $.ajax({
                url: '<?php echo base_url(); ?>Examination/insert_subjects/',
                type: 'POST',
                data: {acedemic_session:acedemic_session,course_id:course_id,acedemic_session_info:acedemic_session_info,subject:subject},
                success: function(data){
                    alert(data);
                       /*$("#form")[0].reset();*/
                       
               }
            });
            
        });
        // location.reload();
        
        // }else{
        //         alert("Select Task Date");
        //     }
        
    });
});
</script>