<?php 
set_time_limit(10000);
include('../dbconnect.php');
if($_POST['subsched']){

    $start = $_POST['start'];
    $end = $_POST['end'];
    $pststart = $_POST['pststart'];
    $pstend = $_POST['pstend'];
     $weekfrom = $_POST['from'];
    $weekto = $_POST['to'];
    $teams = $_POST['team'];
    $employee = array();
    $allteams = array();
    $empteam = array();

    


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

   


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

  $days = array();
  $weeks = array();
  $month = array();
  $date = array();
  $starttime = array();
    $endtime = array();
    $pststarttime = array();
    $pstendtime = array();

  $numberofweeks = ((int)$setto - (int)$setfrom) + 1;

  for ($e=0; $e < $numberofweeks; $e++) {
      
    $starttime = array_merge($starttime, array($start[0],$start[1],$start[2],$start[3],$start[4],$start[5],$start[6]));
    $endtime = array_merge($endtime, array($end[0],$end[1],$end[2],$end[3],$end[4],$end[5],$end[6]));

    }

    for ($f=0; $f < $numberofweeks; $f++) {
        $pststarttime = array_merge($pststarttime, array($pststart[0],$pststart[1],$pststart[2],$pststart[3],$pststart[4],$pststart[5],$pststart[6]));
        $pstendtime = array_merge($pstendtime, array($pstend[0],$pstend[1],$pstend[2],$pstend[3],$pstend[4],$pstend[5],$pstend[6]));
        }
    



  //get date from week range


   
    $range = range($setfrom, $setto); 
    
    foreach ($range as $week_no) {
        $week_start = new DateTime();
        $week_start->setISODate($year, $week_no);
        $week_start->modify('-2 day');

        $seven_day_week = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
        $week = array();

        for ($i = 0; $i < 7; $i++) {
            $day = $seven_day_week[$i];
            $week[$day] = $week_start->format('Y-m-d');
            
            $week_start->modify('+1 day');

            $date[] = $week_start->format('Y-m-d');
            $month[] = $week_start->format('F');
            $weeks[] = $week_no;
            $days[] = $week_start->format('l');
     
            


        }


    }






    if($teams =='All'){


            //INSERT ORIGINAL ALL  per Team
 
        $allteam = "SELECT team FROM empdetails GROUP BY team";
        $allteamsql = mysqli_query($conn, $allteam);
        while($allteamrow= mysqli_fetch_assoc($allteamsql)){
             $allteams[] = $allteamrow['team'];

        }

        
        for($b=0; $b < count($allteams); $b++){
            
            for($c=0; $c < count($date); $c++){
                $years = date('Y', strtotime($date[$c]));

            $schedcode = $allteams[$b].$date[$c];
            $allteamorig = "INSERT INTO `schedule`(`schedcode`, `week`, `day`, `date`, `month`, `year`, `starttime`, `endtime`, `pststarttime`, `pstendtime`, `team`, `indi`) 
            VALUES ('$schedcode','$weeks[$c]','$days[$c]','$date[$c]','$month[$c]','$years','$starttime[$c]','$endtime[$c]','$pststarttime[$c]','$pstendtime[$c]','$allteams[$b]','Original')";

                if(mysqli_query($conn, $allteamorig)){

                    $alert = '<div class="alert alert-success alert-dismissible fade show" role="alert">
                Employee schedule submit successfully
                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                </div>';

                }else{

                $alert = '<div class="alert alert-danger alert-dismissible fade show" role="alert">
                Schedule already exist
                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                </div>';


                }

            }


        }

       
        //get all emp insert all emp sched
        $allemp ="SELECT detailid,team FROM empdetails WHERE status = 'Active' ";
        $allempres = mysqli_query($conn, $allemp);
        while($allrow = mysqli_fetch_assoc($allempres)){
             $employee[] = $allrow['detailid'];
             $empteam[] = $allrow['team'];



        }

        for($a=0; $a < count($employee); $a++){


            for($f=0; $f < count($date); $f++){

                $years = date('Y', strtotime($date[$f]));

                $schedcode = $employee[$a].$date[$f];
                $allteam = "INSERT INTO `schedule`(`schedcode`, `week`, `day`, `date`, `month`, `year`, `starttime`, `endtime`, `pststarttime`, `pstendtime`, `team`, `empid`, `indi`) 
                VALUES ('$schedcode','$weeks[$f]','$days[$f]','$date[$f]','$month[$f]','$years','$starttime[$f]','$endtime[$f]','$pststarttime[$f]','$pstendtime[$f]','$empteam[$a]','$employee[$a]','0')";
           

                if(mysqli_query($conn, $allteam)){

                    $alert = '<div class="alert alert-success alert-dismissible fade show" role="alert">
                   Employee schedule submit successfully
                   <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                     <span aria-hidden="true">&times;</span>
                   </button>
                 </div>';
                
               }else{

                   $alert = '<div class="alert alert-danger alert-dismissible fade show" role="alert">
                   Schedule already exist
                   <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                     <span aria-hidden="true">&times;</span>
                   </button>
                 </div>';


               }

    
            }
            

            

            
        }
        echo $alert;



    

    }else{

        $allemp ="SELECT detailid,team FROM empdetails WHERE status = 'Active' AND team= '$teams'";
        $allempres = mysqli_query($conn, $allemp);
        while($allrow = mysqli_fetch_assoc($allempres)){
             $employee[] = $allrow['detailid'];
          



        }

   
            
            for($c=0; $c < count($date); $c++){
                $years = date('Y', strtotime($date[$c]));

            $schedcode = $teams.$date[$c];
            $allteamorig = "INSERT INTO `schedule`(`schedcode`, `week`, `day`, `date`, `month`, `year`, `starttime`, `endtime`, `pststarttime`, `pstendtime`, `team`, `indi`) 
            VALUES ('$schedcode','$weeks[$c]','$days[$c]','$date[$c]','$month[$c]','$years','$starttime[$c]','$endtime[$c]','$pststarttime[$c]','$pstendtime[$c]','$teams','Original')";

                
                if(mysqli_query($conn, $allteamorig)){

                    $alert = '<div class="alert alert-success alert-dismissible fade show" role="alert">
                Employee schedule submit successfully
                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                </div>';

                

                }else{

                $alert = '<div class="alert alert-danger alert-dismissible fade show" role="alert">
                Schedule already exist 
                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                </div>';
   

                }
            }

            for($a=0; $a < count($employee); $a++){


                for($f=0; $f < count($date); $f++){
    
                    $years = date('Y', strtotime($date[$f]));
    
                    $schedcode = $employee[$a].$date[$f];
                    $allteam = "INSERT INTO `schedule`(`schedcode`, `week`, `day`, `date`, `month`, `year`, `starttime`, `endtime`, `pststarttime`, `pstendtime`, `team`, `empid`, `indi`) 
                    VALUES ('$schedcode','$weeks[$f]','$days[$f]','$date[$f]','$month[$f]','$years','$starttime[$f]','$endtime[$f]','$pststarttime[$f]','$pstendtime[$f]','$teams','$employee[$a]','0')";
                

                if(mysqli_query($conn, $allteam)){

                    $alert = '<div class="alert alert-success alert-dismissible fade show" role="alert">
                   Employee schedule submit successfully
                   <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                     <span aria-hidden="true">&times;</span>
                   </button>
                 </div>';
                
               }else{

                   $alert = '<div class="alert alert-danger alert-dismissible fade show" role="alert">
                   Schedule already exist
                   <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                     <span aria-hidden="true">&times;</span>
                   </button>
                 </div>';


               }
        
                }
    
                
    
                
            }

            echo $alert;
  


    }








}

if($_POST['indisubsched']){

    $start = $_POST['indistart'];
    $end = $_POST['indiend'];
    $pststart = $_POST['indipststart'];
    $pstend = $_POST['indipstend'];
     $weekfrom = $_POST['indifrom'];
    $weekto = $_POST['indito'];
    $emp = $_POST['emp'];
  

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

   


    $from = substr($weekfrom, -2);
    $setfrom = ltrim($from, '0');
    $to = substr($weekto, -2);
    $setto = ltrim($to, '0');
  
    $days = array();
    $weeks = array();
    $month = array();
    $date = array();
    $starttime = array();
      $endtime = array();
      $pststarttime = array();
      $pstendtime = array();

      
  $numberofweeks = ((int)$setto - (int)$setfrom) + 1;

  for ($e=0; $e < $numberofweeks; $e++) {
      
    $starttime = array_merge($starttime, array($start[0],$start[1],$start[2],$start[3],$start[4],$start[5],$start[6]));
    $endtime = array_merge($endtime, array($end[0],$end[1],$end[2],$end[3],$end[4],$end[5],$end[6]));

    }

    for ($f=0; $f < $numberofweeks; $f++) {
        $pststarttime = array_merge($pststarttime, array($pststart[0],$pststart[1],$pststart[2],$pststart[3],$pststart[4],$pststart[5],$pststart[6]));
        $pstendtime = array_merge($pstendtime, array($pstend[0],$pstend[1],$pstend[2],$pstend[3],$pstend[4],$pstend[5],$pstend[6]));
        }
    


      $allemp ="SELECT detailid,team FROM empdetails WHERE detailid='$emp'";
      $allempres = mysqli_query($conn, $allemp);
        $allrow = mysqli_fetch_assoc($allempres);
           $empteam = $allrow['team'];

           $range = range($setfrom, $setto); 
    
           foreach ($range as $week_no) {
               $week_start = new DateTime();
               $week_start->setISODate($year, $week_no);
               $week_start->modify('-1 day');
       
               $seven_day_week = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
               $week = array();
       
               for ($i = 0; $i < 7; $i++) {
                   $day = $seven_day_week[$i];
                   $week[$day] = $week_start->format('Y-m-d');
                   
                   
       
                   $date[] = $week_start->format('Y-m-d');
                   $month[] = $week_start->format('F');
                    $weeks[] = $week_no;
                   $days[] = $week_start->format('l');


                   $week_start->modify('+1 day');

            
                   
       
       
               }
       
       
           }
        



      

 
          



              for($f=0; $f < count($date); $f++){
  
                  $years = date('Y', strtotime($date[$f]));
                
  
                  $schedcode = $emp.$date[$f];
                  $allteam = "INSERT INTO `schedule`(`schedcode`, `week`, `day`, `date`, `month`, `year`, `starttime`, `endtime`, `pststarttime`, `pstendtime`, `team`, `empid`, `indi`) 
                  VALUES ('$schedcode','$weeks[$f]','$days[$f]','$date[$f]','$month[$f]','$years','$starttime[$f]','$endtime[$f]','$pststarttime[$f]','$pstendtime[$f]','$empteam','$emp','0')";
                    if(mysqli_query($conn, $allteam)){

                         $alert = '<div class="alert alert-success alert-dismissible fade show" role="alert">
                        Employee schedule submit successfully
                        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                          <span aria-hidden="true">&times;</span>
                        </button>
                      </div>';
                     
                    }else{

                        $alert = '<div class="alert alert-danger alert-dismissible fade show" role="alert">
                        Schedule already exist
                        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                          <span aria-hidden="true">&times;</span>
                        </button>
                      </div>';


                    }
      
              }

              echo $alert;
			  mysqli_close($conn);
  
              
  
              
          
  
    

    








}

?>