<?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.employeename AS employeename,
          employees.employeesurname AS employeesurname,
          departments.departmentname AS departmentname,
          employees.employee_join_date AS joining_date,
          employees.employee_termination_date AS termination_date,
          positions.positionname AS positionname,
          payrollbasicdetails.netSalary AS netSalary,
          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 payrollbasicdetails
          LEFT JOIN employees
          ON payrollbasicdetails.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 payrollbasicdetails.employeeid = employee_bank.employee_id
          WHERE payrollbasicdetails.payrollid = $payrollid";

  
    $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'];

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

          $sqlViewCompany = "SELECT *
                  FROM companyfile
                  WHERE companyid = $companyID";
                

                  $resultCompany = mysqli_query($db,$sqlViewCompany);

                  $countCompany = mysqli_num_rows($resultCompany);

                  while ($rowCompany = mysqli_fetch_array($resultCompany)) {

                    echo "<h1 class=\"h2\">" . $rowCompany['companyName'] . "</h1>";

                   }

          echo "<h1 class=\"h2\">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>Surname</th>";
  echo "<th>Name</th>";
  echo "<th>Bank Name</th>";
  echo "<th>Bank Account Number</th>";
  echo "<th>Total Amount</th>";
  echo "</thead>";
  echo "<tbody>";

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

    $employeeJoin = $rowpaysheet['joining_date'];
    $employeeTermination = $rowpaysheet['termination_date'];

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

    echo "<tr>";
    echo "<td>" . $rowpaysheet['employeesurname'] . "</td>";
    echo "<td>" . $rowpaysheet['employeename'] . "</td>";
    echo "<td>" . $rowpaysheet['bank_name'] . "</td>";
    echo "<td>" . $rowpaysheet['bank_account'] . "</td>";
    echo "<td>" . number_format($rowpaysheet['netSalary'], 0, '.', ',') . "</td>";

   
    echo "</tr>";


  } // FETCHING EMPLOYEE DATA END

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

    // TABLE ENDS HERE


    } // COUNT EMPLOYEES END HERE

} // GET ENDS HERE

include('footer1.php');

?>