<?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_basic_salary = mysqli_real_escape_string($db,$_POST['employee_basic_salary']);
    $employee_travelling = mysqli_real_escape_string($db,$_POST['employee_travelling']);

    $checkEntry = "SELECT * FROM travellings WHERE employeeid = $employee_id;";

    $resultCheck = mysqli_query($db,$checkEntry);

    $resultCheck = mysqli_num_rows($resultCheck);

    if ($resultCheck == 0) {
      $sqlUpdateEmployeeTravelling = "INSERT INTO travellings (
      employeeid,
      travellingvalue,
      latest
      ) VALUES (
      '$employee_id',
      '$employee_travelling',
      '1');";

    } else {
      $sqlUpdateEmployeeTravelling = "UPDATE travellings
      SET travellingvalue = '$employee_travelling'
      WHERE employeeid = $employee_id;";
    }

    $sqlUpdateEmployeeJob = "UPDATE salaries
    SET salaryvalue = '$employee_basic_salary'
    WHERE employeeid = $employee_id;";

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

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

      $employeeUpdate = "User updated employee ";

      logEvent($db,"Employee updated",$employeeUpdate,$last_id);
      
      header("location: viewEmployee.php?employeeID=$employee_id");

        }
      } else {
         echo "Error: " . mysqli_error($db);
    }

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

?>