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

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

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

  $bonusid = $_GET['bonusperiodid'];

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

  $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_edf_amount AS edfamount,
          employees.employee_paye_rate AS payerate,
          employees.edf_months_value AS edfmonths,
          departments.departmentname AS departmentname,
          positions.positionname AS positionname,
          bonuspayouts.bonusid AS bonusRecordID,
          bonuspayouts.bonusAmount AS bonusAmount,
          bonuspayouts.bonusAdjustment AS bonusAdjustment,
          bonuspayouts.finalBonusValue AS finalBonusValue,
          bonuspayouts.bonusPayeAmount AS bonusPayeAmount,
          employee_bank.employee_bank_name AS bank_name,
          employee_bank.employee_bank_code AS bank_code,
          employee_bank.employee_bank_account AS bank_account
          FROM bonuspayouts
          LEFT JOIN employees
          ON bonuspayouts.employeeid = employees.employeeid
          LEFT JOIN employee_job
          ON employee_job.employeeid = employees.employeeid
          LEFT JOIN positions
          ON employee_job.positionid = positions.positionid
          LEFT JOIN departments
          ON employee_job.departmentid = departments.departmentid
          LEFT JOIN employee_bank
          ON bonuspayouts.employeeid = employee_bank.employee_id
          WHERE bonuspayouts.bonusPayrollid = $bonusid";

  
    $result = mysqli_query($db,$sqlView);

    $count = mysqli_num_rows($result);

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

    echo "<script type=\"text/javascript\" src=\"js/tableExport.js\"></script>";
    echo "<script type=\"text/javascript\" src=\"js/jquery.base64.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\">Edit Bonus Payroll - " . $rowperiods['startDate'] . " - " . $rowperiods['endDate'] . "</h1>";
      }

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

            echo "<div class=\"btn-group mr-2\">";
                echo "<a href=\"viewBonusPayroll.php?payrollID=" . $bonusid . "\" class=\"btn btn-sm btn-outline-secondary\" role=\"button\">Back to Payroll</a>";
            echo "</div>";


          echo "</div>";

    echo "</div>";



    // TABLE STARTS HERE

    echo "<form name=\"add_name\" id=\"add_name\" method=\"POST\" action=\"editBonusValues1.php\">";

    echo "<input type = \"hidden\" value = \"" . $bonusid . "\" name = \"bonuspayrollid\">";

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

  echo "<thead>";
  echo "<th>Employee Name</th>";
  echo "<th>Department</th>";
  echo "<th>Position</th>";
  echo "<th>Bonus Amount</th>";
  echo "<th>Adjustments</th>";
  echo "<th>Final Bonus Value</th>";
  echo "</thead>";
  echo "<tbody>";


  $i = 0;
  while ($rowpaysheet = mysqli_fetch_array($result)) { // FETCHING EMPLOYEE DATA START

    
    
    echo "<tr>";
    echo "<input type = \"hidden\" value = \"" . $rowpaysheet['employeeid'] . "\" name = \"employeeid[]\">";

    echo "<input type = \"hidden\" value = \"" . $rowpaysheet['edfamount'] . "\" name = \"edfamount[]\">";
    echo "<input type = \"hidden\" value = \"" . $rowpaysheet['payerate'] . "\" name = \"payerate[]\">";
    echo "<input type = \"hidden\" value = \"" . $rowpaysheet['edfmonths'] . "\" name = \"edfmonths[]\">";

    echo "<input type = \"hidden\" value = \"" . $rowpaysheet['bonusRecordID'] . "\" name = \"bonusrecordid[]\">";
    echo "<td>" . $rowpaysheet['employeesurname'] . ", " . $rowpaysheet['employeename'] . "</td>";
    echo "<td>" . $rowpaysheet['departmentname'] . "</td>";
    echo "<td>" . $rowpaysheet['positionname'] . "</td>";
    
    echo "<td>";
        echo "<input type=\"number\" name=\"bonus_value[]\" id = \"bonus_value". $i ."\" class=\"form-control\" value = \"" . $rowpaysheet['bonusAmount'] . "\">";
    echo "</td>";

    if ($rowpaysheet['bonusAdjustment'] == 0 || $rowpaysheet['bonusAdjustment'] == null) {
      echo "<td>";
        echo "<input type=\"number\" name=\"bonus_adjustment[]\" id = \"bonus_adjustment". $i ."\" class=\"form-control\" value = \"0\">";
    echo "</td>";
    } else {
      echo "<td>";
        echo "<input type=\"number\" name=\"bonus_adjustment[]\" id = \"bonus_adjustment". $i ."\" class=\"form-control\" value = \"" . $rowpaysheet['bonusAdjustment'] . "\">";
    echo "</td>";
    }

    echo "<td>";
        echo "<input type=\"number\" name=\"adjustedBonusValue[]\" id = \"adjustedBonusValue". $i ."\" class=\"form-control\" value = \"" . $rowpaysheet['finalBonusValue'] . "\" readonly>";
    echo "</td>";


   
    echo "</tr>";

    echo "<script>
              $('#bonus_adjustment" . $i . "').change(function() {
              var total = 0;
              totalBonus = Number($('#bonus_value" . $i . "').val()) + Number($('#bonus_adjustment" . $i . "').val());
              
      
              $('#adjustedBonusValue" . $i . "').attr('value', totalBonus)
              
          });
              </script>";

    $i++;
  } // FETCHING EMPLOYEE DATA END

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

  echo "<p><input type=\"submit\" class=\"btn btn-success\" name=\"submit\" id=\"submit\" value=\"Submit\"></p> </form>";

    // TABLE ENDS HERE


    } // COUNT EMPLOYEES END HERE

} // GET ENDS HERE

include('footer1.php');

?>