<?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.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\">View Bonus Paysheet - " . $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=\"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>Bank Name</th>";
  echo "<th>Bank Code</th>";
  echo "<th>Bank Account Number</th>";
  echo "<th>PAYE</th>";
  echo "<th>Bonus Amount</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['bank_name'] . "</td>";
    echo "<td>" . $rowpaysheet['bank_code'] . "</td>";
    echo "<td>" . $rowpaysheet['bank_account'] . "</td>";

    echo "<td>" . number_format($rowpaysheet['bonusPayeAmount'], 2, '.', ',') . "</td>";

    if ($rowpaysheet['finalBonusValue'] == 0 || $rowpaysheet['finalBonusValue'] == null) {
      echo "<td>" . number_format($rowpaysheet['bonusAmount'], 2, '.', ',') . "</td>";
    } else {
      echo "<td>" . number_format($rowpaysheet['finalBonusValue'], 2, '.', ',') . "</td>";
    }

    

   
    echo "</tr>";


  } // FETCHING EMPLOYEE DATA END

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

    // TABLE ENDS HERE


    } // COUNT EMPLOYEES END HERE

} // GET ENDS HERE

include('footer1.php');

?>