<?php
   include('session.php');
   include('connect.php');
   include('logging.php');

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

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

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

    $employee_id = mysqli_real_escape_string($db,$_POST['employee_id']);
    $employee_fileno = mysqli_real_escape_string($db,$_POST['employee_fileno']);
    $employee_surname = mysqli_real_escape_string($db,$_POST['employee_surname']);
    $employee_maiden_name = mysqli_real_escape_string($db,$_POST['employee_maiden_name']);
    $employee_name = mysqli_real_escape_string($db,$_POST['employee_name']);
    $employee_nic = mysqli_real_escape_string($db,$_POST['employee_nic']);
    $employee_citizenship = mysqli_real_escape_string($db,$_POST['employee_citizenship']);
    $employee_dob = mysqli_real_escape_string($db,$_POST['employee_dob']);
    $employee_gender = mysqli_real_escape_string($db,$_POST['employee_gender']);
    $employee_joining_date = mysqli_real_escape_string($db,$_POST['employee_joining_date']);
    $employee_termination_date = mysqli_real_escape_string($db,$_POST['employee_termination_date']);
    $employee_tan = mysqli_real_escape_string($db,$_POST['employee_tan']);
    $employee_paye_rate = mysqli_real_escape_string($db,$_POST['employee_paye_rate']);
    $employee_edf_amount = mysqli_real_escape_string($db,$_POST['employee_edf_amount']);
    $employee_edf_months = mysqli_real_escape_string($db,$_POST['employee_edf_months']);
    $employee_contributioncode = mysqli_real_escape_string($db,$_POST['employee_contributioncode']);
    $employee_fulltime = mysqli_real_escape_string($db,$_POST['employee_fulltime']);
    $employee_pension = mysqli_real_escape_string($db,$_POST['employee_pension']);

    if ($employee_termination_date == "") {
      $sqlUpdateEmployee = "UPDATE employees
      SET employeefileno = '$employee_fileno',
      employeesurname = '$employee_surname',
      employeemaidenname = '$employee_maiden_name',
      employeename = '$employee_name',
      employeenic = '$employee_nic',
      employee_citizen_boolean = '$employee_citizenship',
      employeedob = '$employee_dob',
      employeegender = '$employee_gender',
      employee_join_date = '$employee_joining_date',
      employee_tan_number = '$employee_tan',
      employee_paye_rate = '$employee_paye_rate',
      employee_edf_amount = '$employee_edf_amount',
      edf_months_value = '$employee_edf_months',
      employee_contribution_code = '$employee_contributioncode',
      employee_fulltime_boolean = '$employee_fulltime',
      employee_pension_boolean = '$employee_pension'
      WHERE employeeid = $employee_id;";
    } else {
      $sqlUpdateEmployee = "UPDATE employees
      SET employeefileno = '$employee_fileno',
      employeesurname = '$employee_surname',
      employeemaidenname = '$employee_maiden_name',
      employeename = '$employee_name',
      employeenic = '$employee_nic',
      employee_citizen_boolean = '$employee_citizenship',
      employeedob = '$employee_dob',
      employeegender = '$employee_gender',
      employee_join_date = '$employee_joining_date',
      employee_termination_date = '$employee_termination_date',
      employee_tan_number = '$employee_tan',
      employee_paye_rate = '$employee_paye_rate',
      employee_edf_amount = '$employee_edf_amount',
      edf_months_value = '$employee_edf_months',
      employee_contribution_code = '$employee_contributioncode',
      employee_fulltime_boolean = '$employee_fulltime',
      employee_pension_boolean = '$employee_pension'
      WHERE employeeid = $employee_id;";
    }

    

    if(mysqli_query($db, $sqlUpdateEmployee)) {

      $employeeUpdate = "User updated employee " . $employee_surname . " " . $employee_name;

      logEvent($db,"Employee Updated",$employeeUpdate,$employee_id);
      
      header("location: viewEmployee.php?employeeID=$employee_id");
      } else {
         echo "Error: " . mysqli_error($db);
    }


   } else {
    echo "Employee not selected!!!";
   }

    
 
}

?>