<?php
   include('session.php');
   include('header1.php');
   include('ietpartial.php');
   include('calculations.php');

// start role specific 
$user_role = $_SESSION['role'];
$companyID = $_SESSION['companyselect'];

if ($_SERVER["REQUEST_METHOD"] == "GET") {

  $payrollid = $_GET['payrollID'];

  $sqlViewPayroll = "SELECT *
  FROM periods
  WHERE periodid = $payrollid;";

  $resultPayroll = mysqli_query($db,$sqlViewPayroll);

  $countPayroll = mysqli_num_rows($resultPayroll);

  if(isset($_GET['updated'])) {

    $updated = $_GET['updated'];

  }

    $sqlView = "SELECT 
          employees.employeeid AS employeeid,
          employees.employeename AS employeename,
          employees.employeesurname AS employeesurname,          
          employees.employee_join_date AS joinDate,
          employees.employee_termination_date AS terminationDate,
          employees.employee_edf_amount AS edf_amount,
          employees.employee_paye_rate AS payeRate,
          departments.departmentname AS department,
          positions.positionname AS position,
          salaries.salaryvalue AS basic,
          travellings.travellingvalue AS travelling
          FROM employees
          LEFT JOIN employee_job
          ON employees.employeeid = employee_job.employeeid
          LEFT JOIN departments
          ON employee_job.departmentid = departments.departmentid
          LEFT JOIN positions
          ON employee_job.positionid = positions.positionid
          LEFT JOIN salaries
          ON employees.employeeid = salaries.employeeid
          LEFT JOIN travellings
          ON employees.employeeid = travellings.employeeid
          WHERE employees.companyid = $companyID;";
  
    $result = mysqli_query($db,$sqlView);

    $count = mysqli_num_rows($result);

    $numberOfDaysInMonth = 0;

  echo "<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js\"></script>";



    echo "<main role=\"main\" class=\"col-md-9 ml-sm-auto col-lg-10 px-4\">";

    echo "<div class=\"d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom\">";
    
    if ($count !== 0) { // COUNT EMPLOYEES START HERE
      $periodSelectID = 0;
      while ($rowperiods = mysqli_fetch_array($resultPayroll)) { 
        $periodSelectID = $rowperiods['periodid'];
          echo "<h1 class=\"h2\">Input Overtime - " . $rowperiods['startDate'] . " - " . $rowperiods['endDate'] . "</h1>";
          $numberOfDaysInMonth = $rowperiods['workingDays'];

          $payrollStart = $rowperiods['startDate'];
          $payrollEnd = $rowperiods['endDate'];
          
      }

      echo "<div class=\"btn-toolbar mb-2 mb-md-0\">"; // TOOLBAR STARTS HERE

      echo "<a href=\"payrolls.php\" class=\"btn btn-sm btn-outline-secondary\" role=\"button\">Back to Payrolls</a>";

          echo "</div>";

    echo "</div>";

    // TABLE STARTS HERE

  echo "<table class = \"table\">";

  echo "<thead>";
  echo "<td>Employee Name</td>";
  echo "<td>Overtime 1.5</td>";
  echo "<td>Overtime 2.0</td>";
  echo "<td>Overtime 3.0</td>";
  echo "<td>Total</td>";
  echo "<td>Check</td>";
  echo "<td>Submit</td>";
  echo "</thead>";
  echo "<tbody>";

  $sqlCheckExistingOvertime = "
  SELECT
employees.employeename AS employeename,
employees.employeesurname AS employeesurname,
employees.employeeid AS employeeid,
salaries.salaryvalue AS basic,
overtimes.periodid AS periodid,
overtimes.1_5_units AS 1_5units,
overtimes.2_0_units AS 2_0units,
overtimes.3_0_units AS 3_0units
FROM overtimes
RIGHT JOIN employees
ON overtimes.employeeid = employees.employeeid
LEFT JOIN salaries
ON employees.employeeid = salaries.employeeid
WHERE overtimes.periodid = '$periodSelectID';";

  $resultCheckExistingOvertime = mysqli_query($db,$sqlCheckExistingOvertime);

    $countCheckExistingOvertime = mysqli_num_rows($resultCheckExistingOvertime);




    /* disable v1

      if ($countCheckExistingOvertime !== 0) { 

        while ($rowdCheckExistingOvertime = mysqli_fetch_array($resultCheckExistingOvertime)) {
          
         
                    echo "<tr id = \"employeerow" . $rowdCheckExistingOvertime['employeeid'] . "\" >";


                    echo "<form id=\"payrollform\" name=\"form\">";
                    
                    
                    echo "<input type=\"hidden\" id = \"payrollid" . $rowdCheckExistingOvertime['employeeid'] . "\" value = \"" . $periodSelectID . "\" >";
                    echo "<input type=\"hidden\" id = \"employeeid" . $rowdCheckExistingOvertime['employeeid'] . "\" value = \"" . $rowdCheckExistingOvertime['employeeid'] . "\" >";
                    echo "<input type=\"hidden\" id = \"basicsalary" . $rowdCheckExistingOvertime['employeeid'] . "\" value = \"" . $rowdCheckExistingOvertime['basic'] . "\" >";
                    echo "<td>" . $rowdCheckExistingOvertime['employeesurname'] . ", " . $rowdCheckExistingOvertime['employeename'] . "</td>";

                    echo "<td><input type = \"number\" style=\"background:#FCEC85;\" id = \"overtime1_5" . $rowdCheckExistingOvertime['employeeid'] . "\" value = \"" . $rowdCheckExistingOvertime['1_5units'] . "\"></td>";

                    echo "<td><input type = \"number\" style=\"background:#FCEC85;\" id = \"overtime2_0" . $rowdCheckExistingOvertime['employeeid'] . "\" value = \"" . $rowdCheckExistingOvertime['2_0units'] . "\"></td>";

                    echo "<td><input type = \"number\" style=\"background:#FCEC85;\" id = \"overtime3_0" . $rowdCheckExistingOvertime['employeeid'] . "\" value = \"" . $rowdCheckExistingOvertime['3_0units'] . "\"></td>";

                    echo "<td><input type = \"number\"  id = \"totalOvertime" . $rowdCheckExistingOvertime['employeeid'] . "\" readonly=\"readonly\" ></td>";


                          echo "<script type=\"text/javascript\">
                      function loadDoc" . $rowdCheckExistingOvertime['employeeid'] . "() {

                        var hourlyRate = $('#basicsalary" . $rowdCheckExistingOvertime['employeeid'] . "').val() / 26 / 8;

                        $('#totalOvertime" . $rowdCheckExistingOvertime['employeeid'] . "').val( ( (Number($('#overtime1_5" . $rowdCheckExistingOvertime['employeeid'] . "').val()) * hourlyRate * 1.5) + (Number($('#overtime2_0" . $rowdCheckExistingOvertime['employeeid'] . "').val()) * hourlyRate * 2.0) + (Number($('#overtime3_0" . $rowdCheckExistingOvertime['employeeid'] . "').val()) * hourlyRate * 3.0) ).toFixed(0) );

                        $('#totalOvertime" . $rowdCheckExistingOvertime['employeeid'] . "').css('background-color', '#85FC89');

                        $('#employeerow" . $rowdCheckExistingOvertime['employeeid'] . "').css('background-color', '#85FC89');

                        $('#submit" . $rowdCheckExistingOvertime['employeeid'] . "').attr(\"disabled\", false);

                      };     
                      </script>";

                      echo "<td><button type=\"button\" onclick=\"loadDoc" . $rowdCheckExistingOvertime['employeeid'] . "()\">Ok</button></td>";

                    echo "<td><input id=\"submit" . $rowdCheckExistingOvertime['employeeid'] . "\" class=\"submit\" onclick=\"formsubmit" . $rowdCheckExistingOvertime['employeeid'] . "()\" type=\"button\" value=\"Submit\" disabled=\"1\"></td>";


                    echo "    

                    <script type=\"text/javascript\">
                    
                        function formsubmit" . $rowdCheckExistingOvertime['employeeid'] . "()
                        {
                         
                         var payrollid =document.getElementById( \"payrollid" . $rowdCheckExistingOvertime['employeeid'] . "\" ).value;

                         var employeeid =document.getElementById( \"employeeid" . $rowdCheckExistingOvertime['employeeid'] . "\" ).value;

                         var overtime1_5 =document.getElementById( \"overtime1_5" . $rowdCheckExistingOvertime['employeeid'] . "\" ).value;
                         var overtime2_0 =document.getElementById( \"overtime2_0" . $rowdCheckExistingOvertime['employeeid'] . "\" ).value;
                         var overtime3_0 =document.getElementById( \"overtime3_0" . $rowdCheckExistingOvertime['employeeid'] . "\" ).value;
                         var totalOvertime =document.getElementById( \"totalOvertime" . $rowdCheckExistingOvertime['employeeid'] . "\" ).value;
                             
                          var formdata = 
                              'payrollid=' + payrollid + 
                              '&employeeid=' + employeeid + 
                              '&overtime1_5=' + overtime1_5 + 
                              '&overtime2_0=' + overtime2_0 + 
                              '&overtime3_0=' + overtime3_0 +
                              '&totalOvertime=' + totalOvertime;

                  
                  // AJAX code to submit form.
                  $.ajax({
                     type: \"POST\",
                     url: \"insertovertimedata.php\", 
                     data: formdata,
                     cache: false,
                     success: function(html) {
                      alert(html);
                     }
                  });

                  return false;
                }
                    
                        </script>";

                    echo "</form>";

                    echo "</tr>";

        }

       } else { // IF OVERTIME DATA DOESNT EXIST

        disable v1 */

                  while ($rowemployee = mysqli_fetch_array($result)) { // FETCHING EMPLOYEE DATA START
                    echo "<tr id = \"employeerow" . $rowemployee['employeeid'] . "\" >";

                    $sqlCheckForSavedOvertimeData = "
                    SELECT * FROM overtimes
                    WHERE periodid = '$payrollid' AND employeeid = '" . $rowemployee['employeeid'] . "';";
                
                    $resultCheckSavedOvertimeData = mysqli_query($db,$sqlCheckForSavedOvertimeData);
                
                    $count101Check = mysqli_num_rows($resultCheckSavedOvertimeData);
                
                    if ($count101Check !== 0) { 
                      $rowSavedOvertimeData = mysqli_fetch_assoc($resultCheckSavedOvertimeData); 
                    }
                

                    
                    // check employee start date and end date against payroll data
                    $employeeJoin = $rowemployee['joinDate'];
                    $employeeTermination = $rowemployee['terminationDate'];

                    if (($employeeTermination == NULL || $employeeTermination == "0000-00-00")) {
                        if ($employeeJoin > $payrollEnd) {
                          continue;
                        } 
                      } else {
                        if ($employeeTermination < $payrollStart) {
                          continue;
                        } 
                      }




                    echo "<form id=\"payrollform\" name=\"form\">";
                    
                    
                    echo "<input type=\"hidden\" id = \"payrollid" . $rowemployee['employeeid'] . "\" value = \"" . $periodSelectID . "\" >";
                    echo "<input type=\"hidden\" id = \"employeeid" . $rowemployee['employeeid'] . "\" value = \"" . $rowemployee['employeeid'] . "\" >";
                    echo "<input type=\"hidden\" id = \"basicsalary" . $rowemployee['employeeid'] . "\" value = \"" . $rowemployee['basic'] . "\" >";
                    echo "<td>" . $rowemployee['employeesurname'] . ", " . $rowemployee['employeename'] . "</td>";

                    if ($count101Check !== 0) {
                        echo "<td><input type = \"number\" style=\"background:#FCEC85;\" value = " . $rowSavedOvertimeData['1_5_units'] . " id = \"overtime1_5" . $rowemployee['employeeid'] . "\"></td>";
                    } else {
                  
                        echo "<td><input type = \"number\" style=\"background:#FCEC85;\" value = '0' id = \"overtime1_5" . $rowemployee['employeeid'] . "\"></td>";
                      }

                      if ($count101Check !== 0) {
                        echo "<td><input type = \"number\" style=\"background:#FCEC85;\" value = " . $rowSavedOvertimeData['2_0_units'] . " id = \"overtime2_0" . $rowemployee['employeeid'] . "\"></td>";
                    } else {
                  
                        echo "<td><input type = \"number\" style=\"background:#FCEC85;\" value = '0' id = \"overtime2_0" . $rowemployee['employeeid'] . "\"></td>";
                      }

                      if ($count101Check !== 0) {
                        echo "<td><input type = \"number\" style=\"background:#FCEC85;\" value = " . $rowSavedOvertimeData['3_0_units'] . " id = \"overtime3_0" . $rowemployee['employeeid'] . "\"></td>";
                    } else {
                  
                        echo "<td><input type = \"number\" style=\"background:#FCEC85;\" value = '0' id = \"overtime3_0" . $rowemployee['employeeid'] . "\"></td>";
                      }

                    echo "<td><input type = \"number\"  id = \"totalOvertime" . $rowemployee['employeeid'] . "\" readonly=\"readonly\" ></td>";


                          echo "<script type=\"text/javascript\">
                      function loadDoc" . $rowemployee['employeeid'] . "() {

                        var hourlyRate = $('#basicsalary" . $rowemployee['employeeid'] . "').val() / 26 / 8;

                        $('#totalOvertime" . $rowemployee['employeeid'] . "').val( ( (Number($('#overtime1_5" . $rowemployee['employeeid'] . "').val()) * hourlyRate * 1.5) + (Number($('#overtime2_0" . $rowemployee['employeeid'] . "').val()) * hourlyRate * 2.0) + (Number($('#overtime3_0" . $rowemployee['employeeid'] . "').val()) * hourlyRate * 3.0) ).toFixed(0) );

                        $('#totalOvertime" . $rowemployee['employeeid'] . "').css('background-color', '#85FC89');

                        $('#employeerow" . $rowemployee['employeeid'] . "').css('background-color', '#85FC89');

                        $('#submit" . $rowemployee['employeeid'] . "').attr(\"disabled\", false);

                      };     
                      </script>";

                      echo "<td><button type=\"button\" onclick=\"loadDoc" . $rowemployee['employeeid'] . "()\">Ok</button></td>";

                    echo "<td><input id=\"submit" . $rowemployee['employeeid'] . "\" class=\"submit\" onclick=\"formsubmit" . $rowemployee['employeeid'] . "()\" type=\"button\" value=\"Submit\" disabled=\"1\"></td>";


                    echo "    

                    <script type=\"text/javascript\">
                    
                        function formsubmit" . $rowemployee['employeeid'] . "()
                        {
                         
                         var payrollid =document.getElementById( \"payrollid" . $rowemployee['employeeid'] . "\" ).value;

                         var employeeid =document.getElementById( \"employeeid" . $rowemployee['employeeid'] . "\" ).value;

                         var overtime1_5 =document.getElementById( \"overtime1_5" . $rowemployee['employeeid'] . "\" ).value;
                         var overtime2_0 =document.getElementById( \"overtime2_0" . $rowemployee['employeeid'] . "\" ).value;
                         var overtime3_0 =document.getElementById( \"overtime3_0" . $rowemployee['employeeid'] . "\" ).value;
                         var totalOvertime =document.getElementById( \"totalOvertime" . $rowemployee['employeeid'] . "\" ).value;
                             
                          var formdata = 
                              'payrollid=' + payrollid + 
                              '&employeeid=' + employeeid + 
                              '&overtime1_5=' + overtime1_5 + 
                              '&overtime2_0=' + overtime2_0 + 
                              '&overtime3_0=' + overtime3_0 +
                              '&totalOvertime=' + totalOvertime;

                  
                  // AJAX code to submit form.
                  $.ajax({
                     type: \"POST\",
                     url: \"insertovertimedata.php\", 
                     data: formdata,
                     cache: false,
                     success: function(html) {
                      alert(html);
                     }
                  });

                  return false;
                }
                    
                        </script>";

                    echo "</form>";

                    echo "</tr>";


                  } // FETCHING EMPLOYEE DATA END





       /* } */




  echo "</tbody>";
  echo "</table>";

    // TABLE ENDS HERE


    } // COUNT EMPLOYEES END HERE

} // GET ENDS HERE

include('footer1.php');

?>