<?php 

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


if( !isset($_SESSION['user']) ) {
 header("Location: ../index.php");
 exit;
}
$empid = $_SESSION['user'];
$year = date('Y');
$weeks = date('W');
$TZ = "manila";
$manila ='';
$pacific ='hide';


$month = date('F');
 $myear = date('Y');
 $TZM = 'manila';
 $tzmanila ='';
 $tzmpacific ='hidden';


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

  $week =  $_POST['week'];
  $year = substr($week, 0, 4);
  $w = substr($week, -2);
  $weeks = ltrim($w, '0');
  $TZ = $_POST['tzwk'];

 

}

if($TZ == 'manila'){
  $manila ='';
  $pacific ='hidden';
}else{

  $manila ='hidden';
  $pacific ='';

}



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

 $months = $_POST['month'];
 $myear = substr($months, 0, 4);

   $month1 = substr($months, -2);

    $month =  date("F", strtotime(date("Y") ."-". $month1 ."-01"));

  

  $TZM = $_POST['MTZ'];

  
 if($TZM == 'manila'){
  $tzmmanila ='';
  $tzmpacific ='hidden';
}else{

  $tzmmanila ='hidden';
  $tzmpacific ='';

}


 

}


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);
    
    
}
function psttomnl($date, $time){
    
  $convert = new DateTime($date.' '.$time, new DateTimeZone('America/Los_Angeles'));
  
  
  $convert->setTimezone(new DateTimeZone('Asia/Manila'));
  
  $datetime = $convert->format('m/d/Y h:i A');
  
  
  return ($datetime);
  
  
  
  
  }
  
if(isset($_POST['teamweek'])){

  $week =  $_POST['week'];
  $year = substr($week, 0, 4);
  $w = substr($week, -2);
  $weeks = ltrim($w, '0');
  $TZ = $_POST['tzwk'];

 

}

if($TZ == 'manila'){
  $manila ='';
  $pacific ='hidden';
}else{

  $manila ='hidden';
  $pacific ='';

}



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

 $months = $_POST['month'];
 $myear = substr($months, 0, 4);

   $month1 = substr($months, -2);

    $month =  date("F", strtotime(date("Y") ."-". $month1 ."-01"));

    $empid = $_POST['Memp'];

  $TZM = $_POST['MTZ'];

  
 if($TZM == 'manila'){
  $tzmmanila ='';
  $tzmpacific ='hidden';
}else{

  $tzmmanila ='hidden';
  $tzmpacific ='';

}


 

}

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



  </head>

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



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





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

        <div class="container-fluid">

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

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

          <div class="text-right"><a href="updatesched.php?teamtab=<?php echo $style?>" class="btn btn-primary btn-sm"><i class="fas fa-edit"></i> Edit Schedule</a></div>

         
            <ul class="nav nav-tabs" id="myTab" role="tablist">
            <li class="nav-item">
                <a class="nav-link active" id="home-tab" data-toggle="tab" href="#home" role="tab" aria-controls="home" aria-selected="true"> <i class="fas fa-user"> </i> My Schedule & Attendance</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" id="profile-tab" data-toggle="tab" href="#profile" role="tab" aria-controls="profile" aria-selected="false"> <i class="fas fa-users"> </i> Employee Schedule & Attendance</a>
            </li>

            </ul>
            <div class="tab-content" id="myTabContent">
                <div class="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="home-tab">
                    <div class="row p-3">
                        <div class="col-lg-6 col-md-12 col-sm-12">
                            <h5>Schedule</h5>
                        </div>
                        <div class="col-lg-6 col-md-12 col-sm-12">
                            <h5>Attendance</h5>
                        </div>
                        <div class="col-lg-12 col-md-12 col-sm-12 form-inline"> <!-- FILTER  WEEK  -->
                            <form method="post" enctype="multipart/form-data">  
                                <button type="submit" class="btn btn-primary btn-sm" name="filter"> <i class='fas fa-filter'></i> Filter </button> &nbsp
                                <input type="week" name="week" class="form-control form-control-sm" value="<?php echo $week?>"> 

                                <select name="tzwk" id="tzwk" class="form-control form-control-sm" >
                                <?php 
                                switch ($TZ) {
                                  case 'manila':
                                  $pfc ='';
                                   $mnl ='selected';
                                    break;

                                    case 'pacific':
                                    $pfc ='selected';
                                    $mnl ='';
                                     break;
                                  
                                  default:
                                    # code...
                                    break;
                                }
                                ?>
                                <option value="manila" <?php echo $mnl?>> Manila </option>
                                <option value="pacific" <?php echo $pfc?>> Pacific </option>


                                </select>


                            </form>
                        

                        </div>
                        <div class="col-md-6 p-3">
                            <table class="table text-center table-bordered table-sm" <?php echo $manila?>>
                                <thead class="thead-dark">
                                    <?php 
                                    $theadate = "SELECT * FROM schedule WHERE year='$year' AND week='$weeks' LIMIT 7";
                                    $headdate = mysqli_query($conn, $theadate);

                                    while($headrow = mysqli_fetch_assoc($headdate)){
                                        echo '<th>
                                        '.$headrow['day'].'
                                        <br>'.date('m/d/Y', strtotime($headrow['date'])).'</th>';
                                    }

                                    ?>
                                </thead>
                                <tbody>
                                    <tr>
                                   <?php 
                                   $schedsql = "SELECT * FROM schedule WHERE empid='$empid' AND week='$weeks' AND year='$year'";
                                   $scheres = mysqli_query($conn, $schedsql);

                                   while($schedrow = mysqli_fetch_assoc($scheres)){
                                     $idl = $schedrow['idl'];

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



                                     if($schedrow['starttime'] == 'OFF'){

                                        echo '<td><span class="text-danger">OFF</span></td>';



                                     }else if($schedrow['starttime'] == 'HOLIDAY OFF'){

                                      echo '<td><span class="text-danger">HOLIDAY OFF</span></td>';



                                    }else if(!empty($leaverow['name'])){

                                      if( $schedrow['leaveduration'] == '0.5' ){

                                        echo ' <td style="text-align:center">';
                                        echo ' <span class="text-danger">'.$leaverow['name'].' Half Day</span> </td>';
          
                                      }else{
          
                                        echo ' <td style="text-align:center">';
                                        echo ' <span class="text-danger">'.$leaverow['name'].'  </span> </td>';
          
          
          
                                      }

                                       
                                        


                                    }else{

                                      echo '<td>'.date('h:i A', strtotime( $schedrow['starttime'])).'</td>';

                                    }

                                 

                                       
                                   }


                                   ?>
                                    </tr>
                                </tbody>
                                
                            </table> <!-- MANILA SCHEDULE -->
                            <table class="table text-center table-bordered table-sm" <?php echo $pacific?>>
                                <thead class="thead-dark">
                                    <?php 
                               
                                    $theadate = "SELECT * FROM schedule WHERE year='$year' AND week='$weeks' LIMIT 7";
                                    $headdate = mysqli_query($conn, $theadate);

                                    while($headrow = mysqli_fetch_assoc($headdate)){
                                        echo '<th>
                                        '.$headrow['day'].'
                                        <br>'.date('m/d/Y', strtotime($headrow['date'])).'</th>';

                                        
                                    }

                                    ?>
                                </thead>
                                <tbody>
                                    <tr>
                                   <?php 
                                   $schedsql = "SELECT * FROM schedule WHERE empid='$empid' AND week='$weeks' AND year='$year'";
                                   $scheres = mysqli_query($conn, $schedsql);

                                   while($schedrow = mysqli_fetch_assoc($scheres)){
                                     $Ymddate = date('Y-m-d', strtotime($schedrow['date']));

                                    
                                    $schedatttsql = "SELECT * FROM schedule WHERE empid='$empid' AND date='$Ymddate'";
                                    $schedattres = mysqli_query($conn, $schedatttsql);
                                    $schedattrow = mysqli_fetch_assoc($schedattres);
                                    //$idl = $schedattrow['idl'];
                                    $pstST =  $schedattrow['pstdate'];
                                    $mnlST = date('H:i:s', strtotime($schedattrow['starttime']));

                                    $getidl  ="SELECT * FROM schedule WHERE empid='$empid' AND pstdate='$Ymddate'";
                                    $getidlres = mysqli_query($conn, $getidl);

                                    $getidlrow = mysqli_fetch_assoc($getidlres);
                                    $idl = $getidlrow['idl'];


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

                                    
                                    $datetime= $Ymddate.' '.$pstST;
                                    $leaveres = mysqli_query($conn, $leavesql);
                                    $leaverow = mysqli_fetch_assoc($leaveres);

                                 



                                   if($schedrow['pststarttime'] == 'OFF'){

                                    echo '<td><span class="text-danger">OFF</span></td>';



                                 }else if($schedrow['pststarttime'] == 'HOLIDAY OFF'){

                                  echo '<td><span class="text-danger">HOLIDAY OFF</span></td>';



                                }else if(!empty($leaverow['name'])){

                                  if( $leaverow['duration'] == '0.5' ){

                                    echo ' <td style="text-align:center">';
                                    echo ' <span class="text-danger">'.$leaverow['name'].' Half Day</span> </td>';
      
                                  }else{
      
                                    echo ' <td style="text-align:center">';
                                    echo ' <span class="text-danger">'.$leaverow['name'].'  </span> </td>';
      
      
      
                                  }

                                   
                                    


                                }else{

                                  echo '<td>'.date('h:i A', strtotime( $schedrow['pststarttime'])).'</td>';

                                }


                            
                                    

                          

                                 

                                       
                                   }


                                   ?>
                                    </tr>
                                </tbody>
                                
                            </table><!-- PST SCHEDULE -->
                            
                        </div>

                        <div class="col-md-6 p-3">
                            <table class="table text-center table-bordered table-sm" <?php echo $manila?>>
                                <thead class="thead-dark">
                                    <?php 
                                    $headerdate = array();
                                    $theadate = "SELECT * FROM schedule WHERE year='$year' AND week='$weeks' LIMIT 7";
                                    $headdate = mysqli_query($conn, $theadate);

                                    while($headrow = mysqli_fetch_assoc($headdate)){
                                        echo '<th>
                                        '.$headrow['day'].'
                                        <br>'.date('m/d/Y', strtotime($headrow['date'])).'</th>';
                                        $headerdate[] = date('m/d/Y', strtotime($headrow['date']));
                                    }

                                    ?>
                                </thead>
                                <tbody>
                                    <tr>
                                   <?php 
                                    for($a=0; $a < count($headerdate); $a++){

                                      $Ymddate = date('Y-m-d', strtotime($headerdate[$a]));

                                        $gethrs = "SELECT * FROM schedule WHERE empid='$empid' AND year='$year' AND week='$weeks' AND starttime !='OFF' AND starttime !='HOLIDAY OFF' LIMIT 1";
                                      $gehrsres = mysqli_query($conn, $gethrs);
                                      $gethrsrow = mysqli_fetch_assoc($gehrsres);
                                      $hrs = $gethrsrow['starttime'];

                                      $PSTdate = date('m/d/Y', strtotime(mnltopst($headerdate[$a], $hrs)));

                                      $attsql = "SELECT * FROM tbl_login WHERE empid='$empid' AND date='$PSTdate'";
                                      $attres = mysqli_query($conn, $attsql);
                                      $attrow = mysqli_fetch_assoc($attres);

                                      $schedatttsql = "SELECT * FROM schedule WHERE empid='$empid' AND date='$Ymddate'";
                                      $schedattres = mysqli_query($conn, $schedatttsql);
                                      $schedattrow = mysqli_fetch_assoc($schedattres);
                                      $idl = $schedattrow['idl'];
                                  

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

                                      
                                
                                        

                                        if(empty($attrow['timein'])){
                                          $late='';
                                          
                                          $OT = '';
                                          $UT ='';
                                         

                                          

                                          if(!empty($leaverow['name'])){

                                             

                                              if($leaverow['duration'] == '0.5'){
                                                $attendance = '<span class="text-danger">ABSENT <br>'.$leaverow['name'].' Half Day</span>';


                                              }else{

                                                $attendance = '<span class="text-danger">'.$leaverow['name'].'</span>';

                                              }

                                          }else if($schedattrow['starttime'] == 'OFF'){

                                              $attendance ='<span class="text-danger">OFF</span>';

                                          }else if($schedattrow['starttime'] == 'HOLIDAY OFF'){

                                            $attendance = '<span class="text-danger">HOLIDAY OFF</span>';

                                          }else{
                                         
                                            $scheddate = new DateTime($Ymddate);
                                            $today = new DateTime(date('Y-m-d'));

                                            if($scheddate > $today){
                                              $attendance = '<span>---</span>';


                                            }else{

                                              $scheddate = new DateTime($Ymddate);
                                              $today = new DateTime(date('Y-m-d'));
  
                                              if($scheddate > $today){
                                                $attendance = '<span>---</span>';
  
  
                                              }else{
  
                                                $scheddate = new DateTime($Ymddate);
                                                $today = new DateTime(date('Y-m-d'));
    
                                                if($scheddate > $today){
                                                  $attendance = '<span>---</span>';
    
    
                                                }else{
    
                                                  $attendance = '<span class="text-danger">ABSENT</span>';
    
                                                }
    
                                              }
  

                                            }


                                            
                                          }

                                            



                                        }else{

                                          
                                           $login = date('h:i A', strtotime($attrow['timein']));
                                           $logout = date('h:i A', strtotime($attrow['timeout']));
                                           $starttime = $schedattrow['starttime'];
                                           $endtime = $schedattrow['endtime'];


                                           $timein = date('h:i A',strtotime(psttomnl($headerdate[$a], $login)));
                                           $timeout = date('h:i A',strtotime(psttomnl($headerdate[$a], $logout)));
                                           


                                                
                                            if($schedattrow['starttime'] == 'OFF'){

                                              if(!empty($othrs)){
                                                $attendance = $timein.'<br>'.$ottype.' '.$othrs;


                                              }else{
                                                $attendance =$schedattrow['starttime'].$time;
                                              }

                                            }else if($schedattrow['starttime'] == 'HOLIDAY OFF'){

                                              if(!empty($othrs)){
                                                $attendance = $timein.'<br>'.$ottype.' '.$othrs;


                                              }else{
                                                $attendance  =$schedattrow['starttime'];
                                              }

                                            }else if(!empty($leaverow['name'])){

                                              

                                              $attendance = $timein.' <br><span class="text-danger">'.$leaverow['name'].' Half Day</span><br>';
                                              


                                            }else{
                                              $ST = new DateTime($starttime);
                                              $ET = new DateTime($endtime);
                                              $TIN = new DateTime($timein);
                                              $TOUT = new DateTime($timeout);
                                        

             
                                              

                                              if($TIN > $ST){
                                                $attendance = $timein;
                                                $latehrs = $TIN->diff($ST);
                                                $latehrs = $latehrs->format('%H:%I');
                                                $late = '<span class="text-danger"><br> LATE  '.$latehrs.'</span>';

                                                if($TOUT< $ET){
                                                  $UThrs = $ET->diff($TOUT);
                                                  $UThrs = $UThrs->format('%H:%I');
                                                  $UT = '<span class="text-danger"><br> UNDERTIME  '.$UThrs.'</span>';
                                                }else if(!empty($othrs)){

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

                                                }else{
                                                  $OT = '';
                                                  $UT ='';

                                                  

                                                }


                                              }else{
                                                $attendance = $timein;
                                                

                                                if($TOUT< $ET){
                                                  $UThrs = $ET->diff($TOUT);
                                                  $UThrs = $UThrs->format('%H:%I');
                                                  $UT = '<span class="text-danger"><br> UNDERTIME  '.$UThrs.'</span>';
                                                }else if(!empty($othrs)){

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

                                                }else{

                                                  $OT = '';
                                                  $UT ='';
                                                  $late='';


                                                  
                                                }



                                              }

                                              


                                            }

                                    

                                          
                                      }
                                      echo '<td>'.$attendance.$late.$OT.$UT.'</td>';



                                    }



                                   ?>
                                    </tr>
                                </tbody>
                                
                            </table><!-- MANILA ATTENDANCE -->
                            <table class="table text-center table-bordered table-sm" <?php echo $pacific?>>
                                <thead class="thead-dark">
                                    <?php 
                                    $headerdate = array();
                                    $theadate = "SELECT * FROM schedule WHERE year='$year' AND week='$weeks' LIMIT 7";
                                    $headdate = mysqli_query($conn, $theadate);

                                    while($headrow = mysqli_fetch_assoc($headdate)){
                                        echo '<th>
                                        '.$headrow['day'].'
                                        <br>'.date('m/d/Y', strtotime($headrow['date'])).'</th>';
                                        $headerdate[] = date('m/d/Y', strtotime($headrow['date']));
                                    }

                                    ?>
                                </thead>
                                <tbody>
                                    <tr>
                                   <?php 
                                    for($a=0; $a < count($headerdate); $a++){

                                      $Ymddate = date('Y-m-d', strtotime($headerdate[$a]));

                                      $attsql = "SELECT * FROM tbl_login WHERE empid='$empid' AND date='$headerdate[$a]'";
                                      $attres = mysqli_query($conn, $attsql);
                                      $attrow = mysqli_fetch_assoc($attres);

                                      $schedatttsql = "SELECT * FROM schedule WHERE empid='$empid' AND date='$Ymddate'";
                                      $schedattres = mysqli_query($conn, $schedatttsql);
                                      $schedattrow = mysqli_fetch_assoc($schedattres);
                                      $idl = $schedattrow['idl'];
                                       $pstST =  $schedattrow['pststarttime'];
                                  

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

                                      $datetime= $Ymddate.' '.$pstST;

                                      
                                    $getidl  ="SELECT * FROM schedule WHERE empid='$empid' AND pstdate='$Ymddate'";
                                    $getidlres = mysqli_query($conn, $getidl);

                                    $getidlrow = mysqli_fetch_assoc($getidlres);
                                    $idl = $getidlrow['idl'];


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

                                    

  
  
                                      
 

                                     

                                      
                                
                                        

                                        if(empty($attrow['timein'])){
                                          $late='';
                                          
                                          $OT = '';
                                          $UT ='';
                                         

                                          

                                          if(!empty($leaverow['name'])){

                                              $attendance = '<span class="text-danger">'.$leaverow['name'].'</span>';

                                              if( $leaverow['duration'] == '0.5' ){

                                         

                                                
                                              $attendance = '<span class="text-danger">ABSENT <br>'.$leaverow['name'].' Half Day </span>';
                  
                                              }else{
                  
                                


                                                $attendance = '<span class="text-danger">'.$leaverow['name'].'</span>';
                  
                  
                  
                                              }
            

                                          }else if($schedattrow['pststarttime'] == 'OFF'){

                                              $attendance ='<span class="text-danger">OFF</span>';

                                          }else if($schedattrow['pststarttime'] == 'HOLIDAY OFF'){

                                            $attendance = '<span class="text-danger">HOLIDAY OFF</span>';

                                          }else{
                                            $scheddate = new DateTime($Ymddate);
                                            $today = new DateTime(date('Y-m-d'));

                                            if($scheddate > $today){
                                              $attendance = '<span>---</span>';


                                            }else{

                                              $scheddate = new DateTime($Ymddate);
                                              $today = new DateTime(date('Y-m-d'));
  
                                              if($scheddate > $today){
                                                $attendance = '<span>---</span>';
  
  
                                              }else{
  
                                                $attendance = '<span class="text-danger">ABSENT</span>';
  
                                              }
  

                                            }

                                          }

                                            



                                        }else{

                                          

                                          
                                           $login = date('h:i A', strtotime($attrow['timein']));
                                           $logout = date('h:i A', strtotime($attrow['timeout']));
                                           $starttime = $schedattrow['pststarttime'];
                                           $endtime = $schedattrow['pstendtime'];

                                           

                                           $timein = date('h:i A',strtotime(psttomnl($headerdate[$a], $login)));
                                           $timeout = date('h:i A',strtotime(psttomnl($headerdate[$a], $logout)));
                                           //echo $logout.' '.date('h:i A', strtotime($endtime)).'<br>';

                                                
                                            if($schedattrow['pststarttime'] == 'OFF'){

                                              if(!empty($othrs)){
                                                $attendance = $login.'<br>'.$ottype.' '.$othrs;


                                              }else{
                                                $attendance =$schedattrow['pststarttime'];
                                              }

                                            }else if($schedattrow['pststarttime'] == 'HOLIDAY OFF'){

                                              if(!empty($othrs)){
                                                $attendance = $login.'<br>'.$ottype.' '.$othrs;


                                              }else{
                                                $attendance  =$schedattrow['pststarttime'];
                                              }

                                            }else if(!empty($leaverow['name'])){

                                              $attendance = $login.' <span class="text-danger"><br>'.$leaverow['name'].' Half Day</span>';
                                              


                                            }else{

                                              $TIN = new DateTime($login);
                                              $TOUT = new DateTime($logout);
                                              $ST = new DateTime($starttime);
                                              $ET = new DateTime($endtime);
                                              $attendance = $login;
                                        

             
                                              
                                    

                                              if($TIN > $ST){
                                                $attendance = $login;
                                                $latehrs = $TIN->diff($ST);
                                                $latehrs = $latehrs->format('%H:%I');
                                                $late = '<span class="text-danger"><br> LATE  '.$latehrs.'</span>';

                                                if($TOUT< $ET){
                                                  $UThrs = $ET->diff($TOUT);
                                                  $UThrs = $UThrs->format('%H:%I');
                                                  $UT = '<span class="text-danger"><br> UNDERTIME  '.$UThrs.'</span>';
                                                }else if(!empty($othrs)){

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

                                                }else{
                                                  $OT = '';
                                                  $UT ='';

                                                  

                                                }


                                              }else{

        

                                                if($TOUT< $ET){
                                                  $UThrs = $ET->diff($TOUT);
                                                  $UThrs = $UThrs->format('%H:%I');
                                                  $UT = '<span class="text-danger"><br> UNDERTIME  '.$UThrs.'</span>';
                                                }else if(!empty($othrs)){

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

                                                }else{

                                                  $OT = '';
                                                  $UT ='';
                                                  $late='';


                                                  
                                                }



                                              }

                                              


                                            }

                                    

                                          
                                      }
                                      echo '<td>'.$attendance.$late.$OT.$UT.'</td>';



                                    }



                                   ?>
                                    </tr>
                                </tbody>
                                
                            </table><!-- PST ATTENDANCE  -->

                        </div>
                        <div class="col-lg-12 col-md-12 col-sm-12 form-inline"> <!-- FILTER  MONTHLY WEEK  -->
                            <form method="post" enctype="multipart/form-data">  
                                <button type="submit" class="btn btn-primary btn-sm" name="mfilter"> <i class='fas fa-filter'></i> Filter </button> &nbsp
                                <input type="month" name="month" class="form-control form-control-sm" value="<?php echo $months?>"> 

                                <select name="MTZ" id="MTZ" class="form-control form-control-sm" >
                                <?php 
                                switch ($TZM) {
                                  case 'manila':
                                  $Mpfc ='';
                                   $Mmnl ='selected';
                                    break;

                                    case 'pacific':
                                    $Mpfc ='selected';
                                    $Mmnl ='';
                                     break;
                           
                                }
                                ?>
                                <option value="manila" <?php echo $Mmnl?>> Manila </option>
                                <option value="pacific" <?php echo $Mpfc?>> Pacific </option>


                                </select>


                            </form>
                        

                        </div>
                        <div class="col-md-6 p-3">
                            <table class="table table-bordered table-hover table-sm text-center" <?php echo $tzmmanila?>>
                                <thead class="thead thead-dark">
                                  <th>Sunday</th>
                                  <th> Monday </th>
                                  <th> Tuesday </th>
                                  <th>  Wednesday </th>
                                  <th> Thursday </th>
                                  <th> Friday </th>
                                  <th>  Saturday </th>
                                </thead>
                                <tbody>
                                  <?php 
                                    $getnumweeks = "SELECT * FROM schedule WHERE empid='$empid' AND month='$month' AND year='$myear' GROUP BY week";
                                  $getnowkres = mysqli_query($conn, $getnumweeks);
                                  $weekno = array();
                                  while($weeknorow = mysqli_fetch_assoc($getnowkres)){
                                    $weekno[] =  $weeknorow['week'];

                                  }
                                  for($b =0; $b < count($weekno); $b++){
                                    $getdate ="SELECT * FROM schedule WHERE empid='$empid' AND week='$weekno[$b]' AND year='$myear'";
                                    $getdateres = mysqli_query($conn, $getdate);
                                    echo '<tr>';
                                    while($daterow = mysqli_fetch_assoc($getdateres)){
                                     
                                      echo '<td><span class="bg-primary text-white rounded p-1 inline-block">'.date('m/d/Y', strtotime($daterow['date'])).'</span><br>';
                                      $idl = $daterow['idl'];
   
                                      $leavesql ="SELECT * FROM `leave` WHERE idl='$idl' AND status='3'";
                                      $leaveres = mysqli_query($conn, $leavesql);
                                      $leaverow = mysqli_fetch_assoc($leaveres);
  
  
  
                                       if($daterow['starttime'] == 'OFF'){
  
                                          echo '<span class="text-danger">OFF</span>';
  
  
  
                                       }else if($daterow['starttime'] == 'HOLIDAY OFF'){
  
                                        echo '<span class="text-danger">HOLIDAY OFF</span>';
  
  
  
                                      }else if(!empty($leaverow['name'])){
  
                                        if( $daterow['leaveduration'] == '0.5' ){
  
                                         
                                          echo ' <span class="text-danger">'.$leaverow['name'].' Halfday</span> ';
            
                                        }else{
            
                                      
                                          echo ' <span class="text-danger">'.$leaverow['name'].'  </span> ';
            
            
            
                                        }
  
                                         
                                          
  
  
                                      }else{
  
                                        echo date('h:i A', strtotime( $daterow['starttime']));
  
                                      }
  
                                   
  

                                      echo '</td>';
                                

                                    }
                                    echo '</tr>';
                              

                                  }
                              

                                  
                                  ?>
                                </tbody>
                            </table>

                            <table class="table table-bordered table-hover table-sm text-center" <?php echo $tzmpacific?>>
                                <thead class="thead thead-dark">
                                  <th>Sunday</th>
                                  <th> Monday </th>
                                  <th> Tuesday </th>
                                  <th>  Wednesday </th>
                                  <th> Thursday </th>
                                  <th> Friday </th>
                                  <th>  Saturday </th>
                                </thead>
                                <tbody>
                                  <?php 
                                    $getnumweeks = "SELECT * FROM schedule WHERE empid='$empid' AND month='$month' AND year='$myear' GROUP BY week";
                                  $getnowkres = mysqli_query($conn, $getnumweeks);
                                  $weekno = array();
                                  while($weeknorow = mysqli_fetch_assoc($getnowkres)){
                                    $weekno[] =  $weeknorow['week'];

                                  }
                                  for($b =0; $b < count($weekno); $b++){
                                    $getdate ="SELECT * FROM schedule WHERE empid='$empid' AND week='$weekno[$b]' AND year='$myear'";
                                    $getdateres = mysqli_query($conn, $getdate);
                                    echo '<tr>';
                                    while($daterow = mysqli_fetch_assoc($getdateres)){
                                     
                                      echo '<td><span class="bg-primary text-white rounded p-1 inline-block">'.date('m/d/Y', strtotime($daterow['date'])).'</span><br>';

                                      $Ymddate = date('Y-m-d', strtotime($daterow['date']));

                                    
                                      $schedatttsql = "SELECT * FROM schedule WHERE empid='$empid' AND date='$Ymddate'";
                                      $schedattres = mysqli_query($conn, $schedatttsql);
                                      $schedattrow = mysqli_fetch_assoc($schedattres);
                                      $idl = $schedattrow['idl'];
                                      $pstST =  $schedattrow['pststarttime'];

                                      $getidl  ="SELECT * FROM schedule WHERE empid='$empid' AND pstdate='$Ymddate'";
                                      $getidlres = mysqli_query($conn, $getidl);
  
                                      $getidlrow = mysqli_fetch_assoc($getidlres);
                                      $idl = $getidlrow['idl'];
  
  
                                      $leavesql ="SELECT * FROM `leave` WHERE  idl='$idl' AND status='3'";
                                      $leaveres = mysqli_query($conn, $leavesql);
                                      $leaverow = mysqli_fetch_assoc($leaveres);
  
                                      
  
  
                                     if($daterow['pststarttime'] == 'OFF'){
  
                                      echo '<span class="text-danger">OFF</span>';
  
  
  
                                   }else if($daterow['pststarttime'] == 'HOLIDAY OFF'){
  
                                    echo '<span class="text-danger">HOLIDAY OFF</span>';
  
  
  
                                  }else if(!empty($leaverow['name'])){
  
                                    if( $leaverow['duration'] == '0.5' ){
  
                                    
                                      echo ' <span class="text-danger">'.$leaverow['name'].' Halfday</span>';
        
                                    }else{
        
                                      echo ' <span class="text-danger">'.$leaverow['name'].'  </span>';
        
        
        
                                    }
  
                                     
                                      
  
  
                                  }else{
  
                                    echo date('h:i A', strtotime( $daterow['pststarttime']));
  
                                  }
  

        

                                    
                                

                                    }
                                    echo '</tr>';
                              

                                  }
                              

                                  
                                  ?>
                                </tbody>
                            </table>
                        </div>
                        <div class="col-md-6 p-3">
                            <table class="table table-bordered table-hover table-sm text-center table-sm" <?php echo $tzmmanila?>>
                                <thead class="thead thead-dark">
                                  <th>Sunday</th>
                                  <th> Monday </th>
                                  <th> Tuesday </th>
                                  <th>  Wednesday </th>
                                  <th> Thursday </th>
                                  <th> Friday </th>
                                  <th>  Saturday </th>
                                </thead>
                                <tbody>
                                  <?php 
                                    $getnumweeks = "SELECT * FROM schedule WHERE empid='$empid' AND month='$month' AND year='$myear' GROUP BY week";
                                  $getnowkres = mysqli_query($conn, $getnumweeks);
                                  $weekno = array();
                                  while($weeknorow = mysqli_fetch_assoc($getnowkres)){
                                    $weekno[] =  $weeknorow['week'];

                                  }
                                  for($b =0; $b < count($weekno); $b++){
                                    $getdate ="SELECT * FROM schedule WHERE empid='$empid' AND week='$weekno[$b]' AND year='$myear'";
                                    $getdateres = mysqli_query($conn, $getdate);
                                    echo '<tr>';
                                    while($daterow = mysqli_fetch_assoc($getdateres)){
                                     
                                      echo '<td><span class="bg-primary text-white rounded p-1 inline-block">'.date('m/d/Y', strtotime($daterow['date'])).'</span><br>';

                                      $Ymddate = date('Y-m-d', strtotime($daterow['date']));

                                      $gethrs = "SELECT * FROM schedule WHERE empid='$empid' AND year='$year' AND week='$weeks' AND starttime !='OFF' AND starttime !='HOLIDAY OFF' LIMIT 1";
                                    $gehrsres = mysqli_query($conn, $gethrs);
                                    $gethrsrow = mysqli_fetch_assoc($gehrsres);
                                    $hrs = $gethrsrow['starttime'];

                                    $PSTdate = date('m/d/Y', strtotime(mnltopst($daterow['date'], $hrs)));

                                    $attsql = "SELECT * FROM tbl_login WHERE empid='$empid' AND date='$PSTdate'";
                                    $attres = mysqli_query($conn, $attsql);
                                    $attrow = mysqli_fetch_assoc($attres);

                                    $schedatttsql = "SELECT * FROM schedule WHERE empid='$empid' AND date='$Ymddate'";
                                    $schedattres = mysqli_query($conn, $schedatttsql);
                                    $schedattrow = mysqli_fetch_assoc($schedattres);
                                    $idl = $schedattrow['idl'];
                                

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

                                      if(empty($attrow['timein'])){
                                        $late='';
                                        
                                        $OT = '';
                                        $UT ='';

                                      
                                      if(!empty($leaverow['name'])){

                                        if( $leaverow['duration'] == '0.5' ){

                                         

                                                
                                          $attendance = '<span class="text-danger">ABSENT <br>'.$leaverow['name'].' Half Day </span>';
              
                                          }else{
              
                            


                                            $attendance = '<span class="text-danger">'.$leaverow['name'].'</span>';
              
              
              
                                          }

                        

                                    }else if($schedattrow['starttime'] == 'OFF'){

                                        $attendance ='<span class="text-danger">OFF</span>';

                                    }else if($schedattrow['starttime'] == 'HOLIDAY OFF'){

                                      $attendance = '<span class="text-danger">HOLIDAY OFF</span>';

                                    }else{
                                      $scheddate = new DateTime($Ymddate);
                                      $today = new DateTime(date('Y-m-d'));

                                      if($scheddate > $today){
                                        $attendance = '<span>---</span>';


                                      }else{

                                        $attendance = '<span class="text-danger">ABSENT</span>';

                                      }

                                    }

                                      



                                  }else{

                                    
                                     $login = date('h:i A', strtotime($attrow['timein']));
                                     $logout = date('h:i A', strtotime($attrow['timeout']));
                                     $starttime = $schedattrow['starttime'];
                                     $endtime = $schedattrow['endtime'];


                                     $timein = date('h:i A',strtotime(psttomnl($daterow['date'], $login)));
                                     $timeout = date('h:i A',strtotime(psttomnl($daterow['date'], $logout)));
                                     


                                          
                                      if($schedattrow['starttime'] == 'OFF'){

                                        if(!empty($othrs)){
                                          $attendance = $timein.'<br>'.$ottype.' '.$othrs;


                                        }else{
                                          $attendance =$schedattrow['starttime'].$time;
                                        }

                                      }else if($schedattrow['starttime'] == 'HOLIDAY OFF'){

                                        if(!empty($othrs)){
                                          $attendance = $timein.'<br>'.$ottype.' '.$othrs;


                                        }else{
                                          $attendance  =$schedattrow['starttime'];
                                        }

                                      }else if(!empty($leaverow['name'])){

                                        $attendance = $timein.'</span> <br> <span class="text-danger">'.$leaverow['name'].' Half Day</span><br>';
                                        


                                      }else{
                                        $ST = new DateTime($starttime);
                                        $ET = new DateTime($endtime);
                                        $TIN = new DateTime($timein);
                                        $TOUT = new DateTime($logout);

                                        
                                  

       
                                        

                                        if($TIN > $ST){
                                          $attendance = $timein;
                                          $latehrs = $TIN->diff($ST);
                                          $latehrs = $latehrs->format('%H:%I');
                                          $late = '<span class="text-danger"><br> LATE  '.$latehrs.'</span>';

                                          if($TOUT< $ET){
                                            $UThrs = $ET->diff($TOUT);
                                            $UThrs = $UThrs->format('%H:%I');
                                            $UT = '<span class="text-danger"><br> UNDERTIME  '.$UThrs.'</span>';
                                          }else if(!empty($othrs)){

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

                                          }else{
                                            $OT = '';
                                            $UT ='';

                                            

                                          }


                                        }else{
                                          $attendance = $timein;

                                          if($TOUT < $ET){
                                            $UThrs = $ET->diff($TOUT);
                                            $UThrs = $UThrs->format('%H:%I');
                                            $UT = '<span class="text-danger"><br> UNDERTIME  '.$UThrs.'</span>';
                                          }else if(!empty($othrs)){

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

                                          }else{

                                            $OT = '';
                                            $UT ='';
                                            $late='';


                                            
                                          }



                                        }

                                        


                                      }

                              

                                    
                                }
                                echo $attendance.$late.$OT.$UT;






   

                                      echo '</td>';
                                

                                    }
                                    echo '</tr>';
                              

                                  }
                              

                                  
                                  ?>
                                </tbody>
                            </table>

                            <table class="table table-bordered table-hover table-sm text-center table-sm" <?php echo $tzmpacific?>>
                                <thead class="thead thead-dark">
                                  <th>Sunday</th>
                                  <th> Monday </th>
                                  <th> Tuesday </th>
                                  <th>  Wednesday </th>
                                  <th> Thursday </th>
                                  <th> Friday </th>
                                  <th>  Saturday </th>
                                </thead>
                                <tbody>
                                  <?php 
                                    $getnumweeks = "SELECT * FROM schedule WHERE empid='$empid' AND month='$month' AND year='$myear' GROUP BY week";
                                  $getnowkres = mysqli_query($conn, $getnumweeks);
                                  $weekno = array();
                                  while($weeknorow = mysqli_fetch_assoc($getnowkres)){
                                    $weekno[] =  $weeknorow['week'];

                                  }
                                  for($b =0; $b < count($weekno); $b++){
                                    $getdate ="SELECT * FROM schedule WHERE empid='$empid' AND week='$weekno[$b]' AND year='$myear'";
                                    $getdateres = mysqli_query($conn, $getdate);
                                    echo '<tr>';
                                    while($daterow = mysqli_fetch_assoc($getdateres)){

                                      echo '<td><span class="bg-primary text-white rounded p-1 inline-block">'.date('m/d/Y', strtotime($daterow['date'])).'</span><br>';
                                      

                                      $Ymddate = date('Y-m-d', strtotime($daterow['date']));
                                      $mdYdate = date('m/d/Y', strtotime($daterow['date']));

                                      $attsql = "SELECT * FROM tbl_login WHERE empid='$empid' AND date='$mdYdate'";
                                      $attres = mysqli_query($conn, $attsql);
                                      $attrow = mysqli_fetch_assoc($attres);

                                      $schedatttsql = "SELECT * FROM schedule WHERE empid='$empid' AND date='$Ymddate'";
                                      $schedattres = mysqli_query($conn, $schedatttsql);
                                      $schedattrow = mysqli_fetch_assoc($schedattres);
                                      $idl = $schedattrow['idl'];
                                       $pstST =  $schedattrow['pststarttime'];
                                  

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

                                      $datetime= $Ymddate.' '.$pstST;

                                      
                                    $getidl  ="SELECT * FROM schedule WHERE empid='$empid' AND pstdate='$Ymddate'";
                                    $getidlres = mysqli_query($conn, $getidl);

                                    $getidlrow = mysqli_fetch_assoc($getidlres);
                                    $idl = $getidlrow['idl'];


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

                                    

  
  

                                     

                                     

                                      
                                
                                        

                                        if(empty($attrow['timein'])){
                                          $late='';
                                          
                                          $OT = '';
                                          $UT ='';
                                         

                                          

                                          if(!empty($leaverow['name'])){

                                             
                                            if($leaverow['duration'] == '0.5'){
                                              $attendance = '<span class="text-danger">ABSENT <br>'.$leaverow['name'].' Half Day</span>';


                                            }else{

                                              $attendance = '<span class="text-danger">'.$leaverow['name'].'</span>';

                                            }

                                          }else if($schedattrow['pststarttime'] == 'OFF'){

                                              $attendance ='<span class="text-danger">OFF</span>';

                                          }else if($schedattrow['pststarttime'] == 'HOLIDAY OFF'){

                                            $attendance = '<span class="text-danger">HOLIDAY OFF</span>';

                                          }else{
                                            $scheddate = new DateTime($Ymddate);
                                            $today = new DateTime(date('Y-m-d'));

                                            if($scheddate > $today){
                                              $attendance = '<span>---</span>';


                                            }else{

                                              $attendance = '<span class="text-danger">ABSENT</span>';

                                            }

                                            
                                          }

                                            



                                        }else{

                                          

                                          
                                           $login = date('h:i A', strtotime($attrow['timein']));
                                           $logout = date('h:i A', strtotime($attrow['timeout']));
                                           $starttime = $schedattrow['pststarttime'];
                                           $endtime = $schedattrow['pstendtime'];

                                           

                                           $timein = date('h:i A',strtotime(psttomnl($daterow['date'], $login)));
                                           $timeout = date('h:i A',strtotime(psttomnl($daterow['date'], $logout)));
                                          // echo $logout.' '.date('h:i A', strtotime($endtime)).'<br>';

                                                
                                            if($schedattrow['pststarttime'] == 'OFF'){

                                              if(!empty($othrs)){
                                                $attendance = $login.'<br>'.$ottype.' '.$othrs;


                                              }else{
                                                $attendance =$schedattrow['pststarttime'];
                                              }

                                            }else if($schedattrow['pststarttime'] == 'HOLIDAY OFF'){

                                              if(!empty($othrs)){
                                                $attendance = $login.'<br>'.$ottype.' '.$othrs;


                                              }else{
                                                $attendance  =$schedattrow['pststarttime'];
                                              }

                                            }else if(!empty($leaverow['name'])){

                                              $attendance = $login.' <br> <span class="text-danger">'.$leaverow['name'].' Half Day</span><br>';
                                              


                                            }else{

                                              $TIN = new DateTime($login);
                                              $TOUT = new DateTime($logout);
                                              $ST = new DateTime($starttime);
                                              $ET = new DateTime($endtime);
                                              $attendance = $login;
                                        

             
                                              

                                              if($TIN > $ST){
                                                $attendance = $login;
                                                $latehrs = $TIN->diff($ST);
                                                $latehrs = $latehrs->format('%H:%I');
                                                $late = '<span class="text-danger"><br> LATE  '.$latehrs.'</span>';

                                                if($TOUT< $ET){
                                                  $UThrs = $ET->diff($TOUT);
                                                  $UThrs = $UThrs->format('%H:%I');
                                                  $UT = '<span class="text-danger"><br> UNDERTIME  '.$UThrs.'</span>';
                                                }else if(!empty($othrs)){

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

                                                }else{
                                                  $OT = '';
                                                  $UT ='';

                                                  

                                                }


                                              }else{

        

                                                if($TOUT< $ET){
                                                  $UThrs = $ET->diff($TOUT);
                                                  $UThrs = $UThrs->format('%H:%I');
                                                  $UT = '<span class="text-danger"><br> UNDERTIME  '.$UThrs.'</span>';
                                                }else if(!empty($othrs)){

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

                                                }else{

                                                  $OT = '';
                                                  $UT ='';
                                                  $late='';


                                                  
                                                }



                                              }

                                              


                                            }

                                    

                                          
                                      }
                                      echo $attendance.$late.$OT.$UT;


                                     

                                    
                                echo '</td>';

                                    }
                                    echo '</tr>';
                              

                                  }
                              

                                  
                                  ?>
                                </tbody>
                            </table>
                        </div>
                    
                        
                    </div>
                </div>
                <div class="tab-pane fade" id="profile" role="tabpanel" aria-labelledby="profile-tab">
                <div class="row p-3">
                        <div class="col-lg-6 col-md-12 col-sm-12">
                            <h5>Schedule</h5>
                        </div>
                        <div class="col-lg-6 col-md-12 col-sm-12">
                            <h5>Attendance</h5>
                        </div>
                        <div class="col-lg-12 col-md-12 col-sm-12 form-inline"> <!-- FILTER  WEEK  -->
                            <form method="post" enctype="multipart/form-data">  
                                <button type="submit" class="btn btn-primary btn-sm" name="teamweek"> <i class='fas fa-filter'></i> Filter </button> &nbsp
                                <input type="week" name="week" class="form-control form-control-sm" value="<?php echo $week?>"> 

                                <select name="tzwk" id="tzwk" class="form-control form-control-sm" >
                                <?php 
                                switch ($TZ) {
                                  case 'manila':
                                  $pfc ='';
                                   $mnl ='selected';
                                    break;

                                    case 'pacific':
                                    $pfc ='selected';
                                    $mnl ='';
                                     break;
                                  
                                  default:
                                    # code...
                                    break;
                                }
                                ?>
                                <option value="manila" <?php echo $mnl?>> Manila </option>
                                <option value="pacific" <?php echo $pfc?>> Pacific </option>


                                </select>


                            </form>
                        

                        </div>
                        <div class="col-md-6 p-3">
                            <div class="tableFixHead" style="height: 350px">
                              <table class="table text-center table-bordered table-sm" style="font-size: 12px;" <?php echo $manila?>>
                                  <thead class="thead-dark">
                                      <?php 
                                      $theadate = "SELECT * FROM schedule WHERE year='$year' AND week='$weeks' LIMIT 7";
                                      $headdate = mysqli_query($conn, $theadate);
                                      echo'
                                      <th>Employee ID</th>
                                      <th> Name </th>
                                      ';
                                      

                                      while($headrow = mysqli_fetch_assoc($headdate)){
                                          echo '<th>
                                          '.$headrow['day'].'
                                          <br>'.date('m/d/Y', strtotime($headrow['date'])).'</th>';
                                      }

                                      ?>
                                  </thead>
                                  <tbody>
                                    
                                    <?php 
                                    $emplist = array();
                                    $empname = array();
                                    echo$empinfo = "SELECT * FROM empinfo JOIN empdetails ON empinfo.empid = empdetails.detailid WHERE status = 'Active' ";
                                    $eminfores =  mysqli_query($conn, $empinfo);

                                    while($emprow = mysqli_fetch_assoc($eminfores)){

                                      $emplist[] = $emprow['empid'];
                                      $empname[] =$emprow['firstname'].' '.$emprow['surname'];
                                    }

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

                                        echo'<tr>';

                                        echo'<td>'.$emplist[$a].'</td>';
                                        echo'<td>'.$empname[$a].'</td>';
                                        

                                        $schedsql = "SELECT * FROM schedule WHERE empid='$emplist[$a]' AND week='$weeks' AND year='$year'";
                                        $scheres = mysqli_query($conn, $schedsql);

                                        while($schedrow = mysqli_fetch_assoc($scheres)){
                                          $idl = $schedrow['idl'];

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



                                          if($schedrow['starttime'] == 'OFF'){

                                              echo '<td><span class="text-danger">OFF</span></td>';



                                          }else if($schedrow['starttime'] == 'HOLIDAY OFF'){

                                            echo '<td><span class="text-danger">HOLIDAY OFF</span></td>';



                                          }else if(!empty($leaverow['name'])){

                                            if( $schedrow['leaveduration'] == '0.5' ){

                                              echo ' <td style="text-align:center">';
                                              echo ' <span class="text-danger">'.$leaverow['name'].' Halfday</span> </td>';
                
                                            }else{
                
                                              echo ' <td style="text-align:center">';
                                              echo ' <span class="text-danger">'.$leaverow['name'].'  </span> </td>';
                
                
                
                                            }

                                            
                                              


                                          }else{

                                            echo '<td>'.date('h:i A', strtotime( $schedrow['starttime'])).'</td>';

                                          }

                                      

                                            
                                        }

                                        echo'</tr>';


                                    }



                                    ?>
                                    
                                  </tbody>
                                  
                              </table> <!-- MANILA SCHEDULE -->

                              <table class="table text-center table-bordered table-sm" style="font-size: 12px;" <?php echo $pacific?>>
                               
                                 <thead class="thead-dark">
                                      <?php 
                                
                                      $theadate = "SELECT * FROM schedule WHERE year='$year' AND week='$weeks' LIMIT 7";
                                      $headdate = mysqli_query($conn, $theadate);
                                      echo'
                                      <th>Employee ID</th>
                                      <th> Name </th>
                                      ';

                                      while($headrow = mysqli_fetch_assoc($headdate)){
                                          echo '<th>
                                          '.$headrow['day'].'
                                          <br>'.date('m/d/Y', strtotime($headrow['date'])).'</th>';

                                          
                                      }

                                      ?>
                                  </thead>
                                  <tbody>
                                    
                                    <?php 
                                    $emplist = array();
                                    $empname = array();
                                    $empinfo = "SELECT * FROM empinfo JOIN empdetails ON empinfo.empid = empdetails.detailid WHERE status = 'Active'";
                                    $eminfores =  mysqli_query($conn, $empinfo);
                                    
                                    
                                    while($emprow = mysqli_fetch_assoc($eminfores)){

                                      $emplist[] = $emprow['empid'];
                                      $empname[] =$emprow['firstname'].' '.$emprow['surname'];
                                    }

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

                                      echo'<tr>';

                                      echo'<td>'.$emplist[$a].'</td>';
                                      echo'<td>'.$empname[$a].'</td>';
                                      

                                    
                                      $schedsql = "SELECT * FROM schedule WHERE empid='$emplist[$a]' AND week='$weeks' AND year='$year'";
                                      $scheres = mysqli_query($conn, $schedsql);

                                        while($schedrow = mysqli_fetch_assoc($scheres)){
                                          $Ymddate = date('Y-m-d', strtotime($schedrow['date']));

                                          
                                          $schedatttsql = "SELECT * FROM schedule WHERE empid='$emplist[$a]' AND date='$Ymddate'";
                                          $schedattres = mysqli_query($conn, $schedatttsql);
                                          $schedattrow = mysqli_fetch_assoc($schedattres);
                                          //$idl = $schedattrow['idl'];
                                          $pstST =  $schedattrow['pstdate'];
                                          $mnlST = date('H:i:s', strtotime($schedattrow['starttime']));

                                          $getidl  ="SELECT * FROM schedule WHERE empid='$emplist[$a]' AND pstdate='$Ymddate'";
                                          $getidlres = mysqli_query($conn, $getidl);

                                          $getidlrow = mysqli_fetch_assoc($getidlres);
                                          $idl = $getidlrow['idl'];


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

                                          
                                          $datetime= $Ymddate.' '.$pstST;
                                          $leaveres = mysqli_query($conn, $leavesql);
                                          $leaverow = mysqli_fetch_assoc($leaveres);

                                      



                                          if($schedrow['pststarttime'] == 'OFF'){

                                            echo '<td><span class="text-danger">OFF</span></td>';



                                          }else if($schedrow['pststarttime'] == 'HOLIDAY OFF'){

                                            echo '<td><span class="text-danger">HOLIDAY OFF</span></td>';



                                          }else if(!empty($leaverow['name'])){

                                            if( $leaverow['duration'] == '0.5' ){

                                              echo ' <td style="text-align:center">';
                                              echo ' <span class="text-danger">'.$leaverow['name'].' Half Day</span> </td>';
                
                                            }else{
                
                                              echo ' <td style="text-align:center">';
                                              echo ' <span class="text-danger">'.$leaverow['name'].'  </span> </td>';
                
                
                
                                            }

                                            
                                              


                                          }else{

                                            echo '<td>'.date('h:i A', strtotime( $schedrow['pststarttime'])).'</td>';

                                          }


                                  
                                          

                                

                                      

                                            
                                        }

                                        echo'</tr>';

                                      }


                                    ?>
                                
                                  </tbody>
                                  
                              </table><!-- PST SCHEDULE -->
                            </div>
                            
                        </div>

                        <div class="col-md-6 p-3">
                          <div class="tableFixHead" style="height: 350px">
                            <table class="table text-center table-bordered table-sm" style="font-size: 11px;" <?php echo $manila?>>
                                <thead class="thead-dark">
                                    <?php 
                                    $headerdate = array();
                                    $theadate = "SELECT * FROM schedule WHERE year='$year' AND week='$weeks' LIMIT 7";
                                    $headdate = mysqli_query($conn, $theadate);
                                    echo'
                                    <th>Employee ID</th>
                                    <th> Name </th>
                                    ';
                                    

                                    while($headrow = mysqli_fetch_assoc($headdate)){
                                        echo '<th>
                                        '.$headrow['day'].'
                                        <br>'.date('m/d/Y', strtotime($headrow['date'])).'</th>';
                                        $headerdate[] = date('m/d/Y', strtotime($headrow['date']));
                                    }

                                    ?>
                                </thead>
                                <tbody>
                                    
                                   <?php 
                                      $emplist = array();
                                      $empname = array();
                                      $empinfo = "SELECT * FROM empinfo JOIN empdetails ON empinfo.empid = empdetails.detailid WHERE status = 'Active'";
                                      $eminfores =  mysqli_query($conn, $empinfo);
  
                                      while($emprow = mysqli_fetch_assoc($eminfores)){
  
                                        $emplist[] = $emprow['empid'];
                                        $empname[] =$emprow['firstname'].' '.$emprow['surname'];
                                      }
                                      for($b=0; $b < count($emplist); $b++){

                                          echo'<tr>';

                                          echo'<td>'.$emplist[$b].'</td>';
                                          echo'<td>'.$empname[$b].'</td>';

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

                                          $Ymddate = date('Y-m-d', strtotime($headerdate[$a]));

                                          $gethrs = "SELECT * FROM schedule WHERE empid='$emplist[$b]' AND year='$year' AND week='$weeks' AND starttime !='OFF' AND starttime !='HOLIDAY OFF' LIMIT 1";
                                          $gehrsres = mysqli_query($conn, $gethrs);
                                          $gethrsrow = mysqli_fetch_assoc($gehrsres);
                                          $hrs = $gethrsrow['starttime'];

                                          $PSTdate = date('m/d/Y', strtotime(mnltopst($headerdate[$a], $hrs)));

                                          $attsql = "SELECT * FROM tbl_login WHERE empid='$emplist[$b]' AND date='$PSTdate'";
                                          $attres = mysqli_query($conn, $attsql);
                                          $attrow = mysqli_fetch_assoc($attres);

                                          $schedatttsql = "SELECT * FROM schedule WHERE empid='$emplist[$b]' AND date='$Ymddate'";
                                          $schedattres = mysqli_query($conn, $schedatttsql);
                                          $schedattrow = mysqli_fetch_assoc($schedattres);
                                          $idl = $schedattrow['idl'];
                                      

                            
                                          $otsql = "SELECT * FROM ot WHERE empid='$emplist[$b]' AND attendance ='$liloid' AND status='3'";
                                          $otres = mysqli_query($conn, $otsql);
                                          $otrow =  mysqli_fetch_assoc($otres);
                                          $ottype = $otrow['type'];
                                          $othrs = $otrow['hrs'];
      
      
                                          
                                          $leavesql ="SELECT * FROM `leave` WHERE idl='$idl' AND status='3'";
                                          $leaveres = mysqli_query($conn, $leavesql);
                                          $leaverow = mysqli_fetch_assoc($leaveres);

                                          
                                    
                                            

                                            if(empty($attrow['timein'])){
                                              $late='';
                                              
                                              $OT = '';
                                              $UT ='';
                                            

                                              

                                              if(!empty($leaverow['name'])){

                                                

                                                  if($leaverow['duration'] == '0.5'){
                                                    $attendance = '<span class="text-danger">ABSENT <br>'.$leaverow['name'].' Half Day</span>';


                                                  }else{

                                                    $attendance = '<span class="text-danger">'.$leaverow['name'].'</span>';

                                                  }

                                              }else if($schedattrow['starttime'] == 'OFF'){

                                                  $attendance ='<span class="text-danger">OFF</span>';

                                              }else if($schedattrow['starttime'] == 'HOLIDAY OFF'){

                                                $attendance = '<span class="text-danger">HOLIDAY OFF</span>';

                                              }else{
                                                $scheddate = new DateTime($Ymddate);
                                                $today = new DateTime(date('Y-m-d'));
    
                                                if($scheddate > $today){
                                                  $attendance = '<span>---</span>';
    
    
                                                }else{
    
                                                  $attendance = '<span class="text-danger">ABSENT</span>';
    
                                                }
    
                                              }

                                                



                                            }else{

                                              
                                              $login = date('h:i A', strtotime($attrow['timein']));
                                              $logout = date('h:i A', strtotime($attrow['timeout']));
                                              $starttime = $schedattrow['starttime'];
                                              $endtime = $schedattrow['endtime'];


                                              $timein = date('h:i A',strtotime(psttomnl($headerdate[$a], $login)));
                                              $timeout = date('h:i A',strtotime(psttomnl($headerdate[$a], $logout)));
                                              


                                                    
                                                if($schedattrow['starttime'] == 'OFF'){

                                                  if(!empty($othrs)){
                                                    $attendance = $timein.'<br>'.$ottype.' '.$othrs;


                                                  }else{
                                                    $attendance =$schedattrow['starttime'].$time;
                                                  }

                                                }else if($schedattrow['starttime'] == 'HOLIDAY OFF'){

                                                  if(!empty($othrs)){
                                                    $attendance = $timein.'<br>'.$ottype.' '.$othrs;


                                                  }else{
                                                    $attendance  =$schedattrow['starttime'];
                                                  }

                                                }else if(!empty($leaverow['name'])){

                                                  

                                                  $attendance = $timein.' '.$leaverow['name'].' Half Day<br>';
                                                  


                                                }else{
                                                  $ST = new DateTime($starttime);
                                                  $ET = new DateTime($endtime);
                                                  $TIN = new DateTime($timein);
                                                  $TOUT = new DateTime($timeout);
                                            

                
                                                  

                                                  if($TIN > $ST){
                                                    $attendance = $timein;
                                                    $latehrs = $TIN->diff($ST);
                                                    $latehrs = $latehrs->format('%H:%I');
                                                    $late = '<span class="text-danger"><br> LATE  '.$latehrs.'</span>';

                                                    if($TOUT< $ET){
                                                      $UThrs = $ET->diff($TOUT);
                                                      $UThrs = $UThrs->format('%H:%I');
                                                      $UT = '<span class="text-danger"><br> UNDERTIME  '.$UThrs.'</span>';
                                                    }else if(!empty($othrs)){

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

                                                    }else{
                                                      $OT = '';
                                                      $UT ='';

                                                      

                                                    }


                                                  }else{
                                                    $attendance = $timein;
                                                    

                                                    if($TOUT< $ET){
                                                      $UThrs = $ET->diff($TOUT);
                                                      $UThrs = $UThrs->format('%H:%I');
                                                      $UT = '<span class="text-danger"><br> UNDERTIME  '.$UThrs.'</span>';
                                                    }else if(!empty($othrs)){

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

                                                    }else{

                                                      $OT = '';
                                                      $UT ='';
                                                      $late='';


                                                      
                                                    }



                                                  }

                                                  


                                                }

                                        

                                              
                                          }
                                          echo '<td>'.$attendance.$late.$OT.$UT.'</td>';



                                        }

                                        echo'</tr>';

                                      }



                                   ?>
                                  
                                </tbody>
                                
                            </table><!-- MANILA ATTENDANCE -->
                            <table class="table text-center table-bordered table-sm" style="font-size: 11px;"  <?php echo $pacific?>>
                                <thead class="thead-dark">
                                    <?php 
                                    $headerdate = array();
                                    $theadate = "SELECT * FROM schedule WHERE year='$year' AND week='$weeks' LIMIT 7";
                                    $headdate = mysqli_query($conn, $theadate);

                                    echo'
                                    <th>Employee ID</th>
                                    <th> Name </th>
                                    ';
                                    

                                    while($headrow = mysqli_fetch_assoc($headdate)){
                                        echo '<th>
                                        '.$headrow['day'].'
                                        <br>'.date('m/d/Y', strtotime($headrow['date'])).'</th>';
                                        $headerdate[] = date('m/d/Y', strtotime($headrow['date']));
                                    }

                                    ?>
                                </thead>
                                <tbody>
                                    <tr>
                                   <?php 
                                    $emplist = array();
                                    $empname = array();
                                    $empinfo = "SELECT * FROM empinfo JOIN empdetails ON empinfo.empid = empdetails.detailid WHERE status = 'Active'";
                                    $eminfores =  mysqli_query($conn, $empinfo);

                                    while($emprow = mysqli_fetch_assoc($eminfores)){

                                      $emplist[] = $emprow['empid'];
                                      $empname[] =$emprow['firstname'].' '.$emprow['surname'];
                                    }
                                    for($b=0; $b < count($emplist); $b++){
                                      echo'<tr>';

                                      echo'<td>'.$emplist[$b].'</td>';
                                      echo'<td>'.$empname[$b].'</td>';

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

                                        $Ymddate = date('Y-m-d', strtotime($headerdate[$a]));

                                        $attsql = "SELECT * FROM tbl_login WHERE empid='$emplist[$b]' AND date='$headerdate[$a]'";
                                        $attres = mysqli_query($conn, $attsql);
                                        $attrow = mysqli_fetch_assoc($attres);

                                        $schedatttsql = "SELECT * FROM schedule WHERE empid='$emplist[$b]' AND date='$Ymddate'";
                                        $schedattres = mysqli_query($conn, $schedatttsql);
                                        $schedattrow = mysqli_fetch_assoc($schedattres);
                                        $idl = $schedattrow['idl'];
                                        $pstST =  $schedattrow['pststarttime'];
                                    

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

                                        $datetime= $Ymddate.' '.$pstST;

                                        
                                        $getidl  ="SELECT * FROM schedule WHERE empid='$emplist[$b]' AND pstdate='$Ymddate'";
                                        $getidlres = mysqli_query($conn, $getidl);

                                        $getidlrow = mysqli_fetch_assoc($getidlres);
                                        $idl = $getidlrow['idl'];


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

                                      

    
    
                                        
  

                                      

                                        
                                  
                                          

                                          if(empty($attrow['timein'])){
                                            $late='';
                                            
                                            $OT = '';
                                            $UT ='';
                                          

                                            

                                            if(!empty($leaverow['name'])){

                                                $attendance = '<span class="text-danger">'.$leaverow['name'].'</span>';

                                                if( $leaverow['duration'] == '0.5' ){

                                          

                                                  
                                                $attendance = '<span class="text-danger">ABSENT <br>'.$leaverow['name'].' Half Day </span>';
                    
                                                }else{
                    
                                  


                                                  $attendance = '<span class="text-danger">'.$leaverow['name'].'</span>';
                    
                    
                    
                                                }
              

                                            }else if($schedattrow['pststarttime'] == 'OFF'){

                                                $attendance ='<span class="text-danger">OFF</span>';

                                            }else if($schedattrow['pststarttime'] == 'HOLIDAY OFF'){

                                              $attendance = '<span class="text-danger">HOLIDAY OFF</span>';

                                            }else{
                                              $scheddate = new DateTime($Ymddate);
                                              $today = new DateTime(date('Y-m-d'));
  
                                              if($scheddate > $today){
                                                $attendance = '<span>---</span>';
  
  
                                              }else{
  
                                                $attendance = '<span class="text-danger">ABSENT</span>';
  
                                              }
  
                                            }

                                              



                                          }else{

                                            

                                            
                                            $login = date('h:i A', strtotime($attrow['timein']));
                                            $logout = date('h:i A', strtotime($attrow['timeout']));
                                            $starttime = $schedattrow['pststarttime'];
                                            $endtime = $schedattrow['pstendtime'];

                                            

                                            $timein = date('h:i A',strtotime(psttomnl($headerdate[$a], $login)));
                                            $timeout = date('h:i A',strtotime(psttomnl($headerdate[$a], $logout)));
                                            //echo $logout.' '.date('h:i A', strtotime($endtime)).'<br>';

                                                  
                                              if($schedattrow['pststarttime'] == 'OFF'){

                                                if(!empty($othrs)){
                                                  $attendance = $login.'<br>'.$ottype.' '.$othrs;


                                                }else{
                                                  $attendance =$schedattrow['pststarttime'];
                                                }

                                              }else if($schedattrow['pststarttime'] == 'HOLIDAY OFF'){

                                                if(!empty($othrs)){
                                                  $attendance = $login.'<br>'.$ottype.' '.$othrs;


                                                }else{
                                                  $attendance  =$schedattrow['pststarttime'];
                                                }

                                              }else if(!empty($leaverow['name'])){

                                                $attendance = $login.' '.$leaverow['name'].' Half Day<br>';
                                                


                                              }else{

                                                $TIN = new DateTime($login);
                                                $TOUT = new DateTime($logout);
                                                $ST = new DateTime($starttime);
                                                $ET = new DateTime($endtime);
                                                $attendance = $login;
                                          

              
                                                
                                      

                                                if($TIN > $ST){
                                                  $attendance = $login;
                                                  $latehrs = $TIN->diff($ST);
                                                  $latehrs = $latehrs->format('%H:%I');
                                                  $late = '<span class="text-danger"><br> LATE  '.$latehrs.'</span>';

                                                  if($TOUT< $ET){
                                                    $UThrs = $ET->diff($TOUT);
                                                    $UThrs = $UThrs->format('%H:%I');
                                                    $UT = '<span class="text-danger"><br> UNDERTIME  '.$UThrs.'</span>';
                                                  }else if(!empty($othrs)){

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

                                                  }else{
                                                    $OT = '';
                                                    $UT ='';

                                                    

                                                  }


                                                }else{

          

                                                  if($TOUT< $ET){
                                                    $UThrs = $ET->diff($TOUT);
                                                    $UThrs = $UThrs->format('%H:%I');
                                                    $UT = '<span class="text-danger"><br> UNDERTIME  '.$UThrs.'</span>';
                                                  }else if(!empty($othrs)){

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

                                                  }else{

                                                    $OT = '';
                                                    $UT ='';
                                                    $late='';


                                                    
                                                  }



                                                }

                                                


                                              }

                                      

                                            
                                        }
                                        echo '<td>'.$attendance.$late.$OT.$UT.'</td>';



                                      }

                                      echo'</tr>';
                                    }



                                   ?>
                                    </tr>
                                </tbody>
                                
                            </table><!-- PST ATTENDANCE  -->
                          </div>

                        </div>
                        <div class="col-lg-12 col-md-12 col-sm-12 form-inline"> <!-- FILTER  MONTHLY WEEK  -->
                            <form method="post" enctype="multipart/form-data">  
                                <button type="submit" class="btn btn-primary btn-sm" name="teammonth"> <i class='fas fa-filter'></i> Filter </button> &nbsp
                                <input type="month" name="month" class="form-control form-control-sm" value="<?php echo $months?>"> 

                                <select name="Memp" id="Memp" class="form-control form-control-sm"> 
                                  <option selected disabled>Employee</option>
                                    <?php 
                                      $getemp = "SELECT * FROM empinfo JOIN empdetails ON empinfo.empid = empdetails.detailid WHERE status = 'Active'";
                                      $getempres = mysqli_query($conn, $empinfo);
                                      while($getemprow = mysqli_fetch_assoc($getempres)){

                                        if($empid == $getemprow['empid']){
                                          echo '<option value="'.$getemprow['empid'].'" selected>'.$getemprow['empid'].' '.$getemprow['firstname'].' '.$getemprow['surname'].'</option>';

                                        }else{

                                          echo '<option value="'.$getemprow['empid'].'">'.$getemprow['empid'].' '.$getemprow['firstname'].' '.$getemprow['surname'].'</option>';
                                     

                                        }
                                      }
                                    ?>
                                </select>

                                <select name="MTZ" id="MTZ" class="form-control form-control-sm" >
                                <?php 
                                switch ($TZM) {
                                  case 'manila':
                                  $Mpfc ='';
                                   $Mmnl ='selected';
                                    break;

                                    case 'pacific':
                                    $Mpfc ='selected';
                                    $Mmnl ='';
                                     break;
                           
                                }
                                ?>
                                <option value="manila" <?php echo $Mmnl?>> Manila </option>
                                <option value="pacific" <?php echo $Mpfc?>> Pacific </option>


                                </select>


                            </form>
                        

                        </div>
                        <div class="col-md-6 p-3">
                            <table class="table table-bordered table-hover table-sm text-center" <?php echo $tzmmanila?>>
                                <thead class="thead thead-dark">
                                  <th>Sunday</th>
                                  <th> Monday </th>
                                  <th> Tuesday </th>
                                  <th>  Wednesday </th>
                                  <th> Thursday </th>
                                  <th> Friday </th>
                                  <th>  Saturday </th>
                                </thead>
                                <tbody>
                                  <?php 
                                    $getnumweeks = "SELECT * FROM schedule WHERE empid='$empid' AND month='$month' AND year='$myear' GROUP BY week";
                                  $getnowkres = mysqli_query($conn, $getnumweeks);
                                  $weekno = array();
                                  while($weeknorow = mysqli_fetch_assoc($getnowkres)){
                                    $weekno[] =  $weeknorow['week'];

                                  }
                                  for($b =0; $b < count($weekno); $b++){
                                     $getdate ="SELECT * FROM schedule WHERE empid='$empid' AND week='$weekno[$b]' AND year='$myear'";
                                    $getdateres = mysqli_query($conn, $getdate);
                                    echo '<tr>';
                                    while($daterow = mysqli_fetch_assoc($getdateres)){
                                     
                                      echo '<td><span class="bg-primary text-white rounded p-1 inline-block">'.date('m/d/Y', strtotime($daterow['date'])).'</span><br>';
                                      $idl = $daterow['idl'];
   
                                      $leavesql ="SELECT * FROM `leave` WHERE idl='$idl' AND status='3'";
                                      $leaveres = mysqli_query($conn, $leavesql);
                                      $leaverow = mysqli_fetch_assoc($leaveres);
  
  
  
                                       if($daterow['starttime'] == 'OFF'){
  
                                          echo '<span class="text-danger">OFF</span>';
  
  
  
                                       }else if($daterow['starttime'] == 'HOLIDAY OFF'){
  
                                        echo '<span class="text-danger">HOLIDAY OFF</span>';
  
  
  
                                      }else if(!empty($leaverow['name'])){
  
                                        if( $daterow['leaveduration'] == '0.5' ){
  
                                         
                                          echo ' <span class="text-danger">'.$leaverow['name'].' Halfday</span> ';
            
                                        }else{
            
                                      
                                          echo ' <span class="text-danger">'.$leaverow['name'].'  </span> ';
            
            
            
                                        }
  
                                         
                                          
  
  
                                      }else{
  
                                        echo date('h:i A', strtotime( $daterow['starttime']));
  
                                      }
  
                                   
  

                                      echo '</td>';
                                

                                    }
                                    echo '</tr>';
                              

                                  }
                              

                                  
                                  ?>
                                </tbody>
                            </table>

                            <table class="table table-bordered table-hover table-sm text-center" <?php echo $tzmpacific?>>
                                <thead class="thead thead-dark">
                                  <th>Sunday</th>
                                  <th> Monday </th>
                                  <th> Tuesday </th>
                                  <th>  Wednesday </th>
                                  <th> Thursday </th>
                                  <th> Friday </th>
                                  <th>  Saturday </th>
                                </thead>
                                <tbody>
                                  <?php 
                                    $getnumweeks = "SELECT * FROM schedule WHERE empid='$empid' AND month='$month' AND year='$myear' GROUP BY week";
                                  $getnowkres = mysqli_query($conn, $getnumweeks);
                                  $weekno = array();
                                  while($weeknorow = mysqli_fetch_assoc($getnowkres)){
                                    $weekno[] =  $weeknorow['week'];

                                  }
                                  for($b =0; $b < count($weekno); $b++){
                                    $getdate ="SELECT * FROM schedule WHERE empid='$empid' AND week='$weekno[$b]' AND year='$myear'";
                                    $getdateres = mysqli_query($conn, $getdate);
                                    echo '<tr>';
                                    while($daterow = mysqli_fetch_assoc($getdateres)){
                                     
                                      echo '<td><span class="bg-primary text-white rounded p-1 inline-block">'.date('m/d/Y', strtotime($daterow['date'])).'</span><br>';

                                      $Ymddate = date('Y-m-d', strtotime($daterow['date']));

                                    
                                      $schedatttsql = "SELECT * FROM schedule WHERE empid='$empid' AND date='$Ymddate'";
                                      $schedattres = mysqli_query($conn, $schedatttsql);
                                      $schedattrow = mysqli_fetch_assoc($schedattres);
                                      $idl = $schedattrow['idl'];
                                      $pstST =  $schedattrow['pststarttime'];

                                      $getidl  ="SELECT * FROM schedule WHERE empid='$empid' AND pstdate='$Ymddate'";
                                      $getidlres = mysqli_query($conn, $getidl);
  
                                      $getidlrow = mysqli_fetch_assoc($getidlres);
                                      $idl = $getidlrow['idl'];
  
  
                                      $leavesql ="SELECT * FROM `leave` WHERE  idl='$idl' AND status='3'";
                                      $leaveres = mysqli_query($conn, $leavesql);
                                      $leaverow = mysqli_fetch_assoc($leaveres);
  
                                      
  
  
                                     if($daterow['pststarttime'] == 'OFF'){
  
                                      echo '<span class="text-danger">OFF</span>';
  
  
  
                                   }else if($daterow['pststarttime'] == 'HOLIDAY OFF'){
  
                                    echo '<span class="text-danger">HOLIDAY OFF</span>';
  
  
  
                                  }else if(!empty($leaverow['name'])){
  
                                    if( $leaverow['duration'] == '0.5' ){
  
                                    
                                      echo ' <span class="text-danger">'.$leaverow['name'].' Halfday</span>';
        
                                    }else{
        
                                      echo ' <span class="text-danger">'.$leaverow['name'].'  </span>';
        
        
        
                                    }
  
                                     
                                      
  
  
                                  }else{
  
                                    echo date('h:i A', strtotime( $daterow['pststarttime']));
  
                                  }
  

        

                                    
                                

                                    }
                                    echo '</tr>';
                              

                                  }
                              

                                  
                                  ?>
                                </tbody>
                            </table>
                        </div>
                        <div class="col-md-6 p-3">
                            <table class="table table-bordered table-hover table-sm text-center" <?php echo $tzmmanila?>>
                                <thead class="thead thead-dark">
                                  <th>Sunday</th>
                                  <th> Monday </th>
                                  <th> Tuesday </th>
                                  <th>  Wednesday </th>
                                  <th> Thursday </th>
                                  <th> Friday </th>
                                  <th>  Saturday </th>
                                </thead>
                                <tbody>
                                  <?php 
                                    $getnumweeks = "SELECT * FROM schedule WHERE empid='$empid' AND month='$month' AND year='$myear' GROUP BY week";
                                  $getnowkres = mysqli_query($conn, $getnumweeks);
                                  $weekno = array();
                                  while($weeknorow = mysqli_fetch_assoc($getnowkres)){
                                    $weekno[] =  $weeknorow['week'];

                                  }
                                  for($b =0; $b < count($weekno); $b++){
                                    $getdate ="SELECT * FROM schedule WHERE empid='$empid' AND week='$weekno[$b]' AND year='$myear'";
                                    $getdateres = mysqli_query($conn, $getdate);
                                    echo '<tr>';
                                    while($daterow = mysqli_fetch_assoc($getdateres)){
                                     
                                      echo '<td><span class="bg-primary text-white rounded p-1 inline-block">'.date('m/d/Y', strtotime($daterow['date'])).'</span><br>';

                                      $Ymddate = date('Y-m-d', strtotime($daterow['date']));

                                      $gethrs = "SELECT * FROM schedule WHERE empid='$empid' AND year='$year' AND week='$weeks' AND starttime !='OFF' AND starttime !='HOLIDAY OFF' LIMIT 1";
                                    $gehrsres = mysqli_query($conn, $gethrs);
                                    $gethrsrow = mysqli_fetch_assoc($gehrsres);
                                    $hrs = $gethrsrow['starttime'];

                                    $PSTdate = date('m/d/Y', strtotime(mnltopst($daterow['date'], $hrs)));

                                    $attsql = "SELECT * FROM tbl_login WHERE empid='$empid' AND date='$PSTdate'";
                                    $attres = mysqli_query($conn, $attsql);
                                    $attrow = mysqli_fetch_assoc($attres);

                                    $schedatttsql = "SELECT * FROM schedule WHERE empid='$empid' AND date='$Ymddate'";
                                    $schedattres = mysqli_query($conn, $schedatttsql);
                                    $schedattrow = mysqli_fetch_assoc($schedattres);
                                    $idl = $schedattrow['idl'];
                                

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

                                      if(empty($attrow['timein'])){
                                        $late='';
                                        
                                        $OT = '';
                                        $UT ='';

                                      
                                      if(!empty($leaverow['name'])){

                                        if( $leaverow['duration'] == '0.5' ){

                                         

                                                
                                          $attendance = '<span class="text-danger">ABSENT <br>'.$leaverow['name'].' Half Day </span>';
              
                                          }else{
              
                            


                                            $attendance = '<span class="text-danger">'.$leaverow['name'].'</span>';
              
              
              
                                          }

                        

                                    }else if($schedattrow['starttime'] == 'OFF'){

                                        $attendance ='<span class="text-danger">OFF</span>';

                                    }else if($schedattrow['starttime'] == 'HOLIDAY OFF'){

                                      $attendance = '<span class="text-danger">HOLIDAY OFF</span>';

                                    }else{
                                      $scheddate = new DateTime($Ymddate);
                                      $today = new DateTime(date('Y-m-d'));

                                      if($scheddate > $today){
                                        $attendance = '<span>---</span>';


                                      }else{

                                        $attendance = '<span class="text-danger">ABSENT</span>';

                                      }

                                    }

                                      



                                  }else{

                                    
                                     $login = date('h:i A', strtotime($attrow['timein']));
                                     $logout = date('h:i A', strtotime($attrow['timeout']));
                                     $starttime = $schedattrow['starttime'];
                                     $endtime = $schedattrow['endtime'];


                                     $timein = date('h:i A',strtotime(psttomnl($daterow['date'], $login)));
                                     $timeout = date('h:i A',strtotime(psttomnl($daterow['date'], $logout)));
                                     


                                          
                                      if($schedattrow['starttime'] == 'OFF'){

                                        if(!empty($othrs)){
                                          $attendance = $timein.'<br>'.$ottype.' '.$othrs;


                                        }else{
                                          $attendance =$schedattrow['starttime'].$time;
                                        }

                                      }else if($schedattrow['starttime'] == 'HOLIDAY OFF'){

                                        if(!empty($othrs)){
                                          $attendance = $timein.'<br>'.$ottype.' '.$othrs;


                                        }else{
                                          $attendance  =$schedattrow['starttime'];
                                        }

                                      }else if(!empty($leaverow['name'])){

                                        $attendance = $timein.'</span> <br> <span class="text-danger">'.$leaverow['name'].' Half Day</span><br>';
                                        


                                      }else{
                                        $ST = new DateTime($starttime);
                                        $ET = new DateTime($endtime);
                                        $TIN = new DateTime($timein);
                                        $TOUT = new DateTime($logout);

                                        
                                  

       
                                        

                                        if($TIN > $ST){
                                          $attendance = $timein;
                                          $latehrs = $TIN->diff($ST);
                                          $latehrs = $latehrs->format('%H:%I');
                                          $late = '<span class="text-danger"><br> LATE  '.$latehrs.'</span>';

                                          if($TOUT< $ET){
                                            $UThrs = $ET->diff($TOUT);
                                            $UThrs = $UThrs->format('%H:%I');
                                            $UT = '<span class="text-danger"><br> UNDERTIME  '.$UThrs.'</span>';
                                          }else if(!empty($othrs)){

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

                                          }else{
                                            $OT = '';
                                            $UT ='';

                                            

                                          }


                                        }else{
                                          $attendance = $timein;

                                          if($TOUT < $ET){
                                            $UThrs = $ET->diff($TOUT);
                                            $UThrs = $UThrs->format('%H:%I');
                                            $UT = '<span class="text-danger"><br> UNDERTIME  '.$UThrs.'</span>';
                                          }else if(!empty($othrs)){

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

                                          }else{

                                            $OT = '';
                                            $UT ='';
                                            $late='';


                                            
                                          }



                                        }

                                        


                                      }

                              

                                    
                                }
                                echo $attendance.$late.$OT.$UT;






   

                                      echo '</td>';
                                

                                    }
                                    echo '</tr>';
                              

                                  }
                              

                                  
                                  ?>
                                </tbody>
                            </table>

                            <table class="table table-bordered table-hover table-sm text-center" <?php echo $tzmpacific?>>
                                <thead class="thead thead-dark">
                                  <th>Sunday</th>
                                  <th> Monday </th>
                                  <th> Tuesday </th>
                                  <th>  Wednesday </th>
                                  <th> Thursday </th>
                                  <th> Friday </th>
                                  <th>  Saturday </th>
                                </thead>
                                <tbody>
                                  <?php 
                                    $getnumweeks = "SELECT * FROM schedule WHERE empid='$empid' AND month='$month' AND year='$myear' GROUP BY week";
                                  $getnowkres = mysqli_query($conn, $getnumweeks);
                                  $weekno = array();
                                  while($weeknorow = mysqli_fetch_assoc($getnowkres)){
                                    $weekno[] =  $weeknorow['week'];

                                  }
                                  for($b =0; $b < count($weekno); $b++){
                                    $getdate ="SELECT * FROM schedule WHERE empid='$empid' AND week='$weekno[$b]' AND year='$myear'";
                                    $getdateres = mysqli_query($conn, $getdate);
                                    echo '<tr>';
                                    while($daterow = mysqli_fetch_assoc($getdateres)){

                                      echo '<td><span class="bg-primary text-white rounded p-1 inline-block">'.date('m/d/Y', strtotime($daterow['date'])).'</span><br>';
                                      

                                      $Ymddate = date('Y-m-d', strtotime($daterow['date']));
                                      $mdYdate = date('m/d/Y', strtotime($daterow['date']));

                                      $attsql = "SELECT * FROM tbl_login WHERE empid='$empid' AND date='$mdYdate'";
                                      $attres = mysqli_query($conn, $attsql);
                                      $attrow = mysqli_fetch_assoc($attres);

                                      $schedatttsql = "SELECT * FROM schedule WHERE empid='$empid' AND date='$Ymddate'";
                                      $schedattres = mysqli_query($conn, $schedatttsql);
                                      $schedattrow = mysqli_fetch_assoc($schedattres);
                                      $idl = $schedattrow['idl'];
                                       $pstST =  $schedattrow['pststarttime'];
                                  

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

                                      $datetime= $Ymddate.' '.$pstST;

                                      
                                    $getidl  ="SELECT * FROM schedule WHERE empid='$empid' AND pstdate='$Ymddate'";
                                    $getidlres = mysqli_query($conn, $getidl);

                                    $getidlrow = mysqli_fetch_assoc($getidlres);
                                    $idl = $getidlrow['idl'];


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

                                    

  
  

                                     

                                     

                                      
                                
                                        

                                        if(empty($attrow['timein'])){
                                          $late='';
                                          
                                          $OT = '';
                                          $UT ='';
                                         

                                          

                                          if(!empty($leaverow['name'])){

                                             
                                            if($leaverow['duration'] == '0.5'){
                                              $attendance = '<span class="text-danger">ABSENT <br>'.$leaverow['name'].' Half Day</span>';


                                            }else{

                                              $attendance = '<span class="text-danger">'.$leaverow['name'].'</span>';

                                            }

                                          }else if($schedattrow['pststarttime'] == 'OFF'){

                                              $attendance ='<span class="text-danger">OFF</span>';

                                          }else if($schedattrow['pststarttime'] == 'HOLIDAY OFF'){

                                            $attendance = '<span class="text-danger">HOLIDAY OFF</span>';

                                          }else{
                                            $scheddate = new DateTime($Ymddate);
                                            $today = new DateTime(date('Y-m-d'));

                                            if($scheddate > $today){
                                              $attendance = '<span>---</span>';


                                            }else{

                                              $attendance = '<span class="text-danger">ABSENT</span>';

                                            }

                                          }

                                            



                                        }else{

                                          

                                          
                                           $login = date('h:i A', strtotime($attrow['timein']));
                                           $logout = date('h:i A', strtotime($attrow['timeout']));
                                           $starttime = $schedattrow['pststarttime'];
                                           $endtime = $schedattrow['pstendtime'];

                                           

                                           $timein = date('h:i A',strtotime(psttomnl($daterow['date'], $login)));
                                           $timeout = date('h:i A',strtotime(psttomnl($daterow['date'], $logout)));
                                          // echo $logout.' '.date('h:i A', strtotime($endtime)).'<br>';

                                                
                                            if($schedattrow['pststarttime'] == 'OFF'){

                                              if(!empty($othrs)){
                                                $attendance = $login.'<br>'.$ottype.' '.$othrs;


                                              }else{
                                                $attendance =$schedattrow['pststarttime'];
                                              }

                                            }else if($schedattrow['pststarttime'] == 'HOLIDAY OFF'){

                                              if(!empty($othrs)){
                                                $attendance = $login.'<br>'.$ottype.' '.$othrs;


                                              }else{
                                                $attendance  =$schedattrow['pststarttime'];
                                              }

                                            }else if(!empty($leaverow['name'])){

                                              $attendance = $login.' <br> <span class="text-danger">'.$leaverow['name'].' Half Day</span><br>';
                                              


                                            }else{

                                              $TIN = new DateTime($login);
                                              $TOUT = new DateTime($logout);
                                              $ST = new DateTime($starttime);
                                              $ET = new DateTime($endtime);
                                              $attendance = $login;
                                        

             
                                              

                                              if($TIN > $ST){
                                                $attendance = $login;
                                                $latehrs = $TIN->diff($ST);
                                                $latehrs = $latehrs->format('%H:%I');
                                                $late = '<span class="text-danger"><br> LATE  '.$latehrs.'</span>';

                                                if($TOUT< $ET){
                                                  $UThrs = $ET->diff($TOUT);
                                                  $UThrs = $UThrs->format('%H:%I');
                                                  $UT = '<span class="text-danger"><br> UNDERTIME  '.$UThrs.'</span>';
                                                }else if(!empty($othrs)){

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

                                                }else{
                                                  $OT = '';
                                                  $UT ='';

                                                  

                                                }


                                              }else{

        

                                                if($TOUT< $ET){
                                                  $UThrs = $ET->diff($TOUT);
                                                  $UThrs = $UThrs->format('%H:%I');
                                                  $UT = '<span class="text-danger"><br> UNDERTIME  '.$UThrs.'</span>';
                                                }else if(!empty($othrs)){

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

                                                }else{

                                                  $OT = '';
                                                  $UT ='';
                                                  $late='';


                                                  
                                                }



                                              }

                                              


                                            }

                                    

                                          
                                      }
                                      echo $attendance.$late.$OT.$UT;


                                     

                                    
                                echo '</td>';

                                    }
                                    echo '</tr>';
                              

                                  }
                              

                                  
                                  ?>
                                </tbody>
                            </table>
                        </div>
                    
                        
                    </div>
                    
                </div>

            </div>
   



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

            
            $('a[data-toggle="tab"]').click(function (e) {
            e.preventDefault();
            $(this).tab('show');
            });

            $('a[data-toggle="tab"]').on("shown.bs.tab", function (e) {
                var id = $(e.target).attr("href");
                localStorage.setItem('selectedTab', id)
            });

            var selectedTab = localStorage.getItem('selectedTab');
            if (selectedTab != null) {
                $('a[data-toggle="tab"][href="' + selectedTab + '"]').tab('show');
            }





        </script>
</body>

</html>
