<?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['payrollID'];

  $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.employeename AS employeename,
          employees.employeesurname AS employeesurname,
          departments.departmentname AS departmentname,
          positions.positionname AS positionname,
          bonuspayouts.bonusAmount AS bonusAmount,
          bonuspayouts.bonusAdjustment AS bonusAdjustment,
          bonuspayouts.finalBonusValue AS finalBonusValue,
          bonuspayouts.bonusPayeAmount AS bonusPayeAmount,
          bonuspayouts.employeeCSG AS employeeCSG,
          bonuspayouts.employerCSG AS employerCSG,
          bonuspayouts.employerPRGF AS employerPRGF,
          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\">View 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=\"editBonusValues.php?bonusperiodid=" . $bonusid . "\" class=\"btn btn-sm btn-outline-secondary\" role=\"button\">Edit Bonus</a>";
                echo "<a href=\"payrolls.php\" class=\"btn btn-sm btn-outline-secondary\" role=\"button\">Back</a>";
                echo "<a href=\"#\" class=\"btn btn-sm btn-outline-secondary\" role=\"button\" onclick=\"$('#tabletoexport').tableExport({type:'excel',escape:'false',headings:'true'});\">Export to Excel</a>";
            echo "</div>";


          echo "</div>";

    echo "</div>";



    // TABLE STARTS HERE

  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>Bonus Adjustment</th>";
  echo "<th>Final Bonus</th>";
  echo "<th>PAYE</th>";  
  echo "<th>Employee CSG</th>";
  echo "<th>Net Bonus</th>";
  echo "<th>PRGF</th>";
  echo "<th>Employer CSG</th>";
  
  
  
  echo "</thead>";
  echo "<tbody>";

  while ($rowpaysheet = mysqli_fetch_array($result)) { // FETCHING EMPLOYEE DATA START
    echo "<tr>";
    echo "<td>" . $rowpaysheet['employeesurname'] . ", " . $rowpaysheet['employeename'] . "</td>";
    echo "<td>" . $rowpaysheet['departmentname'] . "</td>";
    echo "<td>" . $rowpaysheet['positionname'] . "</td>";
  
    

    echo "<td>" . $rowpaysheet['bonusAmount'] . "</td>";
    echo "<td>" . $rowpaysheet['bonusAdjustment'] . "</td>";
    echo "<td>" . $rowpaysheet['finalBonusValue'] . "</td>";
    echo "<td>" . $rowpaysheet['bonusPayeAmount'] . "</td>";
    
    
    echo "<td>" . $rowpaysheet['employeeCSG'] . "</td>";

    $netBonus = $rowpaysheet['finalBonusValue'] - $rowpaysheet['bonusPayeAmount'] - $rowpaysheet['employeeCSG'];
    echo "<td>" . $netBonus . "</td>";

    echo "<td>" . $rowpaysheet['employerPRGF'] . "</td>";
    echo "<td>" . $rowpaysheet['employerCSG'] . "</td>";
    
    
    

   
    echo "</tr>";


  } // FETCHING EMPLOYEE DATA END

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

    // TABLE ENDS HERE


    } // COUNT EMPLOYEES END HERE

} // GET ENDS HERE

include('footer1.php');

?>