<style>
    .w-20{
        width:20%;
        float:left;
        padding:0px 10px;
    }
    
    .w-20 label{
        width:100%;
    }
    /*#AddPeriod{*/
    /*    width:100%;*/
    /*}*/
    

.w-15{
    width:15%;
    float:left;
    padding:0px 10px;
    /*margin-top:10px;*/
}

.w-10{
    width:10%;
    float:left;
    padding:0px 10px;
    /*margin-top:10px;*/
}
    
.w-25{
    width:25%;
    float:left;
    padding:0px 10px;
    /*margin-top:10px;*/
}
    .w-30{
        width:30%;
        float:left;
        padding:0px 10px;
        /*margin-top:10px;*/
    }
    .week_day_label{
        width:16%;
    }
    .w-100{
        width:100%;
    }
</style>
<?php 
$id=$this->uri->segment("3");
$table="time_table";
$get_time_table_data=$this->TimeTableModel->get_item_data($id,$table);
if($get_time_table_data){
    foreach($get_time_table_data as $data){
?>
<div class="container-fluid mt-3 mb-3 bg-white">
    <div class="row">
        <div class="col-sm-12 pt-3 pb-3">
            <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(); ?>TimeTableManagement">Time Table</a>
                </li>
                <li class="breadcrumb-item">
                   Schedule
                </li>
            </ul>
            
            <!--<button class="btn btn-sm btn-success add-new-record" type="button">Add New Record</button>-->
            
            <form method="post" action="#" id="AddPeriodForm">
                <h1 class="main-heading" id="staticBackdropLabel">Add New Record</h1><hr>
                <input type="hidden" name="time_table_id" value="<?php echo $id;?>">
                <div class="form-group w-15">
                    <label>Class </label>
                    <select class="form-control course" name="class">
                        <option value="">--- Select Class ---</option>
                        <?php 
                        $all_classes=$this->TimeTableModel->all_classes();
                        if($all_classes){
                            foreach($all_classes as $courses){
                        ?>
                        <option value="<?php echo $courses['course']; ?>"><?php echo $courses['course']; ?></option>
                        <?php
                            }
                        }
                        ?>
                    </select>
                </div>
                <div class="form-group w-15">
                    <label>Section</label>
                    <select class="form-control" name="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>
                        <option value="E">E</option>
                        <option value="F">F</option>
                    </select>
                </div>
                <div class="form-group w-15">
                    <label>Period</label>
                    <select class="form-control" name="period">
                        <option value="">--- Select Period ---</option>
                        <?php 
                        $periods_table="time_table_periods";
                        $all_periods=$this->TimeTableModel->get_schedule_periods($id,$periods_table);
                        if($all_periods){
                            foreach($all_periods as $periods){
                        ?>
                        <option value="<?php echo $periods['id']; ?>"><?php echo $periods['period']; ?></option>
                        <?php
                            }
                        }
                        ?>
                        
                    </select>
                </div>
                    
                <div class="form-group w-30">
                    <label>Teacher</label>
                    <select class="form-control" name="teacher">
                        <option value="">--- Select Teacher ---</option>
                        <?php 
                        $staff_table="school_staff";
                        $all_staff=$this->TimeTableModel->get_all_teaching_staff($staff_table);
                        
                        if($all_staff){
                            foreach($all_staff as $staff){
                        ?>
                        <option value="<?php echo $staff['id']; ?>"><?php echo $staff['salutation']." ".$staff['name']; ?></option>
                        <?php
                            }
                        }
                        
                        ?>
                    </select>
                </div>
                <div class="form-group w-25">
                    <label>Subject</label>
                    <!--<input type="text" name="subject" class="form-control">-->
                    <select class="form-control subjects" name="subject" required>
                        
                    </select>
                </div>    
                <div class="w-100 mt-3">
                  
                    <p class="w-100">Day</p>
                    <!--<select class="form-control" name="week_day">-->
                    <!--    <option value="MON">MON</option>-->
                    <!--    <option value="TUE">TUE</option>-->
                    <!--    <option value="WED">WED</option>-->
                    <!--    <option value="THU">THU</option>-->
                    <!--    <option value="FRI">FRI</option>-->
                    <!--    <option value="SAT">SAT</option>-->
                    <!--</select>-->
                    
                    <label class="week_day_label">
                        <input type="checkbox" name="week_day[]" value="MON">&nbsp;&nbsp;Monday
                    </label>
                    
                    <label class="week_day_label">
                        <input type="checkbox" name="week_day[]" value="TUE">&nbsp;&nbsp;Tuesday
                    </label>
                    
                    <label class="week_day_label">
                        <input type="checkbox" name="week_day[]" value="WED">&nbsp;&nbsp;Wednesday
                    </label>
                    
                    <label class="week_day_label">
                        <input type="checkbox" name="week_day[]" value="THU">&nbsp;&nbsp;Thursday
                    </label>
                    
                    <label class="week_day_label">
                        <input type="checkbox" name="week_day[]" value="FRI">&nbsp;&nbsp;Friday
                    </label>
                    
                    <label class="week_day_label">
                        <input type="checkbox" name="week_day[]" value="SAT">&nbsp;&nbsp;Saturday
                    </label>
                </div>    
                <div class=" w-100">
                    <label>&nbsp;&nbsp;</label><br>
                    <button type="submit" class="btn btn-sm btn-primary">Submit</button>
                </div>
            </form>
            <a href="<?php echo base_url()."TimeTableManagement/time_table_view/".$id;?>" class="btn btn-sm btn-outline-danger mt-3 mb-3" target="_blank">Print</a>
            <div class="table-responsive">
                <table class="table table-bordered">
                    <thead>
                        <tr>
                            <th>Class</th>
                            <th>Action</th>
                            <?php 
                            $p_count=0;
                            $periods_table="time_table_periods";
                            $all_periods=$this->TimeTableModel->get_schedule_periods($id,$periods_table);
                            if($all_periods){
                            foreach($all_periods as $periods){
                                $p_count=$p_count + 1;
                            ?>
                            <th><?php echo $periods['period']."(".date('h:i:sa',strtotime($periods['start_time']))."-".date('h:i:sa',strtotime($periods['end_time'])).")"; ?></th>
                            <?php
                                }
                            }
                            ?>
                            
                        </tr>
                        
                    </thead>
                    <tbody>
                        <?php
                        $acedemic_session=$this->session->userdata('acedemic_session');
                        $all_classes=$this->TimeTableModel->get_classes_structre($acedemic_session);
                        if($all_classes){
                            foreach($all_classes as $classes){
                                $class=$classes['class'];
                                $section=$classes['section'];
                                
                        ?>
                        <tr>
                            <th><?php echo $classes['class']."-".$classes['section']; ?></th>
                            <th><a href='<?php echo base_url()."TimeTableManagement/class_section_time_table/".$id."/".$class."/".$section; ?>' class="btn btn-outline-warning btn-sm" target="_blank">Update</a></th>
                            
                            <?php 
                            $all_periods=$this->TimeTableModel->get_schedule_periods($id,$periods_table);
                            if($all_periods){
                            foreach($all_periods as $periods){
                                echo "<td>";
                                $period=$periods['id'];
                                $get_class_section_record=$this->TimeTableModel->get_class_section_record($id,$class,$section,$period);
                                if($get_class_section_record){
                                    foreach($get_class_section_record as $sch_data){
                                        echo $sch_data['week_day']." : ";
                                        echo $sch_data['salutation']." ".$sch_data['name']."<sup>(".$sch_data['subject'].")</sup><br>";
                                        
                                    }
                                }else{
                                    echo "---";
                                }
                            
                                echo "</td>";
                                }
                            }
                            ?>
                        </tr>
                        <?php
                            }
                        }
                        ?>
                        
                    </tbody>
                </table>
            </div>
            
        </div>
    </div>
</div>

<script>
    $(document).ready(function(){
        // $(document).on("click",".add-new-record",function(){
        //     $('#AddPeriod').modal({
        //         backdrop: 'static',
        //         keyboard: false
        //     });
        //     $("#AddNewRecordModal").modal('show');
        // });
        
        // $(".btn-close").on("click",function(){
        //     $("#AddPeriod").modal('hide');   
        // });
        
        $("#AddPeriodForm").submit(function(e){
            e.preventDefault();           
            var error_count=0;                      
            if(error_count == 0){
                // $("#staticBackdrop").modal('show');
                $.ajax({
                    url: "<?php echo base_url(); ?>TimeTableManagement/generate_time_table",
                    type: 'post',
                    contentType: false,
                    cache: false,
                    processData:false,
                    data:  new FormData(this),
                    success: function(response) {
                        alert(response);
                        location.reload();
                    },
                    // error: function(jqXHR, textStatus, errorThrown) {
                    //     console.log(textStatus, errorThrown);
                    // }
                });
            }else{
                // alert("Please add order details !");
            }
        });
        
        $(".course").change(function(){
            // alert("Hi");
            var course=$(this).val();
            // alert(course);
            $.ajax({
                url: '<?php echo base_url(); ?>TimeTableManagement/get_course_subjects',
                type: 'POST',
                data:{course:course},
                success: function(response){
                    // alert(data);
                    // if(data !=""){
                        $(".subjects").html(response);
                    // }
                }
            })
        })
    });
</script>
<?php
    }
}
?>