<?php 

include('../dbconnect.php');
include('../phps.php');
ob_start();
session_start();

if(!isset($_SESSION['user'])) {
 header("Location: ../index.php");
 exit;
}
include('uppernav.php');

$empid = $_SESSION['user'];



if(isset($_POST['updatesched'])){



    $emp =  $_POST['emp'];
    $weekfrom =  $_POST['from'];
    
    $year = substr($weekfrom, 0, 4);
    $from = substr($weekfrom, -2);
    $wfrom = ltrim($from, '0');


    $weekto =  $_POST['to'];

    $to = substr($weekto, -2);
    $wto = ltrim($to, '0');


    $start=  $_POST['start'];
    $end=  $_POST['end'];
$date = array();
$week = array();
$noweek = array();

$starttime = array();
$endtime = array();

$employee= array();




 if($emp == 'All'){

     $getemp = "SELECT * FROM empdetails WHERE supervisor = '$accountname' OR detailid='$empid'";
   $empres = mysqli_query($conn, $getemp);
   while($emprow = mysqli_fetch_assoc($empres)){

       $employee[] = $emprow['detailid'];



  
   }




   $getdate = "SELECT * FROM schedule WHERE empid='$empid' AND year = '$year' AND week BETWEEN $wfrom AND $wto ORDER BY DATE ASC";

   $dateres = mysqli_query($conn, $getdate);
   while($daterow = mysqli_fetch_assoc($dateres)){

       $date[] = $daterow['date'];
       $week[] = $daterow['week'];
   }

   $noweek = array_unique($week);



   
   for ($c=0; $c < count($noweek); $c++) {
   $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($x = 0; $x < count($employee); $x++){


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

        //.echo $date[$a].'<br>';
 
       

          $sqlupdate = "UPDATE schedule SET starttime='$starttime[$a]', endtime='$endtime[$a]' WHERE empid='$employee[$x]' AND date='$date[$a]'";


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

                                   
          $alert='<div class="alert alert-success" role="alert"> Employee Schedule   Update Successfully! </div>';



          header('editsched.php?alert='.$alert);

        }else{

            //echo mysqli_error($conn);
        }
 
 
    }
 


}






 }else{

     $getdate = "SELECT * FROM schedule WHERE empid='$empid' AND year = '$year' AND week BETWEEN $wfrom AND $wto ORDER BY DATE ASC";

    $dateres = mysqli_query($conn, $getdate);
    while($daterow = mysqli_fetch_assoc($dateres)){

        $date[] = $daterow['date'];
        $week[] = $daterow['week'];
    }

    $noweek = array_unique($week);



    
    for ($c=0; $c < count($noweek); $c++) {
    $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($a= 0; $a < count($date); $a++){

        //.echo $date[$a].'<br>';

         $sqlupdate = "UPDATE schedule SET starttime='$starttime[$a]', endtime='$endtime[$a]' WHERE empid='$emp' AND date='$date[$a]'";

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

                       
            $alert='<div class="alert alert-success" role="alert"> Employee Schedule   Update Successfully! </div>';

          header('editsched.php?alert='.$alert);



        }else{


        }


    }


 }

    
}
mysqli_close($conn);
?>
<!DOCTYPE html>
<html lang="en">

  <head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>Team Head | My Team</title>

    <!-- Bootstrap core CSS-->
    <link href="../vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom fonts for this template-->
    <link href="../vendor/fontawesome-free/css/all.min.css" rel="stylesheet" type="text/css">

    <!-- Page level plugin CSS-->
    <link href="../vendor/datatables/dataTables.bootstrap4.css" rel="stylesheet">

    <!-- Custom styles for this template-->
        <link href="../css/sb-admin.css" rel="stylesheet">
    <link href="../css/style.css" rel="stylesheet">
    <script>
    
    function cb() {
        var off = document.getElementsByName("off[]");
        var offlength = off.length;
        var hol = document.getElementsByName("hol[]");
        var hollength = hol.length;
        var start = document.getElementsByName("start[]");
        var end = document.getElementsByName("end[]");
        for (k = 0; k < offlength; k++) {
            //off[k].checked = false;
            start[k].readOnly = false;
            end[k].readOnly = false;
            start[k].type = "time";
            end[k].type = "time";
            end[k].hidden = false;
            if (off[k].checked == true) {
                hol[k].disabled = true;
                start[k].readOnly = true;
                end[k].readOnly = true;
                start[k].type = 'text';
                start[k].value = 'OFF';
                end[k].type = 'text';
                end[k].value = 'OFF';
                end[k].hidden = true;;
            } else {
                hol[k].disabled = false;
            }
            if (hol[k].checked == true) {
                start[k].readOnly = true;
                end[k].readOnly = true;
                off[k].disabled = true;
                start[k].type = 'text';
                start[k].value = 'HOLIDAY OFF';
                end[k].type = 'text';
                end[k].value = 'HOLIDAY OFF';
                end[k].hidden = true;
            } else {
                off[k].disabled = false;
            }
        }

    }

    </script>


  </head>

  <body id="page-top" onload=display_ct();>
      
    <div id="pageloader">
    <img src="../images/loaderS.gif" alt="processing..." height="150px" width="200px"/>
    </div>

    <div id="preloader">
        <img src="../images/loaderS.gif" alt="processing..." height="150px" width="200px"/>
    </div>







      <!-- Sidebar -->
      
      <?php 
      $employee="active";
      $team="active";
      include('sidebar.php');
       ?>
      <div id="content-wrapper">

        <div class="container-fluid">

          <!-- Breadcrumbs-->
          <ol class="breadcrumb">

            <li class="breadcrumb-item active">My Team</li>
          </ol>

          <?php echo $alert;?>
        <form method="POST">

        <div class="row p-3">
            
            <div class="col-lg-12 form-inline p-3">
               

                    <select name="emp" id="emp" class="form-control" required>
                        <option value="All" >All</option>

                        <?php 
                        $sql="SELECT * FROM empdetails JOIN empinfo ON empdetails.detailid = empinfo.empid  WHERE supervisor = '$accountname' OR detailid='$empid'";
                        $sqlemp = mysqli_query($conn, $sql);
                        while($sqlrow = mysqli_fetch_assoc($sqlemp)){
                            echo '<option value="'.$sqlrow['detailid'].'">'.$sqlrow['detailid'].' '.$sqlrow['firstname'].' '.$sqlrow['surname'].'</option>';
                        }
                        ?>
                    </select>

                    &nbsp &nbsp <input type="week" name="from" class="form-control" required> &nbsp - &nbsp <input type="week" name="to" class="form-control" required> 
                 




            </div>
           
       

                    <?php 

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

                        for($a=0; $a < 7; $a++){
                        echo'
                        <div class="col-sm-3 form-group">
                            <div class="col-sm-12">
                            <label> <b>'. $days[$a].'</b></label>
                            </div>
                            <div class="col-sm-12 form-inline">
                            <input type="checkbox" class="off" name="off[]" id ="off" onclick="cb()" value="OFF" style="height: 20px; width: 20px;">&nbsp<label>&nbsp<b> OFF</b> </label>
                            &nbsp&nbsp&nbsp&nbsp
                            <input type="checkbox"  class="hol" name="hol[]" id="hol" onclick="cb()" 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="start[]" id="start" placeholder="Time" class="form-control" required> &nbsp-&nbsp<input type="time" name="end[]" id="end" placeholder="Time" class="form-control" required >
                            </div>
                        </div>';
                        }

                    
                    
                    ?>
                    <div class="col-lg-12 text-right">
                    <button type="button" class="btn btn-success" data-toggle="modal" data-target="#updatesched"><i class="fa fa-check"></i> Submit </button>
                    </div>

                   
                                
         
 
        </div>
        <div class="modal fade" id="updatesched" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
            <div class="modal-dialog modal-dialog-centered" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="exampleModalLabel">Confirm Schedule Update
                        </h5>
                        <button class="close" type="button" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">×
                            </span>
                        </button>
                    </div>
                    <div class="modal-body">Are you sure you want to update schedule?
                    </div>
                    <div class="modal-footer">
                        <input type="submit" class="btn btn-success" name="updatesched" value=" ✓ Yes"
                            style="margin-right: 5px;">
                        <button class="btn btn-danger" type="button" data-dismiss="modal">
                            <i class="fa fa-close">
                            </i> No
                        </button>
                    </div>
                </div>
            </div>
        </div>      
        </form>
            
        <?php  mysqli_close($conn)?>

        
      
        <!-- /.container-fluid -->

        <!-- Sticky Footer -->

      </div>
      <!-- /.content-wrapper -->

    </div>
    <!-- /#wrapper -->

    <!-- Scroll to Top Button-->
    <a class="scroll-to-top rounded" href="#page-top">
      <i class="fas fa-angle-up"></i>
    </a>

    <!-- Logout Modal-->
    <div class="modal fade" id="logoutModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title" id="exampleModalLabel">Ready to Leave?</h5>
            <button class="close" type="button" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">×</span>
            </button>
          </div>
          <div class="modal-body">Select "Logout" below if you are ready to end your current session.</div>
          <div class="modal-footer">
            <button class="btn btn-secondary" type="button" data-dismiss="modal">Cancel</button>
            <a class="btn btn-primary" href="login.html">Logout</a>
          </div>
        </div>
      </div>
    </div>

    <!-- Bootstrap core JavaScript-->
    <script src="../vendor/jquery/jquery.min.js"></script>
    <script src="../vendor/bootstrap/js/bootstrap.bundle.min.js"></script>

    <!-- Core plugin JavaScript-->
    <script src="../vendor/jquery-easing/jquery.easing.min.js"></script>

    <!-- Page level plugin JavaScript-->
    <script src="../vendor/chart.js/Chart.min.js"></script>
    <script src="../vendor/datatables/jquery.dataTables.js"></script>
    <script src="../vendor/datatables/dataTables.bootstrap4.js"></script>

    <!-- Custom scripts for all pages-->
    <script src="../js/sb-admin.min.js"></script>

    <!-- Demo scripts for this page-->
    <script src="../js/demo/datatables-demo.js"></script>
    <script src="../js/demo/chart-area-demo.js"></script>

          <script language="javascript" type="text/javascript">
            $(document).ready(function(){
            $("form").on("submit", function(){
                $("#pageloader").fadeIn();
            });//submit

             $('#preloader').fadeOut();
            });//document ready

            
            $(document).ready(function() {
              $('#teamtbl').DataTable( {

                responsive: true

              } );
            } );



        </script>
</body>

</html>
