<?php 

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

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


if(isset($_POST['cancelleave'])){
  $idl = $_POST['id'];
  $ptoid = $_POST['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['cancelduration'];

  $available = $availablepto + $cancelduration;
  $used = $usedpto - $cancelduration; 

$cancel = "UPDATE `leave` SET status = '5' WHERE idl='$idl'" ;
$ptoupdate = "UPDATE `pto` SET usedpto='$used', availablepto='$available' WHERE id='$ptoid'";

if(mysqli_query($conn, $cancel) && mysqli_query($conn, $ptoupdate)){
header('location: leave.php');

}
else{
  echo mysqli_error($conn);
}
}

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


    <?php include('uppernav.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">Leave</li>
          </ol>
          
          <div class="col-sm-12 col-md-12 col-lg-12">

              <h3>Leave</h3>
              
              <div class="col-sm-12 col-md-12 col-lg-12">
              <?php echo $alert.$_GET['alert']?>
                <span class="pull-left"><a href="addleave.php" class="btn btn-success"> <i class="fas fa-plus">  </i> Add PTO </a></span>
               
              </div>
              <br>
              <br>

              <div class="col-sm-12 col-md-12 col-lg-12 table-responsive">


                

                <table class="table" id='leavetbl'>
                <thead class="thead-dark">
                <tr>
                  <th colspan="8">
                    <?php 
                    $ptoheading = "SELECT * FROM pto WHERE empid='$empid'";
                    $ptoheadingres =mysqli_query($conn, $ptoheading);

                    while($ptoheadingrow = mysqli_fetch_assoc($ptoheadingres)){
                      
                     echo  $ptoheadingrow['name'].' : '.$ptoheadingrow['availablepto'].'&nbsp &nbsp &nbsp  &nbsp &nbsp &nbsp ';


                    }
                    ?>
                  </th>
                </tr>
                <tr class="thead-dark">
                  <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>
                
                <?php 
                $leavesql = "SELECT * FROM `leave` WHERE empid='$empid' ORDER BY id DESC";
                $leaveres =mysqli_query($conn, $leavesql);

                while($leaverow = mysqli_fetch_assoc($leaveres)){
                  $id = $leaverow['idl'];
                  if($leaverow['status'] == '3'){
                    echo ' <tr><td>Approved</td> ';
                  }else if($leaverow['status'] == '5'){
                    echo ' <tr><td>Cancelled</td> ';
                  }else if($leaverow['status'] == '4.1' || $leaverow['status'] == '4.2' || $leaverow['status'] == '4.3'){
                    echo ' <tr><td>Declined</td> ';
                  }else{

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

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

                  if($leaverow['status'] == '0'){
                    $status = 'Pending Approval';
                    echo ' <td>Pending</td> ';
                    echo ' <td>Pending</td> ';
                    echo ' <td>Pending</td> ';
                  }else if($leaverow['status'] == '1'){
                    $status = 'Pending Approval';
                    echo ' <td>Approved</td> ';
                    echo ' <td>Pending</td> ';
                    echo ' <td>Pending</td> ';
                  }

                  
                  else if($leaverow['status'] == '2'){
                    $status = 'Pending Approval';
                    
                    echo ' <td>Approved</td> ';
                    echo ' <td>Approved</td> ';
                    echo ' <td>Pending</td> ';
                    
                  }

                  
                else if($leaverow['status'] == '3'){
                    $status = 'Approved';
                    echo ' <td>Approved</td> ';
                    echo ' <td>Approved</td> ';
                    echo ' <td>Approved</td> ';
                  }

                  else if($leaverow['status'] == '5'){
                    $status = 'Cancelled';
                    echo ' <td>Cancelled</td> ';
                    echo ' <td>Cancelled</td> ';
                    echo ' <td>Cancelled</td> ';
                  }
                  else if($leaverow['status'] == '4.1'){
                    $status = 'Team Head Declined';
                    echo ' <td>Declined</td> ';
                    echo ' <td>Declined</td> ';
                    echo ' <td>Declined</td> ';
                  }
                  else if($leaverow['status'] == '4.2'){
                    $status = 'HR Declined';
                    echo ' <td>Declined</td> ';
                    echo ' <td>Declined</td> ';
                    echo ' <td>Declined</td> ';
                  }
                  else if($leaverow['status'] == '4.3'){
                    $status = 'Manager Declined';
                    echo ' <td>Declined</td> ';
                    echo ' <td>Declined</td> ';
                    echo ' <td>Declined</td> ';
                  }


              

                
                ?>
                  <td>
                  <a href="#viewpending<?php echo $id;?>" data-toggle="modal">
                        <button type="button" class="btn btn-primary btn-sm"> <i class="fa fa-eye"></i>  View </button> 
                  </a>
                  
                  <?php 
                  $sdate = date('Y-m-d', strtotime($leaverow['datefrom']));
                  $sdate = new DateTime($sdate);
                  $tdate = date('Y-m-d');
                  $tdate = new DateTime($tdate);
				  
				  if(($tdate >= $sdate) && ($leaverow['status'] == '4.3' || $leaverow['status'] == '4.2' || $leaverow['status'] == '4.1' || $leaverow['status'] == '2' || $leaverow['status'] == '1' || $leaverow['status'] == '5')){

					echo'
					<button type="button" class="btn btn-danger btn-sm" disabled> <i class="fa fa-close"></i> Cancel </button> 
					';
					}elseif($leaverow['status'] == '3'){
					    
					}else if(($tdate < $sdate) && ($leaverow['status'] == '4.3' || $leaverow['status'] == '4.2' || $leaverow['status'] == '4.1' || $leaverow['status'] == '5' )){
					echo'
					<button type="button" class="btn btn-danger btn-sm" disabled> <i class="fa fa-close"></i> Cancel </button> 
					';

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


					}

                
                  
                  ?>

                    </td>
                    </tr>

                </tr>
                    <div id="cancel<?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">Cancel 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" name="ptoid" value="<?php echo $leaverow['ptoid']; ?>">
                                        <input type="hidden" name="cancelduration" value="<?php echo $leaverow['duration']; ?>">

                                        Are you sure you want to cancel your leave ?
                                       

                                    </div>

                                    <div class="modal-footer">
                                        <button type="submit" class="btn btn-success" name="cancelleave"><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="viewpending<?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> Leave Type </label>
                                            <input type="text" name="type" class="form-control" value="<?php echo $leaverow['name']; ?>" readonly>
                                            </div>

                                            <div class="col-6">
                                            <label> Status </label>
                                            <input type="text" name="type" class="form-control" value="<?php echo $status?>" 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($leaverow['datefrom'])).' - '.date('m/d/Y', strtotime($leaverow['dateto'])) ?>" readonly>
                                            </div>
                                            <div class="col-6">
                                            <label> Duration </label>
                                            <input type="text" name="duration" class="form-control" value="<?php echo $leaverow['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 
                                            if($leaverow['status'] == '4.1'|| $leaverow['status'] == '4.2' || $leaverow['status'] == '4.3'){
                                            echo $leaverow['reason'];
                                       
                                              }
                                            else{
                                              echo $leaverow['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>
                    

                    


               <?php } 
               mysqli_close($conn);
               ?>
                </tbody>
                </table>


              </div>


           
           

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

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

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

                responsive: true,
                ordering: false
             

              } );
            } );




        </script>
</body>

</html>
