
<?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_department = mysqli_real_escape_string($db,$_POST['employee_department']);
    $employee_position = mysqli_real_escape_string($db,$_POST['employee_position']);


    $sqlGetEmployeeJob = "
    SELECT *
    FROM employee_job
    WHERE employeeid = $employee_id;";

    $resultEmployeeJob = mysqli_query($db,$sqlGetEmployeeJob);

    $countEmployeeJob = mysqli_num_rows($resultEmployeeJob);

    if ($countEmployeeJob == 0) {

          $sqlAddEmployeeJob = "INSERT INTO employee_job (
      employeeid,
      departmentid,
      positionid,
      latest
      ) VALUES (
      '$employee_id',
      '$employee_department',
      '$employee_position',
      '1');";

    } else {

            $sqlAddEmployeeJob = "
              UPDATE employee_job
              SET departmentid = '$employee_department',
                  positionid = '$employee_position'
              WHERE employeeid = $employee_id;";


    }




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

      $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!!!";
   }
 
}

?>