<?php 

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

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

include('uppernav.php');


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

    $reason = phps($_POST['reason']);
    $idl = phps($_POST['id']);
    $ptoid = phps($_POST['ptoid']);

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

  
    $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; 
  if($position == 'Associate Director of Shared Service'){

    $cancel = "UPDATE `leave` SET status = '4.2' WHERE idl='$idl'" ;

  }
  else{

    $cancel = "UPDATE `leave` SET status = '4.3' 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>';
  
  
  }
  else{
    echo mysqli_error($conn);
  }

}

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


  $id = $_POST['id'];
  if($position == 'Associate Director of Shared Service'){
    $acceptsql = "UPDATE `leave` SET `status` = '2' WHERE `idl`='$id'";
  }else{

    $acceptsql = "UPDATE `leave` SET `status` = '3' WHERE `idl`='$id'";


  }

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

    }


}



?>
<!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>Employee | 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">


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






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

        <div class="container-fluid">

        

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

            <li class="breadcrumb-item active">Leave Request</li>
          </ol>
          <?php echo $alert?>
          <h4>Leave Request</h4>
          <br>
          <div class="col-sm-12 col-md-12  col-lg-12">
          <table class="table" id="leave">
          <thead 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>
        
          </thead>
          <tbody>
          <tr>
          <?php 
            if($position == 'Associate Director of Shared Service'){
      
          $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 ('1','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 IN ('2','3')";


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



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


            
            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.1'){
              echo ' <td>Disapproved</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 = 'Disapproved';
              echo ' <td>Disapproved</td> ';
              echo ' <td>Disapproved</td> ';
              echo ' <td>Disapproved</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">Disapprove 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> 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">
                                    <?php echo $rowrequest['firstname'].' '.$rowrequest['surname']?>
                                        <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> 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; ?>">

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



            <?php

          }

         
       

          ?>
          </tr>
          </tbody>
          </table>


          </div>


         


            

          <?php  mysqli_close($conn)?>

     



          <!-- 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 language="javascript" type="text/javascript">
            $(document).ready(function(){
            $("form").on("submit", function(){
                $("#pageloader").fadeIn();
            });//submit

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

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

                responsive: true

              } );
            } );





        </script>
</body>

</html>
