<?php 

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

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

function mnltopst($date, $time){

    $convert = new DateTime($date.' '.$time, new DateTimeZone('Asia/Manila'));
    
    
    $convert->setTimezone(new DateTimeZone('America/Los_Angeles'));
    
    $datetime = $convert->format('m/d/Y h:i A');
    
    
    return ($datetime);
    
    
}
function psttomnl($date, $time){
    
  $convert = new DateTime($date.' '.$time, new DateTimeZone('America/Los_Angeles'));
  
  
  $convert->setTimezone(new DateTimeZone('Asia/Manila'));
  
  $datetime = $convert->format('m/d/Y h:i A');
  
  
  return ($datetime);
  
  
  
  
  }

  


$datefiled = date('m/d/Y');

$day = date('l');
$dates = date('Y-m-d');
$start = date('h:i:sa');



$GMT = 'GMT+1';
$parameter = $day.', '.$dates.' '.$start.' '.$GMT;

$tz = new DateTimeZone('America/Los_Angeles');
$PST = new DateTime($parameter);
$PST->setTimezone($tz);

$datefiled = $PST->format('m/d/Y');
 $days = $PST->format('l');
 $curt = $PST->format('g:i A');



$sql = "SELECT * FROM `leave` ORDER BY id DESC LIMIT 1";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);


$curyear = date('y');

$rowcount=mysqli_num_rows($result);

$row['idl'];



 $rowcount = substr($row['idl'], 9);


 $count = $rowcount + 1;

$strlength = strlen($count);


if($strlength >= 1 && $strlength <= 4){
     $ATVID = str_pad($count, 5, '0', STR_PAD_LEFT);
  
   
}
else{
  $ATVID  = $count;
}
 $idl = 'ATVPTO-'.$curyear.$ATVID;




if(isset($_POST['submitleave'])){
  
    $leave_type = $_POST['type'] == 'EL_Paid Time Off' || $_POST['type'] == 'SL_Paid Time Off' || $_POST['type'] == 'VL_Paid Time Off' ? substr($_POST['type'],3) : $_POST['type'];
    $leavetype =  $leave_type;
$leavename = phps($_POST['typename']);
 
$datefrom = phps($_POST['from']);
$dateto = phps($_POST['to']);

$duration = phps($_POST['duration']);
$datefiled = phps($_POST['datefiled']);



$leavetype = mysqli_real_escape_string($conn, $leavetype);
$leavename = mysqli_real_escape_string($conn, $leavename);
 
$datefrom = mysqli_real_escape_string($conn, $datefrom);
$dateto = mysqli_real_escape_string($conn,  $dateto);

$duration = mysqli_real_escape_string($conn, $duration);
$datefiled = mysqli_real_escape_string($conn, $datefiled);


$filed = date('Y-m-d', strtotime($datefiled));
$notes = phps($_POST['notes']);
$notes = mysqli_real_escape_string($conn, $notes);


if($datefrom != $dateto){

    $begin = new DateTime($datefrom);
    $end = new DateTime($dateto);
    $end = $end->modify( '+1 day' ); 

    $interval = new DateInterval('P1D');
    $daterange = new DatePeriod($begin, $interval ,$end);

    $range = array();

    foreach($daterange as $dates){         

    $range[] = $dates->format("Y-m-d");


    }

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

        $existdate = "SELECT * FROM schedule JOIN `leave` ON schedule.idl = `leave`.`idl` WHERE schedule.date = '$range[$a]' AND `status` IN ('0','1','2','3') AND schedule.empid = '$empid'";

        $existdatequery = mysqli_query($conn, $existdate);
        if(mysqli_num_rows($existdatequery) > 0){

            $error ='<div class="alert alert-danger" role="alert"> Given date is not available </div>';

        }


///check schedule id already added 



$checksched = "SELECT * FROM schedule WHERE empid='$empid' AND date='$range[$a]'";

$checkres = mysqli_query($conn, $checksched);
if(mysqli_num_rows($checkres) < 1){

    $error ='<div class="alert alert-danger" role="alert"> Schedule are not yet added  </div>';

}







    }


}else{

    $existdate = "SELECT * FROM schedule JOIN `leave` ON schedule.idl = `leave`.`idl` WHERE schedule.date IN('$datefrom', '$dateto') AND `status` IN ('0','1','2','3') AND schedule.empid = '$empid'";
    $existdatequery = mysqli_query($conn, $existdate);
if(mysqli_num_rows($existdatequery) > 0){

    $error ='<div class="alert alert-danger" role="alert"> Given date is not available </div>';

}

$checksched = "SELECT * FROM schedule WHERE empid='$empid' AND date='$datefrom'";

$checkres = mysqli_query($conn, $checksched);
if(mysqli_num_rows($checkres) < 1){

    $error ='<div class="alert alert-danger" role="alert"> Schedule are not yet added  </div>';

}



   


}

if(empty($error)){

    

if($leavetype != "LWOP" && $leavetype != "Holiday Off"){
    








    ///DATE DUPLICATE CHECK
    
    $ptosql  ="SELECT * FROM pto WHERE empid='$empid' AND name='$leavetype'";
    $query = mysqli_query($conn, $ptosql);
    $row = mysqli_fetch_assoc($query);
    $availablepto = $row['availablepto'];
    $usedpto = $row['usedpto'];
    $ptoid = $row['id'];
    
    
    if($duration > $availablepto ){
    
        $error ='<div class="alert alert-danger" role="alert"> You dont have enough '.$leavetype.' Leave </div>';
    
    
    }
    
    
    
    
    
    
    
    /* if($datefrom != $dateto){
    
        $begin = new DateTime($datefrom);
        $end = new DateTime($dateto);
        $end = $end->modify( '+1 day' ); 
    
        $interval = new DateInterval('P1D');
        $daterange = new DatePeriod($begin, $interval ,$end);
    
        $range = array();
    
        foreach($daterange as $dates){         
    
        $range[] = $dates->format("Y-m-d");
    
        }
    
    
    /*     for($a=0; $a < count($range); $a++){
    
            $schedupdate = "UPDATE schedule SET idl='$idl' WHERE empid='$empid' AND date='$range[$a]'";
    
            if(mysqli_query($conn, $schedupdate)){
    
     
            }
     
        }
    */
    
      }else{
    
        $error = '';
      }
    
      if(empty($error)){
    
     

    
        if($leavetype == "LWOP" || $leavetype=="Holiday Off"){
            $ptoid = 'NULL';
    
    
    
    
            if($acclevel== 'team leader'){
                $insertleave = "INSERT INTO `leave`(`idl`, `type`, `datefiled`, `datefrom`, `dateto`, `duration`, `description`, `status`, `empid`, `reason`,  `name`, tl)
                 VALUES ('$idl','$leavetype','$filed','$datefrom','$dateto','$duration','$notes','1','$empid','','$leavename', '$empid')";
        
                
            }else if($acclevel == 'Senior Human Resource' || $acclevel == 'Manager'){
        
                $insertleave = "INSERT INTO `leave`(`idl`, `type`, `datefiled`, `datefrom`, `dateto`, `duration`, `description`, `status`, `empid`, `reason`,  `name`, hr)
                VALUES ('$idl','$leavetype','$filed','$datefrom','$dateto','$duration','$notes','2','$empid','','$leavename','$empid')";
        
        
            }else if($acclevel== 'Senior Manager'){
        
                $insertleave = "INSERT INTO `leave`(`idl`, `type`, `datefiled`, `datefrom`, `dateto`, `duration`, `description`, `status`, `empid`, `reason`, `name`,`approver`)
                VALUES ('$idl','$leavetype','$filed','$datefrom','$dateto','$duration','$notes','3','$empid','','$leavename', '$empid')";
        
        
            }else{
        
         
        
               $insertleave = "INSERT INTO `leave`(`idl`, `type`, `datefiled`, `datefrom`, `dateto`, `duration`, `description`, `status`, `empid`, `reason`, `name`)
                VALUES ('$idl','$leavetype','$filed','$datefrom','$dateto','$duration','$notes','0','$empid','','$leavename')";
        
        
            }
    
            if(mysqli_query($conn, $insertleave)){
    
                if($datefrom != $dateto){
    
                    for($a=0; $a < count($range); $a++){
            
                        $du = substr($duration, -2);
                 
                         if($du != '.5'){
                 
                             $leaveduration = $duration / $duration;
                 
                         }else{
                 
                           
                        
                              $x = count($range) - 1;
                             
                             if($a == $x){
                                 $leaveduration = '0.5';
                             }else{
                 
                                 $leaveduration = $duration / $duration;
                             }
                         }
    
    

                 
                 
                         
                 
                         $week = date('W', strtotime($range[$a]));
                         $hrsyear = date('Y', strtotime($range[$a]));
                         $gethrs ="SELECT * FROM schedule WHERE week='$week' AND year='$hrsyear'  AND empid='$empid' AND starttime !='OFF' AND starttime !='HOLIDAY OFF'";
                         $hrsres = mysqli_query($conn, $gethrs);
                         $hrsrow = mysqli_fetch_assoc($hrsres);

                         $hrs = $hrsrow['starttime'];
                 
                          $pstdate = date('Y-m-d', strtotime(mnltopst($range[$a], $hrs)));
                 
                        $schedupdate = "UPDATE schedule SET idl='$idl', leaveduration='$leaveduration', pstdate='$pstdate' WHERE empid='$empid' AND date='$range[$a]'";
    
                  
                  
    
                
                         if(mysqli_query($conn, $schedupdate)){
                            $alert = '<div class="alert alert-success" role="alert"> Leave Successfully Submitted </div>';
                
                         
                
                         }else{
                            echo mysqli_error($conn);
                         }
                 
                 
                 
                 
                 
                 
                 
                   }
                      
                
                   $alert = '<div class="alert alert-success" role="alert"> Leave Successfully Submitted </div>';
                  }else{
                 
                    $week = date('W', strtotime($datefrom));
                    $hrsyear = date('Y', strtotime($datefrom));
                    $gethrs ="SELECT * FROM schedule WHERE week='$week' AND year='$hrsyear'  AND empid='$empid' AND starttime !='OFF' AND starttime !='HOLIDAY OFF'";
                    $hrsres = mysqli_query($conn, $gethrs);
                    $hrsrow = mysqli_fetch_assoc($hrsres);

                    $hrs = $hrsrow['starttime'];
            
                     $pstdate = date('Y-m-d', strtotime(mnltopst($datefrom, $hrs)));
            
                   //$schedupdate = "UPDATE schedule SET idl='$idl', leaveduration='$leaveduration', pstdate='$pstdate' WHERE empid='$empid' AND date='$range[$a]'";


                 $schedupdate = "UPDATE schedule SET idl='$idl', leaveduration='$duration' , pstdate='$pstdate' WHERE empid='$empid' AND date='$datefrom'";
            
            
                    if(mysqli_query($conn, $schedupdate)){
                
               
            
                     }else{
                        echo mysqli_error($conn);
                     }
             
                     
                  }
            
    
                $alert = '<div class="alert alert-success" role="alert"> Leave Successfully Submitted </div>';
    
    
            }
            else{
                echo mysqli_error($conn);
            }
        
        
    
            
        
    
        }else{
    
             
    
    
         
     
        
    
    
         $available = $availablepto - $duration;
         $used = $usedpto + $duration;
        $ptoid;
    
    
         if($acclevel== 'team leader'){
            $insertleave = "INSERT INTO `leave`(`idl`, `type`, `datefiled`, `datefrom`, `dateto`, `duration`, `description`, `status`, `empid`, `reason`, `ptoid`, `name`, `tl`)
             VALUES ('$idl','$leavetype','$filed','$datefrom','$dateto','$duration','$notes','1','$empid','','$ptoid','$leavename', '$empid')";

            $updatepto = "UPDATE pto SET availablepto = '$available', usedpto='$used' WHERE id='$ptoid'";
    
            
        }else if($acclevel== 'Senior Manager'){
    
            $insertleave = "INSERT INTO `leave`(`idl`, `type`, `datefiled`, `datefrom`, `dateto`, `duration`, `description`, `status`, `empid`, `reason`, `ptoid`, `name`, `approver`)
            VALUES ('$idl','$leavetype','$filed','$datefrom','$dateto','$duration','$notes','3','$empid','','$ptoid','$leavename','$empid')";

            $updatepto = "UPDATE pto SET availablepto = '$available', usedpto='$used' WHERE id='$ptoid'";
    
    
        }else if($acclevel== 'Manager' || $acclevel == 'Senior Human Resource'){
    
            $insertleave = "INSERT INTO `leave`(`idl`, `type`, `datefiled`, `datefrom`, `dateto`, `duration`, `description`, `status`, `empid`, `reason`, `ptoid`, `name`,`hr`)
            VALUES ('$idl','$leavetype','$filed','$datefrom','$dateto','$duration','$notes','2','$empid','','$ptoid','$leavename','$empid')";

           $updatepto = "UPDATE pto SET availablepto = '$available', usedpto='$used' WHERE id='$ptoid'";
    
    
        }else{
    
        
          $insertleave = "INSERT INTO `leave`(`idl`, `type`, `datefiled`, `datefrom`, `dateto`, `duration`, `description`, `status`, `empid`, `reason`, `ptoid`, `name`)
            VALUES ('$idl','$leavetype','$filed','$datefrom','$dateto','$duration','$notes','0','$empid','','$ptoid','$leavename')";
             $updatepto = "UPDATE pto SET availablepto = '$available', usedpto='$used' WHERE id='$ptoid'";
    
    
        }

        if(mysqli_query($conn, $insertleave) && mysqli_query($conn, $updatepto)){
    
     
    
            if($datefrom != $dateto){
        
                for($a=0; $a < count($range); $a++){
        
                    $du = substr($duration, -2);
             
                     if($du != '.5'){
             
                         $leaveduration = $duration / $duration;
             
                     }else{
             
                       
                    
                          $x = count($range) - 1;
                         
                         if($a == $x){
                             $leaveduration = '0.5';
                         }else{
             
                             $leaveduration = $duration / $duration;
                         }
                     }
             
             
             
                     
             
                     $week = date('W', strtotime($range[$a]));
                     $hrsyear = date('Y', strtotime($range[$a]));
                     $gethrs ="SELECT * FROM schedule WHERE week='$week' AND year='$hrsyear'  AND empid='$empid' AND starttime !='OFF' AND starttime !='HOLIDAY OFF'";
                     $hrsres = mysqli_query($conn, $gethrs);
                     $hrsrow = mysqli_fetch_assoc($hrsres);

                     $hrs = $hrsrow['starttime'];
             
                      $pstdate = date('Y-m-d', strtotime(mnltopst($range[$a], $hrs)));
             
                    $schedupdate = "UPDATE schedule SET idl='$idl', leaveduration='$leaveduration', pstdate='$pstdate' WHERE empid='$empid' AND date='$range[$a]'";
            
                     if(mysqli_query($conn, $schedupdate)){
            
                    
                     }else{
                        echo mysqli_error($conn);
                     }
             
             
             
             
             
             
             
               }
                  
            
               $alert = '<div class="alert alert-success" role="alert"> Leave Successfully Submitted </div>';
              }else{
                $week = date('W', strtotime($datefrom));
                $hrsyear = date('Y', strtotime($datefrom));
                $gethrs ="SELECT * FROM schedule WHERE week='$week' AND year='$hrsyear'  AND empid='$empid' AND starttime !='OFF' AND starttime !='HOLIDAY OFF'";
                $hrsres = mysqli_query($conn, $gethrs);
                $hrsrow = mysqli_fetch_assoc($hrsres);

                $hrs = $hrsrow['starttime'];
        
                 $pstdate = date('Y-m-d', strtotime(mnltopst($datefrom, $hrs)));
        
               //$schedupdate = "UPDATE schedule SET idl='$idl', leaveduration='$leaveduration', pstdate='$pstdate' WHERE empid='$empid' AND date='$range[$a]'";


                $schedupdate = "UPDATE schedule SET idl='$idl', leaveduration='$duration' , pstdate='$pstdate' WHERE empid='$empid' AND date='$datefrom'";
                
        
                if(mysqli_query($conn, $schedupdate)){
            
            
        
                 }else{
                    echo mysqli_error($conn);
                 }
         
                 
              }
    
              $alert = '<div class="alert alert-success" role="alert"> Leave Successfully Submitted </div>';
        
        
            }else{

                echo mysqli_error($conn);

           
            }
  
    
    
        }



      

     
    
    }///empty error
    else{

      
    }

}



}

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

<head><meta http-equiv="Content-Type" content="text/html; 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 | Add PTO</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 deleteRow(obj) {
        var index = obj.parentNode.parentNode.rowIndex;
        var table = document.getElementById("Tabledata");
        table.deleteRow(index);
    }
    </script>


</head>

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






    <!-- Sidebar -->

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

        <div class="container-fluid">



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

                <li class="breadcrumb-item "><a href="leave.php">Paid Time Off</a></li>
                <li class="breadcrumb-item active">Add PTO</li>
            </ol>


            <form method="POST" id="theForm">

                <div class="row">
                    <div class="col-lg-12">
                        <?php echo $alert.$leavealert.$error?>
                        <div class="alert alert-danger alert-dismissible fade show" id="datealert" role="alert" hidden>
                            Date to must be greater than Date from
                            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                            </button>
                        </div>

                        <div class="alert alert-danger alert-dismissible fade show" id="datemonth" role="alert" hidden>
                            Date leave must be 1 month before filling date
                            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                            </button>
                        </div>

                        

                        <div class="alert alert-danger alert-dismissible fade show" id="datefiledalert" role="alert" hidden>
                            Date leave must greater than date today
                            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                            </button>
                        </div>



                        <div class="alert alert-danger alert-dismissible fade show" id="APTOalert" role="alert" hidden>
                            You dont have enough PTO
                            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                            </button>
                        </div>


                        <h5>Add Leave</h5>

                    </div>





                    <div class="col-6 mx-auto">
                        <div class="row justify-content-center">



                            <div class="col-sm-6 form-group">
                                <label for="type" class="label-control"><b>Date Filed </b></label>
                                <input type="text" name="datefiled" id="datefiled" class="form-control"
                                    value="<?php echo $datefiled?>" readOnly required>

                            </div>
                            <div class="col-sm-6 form-group">
                                <label for="type" class="label-control"><b>Type </b></label>

                                <select name="type" id="type" class="form-control" onchange='selected()' required>
                                <option selected disabled> Leave Type</option>

                                <option value="EL_Paid Time Off" <?php echo $_POST['type'] == 'EL_Paid Time Off' ? 'selected' : ''?>> Emergency Leave</option>
                                    <option value="SL_Paid Time Off" <?php echo $_POST['type'] == 'SL_Paid Time Off' ? 'selected' : ''?>> Sick Leave</option>
                                    <option value="VL_Paid Time Off" <?php echo $_POST['type'] == 'VL_Paid Time Off' ? 'selected' : ''?>> Vacation Leave</option>

                                    <option value="LWOP" <?php echo $_POST['type'] == 'LWOP' ? 'selected' : ''?> > LWOP - Unpaid Leave</option>
                                    <option value="Holiday Off" <?php  echo $_POST['type'] == 'Holiday Off' ? 'selected' : ''?> > Holiday Off</option>
                                    <?php
                            
                         
                                $sqltype ="SELECT * FROM pto WHERE empid='$empid' AND name !='Paid Time Off'";
                            
                      
                            $sqlquery = mysqli_query($conn, $sqltype);
                            while($typerow = mysqli_fetch_assoc($sqlquery)){
                                if($leavetype == $typerow['name']){

                                    echo'<option value="'.$typerow['name'].'" selected>'.$typerow['name'].' Leave</option>';

                                }
                                else{

                                    echo'<option value="'.$typerow['name'].'">' .$typerow['name'].' Leave</option>';

                                }

                                

                            }
                            ?>
                                </select>

                            </div>
                            <div class="col-sm-12">
                                <div class="row">

                                    <!-- HIDDEN FIELD-->
                                    <input type="hidden" name="" id="APTO" value="<?php echo $Apto?>">


                                    <div class="col-sm-12">
                                        <label for="type" class="label-control"><b>Leave Date </b></label>
                                    </div>

                                    <script>

                                    function selected(){
                           

                                        
                                    var typename = document.getElementById('type');
                                    var selectedtext = typename.options[typename.selectedIndex].text;
                                    document.getElementById('typename').value = selectedtext;

                                    }



                                    </script>
                                    <input type="hidden" id='typename' name="typename" >




                                    <div class="col-sm-4">

                                        <input type="date" name="from" id="from" class="form-control"
                                            value="<?php echo $datefrom?>" onchange="daterangecheck()" required>
                                    </div>
                                    <div class="col-sm-4">
                                        <input type="date" name="to" id="to" class="form-control"
                                            value="<?php echo $dateto?>" onchange="daterangecheck()" required>
                                    </div>

                                    <div class="col-sm-3">
                                        <input type="number" name="duration" id="duration" class="form-control"
                                            value="<?php echo $duration?>" placeholder="Duration" required readonly>
                                    </div>


                                    <div class="col-sm-1">

                                        <div class="custom-control custom-checkbox my-1 mr-sm-2">
                                            <input type="checkbox" class="custom-control-input" id="cb"
                                                onclick="daterangecheck()">
                                            <label class="custom-control-label" for="cb">0.5</label>
                                        </div>

                                    </div>



                                </div>









                            </div>
                            <div class="col-sm-12 form-group">
                                <br>
                                <label for="type" class="label-control"><b>Reason </b></label>
                                <textarea name="notes" id="notes" cols="30" rows="10" class="form-control"
                                    placeholder="Reason" required><?php echo $notes?></textarea>



                            </div>

                            <div class="col-sm-12 form-group">

                                <span class="pull-right"> <button type="button" class="btn btn-success"
                                        data-toggle="modal" id='subleave' data-target="#leavemodal"> <i class="fas fa-check"></i>
                                        Submit</button></span>





                            </div>



                        </div>

                    </div>



                </div>

                <div class="modal fade" id="leavemodal" tabindex="-1" role="dialog"
                    aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
                    <div class="modal-dialog modal-dialog-centered" role="document">
                        <div class="modal-content">
                            <div class="modal-header">
                                <h5 class="modal-title" id="exampleModalLongTitle">Confirm Leave</h5>
                                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                    <span aria-hidden="true">&times;</span>
                                </button>
                            </div>
                            <div class="modal-body">
                                Are you sure you to submit file a leave?
                            </div>
                            <div class="modal-footer">
                                <button type="submit" class="btn btn-success" name="submitleave"><i
                                        class="fas fa-check"></i> Yes</button>
                                <button type="button" class="btn btn-danger" data-dismiss="modal"><i
                                        class="fas fa-close"></i> No</button>
                            </div>
                        </div>
                    </div>
                </div>


                <div class="modal fade" id="dupmodal" tabindex="-1" role="dialog"
                    aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
                    <div class="modal-dialog modal-dialog-centered" role="document">
                        <div class="modal-content">
                            <div class="modal-header">
                                <h5 class="modal-title" id="exampleModalLongTitle"><i class="fa fa-warning"> </i> Error
                                </h5>
                                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                    <span aria-hidden="true">&times;</span>
                                </button>
                            </div>
                            <div class="modal-body">
                                Duplicate Leave Date
                            </div>
                            <div class="modal-footer">

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

            </form>
            <?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>
    function daterangecheck() {

        var from = document.getElementById('from').value;
        var to = document.getElementById('to').value;
        var cb = document.getElementById('cb').value;
        var APTO = document.getElementById('APTO').value;
        var du = document.getElementById('duration');
        var leavetype = document.getElementById('type').value;
        var subleave = document.getElementById('subleave');

        var datefiled = document.getElementById('datefiled').value



        var datefrom = new Date(from);
        var dateto = new Date(to);

        var datetoday = new Date(datefiled);



        var today = new Date().toISOString().split('T')[0];

        var startDate = Date.parse(from);
        var endDate = Date.parse(to);
        var timeDiff = endDate - startDate;

        daysDiff = Math.floor(timeDiff / (1000 * 60 * 60 * 24) + 1);


        if (document.getElementById("cb").checked == true) {
            du.value = daysDiff - 0.5;

        } else {
            du.value = daysDiff;
        }






     if (datefrom > dateto) {



            document.getElementById('datealert').hidden = false;
            document.getElementById('subleave').disabled = true;



        } 
        /*    
        else if(datefrom < datetoday ){

            document.getElementById('datefiledalert').hidden = false;
            document.getElementById('subleave').disabled = true;


        }*/

        
         else {

            document.getElementById('datealert').hidden = true;
            document.getElementById('subleave').disabled = false;
            document.getElementById('datefiledalert').hidden = true;






        } 
    }


    </script>


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

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



        </script>
</body>

</html>