<?php 


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

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


$sqlmonthcheck = "SELECT * FROM monthcheck";
$sqlmonthquery = mysqli_query($conn, $sqlmonthcheck);

if(isset($_POST['refresh'])){
    $conn->query("UPDATE pto SET ptoearned='0',usedpto='0',availablepto='0' WHERE name='Paid Time Off' ");
    $conn->query("UPDATE pto SET ptoearned='3',usedpto='0',availablepto='3' WHERE name='Bereavement' ");
    $conn->query("UPDATE pto SET ptoearned='60',usedpto='0',availablepto='60' WHERE name='Magna Carta' ");
    $conn->query("UPDATE pto SET ptoearned='60',usedpto='0',availablepto='60' WHERE name='Maternity' ");
    $conn->query("UPDATE pto SET ptoearned='7',usedpto='0',availablepto='7' WHERE name='Maternity Allocation' ");
    $conn->query("UPDATE pto SET ptoearned='7',usedpto='0',availablepto='7' WHERE name='Maternity Allocation' ");
    $conn->query("UPDATE pto SET ptoearned='7',usedpto='0',availablepto='7' WHERE name='Paternity' ");
    $conn->query("UPDATE pto SET ptoearned='7',usedpto='0',availablepto='7' WHERE name='Solo Parent' ");
    $conn->query("UPDATE pto SET ptoearned='10',usedpto='0',availablepto='10' WHERE name='VAWC' ");

}


mysqli_close($conn);


?>
<!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>Super Admin | 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">
    <link rel="icon" href="../images/logo.jpg" type="image/gif" sizes="16x16">
    <script>
    function display_c() {
        var refresh = 1000; // Refresh rate in milli seconds
        mytime = setTimeout('display_ct()', refresh)
    }

    function display_ct() {
        var x = new Date();
        var hour = x.getHours();
        var minute = x.getMinutes();
        var second = x.getSeconds();
        if (hour > 12) {
            hour = hour - 12;
        }
        if (hour == 0) {
            hour = 12;
        }
        if (hour < 10) {
            hour = '0' + hour;
        }
        if (minute < 10) {
            minute = '0' + minute;
        }
        if (second < 10) {
            second = '0' + second;
        }

        var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October",
            "November", "December"
        ];
        var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
        var hours = x.getHours();
        var ampm = hours >= 12 ? 'PM' : 'AM';
        document.getElementById('dmdy').innerHTML = days[x.getDay()] + ", " + months[x.getMonth()] + " " + x.getDate() +
            " " + x.getFullYear();
        document.getElementById('tt').innerHTML = hour + ":" + minute + ":" + second + " " + ampm;
        display_c();
    }
    </script>


</head>

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

    <?php include('uppernav.php')?>



    <!-- Sidebar -->

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

        <div class="container-fluid">

            <!-- Breadcrumbs-->
            <ol class="breadcrumb">
                <li class="breadcrumb-item active">
                    <a href="#"> Paid Time Off</a>
                </li>

            </ol>
            <div class="row p-3" >
                <div class="col-sm-12">
                    <h4>Paid Time Off</h4>
                </div>
                <div class="col-sm-12">
                    <form method="POST">
                        <span class="pull-right"><button class="btn btn-success" type="submit" name="refresh"><i
                                    class="fa fa-refresh"></i> Reset PTO </button></span>
                    </form>
                    <br>
                </div>
                <div class="col-sm-12">
                    <table class="table" id='ptotbl'>
                        <thead class="thead-dark">
                            <tr class="thead-dark">
                                <th>Employee ID</th>
                                <th>Name</th>
                                <th>Team</th>
                                <th>Status</th>
                                <th>Hired Date</th>
                                <th>PTO Earned</th>
                                <th>Used PTO</th>
                                <th>Available PTO</th>

                            </tr>

                        </thead>
                        <tbody>
                            <?php 
                            $ptosql = "SELECT * FROM pto JOIN empinfo ON pto.empid = empinfo.empid JOIN empdetails ON pto.empid = empdetails.detailid WHERE pto.name='Paid Time Off'";
                            $ptoquery = mysqli_query($conn, $ptosql);
                            while($ptorow = mysqli_fetch_assoc($ptoquery)){

                                $getday = $ptorow['hireddate'];

                                $today = date('Y-m-d').'';

                                $getday = strtotime($getday);
                                $today = strtotime($today);

                                $ygetday = date('Y', $getday);
                                $ytoday = date('Y', $today);

                                $mgetday = date('m', $getday);
                                $mtoday = date('m', $today);

                                $dgetday = date('d', $getday);
                                $dtoday = date('d', $today); 




                        

                  
                        

                                //echo $diff = (($ytoday - $ygetday) * 12) + ($mtoday - $mgetday);
                               //echo $dgetday.'<br/>';
                               //echo $dtoday.'<br/>';

                               $empid = $ptorow['empid'];

/*                                 
                                if($dgetday == $dtoday && $ptorow['ptoearned'] > 0){
                                    $pto = $diff + 1;
                                    $apto = $diff + 1;
                                    $updatepto = "UPDATE pto SET ptoearned='$pto', availablepto='$apto' WHERE empid='$empid'";
                                    mysqli_query($conn, $updatepto);

                                }

                                 
                                
 */



/*                                     if($dtoday >= $dgetday && $ptorow['updatestat'] == '0'){
                                     // echo'<td> ';
                                     // echo $ptorow['ptoearned'] + $ptorow['multiplier'].'</td>';
                                      $pto = $ptorow['ptoearned'] + $ptorow['multiplier'];
                                      $availablepto = $pto - $ptorow['usedpto'];

                                     echo $monthtoday = date('F');
                                      

                                      $updatepto = "UPDATE pto SET ptoearned='$pto', availablepto='$availablepto', updatestat='1' WHERE empid='$empid'";
3.34
                                      $updatemonth = "UPDATE monthcheck SET `stat`='1' WHERE `month`='$monthtoday'";
                                   

                                      if(mysqli_query($conn, $updatepto) && mysqli_query($conn, $updatemonth)){


                                      }else{
                                        echo "Error updating record: " . mysqli_error($conn);
                                      }
     
                                    } */

                                    
           
                        

                                
                                echo'
                                <tr>  
                                    <td>'.$ptorow['empid'].'</td>
                                    <td>'.$ptorow['firstname'].' '.$ptorow['surname'].'</td>
                                    <td>'.$ptorow['team'].'</td>
                                    <td>'.$ptorow['status'].'</td>
                                    <td>'.date('Y/m/d', strtotime($ptorow['hireddate'])).'</td>';

                                    if($ptorow['status'] == 'Inactive'){

                                        echo '
                                        <td> --- </td>
                                        <td> --- </td>
                                        <td> --- </td>
                                        
                                        ';
                                    }else{
                                        echo'
                                        <td>'.$ptorow['ptoearned'].'</td>
                                        <td>'.$ptorow['usedpto'].'</td>
                                        <td>'.$ptorow['availablepto'].'</td>';



                                    }




                                    

                                    
                                  echo'
                                </tr>
                                 ';
                            }
                            ?>
                        </tbody>
                    </table>
                    <script>
                // get the table element
                var $table = document.getElementById("ptotbl"),
                // number of rows per page
                $n = 12,
                // 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(1);
                }

                // ($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");
                }


                // ($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' 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>

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

            $.ajax({
     
                url: "../ptocron.php",
              
                success: function (response) {

                    /* alert('done'); */
                    
                }
            });



        </script>
</body>

</html>