<?php 

include('../dbconnect.php');
ob_start();
session_start();
$user = $_SESSION['user'];
if( !isset($_SESSION['user']) ) {
  header("Location: ../index.php");
  exit;
 }
 elseif($rowa['level']=='admin'){
   header("Location: ../accessdenied.php");
 }
 
 
$sqla = "SELECT * FROM account WHERE userid = '$user'";
$resulta = mysqli_query($conn, $sqla);
$rowa  = mysqli_fetch_assoc($resulta);



$week = "1";

$year = date('Y');

$getmonth = "January";

$weeklytab = 'style="display: block;"';
$monthlytab = 'style="display: none;"';

if(isset($_POST['filterw'])){
  $weekno = $_POST['weekno'];
  $team = $_POST['team'];
  $week = $weekno;
 
  $weeklytab = 'style="display: block;"';
  $monthlytab = 'style="display: none;"';

}

if(isset($_POST['filterm'])){
  $getmonth = $_POST['month'];
  $employee = $_POST['employee'];
  $weeklytab = 'style="display: none;"';
  $monthlytab = 'style="display: block;"';


}




$search = $_POST['search'];
/*SELECt * FROM compensation JOIN salarypackage ON compensation.salarycode = salarypackage.salarycode 
JOIN monetary ON compensation.monetaryid = monetary.monetaryid
WHERE compensation.salarycode = salarypackage.salarycode AND compensation.monetaryid = monetary.monetaryid
*/


?>
<!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>Super Admin | Salary Package</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 rel="icon" href="../images/logo.jpg" type="image/gif" sizes="16x16">

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>

    <link href="../css/style.css" rel="stylesheet">
    <script>
    function opentm() {
        document.getElementById('teamtab').style.display = 'block';
        document.getElementById('inditab').style.display = 'none';
        document.getElementById('tm').style.backgroundColor = '#dddddd';
        document.getElementById('indi').style.backgroundColor = '#f1f1f1';

    }

    function openindi() {
        document.getElementById('inditab').style.display = 'block';
        document.getElementById('teamtab').style.display = 'none';
        document.getElementById('indi').style.backgroundColor = '#dddddd';
        document.getElementById('tm').style.backgroundColor = '#f1f1f1';
    }

    function con() {
        var dropdown = document.getElementById('timezone').value;
        var monthdrop = document.getElementById('monthtimezone').value;


        if (dropdown == 'pacific') {
            document.getElementById('indipst').hidden = false;
            document.getElementById('inditable').hidden = true;

        } else {
            document.getElementById('indipst').hidden = true;
            document.getElementById('inditable').hidden = false;
        }

        if (monthdrop == 'pacific') {
            document.getElementById('pstmonthtable').hidden = false;
            document.getElementById('manilamonthtable').hidden = true;

        } else {
            document.getElementById('pstmonthtable').hidden = true;
            document.getElementById('manilamonthtable').hidden = false;
        }

    }
    </script>



</head>

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

    <?php include('uppernav.php')?>


    <!-- Sidebar -->

    <?php 
      $sched="active";
      $admintools = "active";
      include('sidebar.php');
      ?>
    <div id="content-wrapper">

        <div class="container-fluid">

            <!-- Breadcrumbs-->
            <ol class="breadcrumb">
                <li class="breadcrumb-item">
                    <a href="schedule.php?page=1">Employee Schedule</a>
                </li>

            </ol>

            <table class="table table-bordered">
                <thead class="thead-dark text-center">
                                <th>Employee ID</th>
                                <th>Name</th>
                                <th>Team</th>
                                <th>Date </th>
                                <th>Login</th>
                                <th>Logout</th>
                                <th>Late</th>
                                <th>Pre OT</th>
                                <th>Post OT</th>
                                <th>RDOT</th>
                     
                                <th>Schedule</th>

                                
                </thead>

              <?php 

                    $indidatefrom = '2019-03-24';
                    $indidateto = '2019-03-30';
                    $weekno = date('W', strtotime($indidatefrom));
                    $year = date('Y', strtotime($indidatefrom));
                  
                  $sql = "SELECT *, schedule.empid AS 'id',schedule.date AS 'schedate' FROM schedule JOIN empinfo ON schedule.empid = empinfo.empid JOIN empdetails ON schedule.empid = empdetails.detailid WHERE schedule.date 
                  BETWEEN '$indidatefrom' AND '$indidateto'   AND  empdetails.status = 'Active' ORDER BY empdetails.detailid ";
    
                  $sqlres = mysqli_query($conn, $sql);
    
    
                  while($sqlrow = mysqli_fetch_assoc($sqlres)){
                    $emp = $sqlrow['empid'];
                    $date = date('m/d/Y', strtotime($sqlrow['schedate']));

                    $sqllogin = "SELECT * FROM tbl_login WHERE empid='$emp' AND date='$date'";
                    $sqlloginres = mysqli_query($conn, $sqllogin);
                    $sqlloginrow = mysqli_fetch_assoc($sqlloginres);
                    $liloid = $sqlloginrow['id'];

                    $hrssql = "SELECT starttime FROM schedule WHERE year='$year' AND week = '$weekno' AND starttime != 'OFF' AND endtime != 'HOLIDAY OFF' AND empid='$emp' ";
                    $hrssched = mysqli_query($conn, $hrssql);
                    $hrsrow = mysqli_fetch_assoc($hrssched);
                    $mnlhrs = $hrsrow['starttime'];


                    echo $hrsql.'<br/>';


                    
                    $schedPSTST1 = new DateTime($mnlhrs, new DateTimeZone('Asia/Manila'));
                    
                    $schedPSTST1->setTimezone(new DateTimeZone('America/Los_Angeles'));
                      $mnltopsthrs = $schedPSTST1->format('h:i A');




                    $MNLdate = new DateTime($date.' '.$mnltopsthrs, new DateTimeZone('America/Los_Angeles'));
                 

                    $MNLdate->setTimezone(new DateTimeZone('Asia/Manila'));
                    $scheddate = $MNLdate->format('Y-m-d');


                    $schedsql = "SELECT * FROM schedule WHERE year='$year' AND date='$scheddate' AND  empid='$emp'";
                    $schedres = mysqli_query($conn, $schedsql);
                    $schedrow =  mysqli_fetch_assoc($schedres);
                    $schedST = $schedrow['starttime'];
                    $schedET = $schedrow['endtime'];
                    $idl = $schedrow['idl'];
                    $leaveduration = $schedrow['leaveduration'];



                    ///OT 


                    $otsql = "SELECT * FROM ot WHERE empid='$emp' AND attendance ='$liloid' AND status='3'";
                    $otres = mysqli_query($conn, $otsql);
                    $otrow =  mysqli_fetch_assoc($otres);
                   $ottype = $otrow['type'];
                    $othrs = $otrow['hrs'];

   




                    if($schedST == 'OFF'){

                     

                        if(!empty($ottype)){


                            $schedPSTstarttime = 'OFF';
                            $OT =  $ottype.' '.$othrs;


                        }else{

                            $schedule = 'OFF';

                          


                        }

                        if(empty($sqlloginrow['timein'])){

                            $in = '---';
                            $out = '---';


                        }else{
                            $in = $sqlloginrow['timein'];
                            $out = $sqlloginrow['timeout'];
                        }


                    }else if($schedST == 'HOLIDAY OFF'){
                        $schedule = 'HOLIDAY OFF';

                        if(empty($sqlloginrow['timein'])){

                            $in = '---';
                            $out = '---';


                        }else{
                            $in = $sqlloginrow['timein'];
                            $out = $sqlloginrow['timeout'];
                        }



                    } else if(!empty($idl)){

                        $leavesql = "SELECT * FROM `leave` WHERE idl = '$idl' AND status = '3' ";
                        $leaveres = mysqli_query($conn, $leavesql);
                        $leaverow = mysqli_fetch_assoc($leaveres);
                        $leavename = $leaverow['name'];

                        if($leaveduration == '0.5'){

                            if(empty($sqlloginrow['timein'])){

                                $in = '---';
                                $out = '---';
    
    
                            }else{
                                $in = $sqlloginrow['timein'];
                                $out = $sqlloginrow['timeout'];
                            }
    

                            $schedule = $leavename.' Half Day';
                            
                        }else{

                            $schedule = $leavename;
                        }


                        

                        

                    } 
                    else{


                        $schedPSTST = new DateTime($schedST, new DateTimeZone('Asia/Manila'));         
                        $schedPSTST->setTimezone(new DateTimeZone('America/Los_Angeles'));
                        $schedPSTstarttime = $schedPSTST->format('h:i A');


                        $schedPSTET = new DateTime($schedET, new DateTimeZone('Asia/Manila'));         
                        $schedPSTET->setTimezone(new DateTimeZone('America/Los_Angeles'));
                        $schedPSTendtime = $schedPSTET->format('h:i A');

                        $OT =  $othrs;
                        $schedule = $schedPSTstarttime.' - '.$schedPSTendtime;

                        $logintime = $sqlloginrow['timein'];

                       $logintime.' - '.$schedPSTstarttime.'<br>';


                        $login = new DateTime($logintime);
                        $starttime = new DateTime($schedPSTstarttime);

                        if(empty($logintime)){


                            $late = '0';
                            $in = 'ABSENT';
                     
                            $out = 'ABSENT';




                        }else{

                            $in = $sqlloginrow['timein'];
                            $out = $sqlloginrow['timeout'];

                

                            if($login > $starttime){

                                $lateelapse = $starttime->diff($login);
    
                                $late = $lateelapse->format('%H:%I');
    
                            }else{
                                $late = '0';

                            }
                        }



       

 
    
                    

                    }








                    echo'<tr>
                        <td>'.$sqlrow['empid'].'</td>
                        <td>'.$sqlrow['team'].'</td>
                      <td>'.$sqlrow['firstname'].' '.$sqlrow['surname'].'</td>
                      <td>'.$date.'</td>';

                  

                        echo'
                      
                      <td>'.$in.'</td>
                      <td>'.$out.'</td>

                      
                      <td>'.$late.'</td>
                      ';
                      
      

                      if($ottype == 'Pre OT'){
                          echo '
                          <td>'.$OT.'</td>
                          <td> --- </td>
                          <td> --- </td>
                          
                          ';


                      }else if ($ottype == 'Post OT'){

                        echo '
                        <td> --- </td>
                        <td>'.$OT.'</td>
                        <td> --- </td>
                        
                        ';



                      }else if($ottype == 'RDOT'){

                        echo '
                        <td> --- </td>
                        <td> --- </td>
                        <td>'.$OT.'</td>
                        
                        ';



                      }else{

                        echo '
                        <td> --- </td>
                        <td> --- </td>
                        <td> --- </td>
                        
                        ';


                        


                      }


                      echo'

                      
                      <td>'.$schedule.'</td>

                     

                    
                    
                    </tr>';
                    
    
                  }
    
                ?>
            
            </table>





            <!-- /.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>

</body>

</html>