<?php 

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


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

    $week = $_POST['week'];
    $teams = $_POST['team'];
    $tz = $_POST['timezone'];
    
    $year = substr($week, 0, 4);
    $w = substr($week, -2);
    $weeks = ltrim($w, '0');

?>
<div class="tableFixHead">
    <table class="table table-hover text-center table-bordered">
        <thead class="thead-dark">
            <th>Employee ID</th>
            <th>Name</th>
            <?php 
            $header = "SELECT * FROM schedule WHERE week='$weeks' AND year='$year' GROUP BY date";

            $headerres = mysqli_query($conn, $header);
            while($headerrow = mysqli_fetch_assoc($headerres)){
                echo '<th>'.$headerrow['day'].'<br>'.date('m/d/Y', strtotime($headerrow['date'])).'</th>';
            }
            $emplist ="SELECT * FROM empinfo JOIN empdetails ON empinfo.empid = empdetails.detailid WHERE team='$teams' AND empdetails.status='Active'";
            $empres =  mysqli_query($conn, $emplist);

            while($emprow = mysqli_fetch_assoc($empres)){
                $emp = $emprow['detailid'];
            
                echo '<tr>';

                echo '<td>'.$emprow['detailid'].'</td>';
                echo '<td>'.$emprow['firstname'].' '.$emprow['surname'].'</td>';

                $empsched = "SELECT * FROM schedule WHERE empid='$emp' AND week='$weeks' AND year='$year' ORDER BY date";
                $empschedres = mysqli_query($conn, $empsched);
                while($schedrow = mysqli_fetch_assoc($empschedres)){

                    $date = $schedrow['date'];


                    if($tz == 'manila'){
                        $idl = $schedrow['idl'];
                        $leave = "SELECT * FROM `leave` WHERE idl='$idl' AND status ='3'";
                        $leaveres = mysqli_query($conn, $leave);
                        $leaverow = mysqli_fetch_assoc($leaveres);



                        
                    if($schedrow['starttime'] == 'OFF' || $schedrow['starttime'] == 'HOLIDAY OFF'){
                        echo '<td>'.$schedrow['starttime'].'</td>';
                        
                    }else{
                        if(!empty($leaverow['name'])){

                            if($schedrow['leaveduration'] == '0.5'){
                                echo '<td>'.date('h:i A', strtotime($schedrow['starttime'])).' - '.date('h:i A', strtotime($schedrow['endtime'])).'<br> <span class="text-danger">'.$leaverow['name'].' Half Day</span></td>';



                            }else{

                                echo '<td>'.date('h:i A', strtotime($schedrow['starttime'])).' - '.date('h:i A', strtotime($schedrow['endtime'])).'<br> <span class="text-danger">'.$leaverow['name'].'</span></td>';


                            }

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

                        }
                        
                    }



                    }else{

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

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


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

                        if($schedrow['pststarttime'] == 'OFF' || $schedrow['pststarttime'] == 'HOLIDAY OFF'){
                            echo '<td>'.$schedrow['pststarttime'].'</td>';
                            
                        }else{

                            if($schedrow['leaveduration'] == '0.5'){
                                echo '<td>'.date('h:i A', strtotime($schedrow['pststarttime'])).' - '.date('h:i A', strtotime($schedrow['pstendtime'])).'<br> <span class="text-danger">'.$leaverow['name'].' Half Day</span></td>';



                            }else{

                                echo '<td>'.date('h:i A', strtotime($schedrow['pststarttime'])).' - '.date('h:i A', strtotime($schedrow['pstendtime'])).'<br> <span class="text-danger">'.$leaverow['name'].'</span></td>';


                            }

                    
                        }

                        
                    }
            


                }

                echo '</tr>';

            }
            
            ?>
        </thead>
    
    </table>
</div>

<?php
}
if(isset($_POST['MF'])){
    $employee = $_POST['emp'];
    $months = $_POST['month'];
    $myear = substr($months, 0, 4);
    $month1 = substr($months, -2);
     $month =  date("F", strtotime(date("Y") ."-". $month1 ."-01"));

     $tzm = $_POST['tzm'];
?>
<table class="table table-bordered text-center table-hover">
    <thead class="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='$employee' 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='$employee' AND week='$weekno[$b]' AND year='$myear'";
            $getdateres = mysqli_query($conn, $getdate);
            echo '<tr>';
            while($daterow = mysqli_fetch_assoc($getdateres)){

                if($tzm == 'manila'){

                    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'])).' - '.date('h:i A', strtotime( $daterow['endtime']));
      
                    }
      
                 
      
      
                    echo '</td>';
              
      
                  }else{

                    echo '<td><span class="bg-primary text-white rounded p-1 inline-block">'.date('m/d/Y', strtotime($daterow['date'])).'</span><br>';
                    $dates = $daterow['date'];
                    $getidl  ="SELECT * FROM schedule WHERE empid='$employee' AND pstdate='$dates'";
                    $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['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['pststarttime'])).' - '.date('h:i A', strtotime( $daterow['pstendtime']));
      
                    }
      
                 
      
      
                    echo '</td>';
              


                  }
                    
                }
             
             
            echo '</tr>';
      

          }
        ?>
    </tbody>
</table>
<?php 


}
?>


