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

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

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

    $startdate=$_POST['startdate'];
    $enddate=$_POST['enddate'];

                   $getPeriodCounts = "SELECT periodid FROM
                  periods
                  WHERE bonusBoolean = 0
                  AND startDate BETWEEN '$startdate' AND '$enddate' ";

                  $resultPeriodCounts = mysqli_query($db,$getPeriodCounts);

                  $countPeriods = mysqli_num_rows($resultPeriodCounts);


          if(isset($_POST['getallemployees'])) {

            $getallemployees=$_POST['getallemployees'];
          }

          $getemployeeid=$_POST['getemployeeid'];


          $sqlView = "SELECT 
                  employees.employeename AS employeename,
                  employees.employeesurname AS employeesurname,
                  employees.employeeid AS employeeid,
                  departments.departmentname AS departmentname,
                  positions.positionname AS positionname,
                  sum(payrollbasicdetails.s_taken) AS sickTaken,
                  sum(payrollbasicdetails.l_taken) AS localTaken
                  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
                  RIGHT JOIN periods
                  ON payrollbasicdetails.payrollid = periods.periodid                  
                  WHERE periods.startDate BETWEEN '$startdate' AND '$enddate'
                  AND periods.bonusBoolean = 0
                  AND employees.companyid = $companyID";


                  if(isset($getallemployees)) {
                    $sqlView = $sqlView . " GROUP BY employees.employeeid";
                  } else {
                      $sqlAddonEmployee = " AND payrollbasicdetails.employeeid = '$getemployeeid';";
                      $sqlView = $sqlView . $sqlAddonEmployee;

                  }

                  

                  
                  

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

                  $count = mysqli_num_rows($result);

                  echo "<style type=\"text/css\" media=\"print\">
          @page { size: landscape; }
        </style>";

                  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 START HERE
              
                      $sqlViewCompany = "SELECT *
                  FROM companyfile
                  WHERE companyid = $companyID";
                

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

                  $countCompany = mysqli_num_rows($resultCompany);

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

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

                   }




                  echo "<h2 class=\"h3\">Leaves Report - " . $startdate . " - " . $enddate . "</h2>";

                  if (!isset($getallemployees)) {
                    echo " Single Employee ";
                  }

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

                  echo "</div>";

            echo "</div>";



            // TABLE view STARTS HERE

          echo "<table class = \"sortialler table\" border=\"1\">";

          echo "<thead>";
          echo "<th>Employee Name</th>";
          echo "<th>Department</th>";
          echo "<th>Position</th>";
          echo "<th>Sick Leaves Taken</th>";
          echo "<th>Sick Leaves Balance</th>";
          echo "<th>Local Leaves Taken</th>";
          echo "<th>Local Leaves Balance</th>";
          echo "</thead>";
          echo "<tbody>";


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

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

            $sickBalance = 15 - intval($rowpaysheet['sickTaken']);
            echo "<td>" . number_format($rowpaysheet['sickTaken'], 0, '.', ',') . "</td>";
            echo "<td>" . $sickBalance . "</td>";

            $localBalance = 22 - intval($rowpaysheet['localTaken']);
            echo "<td>" . number_format($rowpaysheet['localTaken'], 0, '.', ',') . "</td>";
            echo "<td>" . $localBalance . "</td>";

           
            echo "</tr>";


          } // FETCHING EMPLOYEE DATA END

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

            // TABLE view ENDS HERE


                      // TABLE 1 STARTS HERE

          echo "<table class = \"sortirester table\" border=\"1\">";

          echo "<th>Employee Name</th>";
          echo "<th>Department</th>";
          echo "<th>Position</th>";
          echo "<th>Sick Leaves Taken</th>";
          echo "<th>Sick Leaves Balance</th>";
          echo "<th>Local Leaves Taken</th>";
          echo "<th>Local Leaves Balance</th>";
          echo "</thead>";
          echo "<tbody>";


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

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

            
            $sickBalance = 15 - intval($rowpaysheet['sickTaken']);
            echo "<td>" . number_format($rowpaysheet['sickTaken'], 0, '.', ',') . "</td>";
            echo "<td>" . $sickBalance . "</td>";

            $localBalance = 22 - intval($rowpaysheet['localTaken']);
            echo "<td>" . number_format($rowpaysheet['localTaken'], 0, '.', ',') . "</td>";
            echo "<td>" . $localBalance . "</td>";

           
            echo "</tr>";


          } // FETCHING EMPLOYEE DATA END
          echo "<tr><td>Page 1</td></tr>";
          echo "</tbody>";
          echo "</table>";

            // TABLE 1 ENDS HERE

          



            } // COUNT EMPLOYEES END HERE


      }  else { ///////////////////

  $sqlGetEmployee = "SELECT * FROM employees WHERE companyid = $companyID";

  $resultEmployee = mysqli_query($db,$sqlGetEmployee);

  $countEmployee = mysqli_num_rows($resultEmployee);

  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\">Leaves Report</h1>";
 
    
    	echo "</div>";

    	echo "<form method = \"POST\" action=\"report-leaves.php\">";

      echo "<p>Start Date:<input type = \"date\" class=\"form-control\" name = \"startdate\"></p>";

      echo "<p>End Date:</p><input type = \"date\" class=\"form-control\" name = \"enddate\"></p>";

      echo "<p>All Employees <input type = \"checkbox\" name = \"getallemployees\"></p>";

      echo "<p>If Single Employee, Choose here:";

  if ($countEmployee !== 0) { 

    echo "<select name=\"getemployeeid\" id=\"getemployeeid\" class=\"form-control\">";

    while ($rowemployee = mysqli_fetch_array($resultEmployee)) {

      echo "<option value ='" . $rowemployee['employeeid'] . "'>" . $rowemployee['employeesurname'] . ", " . $rowemployee['employeename']  . "</option>";

    }

      echo "</select></p>";
} else {
  echo "<p>No employee on system..</p>";
}

    echo "<input type=\"submit\"> </form>";

   }



include('footer1.php');
?>