<?php 
include('../dbconnect.php');

function mnltopst($date, $time){

    $convert = new DateTime($date.' '.$time, new DateTimeZone('Asia/Manila'));
    
    
    $convert->setTimezone(new DateTimeZone('America/Los_Angeles'));
    
    $datetime = $convert->format('m/d/Y h:i A');
    
    
    return ($datetime);
    
    
    
    
}
$output = '';
if(isset($_POST['loadsched'])){
    $start = $_POST['start'];
    $end = $_POST['end'];
    $weekfrom = $_POST['from'];
    $weekto = $_POST['to'];
    $strt = array();
    $strt1 = array();
    $nd = array();
    $date = array();



    $year = substr($weekfrom, 0, 4);


    $from = substr($weekfrom, -2);
    $setfrom = ltrim($from, '0');


    $weekto =  $_POST['to'];

    $to = substr($weekto, -2);
    $setto = ltrim($to, '0');
    
    $range = range($setfrom, $setto);

    foreach ($range as $week_no) {

    $week_start = new DateTime();
    $week_start->setISODate((int)$year,$week_no);
    $date[] = $week_start->format('Y-m-d');
    
    }


    $output ='';
    for($b=0; $b < 7; $b++){
        $strt[] = $start[$b];
        $nd[] = $end[$b];
        $strt1[] = $start[$b];

    }

    $date4TZ = date('P', strtotime($date[1]));
    if($date4TZ == '-08:00'){
        //PST

        $ET = "15:59";


    }else{
        //PDT
        $ET = "14:59";
    }





        $pstday = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday','Saturday');
        $ex = array('OFF','HOLIDAY OFF');
        $diff = array_diff($strt1, $ex);
        $hrs = array_values($diff);

        $firstelement = array($strt[0]);
        $removefirst =  array_shift($strt);
        $starttime = array_merge($strt, $firstelement);


        
        $endelement = array($nd[0]);
        $removefirstend =  array_shift($nd);
        $endtime = array_merge($nd, $endelement);


    

    
        
        $c = 1;


        for($a=0; $a < 7; $a++){


            if(new DateTime($hrs[0]) >= new DateTime('00:00') && new DateTime($hrs[0]) <= new DateTime($ET) ){

                if($starttime[$a] == 'OFF'){
                    $output .= '
                    <div class="col-sm-4 form-group">
                    
                        <div class="col-sm-12">
                        <label> <b>'.$pstday[$a].'</b></label>
                        </div>
                        <div class="col-sm-12 form-inline">
                        <input type="checkbox" class="pstoff" name="pstoff[]" id ="pstoff" onclick="ol()" value="OFF" style="height: 20px; width: 20px;" checked>&nbsp<label>&nbsp<b> OFF</b> </label>
                        &nbsp&nbsp&nbsp&nbsp
                        <input type="checkbox"  class="psthol" name="psthol[]" id="psthol" onclick="ol()" value="HOLIDAY OFF" style="height: 20px; width: 20px;" >&nbsp<label>&nbsp<b> HOLIDAY OFF</b> </label>
                        </div>
                        <div class="col-sm-12 form-inline">
                        <input type="time" name="pststart[]" id="pststart" placeholder="Time" class="form-control" value="'.$starttime[$a].'"> &nbsp-&nbsp<input type="time" name="pstend[]" id="pstend" placeholder="Time" value="'.$endtime[$a].'" class="form-control" >
                        </div>

                        <h1 id="display"></h1>
        
  
                    
                    </div>';

                }else if($starttime[$a] == 'HOLIDAY OFF'){
                    $output .= '
                    <div class="col-sm-4 form-group">
                    
                        <div class="col-sm-12">
                        <label> <b>'.$pstday[$a].'</b></label>
                        </div>
                        <div class="col-sm-12 form-inline">
                        <input type="checkbox" class="pstoff" name="pstoff[]" id ="pstoff" onclick="pstcb()" value="OFF" style="height: 20px; width: 20px;">&nbsp<label>&nbsp<b> OFF</b> </label>
                        &nbsp&nbsp&nbsp&nbsp
                        <input type="checkbox"  class="psthol" name="psthol[]" id="psthol" onclick="pstcb()" value="HOLIDAY OFF" style="height: 20px; width: 20px;" checked>&nbsp<label>&nbsp<b> HOLIDAY OFF</b> </label>
                        </div>
                        <div class="col-sm-12 form-inline">
                        <input type="time" name="pststart[]" id="pststart" placeholder="Time" class="form-control" value="'.$starttime[$a].'"> &nbsp-&nbsp<input type="time" name="pstend[]" id="pstend" placeholder="Time" value="'.$endtime[$a].'" class="form-control" >
                        </div>

                        <h1 id="display"></h1>
        
                    
                    </div>';

                }else{
            

                $pstsched =  mnltopst($date[0],$starttime[$a]);

                $pststarttime = date('H:i', strtotime($pstsched));


                $pstschedend =  mnltopst($date[0],$endtime[$a]);

                $pstendtime = date('H:i', strtotime($pstschedend));



                    $output .= '
                    <div class="col-sm-4 form-group">
                    
                        <div class="col-sm-12">
                        <label> <b>'.$pstday[$a].'</b></label>
                        </div>
                        <div class="col-sm-12 form-inline">
                        <input type="checkbox" class="pstoff" name="pstoff[]" id ="pstoff" onclick="ol()" value="OFF" style="height: 20px; width: 20px;" >&nbsp<label>&nbsp<b> OFF</b> </label>
                        &nbsp&nbsp&nbsp&nbsp
                        <input type="checkbox"  class="psthol" name="psthol[]" id="psthol" onclick="ol()" value="HOLIDAY OFF" style="height: 20px; width: 20px;" >&nbsp<label>&nbsp<b> HOLIDAY OFF</b> </label>
                        </div>
                        <div class="col-sm-12 form-inline">
                        <input type="time" name="pststart[]" id="pststart" placeholder="Time" class="form-control" value="'.$pststarttime.'"> &nbsp-&nbsp<input type="time" name="pstend[]" id="pstend" placeholder="Time" value="'.$pstendtime.'" class="form-control" >
                        </div>
                    
                    </div>';

                }



            




            




            }else{


                if($start[$a] == 'OFF'){
                    $output .= '
                    <div class="col-sm-4 form-group">
                    
                        <div class="col-sm-12">
                        <label> <b>'.$pstday[$a].'</b></label>
                        </div>
                        <div class="col-sm-12 form-inline">
                        <input type="checkbox" class="pstoff" name="pstoff[]" id ="pstoff" onclick="ol()" value="OFF" style="height: 20px; width: 20px;" checked>&nbsp<label>&nbsp<b> OFF</b> </label>
                        &nbsp&nbsp&nbsp&nbsp
                        <input type="checkbox"  class="psthol" name="psthol[]" id="psthol" onclick="ol()" value="HOLIDAY OFF" style="height: 20px; width: 20px;" >&nbsp<label>&nbsp<b> HOLIDAY OFF</b> </label>
                        </div>
                        <div class="col-sm-12 form-inline">
                        <input type="time" name="pststart[]" id="pststart" placeholder="Time" class="form-control" value="'.$start[$a].'"> &nbsp-&nbsp<input type="time" name="pstend[]" id="pstend" placeholder="Time" value="'.$start[$a].'" class="form-control" >
                        </div>

                        <h1 id="display"></h1>
        

                    
                    </div>';

                }else if($starttime[$a] == 'HOLIDAY OFF'){
                    $output .= '
                    <div class="col-sm-4 form-group">
                    
                        <div class="col-sm-12">
                        <label> <b>'.$pstday[$a].'</b></label>
                        </div>
                        <div class="col-sm-12 form-inline">
                        <input type="checkbox" class="pstoff" name="pstoff[]" id ="pstoff" onclick="ol()" value="OFF" style="height: 20px; width: 20px;">&nbsp<label>&nbsp<b> OFF</b> </label>
                        &nbsp&nbsp&nbsp&nbsp
                        <input type="checkbox"  class="psthol" name="psthol[]" id="psthol" onclick="ol()" value="HOLIDAY OFF" style="height: 20px; width: 20px;" checked>&nbsp<label>&nbsp<b> HOLIDAY OFF</b> </label>
                        </div>
                        <div class="col-sm-12 form-inline">
                        <input type="time" name="pststart[]" id="pststart" placeholder="Time" class="form-control" value="'.$start[$a].'"> &nbsp-&nbsp<input type="time" name="pstend[]" id="pstend" placeholder="Time" value="'.$start[$a].'" class="form-control" >
                        </div>

                        <h1 id="display"></h1>
        
                    
                    </div>';

                }else{
                    

                $pstsched =  mnltopst($date[0],$start[$a]);

                $pststarttime = date('H:i', strtotime($pstsched));


                $pstschedend =  mnltopst($date[0],$end[$a]);

                $pstendtime = date('H:i', strtotime($pstschedend));



                    $output .= '
                    <div class="col-sm-4 form-group">
                    
                        <div class="col-sm-12">
                        <label> <b>'.$pstday[$a].'</b></label>
                        </div>
                        <div class="col-sm-12 form-inline">
                        <input type="checkbox" class="pstoff" name="pstoff[]" id ="pstoff" onclick="ol()" value="OFF" style="height: 20px; width: 20px;" >&nbsp<label>&nbsp<b> OFF</b> </label>
                        &nbsp&nbsp&nbsp&nbsp
                        <input type="checkbox"  class="psthol" name="psthol[]" id="psthol" onclick="ol()" value="HOLIDAY OFF" style="height: 20px; width: 20px;" >&nbsp<label>&nbsp<b> HOLIDAY OFF</b> </label>
                        </div>
                        <div class="col-sm-12 form-inline">
                        <input type="time" name="pststart[]" id="pststart" placeholder="Time" class="form-control" value="'.$pststarttime.'"> &nbsp-&nbsp<input type="time" name="pstend[]" id="pstend" placeholder="Time" value="'.$pstendtime.'" class="form-control" >
                        </div>


        
                    
                    
                    </div>';

                }



            




            }


            



        }

}

if(isset($_POST['indiloadsched'])){
    
     $start = $_POST['indistart'];
    $end = $_POST['indiend'];
    $weekfrom = $_POST['indifrom'];
    $weekto = $_POST['indito'];
    $strt = array();
    $strt1 = array();
    $nd = array();
    $date = array();

    //$output ='<H1>'.$weekto.'</H1>';
    



    $year = substr($weekfrom, 0, 4);


    $from = substr($weekfrom, -2);
    $setfrom = ltrim($from, '0');




    $to = substr($weekto, -2);
    $setto = ltrim($to, '0');
    
    $range = range($setfrom, $setto);

    foreach ($range as $week_no) {

    $week_start = new DateTime();
    $week_start->setISODate((int)$year,$week_no);
     $date[] = $week_start->format('Y-m-d');
    
    }


    $output ='';
    for($b=0; $b < 7; $b++){
        $strt[] = $start[$b];
        $nd[] = $end[$b];
        $strt1[] = $start[$b];

    }

       $date4TZ = date('P', strtotime($date[0]));
    if($date4TZ == '-08:00'){
            //PST
        $ET = "15:59";


    }else{

        //PDT
        $ET = "14:59";
    }






        $pstday = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday','Saturday');
        $ex = array('OFF','HOLIDAY OFF');
        $diff = array_diff($strt1, $ex);
        $hrs = array_values($diff);

        $firstelement = array($strt[0]);
        $removefirst =  array_shift($strt);
        $starttime = array_merge($strt, $firstelement);


        
        $endelement = array($nd[0]);
        $removefirstend =  array_shift($nd);
        $endtime = array_merge($nd, $endelement);


    

    
        
        $c = 1;

        


        for($a=0; $a < 7; $a++){


            if(new DateTime($hrs[0]) >= new DateTime('00:00') && new DateTime($hrs[0]) <= new DateTime($ET) ){



                if($starttime[$a] == 'OFF'){
                    $output .= '
                    <div class="col-sm-4 form-group">
                    
                        <div class="col-sm-12">
                        <label> <b>'.$pstday[$a].'</b></label>
                        </div>
                        <div class="col-sm-12 form-inline">
                        <input type="checkbox" class="indipstoff" name="indipstoff[]" id ="indipstoff" onclick="indiol()" value="OFF" style="height: 20px; width: 20px;" checked>&nbsp<label>&nbsp<b> OFF</b> </label>
                        &nbsp&nbsp&nbsp&nbsp
                        <input type="checkbox"  class="indipsthol" name="indipsthol[]" id="indipsthol" onclick="indiol()" value="HOLIDAY OFF" style="height: 20px; width: 20px;" >&nbsp<label>&nbsp<b> HOLIDAY OFF</b> </label>
                        </div>
                        <div class="col-sm-12 form-inline">
                        <input type="time" name="indipststart[]" id="indipststart" placeholder="Time" class="form-control" value="'.$starttime[$a].'"> &nbsp-&nbsp<input type="time" name="indipstend[]" id="indipstend" placeholder="Time" value="'.$endtime[$a].'" class="form-control" >
                        </div>

                        <h1 id="display"></h1>
        
           
                    
                    </div>';

                }else if($starttime[$a] == 'HOLIDAY OFF'){
                    $output .= '
                    <div class="col-sm-4 form-group">
                    
                        <div class="col-sm-12">
                        <label> <b>'.$pstday[$a].'</b></label>
                        </div>
                        <div class="col-sm-12 form-inline">
                        <input type="checkbox" class="indipstoff" name="indipstoff[]" id ="indipstoff" onclick="indiol()" value="OFF" style="height: 20px; width: 20px;">&nbsp<label>&nbsp<b> OFF</b> </label>
                        &nbsp&nbsp&nbsp&nbsp
                        <input type="checkbox"  class="indipsthol" name="indipsthol[]" id="indipsthol" onclick="indiol()" value="HOLIDAY OFF" style="height: 20px; width: 20px;" checked>&nbsp<label>&nbsp<b> HOLIDAY OFF</b> </label>
                        </div>
                        <div class="col-sm-12 form-inline">
                        <input type="time" name="indipststart[]" id="indipststart" placeholder="Time" class="form-control" value="'.$starttime[$a].'"> &nbsp-&nbsp<input type="time" name="indipstend[]" id="indipstend" placeholder="Time" value="'.$endtime[$a].'" class="form-control" >
                        </div>

                        <h1 id="display"></h1>
        
                  
                    
                    </div>';

                }else{
            

                $pstsched =  mnltopst($date[0],$starttime[$a]);

                $pststarttime = date('H:i', strtotime($pstsched));


                $pstschedend =  mnltopst($date[0],$endtime[$a]);

                $pstendtime = date('H:i', strtotime($pstschedend));



                    $output .= '
                    <div class="col-sm-4 form-group">
                    
                        <div class="col-sm-12">
                        <label> <b>'.$pstday[$a].'</b></label>
                        </div>
                        <div class="col-sm-12 form-inline">
                        <input type="checkbox" class="indipstoff" name="indipstoff[]" id ="indipstoff" onclick="indiol()" value="OFF" style="height: 20px; width: 20px;" >&nbsp<label>&nbsp<b> OFF</b> </label>
                        &nbsp&nbsp&nbsp&nbsp
                        <input type="checkbox"  class="indipsthol" name="indipsthol[]" id="indipsthol" onclick="indiol()" value="HOLIDAY OFF" style="height: 20px; width: 20px;" >&nbsp<label>&nbsp<b> HOLIDAY OFF</b> </label>
                        </div>
                        <div class="col-sm-12 form-inline">
                        <input type="time" name="indipststart[]" id="indipststart" placeholder="Time" class="form-control" value="'.$pststarttime.'"> &nbsp-&nbsp<input type="time" name="indipstend[]" id="indipstend" placeholder="Time" value="'.$pstendtime.'" class="form-control" >
                        </div>

                   


        
                    
                    
                    </div>';

                }



            




            




            }else{


                if($start[$a] == 'OFF'){
                    $output .= '
                    <div class="col-sm-4 form-group">
                    
                        <div class="col-sm-12">
                        <label> <b>'.$pstday[$a].'</b></label>
                        </div>
                        <div class="col-sm-12 form-inline">
                        <input type="checkbox" class="indipstoff" name="indipstoff[]" id ="indipstoff" onclick="indiol()" value="OFF" style="height: 20px; width: 20px;" checked>&nbsp<label>&nbsp<b> OFF</b> </label>
                        &nbsp&nbsp&nbsp&nbsp
                        <input type="checkbox"  class="indipsthol" name="indipsthol[]" id="indipsthol" onclick="indiol()" value="HOLIDAY OFF" style="height: 20px; width: 20px;" >&nbsp<label>&nbsp<b> HOLIDAY OFF</b> </label>
                        </div>
                        <div class="col-sm-12 form-inline">
                        <input type="time" name="indipststart[]" id="indipststart" placeholder="Time" class="form-control" value="'.$start[$a].'"> &nbsp-&nbsp<input type="time" name="indipstend[]" id="indipstend" placeholder="Time" value="'.$start[$a].'" class="form-control" >
                        </div>

                        <h1 id="display"></h1>
        

                    
                    </div>';

                }else if($starttime[$a] == 'HOLIDAY OFF'){
                    $output .= '
                    <div class="col-sm-4 form-group">
                    
                        <div class="col-sm-12">
                        <label> <b>'.$pstday[$a].'</b></label>
                        </div>
                        <div class="col-sm-12 form-inline">
                        <input type="checkbox" class="indipstoff" name="indipstoff[]" id ="pstoff" onclick="indiol()" value="OFF" style="height: 20px; width: 20px;">&nbsp<label>&nbsp<b> OFF</b> </label>
                        &nbsp&nbsp&nbsp&nbsp
                        <input type="checkbox"  class="indipsthol" name="indipsthol[]" id="psthol" onclick="indiol()" value="HOLIDAY OFF" style="height: 20px; width: 20px;" checked>&nbsp<label>&nbsp<b> HOLIDAY OFF</b> </label>
                        </div>
                        <div class="col-sm-12 form-inline">
                        <input type="time" name="indipststart[]" id="indipststart" placeholder="Time" class="form-control" value="'.$start[$a].'"> &nbsp-&nbsp<input type="time" name="indipstend[]" id="indipstend" placeholder="Time" value="'.$start[$a].'" class="form-control" >
                        </div>

                        <h1 id="display"></h1>
        
                    
                    </div>';

                }else{
                    

                $pstsched =  mnltopst($date[0],$start[$a]);

                $pststarttime = date('H:i', strtotime($pstsched));


                $pstschedend =  mnltopst($date[0],$end[$a]);

                $pstendtime = date('H:i', strtotime($pstschedend));



                    $output .= '
                    <div class="col-sm-4 form-group">
                    
                        <div class="col-sm-12">
                        <label> <b>'.$pstday[$a].'</b></label>
                        </div>
                        <div class="col-sm-12 form-inline">
                        <input type="checkbox" class="indipstoff" name="indipstoff[]" id ="indipstoff" onclick="indiol()" value="OFF" style="height: 20px; width: 20px;" >&nbsp<label>&nbsp<b> OFF</b> </label>
                        &nbsp&nbsp&nbsp&nbsp
                        <input type="checkbox"  class="indipsthol" name="indipsthol[]" id="indipsthol" onclick="indiol()" value="HOLIDAY OFF" style="height: 20px; width: 20px;" >&nbsp<label>&nbsp<b> HOLIDAY OFF</b> </label>
                        </div>
                        <div class="col-sm-12 form-inline">
                        <input type="time" name="indipststart[]" id="indipststart" placeholder="Time" class="form-control" value="'.$pststarttime.'"> &nbsp-&nbsp<input type="time" name="indipstend[]" id="indipstend" placeholder="Time" value="'.$pstendtime.'" class="form-control" >
                        </div>


        
                    
                    
                    </div>';

                }



            




            }


            



        }
  
}

  
//$output = '<h1>'.count($start).'</h1>';



$data = array(
    'sched' => $output
 
);


echo json_encode($data);

?>