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

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

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

  $employeeid = $_GET['employeeID'];

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

    $updated = $_GET['updated'];

  }

  // sql to view a customer
  $sqlView = "SELECT *
  FROM employees
  WHERE employeeid = " . $employeeid .";";

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

  if ($count !== 0) {

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

    echo "<h1 class=\"h2\">" . $rowemployee['employeesurname'] . ", " . $rowemployee['employeename'] . "</h1>";
    echo "<div class=\"btn-toolbar mb-2 mb-md-0\">";

      echo "<div class=\"btn-group mr-2\">";
        echo "<a href=\"editEmployee.php?employeeID=" . $rowemployee['employeeid'] . "\" class=\"btn btn-sm btn-outline-secondary\" role=\"button\">Edit Employee</a>";
        echo "<a href=\"employees.php\" class=\"btn btn-sm btn-outline-secondary\" role=\"button\">Back to Listing</a>";
      echo "</div>";

    echo "</div>";

    echo "</div>";

    echo "<div class=\"row\">";
      echo "<div class=\"col\">";
        echo "<div class=\"card\">" ;
          echo "<div class=\"card-body\">";
            echo "<h5 class=\"card-title\">Information</h5>";
            echo "<p class=\"card-text\">";
              echo "<table class = \"table\">";
                echo "<tr><td><b>File No</b></td><td>" . $rowemployee['employeefileno'] . "</td><td><b>Joining Date</b></td><td>" . $rowemployee['employee_join_date'] . "</td></tr>";
                echo "<tr><td><b>NIC</b></td><td>" . $rowemployee['employeenic'] . "</td><td><b>EDF Amount</b></td><td>" . $rowemployee['employee_edf_amount'] . " - Months : " . $rowemployee['edf_months_value'] . "</td></tr>";
                echo "<tr><td><b>Gender</b></td><td>" . $rowemployee['employeegender'] . "</td><td><b>TAN Number</b></td><td>" . $rowemployee['employee_tan_number'] . "</td></tr>";
                echo "<tr><td><b>DOB</b></td><td>" . $rowemployee['employeedob'] . "</td><td><b>PAYE Rate</b></td><td>" . $rowemployee['employee_paye_rate'] . "</td></tr>";
              echo "</table>";
            echo "</p>";
          echo "</div>";
        echo "</div>";
      echo "</div>";
    echo "</div>";

    echo "<br>";

    echo "<div class=\"row\">";
      echo "<div class=\"col\">";

        echo "<div class=\"card\">" ;
        echo "<div class=\"card-body\">";
        echo "<h5 class=\"card-title\">Personal Details";
        echo "</h5>";
        echo "<p class=\"card-text\">";

          $sqlPersonalView = "SELECT departments.departmentname AS dName, 
          positions.positionname AS pName
          FROM employee_job
          INNER JOIN departments
          ON employee_job.departmentid = departments.departmentid
          INNER JOIN positions
          ON employee_job.positionid = positions.positionid
          WHERE employee_job.employeeid = " . $rowemployee['employeeid'] . "
          AND employee_job.latest = 1";

          $resultPersonal = mysqli_query($db,$sqlPersonalView);

          $countPersonal = mysqli_num_rows($resultPersonal);

          if ($countPersonal !== 0) {

            while ($rowPersonal = mysqli_fetch_array($resultPersonal)) {

              echo "Department: " . $rowPersonal['dName'] . "<br>";
              echo "Job Position: " . $rowPersonal['pName'] ;

            }
          } else {
            echo "No data.";
          }


        echo "</p>";
        
        
        if ($countPersonal !== 0) {echo "<a href=\"editPersonalDetail.php?employeeID=" . $rowemployee['employeeid'] . "\" >Edit</a>";}
        else {echo "<a href=\"addPersonalDetail.php?employeeID=" . $rowemployee['employeeid'] . "\" >Add</a>";}
        echo "</div>";
        echo "</div>";

      echo "</div>";
      echo "<div class=\"col\">";

        echo "<div class=\"card\">" ;
        echo "<div class=\"card-body\">";
        echo "<h5 class=\"card-title\">Payroll Details";
        echo "</h5>";
        echo "<p class=\"card-text\">";

          $sqlPayrollView = "SELECT salaryvalue AS salary
          FROM salaries
          WHERE employeeid = " . $rowemployee['employeeid'] . "
          AND latest = 1";

          $sqlTravellingView = "SELECT travellingvalue AS travelling
          FROM travellings
          WHERE employeeid = " . $rowemployee['employeeid'] . "
          AND latest = 1";

          $resultPayroll = mysqli_query($db,$sqlPayrollView);

          $countPayroll = mysqli_num_rows($resultPayroll);

          if ($countPayroll !== 0) {

            while ($rowPayroll = mysqli_fetch_array($resultPayroll)) {

              echo "Basic Salary: " . $rowPayroll['salary'] . "<br>";

              $sqlTravellingView = "SELECT travellingvalue AS travelling
              FROM travellings
              WHERE employeeid = " . $rowemployee['employeeid'] . "
              AND latest = 1";

              $resultTravelling = mysqli_query($db,$sqlTravellingView);

              $countTravelling = mysqli_num_rows($resultTravelling);

              if ($countTravelling !== 0) {

                while ($rowTravelling = mysqli_fetch_array($resultTravelling)) {

                  echo "Bus Fare: " . $rowTravelling['travelling'] . " /day<br>";

                }
              }

            }
          } else {
            echo "No data.";
          }

        echo "</p>";
        if ($countPayroll !== 0) {echo "<a href=\"editPayrollDetail.php?employeeID=" . $rowemployee['employeeid'] . "\" >Edit</a>";}
        else {echo "<a href=\"addPayrollDetail.php?employeeID=" . $rowemployee['employeeid'] . "\" >Add</a>";}
        echo "</div>";
        echo "</div>";

      echo "</div>";
      echo "<div class=\"col\">";

        echo "<div class=\"card\">" ;
        echo "<div class=\"card-body\">";
        echo "<h5 class=\"card-title\">Benefits";
        echo "</h5>";
        echo "<p class=\"card-text\">";

          $sqlBenefitsView = "SELECT benefits.benefitname AS bName, 
          employee_benefits.benefitvalue AS bValue
          FROM employee_benefits
          INNER JOIN benefits
          ON employee_benefits.benefitid = benefits.benefitid
          WHERE employee_benefits.employeeid = " . $rowemployee['employeeid'] . "
          AND employee_benefits.latest = 1";

          $resultBenefit = mysqli_query($db,$sqlBenefitsView);

          $countBenefit = mysqli_num_rows($resultBenefit);

          if ($countBenefit !== 0) {

            while ($rowBenefit = mysqli_fetch_array($resultBenefit)) {

              echo $rowBenefit['bName'] . ": " . $rowBenefit['bValue'] . "<br>";

            }
          } else {
            echo "No benefits.";
          }

                   $sqlGetCarBenefit = "SELECT tiervalue
          FROM carbenefits
          WHERE carbenefitid = " . $rowemployee['employee_carbenefit'] . ";";

          $resultGetCarBenefit = mysqli_query($db,$sqlGetCarBenefit);

          $countCarBenefit = mysqli_num_rows($resultGetCarBenefit);

          if ($countCarBenefit !== 0) {

            while ($rowCarBenefit = mysqli_fetch_array($resultGetCarBenefit)) {

              echo "Car Benefit: " . $rowCarBenefit['tiervalue'] . "<br>";

            }
          } else {
            echo "No data available...";
          }

        echo "</p>";
        if ($countBenefit !== 0) {echo "<a href=\"editBenefitDetail.php?employeeID=" . $rowemployee['employeeid'] . "\" >Edit Benefits</a>";}
        else {echo "<a href=\"addBenefitDetail.php?employeeID=" . $rowemployee['employeeid'] . "\" >Add Benefits</a>";}
        echo "<br><a href=\"addCarBenefit.php?employeeID=" . $rowemployee['employeeid'] . "\" >Edit Car Benefit</a>";
        echo "</div>";
        echo "</div>";

      echo "</div>";
      echo "<div class=\"col\">";

        echo "<div class=\"card\">" ;
        echo "<div class=\"card-body\">";
        echo "<h5 class=\"card-title\">Deductions";
        echo "</h5>";
        echo "<p class=\"card-text\">";

          $sqlDeductionsView = "SELECT deductions.deductionname AS dName, 
          employee_deductions.deductionvalue AS dValue
          FROM employee_deductions
          INNER JOIN deductions
          ON employee_deductions.deductionid = deductions.deductionid
          WHERE employee_deductions.employeeid = " . $rowemployee['employeeid'] . "
          AND employee_deductions.latest = 1";

          $resultDeduction = mysqli_query($db,$sqlDeductionsView);

          $countDeduction = mysqli_num_rows($resultDeduction);

          if ($countDeduction !== 0) {

            while ($rowDeduction = mysqli_fetch_array($resultDeduction)) {

              echo $rowDeduction['dName'] . ": " . $rowDeduction['dValue'] . "<br>";

            }
          } else {
            echo "No deductions.";
          }



        echo "</p>";
        if ($countDeduction !== 0) {echo "<a href=\"editDeductionDetail.php?employeeID=" . $rowemployee['employeeid'] . "\" >Edit Deductions</a>";}
        else {echo "<a href=\"addDeductionDetail.php?employeeID=" . $rowemployee['employeeid'] . "\" >Add Deductions</a>";}
        echo "</div>";
        echo "</div>";

      echo "</div>";
      echo "<div class=\"col\">";

        echo "<div class=\"card\">" ;
        echo "<div class=\"card-body\">";
        echo "<h5 class=\"card-title\">Bank";
        echo "</h5>";
        echo "<p class=\"card-text\">";

          $sqlBanksView = "SELECT * FROM employee_bank WHERE employee_id = " . $rowemployee['employeeid'] . ";";

          $resultBank = mysqli_query($db,$sqlBanksView);

          $countBank = mysqli_num_rows($resultBank);

          if ($countBank !== 0) {

            while ($rowBank = mysqli_fetch_array($resultBank)) {

              echo "Bank: " . $rowBank['employee_bank_name'] . "(" . $rowBank['employee_bank_code'] . ")<br>";
              echo "Account: " . $rowBank['employee_bank_account'];

            }
          } else {
            echo "No banks registered.";
          }


        echo "</p>";
        if ($countBank !== 0) {echo "<a href=\"editBankDetail.php?employeeID=" . $rowemployee['employeeid'] . "\" >Edit</a>";}
        else {echo "<a href=\"addBankDetail.php?employeeID=" . $rowemployee['employeeid'] . "\" >Add</a>";}
        echo "</div>";
        echo "</div>";

      echo "</div>";


    echo "</div>";

    


    }
   
   } else if ($count == 0) {

      echo "<p>Employee queried not found..";

   }

} else {
  echo "Error ..";
}


include('footer1.php');