<?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 deductions
    ORDER BY deductionname 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['deductionid']] = $rowAllowances['deductionname'];
        }

        foreach (array_filter($allowances) as $deductionid => $deductionname) {
          $$deductionid = mysqli_real_escape_string($db,$_POST[$deductionid]);
          $sqlUpdateEmployeeDeduction = "UPDATE employee_deductions 
              SET deductionvalue = " .$$deductionid . "
              WHERE employeeid = $employee_id AND deductionid = $deductionid;";

              mysqli_query($db, $sqlUpdateEmployeeDeduction);
        }

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


       }

    // --- ALLOWANCES END HERE

    
            
          


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

?>