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

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

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

    $updated = $_GET['updated'];

  }

  
  $sqlView = "SELECT *
  FROM periods
  WHERE companyid = $companyID
  ORDER BY startDate ASC;";

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

  $count = mysqli_num_rows($result);

  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\">";

  echo "<h1 class=\"h2\">Periods</h1>";

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

      echo "<div class=\"btn-group mr-2\">";

if ($user_role == 1 || $user_role == 2) {

        echo "<a href=\"addPayroll.php\" class=\"btn btn-sm btn-outline-secondary\" role=\"button\">Create Period</a>";#
        echo "<a href=\"generateBonus.php\" class=\"btn btn-sm btn-outline-secondary\" role=\"button\">Bonus Period</a>";

      }
      echo "</div>";

  echo "</div>";

  echo "</div>";

  
  if ($count !== 0) {

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

    echo "<thead>";
    echo "<td>Edit</td>";
    echo "<td>Name</td>";
    echo "<td>Start Date</td>";
    echo "<td>End Date</td>";
    echo "<td>Working Days</td>";
    echo "<td>Status</td>";
    echo "<td>Overtime</td>";
    echo "<td>Sheet</td>";
    echo "<td>Paysheet</td>";
    echo "<td>Payroll</td>";
    echo "<td>Payslips</td>";
    echo "<td>CSG CSV</td>";
    echo "<td>PAYE CSV</td>";
    echo "<td>PRGF CSV</td>";
    echo "<td>PACO CSV</td>";
    echo "</thead>";
    echo "<tbody>";

   while ($rowperiods = mysqli_fetch_array($result)) {


      echo "<tr>";

      if ($rowperiods['bonusBoolean'] != '1') {

        if ($rowperiods['status'] == '1'){
            echo "<td><a href = \"editPayrollData.php?payrollID=" . $rowperiods['periodid'] . "\">Edit</a></td>";
          }
            if ($rowperiods['status'] == '2'){
              echo "<td>Locked</td>";
            }

            echo "<td>" . $rowperiods['periodName'] . "</td>";
          echo "<td>" . $rowperiods['startDate'] . "</td>";
          echo "<td>" . $rowperiods['endDate'] . "</td>";
          echo "<td>" . $rowperiods['workingDays'] . "</td>";
        

          if ($rowperiods['status'] == '1'){
            echo "<td>Open</td>";
          }
            if ($rowperiods['status'] == '2'){
              echo "<td>Closed</td>";
            }

          if ($rowperiods['status'] == '1'){
            echo "<td><a href = \"inputOvertimeV2.php?payrollID=" . $rowperiods['periodid'] . "\">Input Overtime</a></td>";
          }
            if ($rowperiods['status'] == '2'){
              echo "<td><i>Locked</i></td>";
            }
          
          
          echo "<td><a href = \"newPayroll.php?payrollID=" . $rowperiods['periodid'] . "\">Calculations Page</a></td>";
          echo "<td><a href = \"viewPaysheet.php?payrollID=" . $rowperiods['periodid'] . "\">View Paysheet</a></td>";
          echo "<td><a href = \"viewPayroll.php?payrollID=" . $rowperiods['periodid'] . "\">View Payroll</a></td>";
          echo "<td><a href = \"viewPayslips.php?payrollID=" . $rowperiods['periodid'] . "\" target = \"_blank\">View Payslips</a></td>";
          echo "<td><a href = \"csgCSV.php?payrollID=" . $rowperiods['periodid'] . "\">Download</a></td>";
          echo "<td><a href = \"payeCSV.php?payrollID=" . $rowperiods['periodid'] . "\">Download</a></td>";
          echo "<td><a href = \"prgfCSV.php?payrollID=" . $rowperiods['periodid'] . "\">Download</a></td>";
          echo "<td><a href = \"pacoCSV.php?payrollID=" . $rowperiods['periodid'] . "\">Download</a></td>";


        
      } else {

        echo "<td><b></b></td>";
        echo "<td><b>Bonus Payroll</b></td>";
      echo "<td>" . $rowperiods['startDate'] . "</td>";
      echo "<td>" . $rowperiods['endDate'] . "</td>";
      echo "<td></td>";
      echo "<td></td>";
      echo "<td></td>";
    


      
      
     
      echo "<td><a href = \"viewBonusCalculations.php?payrollID=" . $rowperiods['periodid'] . "\">View Calculation</a></td>";
      echo "<td><a href = \"viewBonusPaysheet.php?payrollID=" . $rowperiods['periodid'] . "\">View Paysheet</a></td>";
      echo "<td><a href = \"viewBonusPayroll.php?payrollID=" . $rowperiods['periodid'] . "\">View Payroll</a></td>";
      echo "<td><a href = \"viewBonusPayslips.php?payrollID=" . $rowperiods['periodid'] . "\" target = \"_blank\">View Payslips</a></td>";
      echo "<td><a href = \"csgCSVBonus.php?payrollID=" . $rowperiods['periodid'] . "\">Download</a></td>";
      echo "<td><a href = \"payeCSVBonus.php?payrollID=" . $rowperiods['periodid'] . "\">Download</a></td>";
      echo "<td></td>";



      }


      
      echo "</tr>";
        }

        echo "</tbody>";
        echo "</table>";
  
   } else if ($count == 0) {

      
      echo "No periods on Record..";

   }




include('footer1.php');