<style>
    .sch_input{
        width:150px;
        padding:0px;
        margin-top:5px;
    }
    .sch_input option{
        /*padding:0px;*/
        /*width:100%;*/
        /*text-align:center;*/
    }
</style>
<?php 
$schedule_id=$this->uri->segment("3");
$class=$this->uri->segment("4");
$section=$this->uri->segment("5");

$get_class_subjects=$this->TimeTableModel->get_class_subjects($class);
?>
<div class="container-fluid 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"><a href="<?php echo base_url()."TimeTableManagement/add_time_table_records/".$schedule_id; ?>">Schedule</a></li>
                <li class="breadcrumb-item">Update Schedule Information</li>
            </ul>
            
            <form method="post" action="#">
                <h1 class="main-heading">Class : <?php echo $class."-".$section; ?></h1><hr>
                <?php 
                if($p1_d1_success){
                ?>
                <div class="alert alert-success"><?php echo $p1_d1_success; ?></div>
                <?php
                }
                if($p1_d1_error){
                ?>
                <div class="alert alert-danger"><?php echo $p1_d1_error; ?></div>
                <?php
                }
                ?>
                
                <?php 
                if($p2_d2_success){
                ?>
                <div class="alert alert-success"><?php echo $p2_d2_success; ?></div>
                <?php
                }
                if($p2_d2_error){
                ?>
                <div class="alert alert-danger"><?php echo $p2_d2_error; ?></div>
                <?php
                }
                ?>
                
                <?php 
                if($p3_d3_success){
                ?>
                <div class="alert alert-success"><?php echo $p3_d3_success; ?></div>
                <?php
                }
                if($p3_d3_error){
                ?>
                <div class="alert alert-danger"><?php echo $p3_d3_error; ?></div>
                <?php
                }
                ?>
                
                <?php 
                if($p4_d4_success){
                ?>
                <div class="alert alert-success"><?php echo $p4_d4_success; ?></div>
                <?php
                }
                if($p4_d4_error){
                ?>
                <div class="alert alert-danger"><?php echo $p4_d4_error; ?></div>
                <?php
                }
                ?>
                
                <?php 
                if($p5_d5_success){
                ?>
                <div class="alert alert-success"><?php echo $p5_d5_success; ?></div>
                <?php
                }
                if($p5_d5_error){
                ?>
                <div class="alert alert-danger"><?php echo $p5_d5_error; ?></div>
                <?php
                }
                ?>
                
                <?php 
                if($p6_d6_success){
                ?>
                <div class="alert alert-success"><?php echo $p6_d6_success; ?></div>
                <?php
                }
                if($p6_d6_error){
                ?>
                <div class="alert alert-danger"><?php echo $p6_d6_error; ?></div>
                <?php
                }
                ?>
                <div class="table-responsive">
                    <table class="table table-bordered ">
                        <thead>
                            <tr>
                                <th>Day</th>
                                <?php 
                                $p_count=0;
                                $periods_table="time_table_periods";
                                $all_periods=$this->TimeTableModel->get_schedule_periods($schedule_id,$periods_table);
                                if($all_periods){
                                foreach($all_periods as $periods){
                                $p_count=$p_count + 1;
                                ?>
                                <th class="text-center">
                                    <?php echo $periods['period']; ?>
                                    <br>
                                    <?php echo "(".date('h:i:sa',strtotime($periods['start_time']))."<br>".date('h:i:sa',strtotime($periods['end_time'])).")"?>
                                </th>
                                <?php
                                    }
                                }
                                ?>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $acedemic_session=$this->session->userdata('acedemic_session');
                            ?>
                            <tr>
                                <th>Mon</th>
                            
                                <?php
                                if($all_periods){
                                    foreach($all_periods as $periods){
                                ?>
                                <td class='text-center' style="padding:3px !important;">
                                <?php    
                                $period=$periods['id'];
                                $week_day_1="MON";
                                $get_day_schedule=$this->TimeTableModel->get_class_section_day_record($schedule_id,$class,$section,$period,$week_day_1);
                                $mon_tid="";
                                $mon_subject="";
                                $mon_subject_id="";
                                if($get_day_schedule){
                                    foreach($get_day_schedule as $sch_data){
                                        $mon_tid=$sch_data['tid'];
                                        $mon_subject=$sch_data['subject'];
                                        $mon_subject_id=$sch_data['subject_id'];
                                    }
                                }
                                ?>
                                <select class="sch_input" name="mon_schedule_data[<?php echo $period; ?>][teacher]" style="padding:3px !important;color:#000;">
                                    <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 if($mon_tid == $staff['id']){ echo "selected"; } ?>><?php echo $staff['salutation']." ".$staff['name']; ?></option>
                                <?php
                                        }
                                    }
                        
                                ?>
                                </select>
                                <br>
                                
                                <select class="sch_input" name="mon_schedule_data[<?php echo $period; ?>][subject_id]">
                                    <?php 
                                    if($get_class_subjects){
                                        foreach($get_class_subjects as $subjects){
                                    ?>
                                    <option value="<?php echo $subjects['id']; ?>" <?php if($mon_subject == $subjects['subject'] || $mon_subject_id== $subjects['id']){ echo "selected"; } ?>><?php echo $subjects['subject']; ?></option>
                                    <?php
                                        }
                                    }
                                    ?>
                                    
                                </select>
                                </td>
                                <?php 
                                    }
                                }
                                ?>
                            </tr>
                        <tr>
                            <th>Tue</th>
                            <?php
                            if($all_periods){
                                foreach($all_periods as $periods){
                            ?>
                            <td class='text-center' style="padding:3px !important;">
                                <?php    
                                $period=$periods['id'];
                                $week_day_2="TUE";
                                $tue_tid="";
                                $tue_subject="";
                                $tue_subject_id="";
                                
                                $get_day_schedule=$this->TimeTableModel->get_class_section_day_record($schedule_id,$class,$section,$period,$week_day_2);
                                if($get_day_schedule){
                                    foreach($get_day_schedule as $sch_data){
                                        $tue_tid=$sch_data['tid'];
                                        $tue_subject=$sch_data['subject'];
                                        $tue_subject_id=$sch_data['subject_id'];
                                    }
                                }
                                ?>
                                <select class="sch_input" name="tue_schedule_data[<?php echo $period; ?>][teacher]" style="padding:3px !important;color:#000;">
                                    <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 if($tue_tid == $staff['id']){ echo "selected"; } ?>><?php echo $staff['salutation']." ".$staff['name']; ?></option>
                                    <?php
                                        }
                                    }
                        
                                    ?>
                                </select>
                                <br>
                                <select class="sch_input" name="tue_schedule_data[<?php echo $period; ?>][subject_id]">
                                    <?php 
                                    if($get_class_subjects){
                                        foreach($get_class_subjects as $subjects){
                                    ?>
                                    <option value="<?php echo $subjects['id']; ?>" <?php if($tue_subject == $subjects['subject'] || $tue_subject_id == $subjects['id']){ echo "selected"; } ?>><?php echo $subjects['subject']; ?></option>
                                    <?php
                                        }
                                    }
                                    ?>
                                    
                                </select>
                            </td>
                            <?php 
                                }
                            }
                            ?>
                        </tr>
                        <tr>
                            <th>Wed</th>
                            <?php
                            if($all_periods){
                                foreach($all_periods as $periods){
                            ?>
                            <td class='text-center' style="padding:3px !important;">
                                <?php    
                                $period=$periods['id'];
                                $week_day_3="WED";
                                
                                $wed_tid="";
                                $wed_subject="";
                                $wed_subject_id="";
                                
                                $get_day_schedule=$this->TimeTableModel->get_class_section_day_record($schedule_id,$class,$section,$period,$week_day_3);
                                if($get_day_schedule){
                                    foreach($get_day_schedule as $sch_data){
                                        $wed_tid=$sch_data['tid'];
                                        $wed_subject=$sch_data['subject'];
                                        $wed_subject_id=$sch_data['subject_id'];
                                    }
                                }
                                ?>
                                <select class="sch_input" name="wed_schedule_data[<?php echo $period; ?>][teacher]" style="padding:3px !important;color:#000;">
                                    <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 if($wed_tid == $staff['id']){ echo "selected"; } ?>><?php echo $staff['salutation']." ".$staff['name']; ?></option>
                                    <?php
                                        }
                                    }
                        
                                    ?>
                                </select>
                                <br>
                                <select class="sch_input" name="wed_schedule_data[<?php echo $period; ?>][subject_id]">
                                    <?php 
                                    if($get_class_subjects){
                                        foreach($get_class_subjects as $subjects){
                                    ?>
                                    <option value="<?php echo $subjects['id']; ?>" <?php if($wed_subject == $subjects['subject'] || $wed_subject_id == $subjects['id']){ echo "selected"; } ?>><?php echo $subjects['subject']; ?></option>
                                    <?php
                                        }
                                    }
                                    ?>
                                    
                                </select>
                            </td>
                            <?php 
                                }
                            }
                            ?>
                        </tr>
                        <tr>
                            <th>Thu</th>
                            <?php
                            if($all_periods){
                                foreach($all_periods as $periods){
                            ?>
                            <td class='text-center' style="padding:3px !important;">
                                <?php    
                                $period=$periods['id'];
                                $week_day_4="THU";
                                $thu_tid="";
                                $thu_subject="";
                                $thu_subject_id="";
                                
                                $get_day_schedule=$this->TimeTableModel->get_class_section_day_record($schedule_id,$class,$section,$period,$week_day_4);
                                if($get_day_schedule){
                                    foreach($get_day_schedule as $sch_data){
                                        $thu_tid=$sch_data['tid'];
                                        $thu_subject=$sch_data['subject'];
                                        $thu_subject_id=$sch_data['subject_id'];
                                    }
                                }
                                ?>
                                <select class="sch_input" name="thu_schedule_data[<?php echo $period; ?>][teacher]" style="padding:3px !important;color:#000;">
                                    <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 if($thu_tid == $staff['id']){ echo "selected"; } ?>><?php echo $staff['salutation']." ".$staff['name']; ?></option>
                                    <?php
                                        }
                                    }
                        
                                    ?>
                                </select>
                                <br>
                                <select class="sch_input" name="thu_schedule_data[<?php echo $period; ?>][subject_id]">
                                    <?php 
                                    if($get_class_subjects){
                                        foreach($get_class_subjects as $subjects){
                                    ?>
                                    <option value="<?php echo $subjects['id']; ?>" <?php if($thu_subject == $subjects['subject'] || $thu_subject_id == $subjects['id']){ echo "selected"; } ?>><?php echo $subjects['subject']; ?></option>
                                    <?php
                                        }
                                    }
                                    ?>
                                    
                                </select>
                            </td>
                            <?php 
                                }
                            }
                            ?>
                        </tr>
                        <tr>
                            <th>Fri</th>
                            <?php
                            if($all_periods){
                                foreach($all_periods as $periods){
                            ?>
                            <td class='text-center' style="padding:3px !important;">
                                <?php    
                                $period=$periods['id'];
                                $week_day_5="FRI";
                                $fri_tid="";
                                $fri_subject="";
                                $fri_subject_id="";
                                
                                $get_day_schedule=$this->TimeTableModel->get_class_section_day_record($schedule_id,$class,$section,$period,$week_day_5);
                                if($get_day_schedule){
                                    foreach($get_day_schedule as $sch_data){
                                        $fri_tid=$sch_data['tid'];
                                        $fri_subject=$sch_data['subject'];
                                        $fri_subject_id=$sch_data['subject_id'];
                                    }
                                }
                                ?>
                                <select class="sch_input" name="fri_schedule_data[<?php echo $period; ?>][teacher]" style="padding:3px !important;color:#000;">
                                    <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 if($fri_tid == $staff['id']){ echo "selected"; } ?>><?php echo $staff['name']; ?></option>
                                    <?php
                                        }
                                    }
                        
                                    ?>
                                </select>
                                <br>
                                <select class="sch_input" name="fri_schedule_data[<?php echo $period; ?>][subject_id]">
                                    <?php 
                                    if($get_class_subjects){
                                        foreach($get_class_subjects as $subjects){
                                    ?>
                                    <option value="<?php echo $subjects['id']; ?>" <?php if($fri_subject == $subjects['subject'] || $fri_subject_id == $subjects['id']){ echo "selected"; } ?>><?php echo $subjects['subject']; ?></option>
                                    <?php
                                        }
                                    }
                                    ?>
                                    
                                </select>
                            </td>
                            <?php 
                                }
                            }
                            ?>
                        </tr>
                        <tr>
                            <th>Sat</th>
                            <?php
                            if($all_periods){
                                foreach($all_periods as $periods){
                            ?>
                            <td class='text-center' style="padding:3px !important;">
                                <?php    
                                $period=$periods['id'];
                                $week_day_6="SAT";
                                $sat_tid="";
                                $sat_subject="";
                                $sat_subject_id="";
                                
                                $get_day_schedule=$this->TimeTableModel->get_class_section_day_record($schedule_id,$class,$section,$period,$week_day_6);
                                if($get_day_schedule){
                                    foreach($get_day_schedule as $sch_data){
                                        $sat_tid=$sch_data['tid'];
                                        $sat_subject=$sch_data['subject'];
                                        $sat_subject_id=$sch_data['subject_id'];
                                    }
                                }
                                ?>
                                <select class="sch_input" name="sat_schedule_data[<?php echo $period; ?>][teacher]" style="padding:3px !important;color:#000;">
                                    <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 if($sat_tid == $staff['id']){ echo "selected"; } ?>><?php echo $staff['salutation']." ".$staff['name']; ?></option>
                                    <?php
                                        }
                                    }
                        
                                    ?>
                                </select>
                                <br>
                                <select class="sch_input" name="sat_schedule_data[<?php echo $period; ?>][subject_id]">
                                    <?php 
                                    if($get_class_subjects){
                                        foreach($get_class_subjects as $subjects){
                                    ?>
                                    <option value="<?php echo $subjects['id']; ?>" <?php if($sat_subject == $subjects['subject'] || $sat_subject_id == $subjects['id']){ echo "selected"; } ?>><?php echo $subjects['subject']; ?></option>
                                    <?php
                                        }
                                    }
                                    ?>
                                    
                                </select>
                            </td>
                            <?php 
                                }
                            }
                            ?>
                        </tr>
                        <tr>
                            <th colspan="9">
                                <button class="btn btn-sm btn-success" type="submit" name="update_course_sch_data">Submit</button>
                            </th>
                        </tr>
                        </tbody>
                    </table>
                </div>
            </form>
        </div>
    </div>
</div>