<?php 

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

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

$sqlname = "SELECT * FROM empinfo WHERE empid='$empid'";
$sqlnameres = mysqli_query($conn, $sqlname);
$namerow = mysqli_fetch_assoc($sqlnameres);
$fullname = $namerow['firstname'].' '.$namerow['surname'];




$leaverequest = "SELECT *, `leave`.status AS 'stat' FROM `leave` JOIN empdetails ON `leave`.empid = empdetails.detailid JOIN empinfo ON empdetails.detailid = empinfo.empid WHERE `leave`.status IN ('2','3','4.1','4.2','4.3') ";
                                 
if(isset($_POST['filter']) || $_GET['filter'] == '1'){


  


    if($_GET['filter'] == '1'){
   
        $leavestat = $_GET['status'];
        $employee = $_GET['employee'];

    }else{

        $leavestat = $_POST['status'];
        $employee = $_POST['employee'];

    }



    if($leavestat  =='All' && $employee  == 'All'){
         $leaverequest = "SELECT *, `leave`.status AS 'stat' FROM `leave` JOIN empdetails ON `leave`.empid = empdetails.detailid JOIN empinfo ON empdetails.detailid = empinfo.empid WHERE `leave`.status IN ('2','3','4.1','4.2','4.3') ";
 

    }else if($leavestat  =='All' && isset($employee)){


        $leaverequest = "SELECT *, `leave`.status AS 'stat' FROM `leave` JOIN empdetails ON `leave`.empid = empdetails.detailid JOIN empinfo ON empdetails.detailid = empinfo.empid WHERE  `empinfo`.empid = '$employee' AND `leave`.status != '5' ";
    
        

    

        


        



    }

    else if(isset($leavestat) &&  $employee  =='All' ){

        if($leavestat == '4.1'){

            $leaverequest = "SELECT *, `leave`.status AS 'stat' FROM `leave` JOIN empdetails ON `leave`.empid = empdetails.detailid JOIN empinfo ON empdetails.detailid = empinfo.empid WHERE  `leave`.status >= '4.1'  AND `leave`.status != '5' ";
    


        } else if($leavestat == '2'){

            $leaverequest = "SELECT *, `leave`.status AS 'stat' FROM `leave` JOIN empdetails ON `leave`.empid = empdetails.detailid JOIN empinfo ON empdetails.detailid = empinfo.empid WHERE  `leave`.status = '$leavestat'  ";
    

        }else if($leavestat == '3'){

            $leaverequest = "SELECT *, `leave`.status AS 'stat' FROM `leave` JOIN empdetails ON `leave`.empid = empdetails.detailid JOIN empinfo ON empdetails.detailid = empinfo.empid WHERE  `leave`.status = '$leavestat'  ";
    

        }






        



    }

    else if(isset($leavestat) &&  isset($employee) ){

 


        
        if($leavestat == '4.1'){

            if($employee == 'Team Leader'){

                $leaverequest = "SELECT *, `leave`.status AS 'stat' FROM `leave` JOIN empdetails ON `leave`.empid = empdetails.detailid JOIN empinfo ON empdetails.detailid = empinfo.empid WHERE  `leave`.status ='4.1'";
                

            }else if($employee == 'HR'){

                $leaverequest = "SELECT *, `leave`.status AS 'stat' FROM `leave` JOIN empdetails ON `leave`.empid = empdetails.detailid JOIN empinfo ON empdetails.detailid = empinfo.empid WHERE  `leave`.status ='4.2'";
                

            }else if($employee == 'Senior Manager'){

                $leaverequest = "SELECT *, `leave`.status AS 'stat' FROM `leave` JOIN empdetails ON `leave`.empid = empdetails.detailid JOIN empinfo ON empdetails.detailid = empinfo.empid WHERE  `leave`.status ='4.3'";

            }

         

            else{

                $leaverequest = "SELECT *, `leave`.status AS 'stat' FROM `leave` JOIN empdetails ON `leave`.empid = empdetails.detailid JOIN empinfo ON empdetails.detailid = empinfo.empid WHERE  `leave`.status >= '4.1' AND `leave`.empid='$employee' AND `leave`.status != '5' ";

            }

            
    


        } else if($leavestat == '2'){

            if($employee == 'Team Leader'){

                $leaverequest = "SELECT *, `leave`.status AS 'stat' FROM `leave` JOIN empdetails ON `leave`.empid = empdetails.detailid JOIN empinfo ON empdetails.detailid = empinfo.empid WHERE  `leave`.status ='0'";
                

            }else if($employee == 'HR'){

                $leaverequest = "SELECT *, `leave`.status AS 'stat' FROM `leave` JOIN empdetails ON `leave`.empid = empdetails.detailid JOIN empinfo ON empdetails.detailid = empinfo.empid WHERE  `leave`.status ='1'";
                

            }else if($employee == 'Senior Manager'){

                $leaverequest = "SELECT *, `leave`.status AS 'stat' FROM `leave` JOIN empdetails ON `leave`.empid = empdetails.detailid JOIN empinfo ON empdetails.detailid = empinfo.empid WHERE  `leave`.status ='2'";

            }else{

            $leaverequest = "SELECT *, `leave`.status AS 'stat' FROM `leave` JOIN empdetails ON `leave`.empid = empdetails.detailid JOIN empinfo ON empdetails.detailid = empinfo.empid WHERE  `leave`.status = '$leavestat'  AND  `leave`.empid='$employee' ";
            }
    

        }else if($leavestat == '3'){

            if($employee == 'Team Leader'){

                $leaverequest = "SELECT *, `leave`.status AS 'stat' FROM `leave` JOIN empdetails ON `leave`.empid = empdetails.detailid JOIN empinfo ON empdetails.detailid = empinfo.empid WHERE  `leave`.status ='1'";
                

            }else if($employee == 'HR'){

                $leaverequest = "SELECT *, `leave`.status AS 'stat' FROM `leave` JOIN empdetails ON `leave`.empid = empdetails.detailid JOIN empinfo ON empdetails.detailid = empinfo.empid WHERE  `leave`.status ='2'";
                

            }else if($employee == 'Senior Manager'){

                $leaverequest = "SELECT *, `leave`.status AS 'stat' FROM `leave` JOIN empdetails ON `leave`.empid = empdetails.detailid JOIN empinfo ON empdetails.detailid = empinfo.empid WHERE  `leave`.status ='3'";

            }else{

            $leaverequest = "SELECT *, `leave`.status AS 'stat' FROM `leave` JOIN empdetails ON `leave`.empid = empdetails.detailid JOIN empinfo ON empdetails.detailid = empinfo.empid WHERE  `leave`.status = '$leavestat' AND `leave`.empid='$employee' ";

            }
    

        }





    }

                            



}


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

    $reason = phps($_POST['reason']);

    $pageno = $_POST['pagenoreject'];
    $leaverequest = $_POST['leavequery'];



    $reason = mysqli_real_escape_string($conn, $reason);





    $idl = $_POST['id'];
    $ptoid = $_POST['ptoid'];
    $employee = $_POST['emp'];
    $status = $_POST['stat'];

    $alert = $_GET['alert'];
    $getpto = "SELECT * FROM pto WHERE id='$ptoid'";
    $ptores = mysqli_query($conn, $getpto);
    $ptorow = mysqli_fetch_assoc($ptores);
    $availablepto = $ptorow['availablepto'];
    $usedpto = $ptorow['usedpto'];
     $cancelduration = $_POST['duration'];
  
    $available = $availablepto + $cancelduration;
    $used = $usedpto - $cancelduration; 
  
  $cancel = "UPDATE `leave` SET status = '4.3', reason='$reason', approver='$empid'  WHERE idl='$idl'";
  $ptoupdate = "UPDATE `pto` SET usedpto='$used', availablepto='$available' WHERE id='$ptoid'";
  
  if(mysqli_query($conn, $cancel) && mysqli_query($conn, $ptoupdate)){
    $alert = '<div class="alert alert-success" role="alert">Leave successfully cancelled</div>';
    header('location: leaverequest.php?alert='.$alert.'&page_no='.$pageno.'&filter=1&status='.$status.'&employee='.$employee);

  
  
  }
  else{
    echo mysqli_error($conn);
  }

}

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


  $id = $_POST['id'];
  $pageno = $_POST['pagenoaccept'];
  $ptoid = $_POST['ptoid'];
  $employee = $_POST['emp'];
  $status = $_POST['stat'];
    $acceptsql = "UPDATE `leave` SET `status` = '3',approver='$empid' WHERE `idl`='$id' ";

    if(mysqli_query($conn, $acceptsql)){
        $alert = '<div class="alert alert-success" role="alert"> Leave successfully approved</div>';

        header('location: leaverequest.php?alert='.$alert.'&page_no='.$pageno.'&filter=1&status='.$status.'&employee='.$employee);


        //header('location: leaverequest.php?alert='.$alert.'&page_no='.$pageno);

    }


}

      






?>
<!DOCTYPE html>
<html lang="en">

<head>

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

    <title>Team Head | PTO request</title>

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

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

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

    <!-- Custom styles for this template-->
    <link href="../css/sb-admin.css" rel="stylesheet">
    <link href="../css/style.css" rel="stylesheet">

    <script>
        function filters(){

            window.history.pushState('object', document.title, location.href.split("?")[0]);
        }
    </script>


</head>

<body id="page-top">


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

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


    <?php  ?>



    <!-- Sidebar -->

    <?php 
      $tools="active";
      $leave = "active";
      include('sidebar.php');

      
       ?>
    <div id="content-wrapper">

        <div class="container-fluid">



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

                <li class="breadcrumb-item active">PTO Request</li>
            </ol>
   
            <h4>PTO Request</h4>
            <?php echo $alert.$_GET['alert']?>
            <div class="col-sm-12 col-md-12  col-lg-12 p-3 form-inline">
                <form method="POST">
                
                    <button class="btn btn-primary" type="submit" name="filter" onclick="filters()"><i class="fa fa-filter"></i> Filter </button>  &nbsp 
                    <select name="status" id="status" class="form-control">
                    <?php 
                    switch($leavestat ){

                        case 'All': 
                        $all = "selected";
                        break;
                        case '2': 
                        $pending = "selected";
                        break;
                        case '3': 
                        $approved = "selected";
                        break;
                        case '4.1':
                        $declined = "selected";
                        break;



                    }
                    ?>
                    <option value="All" <?php echo $all ?>> All </option>
                    <option value="2" <?php echo $pending ?>> Pending </option>
                    <option value="3" <?php echo $approved ?>> Approved </option>
                    <option value="4.1" <?php echo $declined ?>> Declined </option>
        
                    
                    </select> &nbsp
                    <select name="employee" id="employee" class="form-control">
                    <option value="All" <?php $employee == 'All' ? 'selected' : '' ?>> All </option>
                    <option value="Team Leader" <?php $employee == 'Team Leader' ? 'selected' : '' ?>>Team Leader</option>
                    <option value="HR" <?php $employee == 'HR' ? 'selected' : '' ?>>HR </option>
                    <option value="Senior Manager" <?php $employee == 'Senior Manage' ? 'selected' : '' ?>>Senior Manager </option>
            
                    <?php 
                    $statsql ="SELECT * FROM empinfo JOIN empdetails ON empinfo.empid = empdetails.detailid ";
                    $statres = mysqli_query($conn, $statsql);

               

                    while($statrow = mysqli_fetch_assoc($statres)){

                        if($statrow['empid'] == $employee){

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

                        }else{
                            echo'<option value="'.$statrow['empid'].'">'.$statrow['empid'].'   '.$statrow['firstname'].' '.$statrow['surname'].'</option>';

                        }

                       
            

                       


                    }
                    ?>
                    
                    </select>
                   
     
                </form>

            </div>
            <div class="col-sm-12 col-md-12  col-lg-12">
                
                <table class="table" id="leavepto">
                    <thead class="thead-dark">
                        <tr class="thead-dark">
                            <th>Employee ID</th>
                            <th>Employee Name</th>
                            <th>Status</th>
                            <th>Type</th>
                            <th>Date</th>
                            <th>Duration</th>
                            <th>Team Head</th>
                            <th>HR</th>
                            <th>Manager</th>
                            <th>Action</th>
                        
                        </tr>

                    </thead>
                    <tbody>
                        <tr>
                            <?php 

                            
                            
      
                               $leaverequestres = mysqli_query($conn, $leaverequest);


                            if(mysqli_num_rows($leaverequestres) > 0){


                            



                                while($rowrequest = mysqli_fetch_assoc($leaverequestres)){
                                    
                                    $leaveid = $rowrequest['type'];
                                    $id = $rowrequest['idl'];
                                    $ptoid = $rowrequest['ptoid'];
                                    $leavesql = "SELECT * FROM pto WHERE id='$ptoid'";
                                    $leavequery = mysqli_query($conn, $leavesql);
                                    $ptorow1 = mysqli_fetch_assoc($leavequery);

                                    $AP = $ptorow1['availablepto'];
                                


                                    
                                    echo '
                                    
                                    <td>'.$rowrequest['empid'].'</td>
                                    <td>'.$rowrequest['firstname'].' '.$rowrequest['surname'].'</td>
                        

                                    
                                    ';

                                    if($rowrequest['stat'] == '3'){
                                    echo ' <td>Approved</td> ';
                                    }else if($rowrequest['stat'] == '5'){
                                    echo ' <td>Cancelled</td> ';
                                    }else if($rowrequest['stat'] == '4.3' || $rowrequest['stat'] == '4.2' || $rowrequest['stat'] == '4.1'){
                                    echo ' <td>Declined</td> ';
                                    }else{

                                    echo ' <td>Pending Approval</td> ';

                                    }
                                    echo'
                                    <td>'.$rowrequest['name'].'</td>
                                    <td>'.date('m/d/Y', strtotime($rowrequest['datefrom'])).' - '.date('m/d/Y', strtotime($rowrequest['dateto'])).'</td>
                                    <td>'.$rowrequest['duration'].'</td>
                                    
                                    ';

                                    if($rowrequest['stat'] == '0'){
                                    $status = 'Pending Approval';
                                    echo ' <td>Pending</td> ';
                                    echo ' <td>Pending</td> ';
                                    echo ' <td>Pending</td> ';
                                    }else if($rowrequest['stat'] == '1'){
                                    $status = 'Pending Approval';
                                    echo ' <td>Approved
                                    <br>'.$rowrequest['tl'].'
                                    
                                    </td> ';
                                    echo ' <td>Pending</td> ';
                                    echo ' <td>Pending</td> ';
                                    }

                                    
                                    else if($rowrequest['stat'] == '2'){
                                    $status = 'Pending Approval';
                                    
                                    echo ' <td>Approved
                                    <br>'.$rowrequest['tl'].'
                                    </td> ';
                                    echo ' <td>Approved
                                    <br>'.$rowrequest['hr'].'</td>
                                    ';
                                    echo ' <td>Pending</td> ';
                                    
                                    }

                                    
                                else if($rowrequest['stat'] == '3'){
                                    $status = 'Approved';
                                    echo ' <td>Approved
                                      <br>'.$rowrequest['tl'].'
                                    </td> ';
                                    echo ' <td>Approved
                                    <br>'.$rowrequest['hr'].'</td>
                                     ';
                                    echo ' <td>Approved
                                    <br>'.$rowrequest['approver'].'
                                    </td> ';
                                    }

                                    else if($rowrequest['stat'] == '5'){
                                    $status = 'Cancelled';
                                    echo ' <td>Cancelled</td> ';
                                    echo ' <td>Cancelled</td> ';
                                    echo ' <td>Cancelled</td> ';
                                    }
                                    else if($rowrequest['stat'] == '4.1'){
                                    $status = 'Declined';
                                    echo ' <td>Declined <br>'.$rowrequest['tl'].'
                                    </td> ';
                                    echo ' <td>Declined</td> ';
                                    echo ' <td>Declined</td> ';
                                    }

                                    else if($rowrequest['stat'] == '4.2'){
                                        $status = 'Declined';
                             
                                        echo ' <td>Approved <br>'.$rowrequest['tl'].'';
                                        echo ' <td>Declined  <br>'.$rowrequest['hr'].'
                                        </td> ';
                                        echo ' <td>Declined</td> ';
                                    }

                                    else if($rowrequest['stat'] == '4.3'){
                                        $status = 'Declined';
                                
                                        echo ' <td>Approved <br>'.$rowrequest['tl'].'';
                                        echo ' <td>Approved  <br>'.$rowrequest['hr'].'
                                        </td> ';
                                        echo ' <td>Declined <br>'.$rowrequest['approver'].'
                                        </td> ';
                                    
                                    }

                                    


                                    
                                
                                    ?>
                            <td>
                                <a href="#view<?php echo $id;?>" data-toggle="modal">
                                    <button type='button' class='btn btn-primary btn-sm'> <i class="fa fa-eye"></i>
                                    </button>
                                </a>

                                <small>|</small>


                                <?php 

                                    $sdate = strtotime($rowrequest['datefrom']);
                                    $tdate = strtotime(date('Y-m-d'));



                                    if($rowrequest['stat'] == '4.3' || $rowrequest['stat'] == '4.1' || $rowrequest['stat'] == '4.2' || $rowrequest['stat'] == '3'  ) {

                                        echo'
                                        <button type="button" class="btn btn-success btn-sm" disabled> <i class="fa fa-check"></i>   </button> 
                                        ';

                                    }else{

                                        
                                        echo'
                                        <a href="#accept'.$id.'" data-toggle="modal">
                                        <button type="button" class="btn btn-success btn-sm"> <i class="fa fa-check"></i>   </button> 
                                        </a>
                                        ';


                                    }



                                    

                        /*             if(($sdate > $tdate) && ( $rowrequest['stat'] == '1' || $rowrequest['stat'] == '2' || $rowrequest['stat'] == '3')){


                                    echo'
                                    <button type="button" class="btn btn-success btn-sm" disabled> <i class="fa fa-check"></i>   </button> 
                                    ';
                                    }else if(($sdate < $tdate) && ( $rowrequest['stat'] == '1' || $rowrequest['stat'] == '2' || $rowrequest['stat'] == '3')){

                                    echo'
                                    <button type="button" class="btn btn-success btn-sm" disabled> <i class="fa fa-check"></i>   </button> 
                                    ';

                                    }

                                    else{
                                    echo'
                                    <a href="#accept'.$id.'" data-toggle="modal">
                                    <button type="button" class="btn btn-success btn-sm" > <i class="fa fa-check"></i>   </button> 
                                    </a>

                                    ';


                                    } */

                                    ?>



                                <small>| </small>


                                <?php 

                                $sdate = strtotime($rowrequest['datefrom']);
                                $tdate = strtotime(date('Y-m-d'));

                                if($rowrequest['stat'] == '4.1' || $rowrequest['stat'] == '4.2' || $rowrequest['stat'] == '4.3' ){




                                echo'
                                <button type="button" class="btn btn-danger btn-sm" disabled> <i class="fa fa-close"></i>   </button> 
                                ';
                            }

                                else{

                                echo'<a href="#reject'.$id.'" data-toggle="modal">
                                <button type="button" class="btn btn-danger btn-sm" > <i class="fa fa-close"></i>   </button> 
                                </a>';
                                


                                }

                                ?>
                                <small>| </small>

                                <button type="button" class="btn btn-warning btn-sm text-white"
                                    onclick="printContent('leaveform<?php echo $id?>')"> <i class="fas fa-print"></i>
                                </button>









                            </td>
                        </tr>

                        <div id="reject<?php echo $id; ?>" class="modal fade" role="dialog">
                            <form method="post" class="form-horizontal" role="form">
                                <div class="modal-dialog modal-md modal-dialog-centered">
                                    <!-- Modal content-->
                                    <div class="modal-content">
                                        <div class="modal-header">
                                            <h4 class="modal-title">Decline Leave</h4>
                                            <button type="button" class="close" data-dismiss="modal">&times;</button>

                                        </div>
                                        <div class="modal-body">
                                            <input type="hidden" id="pagenoreject" name="pagenoreject" >
                                            <input type="hidden" id="query" name="leavequery" value="<?php echo $leaverequest?>" >

                                            <input type="hidden" id="stat" name="stat" value="<?php echo $leavestat?>" >
                                            <input type="hidden" id="emp" name="emp" value="<?php echo $employee?>" >

                                            <input type="hidden" name="id" value="<?php echo $id; ?>">
                                            <div class="row">
                                                <div class="col-6">
                                                    <label> Employee ID </label>
                                                    <input type="text" name="empid" class="form-control"
                                                        value="<?php echo $rowrequest['empid']; ?>" readonly>
                                                </div>
                                                <div class="col-6">
                                                    <label> Name </label>
                                                    <input type="text" name="empid" class="form-control"
                                                        value="<?php echo $rowrequest['firstname'].' '.$rowrequest['surname']?> " readonly>
                                                </div>
                                                <div class="col-6">
                                                    <label> Date Filed </label>
                                                    <input type="text" name="empid" class="form-control"
                                                        value="<?php echo date('m/d/Y',strtotime($rowrequest['datefiled'])) ?> " readonly>
                                                </div>
                                                <div class="col-6">
                                                    <label> Leave Type </label>
                                                    <input type="text" name="type" class="form-control"
                                                        value="<?php echo $rowrequest['name']; ?>" readonly>
                                                </div>

                                                <div class="col-6">
                                                    <label>Date </label>
                                                    <input type="text" name="daterange" class="form-control"
                                                        value="<?php echo date('m/d/Y', strtotime($rowrequest['datefrom'])).' - '.date('m/d/Y', strtotime($rowrequest['dateto'])) ?>"
                                                        readonly>
                                                </div>
                                                <div class="col-6">
                                                    <label> Duration </label>
                                                    <input type="text" name="duration" class="form-control"
                                                        value="<?php echo $rowrequest['duration']; ?>" readonly>
                                                </div>

                                                <div class="col-12">
                                                    <label> Reason </label>
                                                    <textarea name="reason" id="" cols="20" rows="8"
                                                        placeholder="Reason" class="form-control" required></textarea>
                                                    <input type="hidden" name="ptoid" value="<?php echo $ptoid; ?>">


                                                </div>

                                            </div>
                                        </div>

                                        <div class="modal-footer">
                                            <button type="submit" class="btn btn-success" name="disapprove"><i
                                                    class="fas fa-check"> </i> Yes</button>
                                            <button type="button" class="btn btn-danger" data-dismiss="modal"><i
                                                    class="fas fa-close"> </i> Cancel</button>
                                        </div>
                                    </div>
                                </div>

                                
                            </form>
                        </div>


                        <div id="view<?php echo $id; ?>" class="modal fade" role="dialog">
                            <form method="post" class="form-horizontal" role="form">
                                <div class="modal-dialog modal-md modal-dialog-centered">
                                    <!-- Modal content-->
                                    <div class="modal-content">
                                        <div class="modal-header">
                                            <h4 class="modal-title"> View Leave</h4>
                                            <button type="button" class="close" data-dismiss="modal">&times;</button>

                                        </div>

                                        <div class="modal-body">
                                     
                                            <input type="hidden" name="id" value="<?php echo $id; ?>">
                                            <div class="row">

                                                <div class="col-6">
                                                    <label> Employee ID </label>
                                                    <input type="text" name="empid" class="form-control"
                                                        value="<?php echo $rowrequest['empid']; ?>" readonly>
                                                </div>
                                                <div class="col-6">
                                                    <label> Name </label>
                                                    <input type="text" name="empid" class="form-control"
                                                        value="<?php echo $rowrequest['firstname'].' '.$rowrequest['surname']?> " readonly>
                                                </div>
                                                <div class="col-6">
                                                    <label> Date Filed </label>
                                                    <input type="text" name="empid" class="form-control"
                                                        value="<?php echo date('m/d/Y',strtotime($rowrequest['datefiled'])) ?> " readonly>
                                                </div>
                                                <div class="col-6">
                                                    <label> Leave Type </label>
                                                    <input type="text" name="type" class="form-control"
                                                        value="<?php echo $rowrequest['name']; ?>" readonly>
                                                </div>

                                                <div class="col-6">
                                                    <label>Date </label>
                                                    <input type="text" name="daterange" class="form-control"
                                                        value="<?php echo date('m/d/Y', strtotime($rowrequest['datefrom'])).' - '.date('m/d/Y', strtotime($rowrequest['dateto'])) ?>"
                                                        readonly>
                                                </div>
                                                <div class="col-6">
                                                    <label> Duration </label>
                                                    <input type="text" name="duration" class="form-control"
                                                        value="<?php echo $rowrequest['duration']; ?>" readonly>
                                                </div>

                                                <div class="col-12">
                                                    <label> Notes </label>
                                                    <textarea name="reason" id="" cols="20" rows="8"
                                                        placeholder="Reason" class="form-control" required
                                                        readonly><?php echo $rowrequest['description']; ?></textarea>

                                                </div>

                                            </div>
                                        </div>

                                        <div class="modal-footer">

                                            <button type="button" class="btn btn-danger" data-dismiss="modal"><i
                                                    class="fas fa-close"> </i> Cancel</button>
                                        </div>
                                    </div>
                                </div>
                                
                            </form>
                            

                        </div>



                        <div id="accept<?php echo $id; ?>" class="modal fade" role="dialog">
                            <form method="post" class="form-horizontal" role="form">
                                <div class="modal-dialog modal-md modal-dialog-centered">
                                    <!-- Modal content-->
                                    <div class="modal-content">
                                        <div class="modal-header">
                                            <h4 class="modal-title">Approve Leave</h4>
                                            <button type="button" class="close" data-dismiss="modal">&times;</button>

                                        </div>
                                        <div class="modal-body">
                                            <input type="hidden" name="id" value="<?php echo $id; ?>">
                                            <input type="hidden" id="pagenoaccept" name="pagenoaccept" >
                                            <input type="hidden" id="query" name="leavequery" value="<?php echo $leaverequest?>" >

                                            <input type="hidden" id="stat" name="stat" value="<?php echo $leavestat?>" >
                                            <input type="hidden" id="emp" name="emp" value="<?php echo $employee?>" >


                                            Are you sure you want to approve <?php echo $rowrequest['empid']?> leave
                                            request?


                                        </div>
                                     

                                        <div class="modal-footer">
                                            <button type="submit" class="btn btn-success" name="approve"><i
                                                    class="fas fa-check"> </i> Yes</button>
                                            <button type="button" class="btn btn-danger" data-dismiss="modal"><i
                                                    class="fas fa-close"> </i> Cancel</button>
                                        </div>
                                    </div>
                                </div>
                                <input type="text" id="pagenoaccept" name="pagenoaccpet" >


                            </form>
                        </div>


                        <div id="leaveform<?php echo $id; ?>" class="leaveform" hidden>
                            <div class="container contain">
                                <div class="row">
                                    <div class="col-lg-12">
                                        <div class="align-middle">
                                            <img src="../images/logo.jpg" class="float-left p-3" alt="" height="150px"
                                                width="200px">
                                            <br>

                                            <h1 style="font-size: 60px">  ATV International ROHQ</h1>
                                            <br>
                                            <hr style="border: 2px solid black">

                                        </div>

                                        <h3 class="text-center">LEAVE FORM</h3>
                                  

                                    </div>
                                    
                                    <div class="col-4 p-3">
                                        <b>NAME: <?php echo $rowrequest['firstname'].' '.$rowrequest['surname'] ?> </b>
                                    </div>
                                    <div class="col-3 p-3">
                                        <b>EMPLOYEE ID: <?php echo $rowrequest['empid'] ?> </b>
                                    </div>
                                    <div class="col-5 p-3">
                                        <b>TEAM / DEPARTMENT: <?php echo $rowrequest['team'] ?> </b>
                                    </div>

                                    <div class="col-6">
                                        <div class="row p-1">
                                     
                    
                               

                                            <div class="col-6 p-1">
                                            Emergency Leave
                                            </div>

                                            <?php 
                                            if($rowrequest['name'] == 'Emergency Leave'){

                                                echo '



                                                <div class="col-2 p-1 border border-dark text-center">
                                                <i class="fa fa-check"> </i>
                                          
                                                </div>
                                                
                                                ';

                                            }else{
                                                echo'

                                                <div class="col-2 p-1 border border-dark">
                                               
                                                </div>
                                                
                                                ';


                                            }
                                            ?>



                                            <div class="col-6 p-1 ">
                                            Sick Leave
                                            </div>

                                            <?php 
                                            if($rowrequest['name'] == 'Sick Leave'){

                                                echo '

       
                                                <div class="col-2 p-1 border border-dark text-center">
                                                <i class="fa fa-check"> </i>
                                          
                                                </div>
                                                
                                                ';

                                            }else{
                                                echo'
                      
                                                <div class="col-2 p-1 border border-dark">
                                               
                                                </div>
                                                
                                                ';


                                            }
                                            ?>

                                            <div class="col-6 p-1">
                                            Vacation Leave
                                            </div>
                                            
                                            <?php 
                                            if($rowrequest['name'] == 'Vacation Leave'){

                                                echo '

                       
                                                <div class="col-2 p-1 border border-dark text-center">
                                                <i class="fa fa-check"> </i>
                                          
                                                </div>
                                                
                                                ';

                                            }else{
                                                echo'
                               
                                                <div class="col-2 p-1 border border-dark">
                                               
                                                </div>
                                                
                                                ';


                                            }
                                            ?>


                                            <div class="col-6 p-1">
                                            LWOP - Unpaid Leave
                                            </div>
                                            
                                            <?php 
                                            if($rowrequest['name'] == 'LWOP - Unpaid Leave'){

                                                echo '

     
                                                <div class="col-2 p-1 border border-dark text-center">
                                                <i class="fa fa-check"> </i>
                                          
                                                </div>
                                                
                                                ';

                                            }else{
                                                echo'
        
                                                <div class="col-2 p-1 border border-dark">
                                               
                                                </div>
                                                
                                                ';


                                            }
                                            ?>

                                            <?php
                                                   $leave1 = "SELECT * FROM pto WHERE name IN('Magna Carta','Bereavement') GROUP BY name ";
                                                    $leaveres1 = mysqli_query($conn, $leave1);
                                                    while($leaverow1 = mysqli_fetch_assoc($leaveres1)){

                                                        echo'<div class="col-6 p-1">'.$leaverow1['name'].' Leave </div>';
                                                        
                                       
                                                    if($leaverow1['name'] == $rowrequest['type']){

                                                        echo '

                               
                                                        <div class="col-2 p-1 border border-dark text-center">
                                                        <i class="fa fa-check"> </i>
                                                
                                                        </div>
                                                        
                                                        ';

                                                    }else{
                                                        echo'
                      
                                                        <div class="col-2 p-1 border border-dark">
                                                    
                                                        </div>
                                                        
                                                        ';


                                                    }
                                                

                                                    }
                                            
                                            ?>




                                        </div>
 
                                        <br>
                                    </div>
                                    <div class="col-6">
                                        <div class="row p-1">
    

                                            <?php
                                                    $leave2 = "SELECT * FROM pto WHERE name != 'Paid Time Off' AND name !='Magna Carta' AND name != 'Bereavement' GROUP BY name ";
                                                    $leaveres2 = mysqli_query($conn, $leave2);
                                                    while($leaverow2 = mysqli_fetch_assoc($leaveres2)){
                                                        echo '<div class="col-6 p-1">'.$leaverow2['name'].' Leave </div>';

                                                        if($leaverow2['name'] == $rowrequest['type']){

                                                            echo '
    
                                           
                                                            <div class="col-2 p-1 border border-dark text-center">
                                                            <i class="fa fa-check"> </i>
                                                    
                                                            </div>
                                                            
                                                            ';
    
                                                        }else{
                                                            echo'
                                    
                                                            <div class="col-2 p-1 border border-dark">
                                                        
                                                            </div>
                                                            
                                                            ';
    
    
                                                        }
                                                    
    

                                                     

                                                     
                                                    }
                                            
                                            ?>
                                            <div class="col-12 p-1 ">
                                            Note/Comment/Reason: <u><?php echo $rowrequest['description']?></u>
                                      
                                            
                                                       
                                            </div>


                                        </div>
                                    </div>
 



                    
                                    <div class="col-12 border border-secondary">
                                        <div class="row">
                                            <div class="col-6">
                                                LEAVE DATE: <?php echo date('m/d/Y', strtotime($rowrequest['datefrom'])).'-'.date('m/d/Y', strtotime($rowrequest['dateto']))?>

                                            </div>
                                            <div class="col-6">

                                                # of Approved Leave <?php echo $rowrequest['duration']?>

                                            </div>
                                        </div>



                                    </div>

                                    <div class="col-12 border-right border-left border-bottom border-secondary">
                                        <div class="row">
                                            <div class="col-6 p-2">
                                            <br>
                                                EMPLOYEE'S SIGNATURE OVER PRINTED NAME <br>
                                                DATE FILED

                                            </div>
                                            <div class="col-6 p-2">
                                                   
                                                :  <img src="../files/signature/<?php echo $rowrequest['signature']?>" alt='Signature' style="height: 30px; width: 100px; border: 0;"> <br>
                                                :  <?php echo '  '.$rowrequest['firstname'].' '.$rowrequest['surname']?><br>
                                                :  <?php echo '  '.date('m/d/Y', strtotime($rowrequest['datefiled']))?>

                                            </div>
                                        </div>



                                    </div>
                                    <div class="col-12 border-right border-left border-bottom border-secondary">
                                        <div class="row">
                                            <div class="col-6 p-2">
                                            <?php 
                                            $tl = $rowrequest['tl'];
                                                $tlsql ="SELECT * FROM empinfo WHERE empid='$tl'";
                                                $tlres = mysqli_query($conn, $tlsql);
                                                $tlrow = mysqli_fetch_assoc($tlres);

                                                $hr = $rowrequest['hr'];
                                                $hrsql ="SELECT * FROM empinfo WHERE empid='$hr'";
                                                $hrres = mysqli_query($conn, $hrsql);
                                                $hrrow = mysqli_fetch_assoc($hrres);

                                                $mng = $rowrequest['approver'];
                                                $mngsql ="SELECT * FROM empinfo WHERE empid='$mng'";
                                                $mngres = mysqli_query($conn, $mngsql);
                                                $mngrow = mysqli_fetch_assoc($mngres);
                                            ?>
                                                NOTED BY <br>
                                                Approver 1<br>
                                                DATE FILED


                                            </div>
                                            <div class="col-6 p-2">

                                                : <img src="../files/signature/<?php  if(empty($tlrow['signature'])) echo 'blank.png';  else echo $tlrow['signature']; ?>" alt='Signature' style="height: 30px; width: 100px; border: 0;"> <br>
                                                : <?php echo $tlrow['firstname'].' '.$tlrow['surname']?> <br>
                                                : <?php echo '  '.date('m/d/Y', strtotime($rowrequest['datefiled']))?>

                                            </div>
                                        </div>



                                    </div>
                                    <div class="col-12 border-right border-left border-bottom border-secondary">
                                        <div class="row">
                                            <div class="col-6 p-2">
                                                NOTED BY <br>
                                                Approver 2 <br>
                                                DATE FILED

                                            </div>
                                            <div class="col-6 p-2">
                                                
                                                : <img src="../files/signature/<?php  if(empty($tlrow['signature'])) echo 'blank.png';  else echo $hrrow['signature']; ?>" alt='Signature' style="height: 30px; width: 100px; border: 0;"> <br>
                                                : <?php echo $hrrow['firstname'].' '.$hrrow['surname']?> <br>
                                                : <?php echo '  '.date('m/d/Y', strtotime($rowrequest['datefiled']))?>

                                            </div>
                                        </div>



                                    </div>

                                    <div class="col-12 border-right border-left border-bottom border-secondary">
                                        <div class="row">
                                            <div class="col-6 p-2">

                                                NOTED BY <br>
                                                Approver 3<br>
                                                DATE FILED

                                            </div>
                                            <div class="col-6 p-2">
                                                

                                                : <img src="../files/signature/<?php  if(empty($tlrow['signature'])) echo 'blank.png';  else echo $mngrow['signature']; ?>" alt='Signature' style="height: 30px; width: 100px; border: 0;"> <br>
                                                : <?php echo $mngrow['firstname'].' '.$mngrow['surname']?> <br>
                                                : <?php echo '  '.date('m/d/Y', strtotime($rowrequest['datefiled']))?>

                                            </div>
                                        </div>



                                    </div>


                                    <div class="col-12 p-4">

                                        <?php 
                                        if($rowrequest['stat'] == '3'){
                                            $approve = '<i class="fa fa-check"></i>';
                                            $disapprove ='';
                                        }else if($rowrequest['stat'] == '0' || $rowrequest['stat'] == '1' || $rowrequest['stat'] == '2'){
                                            $approve = '';
                                            $disapprove ='';

                                        }
                                        
                                        else{
                                            $disapprove ='<i class="fa fa-check"></i>';
                                            $approve = '';
                                        }
                                        ?>
                                        <p class="text-center">( FOR HUMAN RESOURCES ONLY )</p>
                                    
                                        (<?php echo $approve?> ) Approved (<?php echo $disapprove?> ) Disapproved (If Approval is NOT
                                        RECOMMENDED, attach
                                        explanation) <br> <br>

                                        <div class="border border-round border-dark p-2" style="height: 100px">

                                        <?php echo $rowrequest['reason'] ?>

                                        </div>
                                        
                                    </div>
                                


                                    <div class="col-6">
                                
                                        _____________________________________<br>

                                        Human Resources Department/Date
                                       
                                        <br>
                                        <br>

                                        Leave File Number <u><?php echo $rowrequest['idl']?></u>


                                    </div>
                                    <div class="col-6 text-right">
                                   
                                        <u><?php echo '&nbsp &nbsp &nbsp
                                        &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp
                                        ' .$AP.'&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp'; ?></u><br>
                                        REMAINING PTO CREDITS

                                        <br>
                                        <br>
                                        Date Hire    <u><?php echo date('m/d/Y', strtotime($rowrequest['hireddate']))?></u>
                                    </div>

                                    <div class="col-12 p-1 text-center">
                                        <hr style="border: 2px solid black">
                                        <b>ATV International ROHQ </b> <br>
                                        <small>3rd floor Yupangco Bldg. 1606 Investment Dr cor Trade Ave., <br>
                                            Madrigal Business Park Ayala Alabang Muntinlupa City <br>
                                            Tel No. 632.551.55.88
                                        </small>

                                    </div>

                                </div>
                            </div>
                        </div>








                        


                 



                        <?php

                            }
                        }else{

                            echo'<td> No record found</td>';
                        }

                            
                        

                            ?>
                        </tr>
                    </tbody>
                </table>
                <script>
                // get the table element
                var $table = document.getElementById("leavepto"),
                // number of rows per page
                $n = 30,
                // number of rows of the table
                $rowCount = $table.rows.length,
                // get the first cell's tag name (in the first row)
                $firstRow = $table.rows[0].firstElementChild.tagName,
                // boolean var to check if table has a head row
                $hasHead = ($firstRow === "TH"),
                // an array to hold each row
                $tr = [],
                // loop counters, to start count from rows[1] (2nd row) if the first row has a head tag
                $i,$ii,$j = ($hasHead)?1:0,
                // holds the first row if it has a (<TH>) & nothing if (<TD>)
                $th = ($hasHead?$table.rows[(0)].outerHTML:"");
                // count the number of pages
                var $pageCount = Math.ceil($rowCount / $n);
                // if we had one page only, then we have nothing to do ..
                if ($pageCount > 1) {
                  // assign each row outHTML (tag name & innerHTML) to the array
                  for ($i = $j,$ii = 0; $i < $rowCount; $i++, $ii++)
                    $tr[$ii] = $table.rows[$i].outerHTML;
                  // create a div block to hold the buttons
                  $table.insertAdjacentHTML("afterend","<div id='buttons'></div");
                  // the first sort, default page is the first one
                  sort(<?php 
                  if(empty($_GET['page_no'])){

                    echo '1';
                  } else{

                    echo $_GET['page_no'];
                  }
                  
                  
                  ?>);
                }

                // ($p) is the selected page number. it will be generated when a user clicks a button
                function sort($p) {
                  /* create ($rows) a variable to hold the group of rows
                  ** to be displayed on the selected page,
                  ** ($s) the start point .. the first row in each page, Do The Math
                  */
                  var $rows = $th,$s = (($n * $p)-$n);
                  for ($i = $s; $i < ($s+$n) && $i < $tr.length; $i++)
                    $rows += $tr[$i];
                  
                  // now the table has a processed group of rows ..
                  $table.innerHTML = $rows;
                  // create the pagination buttons
                  document.getElementById("buttons").innerHTML = pageButtons($pageCount,$p);
                  // CSS Stuff
                  document.getElementById("id"+$p).setAttribute("class","btn btn-primary btn-sm active");

                 var acc = document.getElementsByName('pagenoaccept');
                 
                 var rj = document.getElementsByName('pagenoreject');
                


       
                for (var a = 0; a < acc.length; a++) {
                   
                    acc[a].value = $p;
                
                }

                for (var b = 0; b < rj.length; b++) {

                    rj[b].value = $p;
                }



                 


                 
                }


                // ($pCount) : number of pages,($cur) : current page, the selected one ..
                function pageButtons($pCount,$cur) {
                  /* this variables will disable the "Prev" button on 1st page
                    and "next" button on the last one */
                  var	$prevDis = ($cur == 1)?"disabled":"",
                    $nextDis = ($cur == $pCount)?"disabled":"",
                    /* this ($buttons) will hold every single button needed
                    ** it will creates each button and sets the onclick attribute
                    ** to the "sort" function with a special ($p) number..
                    */
                    $buttons = "<input type='button' class='btn btn-default btn-sm text-primary' value='« Prev' onclick='sort("+($cur - 1)+")' "+$prevDis+"> &nbsp";

                  for ($i=1; $i<=$pCount;$i++)
                  
                    $buttons += "<input type='button' class='btn btn-default btn-sm text-primary pageno' name='pageno' id='id"+$i+"'value='"+$i+"' onclick='sort("+$i+")'> &nbsp";
                  $buttons += "<input type='button' class='btn btn-default btn-sm text-primary' value='Next »' onclick='sort("+($cur + 1)+")' "+$nextDis+"> &nbsp";
             
                  return $buttons;
                
                }


                </script>
            

            </div>

          
















            <!-- Icon Cards-->

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

            <!-- Sticky Footer -->


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

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

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

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

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

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

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

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

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

    <script>
    function printContent(el) {

        var css = '@page { size: auto }  @media print {.contain { zoom: 100% } }',
            head = document.head || document.getElementsByTagName('head')[0],
            style = document.createElement('style');

        style.type = 'text/css';
        style.media = 'print';

        if (style.styleSheet) {
            style.styleSheet.cssText = css;
        } else {
            style.appendChild(document.createTextNode(css));
        }

        head.appendChild(style);



        var restorepage = document.body.innerHTML;
        var printcontent = document.getElementById(el).innerHTML;
        document.body.innerHTML = printcontent;
        window.print();
        document.body.innerHTML = restorepage;
    }
    </script>

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

        $('#preloader').fadeOut();
    }); //document ready
    </script>
</body>

</html>