<?php
include('../config/dbcon.php');
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
   // Get the POST data
   $type = $_POST['type'];
   $month = $_POST['month'];
   $newValue = $_POST['value'];  // This value is already in rupees

   // Sanitize and validate input (the value is in rupees, not lakhs)
   $newValue = floatval($newValue);

   // Determine the appropriate SQL query based on the type of data being updated
   if ($type == 'salary_actuals') {
      $updateQuery = "UPDATE salaries SET cum_salary = '$newValue' WHERE MONTHNAME(month) = '$month'";
   } elseif ($type == 'salary_budgeted') {
      $updateQuery = "UPDATE salaries SET budget_salary = '$newValue' WHERE MONTHNAME(month) = '$month'";
   } elseif ($type == 'fixed_actuals') {
      $updateQuery = "UPDATE fixed_cost SET cum_fixed_cost = '$newValue' WHERE MONTHNAME(month) = '$month'";
   } elseif ($type == 'fixed_budgeted') {
      $updateQuery = "UPDATE fixed_cost SET budget_fixed_cost = '$newValue' WHERE MONTHNAME(month) = '$month'";
   }

   // Execute the query
   if (mysqli_query($conn, $updateQuery)) {
      echo 'success';
   } else {
      echo 'error';
   }
}
?>
