<?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'])) {


    // --- ALLOWANCES START HERE

    $allowances = array();

    $sqlAllowances = "SELECT *
    FROM benefits
    ORDER BY benefitname ASC;";

    $resultAllowances = mysqli_query($db,$sqlAllowances);

    $countAllowances = mysqli_num_rows($resultAllowances);

      if ($countAllowances !== 0) { 

        $employee_id = mysqli_real_escape_string($db,$_POST['employee_id']);

        while ($rowAllowances = mysqli_fetch_array($resultAllowances)) {
          $allowances[$rowAllowances['benefitid']] = $rowAllowances['benefitname'];
        }

        foreach (array_filter($allowances) as $benefitid => $benefitname) {
          $$benefitid = mysqli_real_escape_string($db,$_POST[$benefitid]);
          $sqlUpdateEmployeeBenefits = "UPDATE employee_benefits 
              SET benefitvalue = " .$$benefitid . "
              WHERE employeeid = $employee_id AND benefitid = $benefitid;";

              mysqli_query($db, $sqlUpdateEmployeeBenefits);
        }

        header("location: viewEmployee.php?employeeID=$employee_id");


       }

    // --- ALLOWANCES END HERE

    
            
          


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

?>