<?php
// Assuming you have a database connection established
require_once '../config.php';
session_start();
//echo $db_host;
// Check if the user is logged in as a system admin
$zoneID = $_GET['id'];

if (!isset($_SESSION['userType']) || $_SESSION['userType'] !== 'admin') {
    header('Location: ../indexX.php');
    exit;
}

// Replace DB_HOST, DB_USER, DB_PASSWORD, and DB_NAME with your actual database credentials
$connection = mysqli_connect($db_host, $db_user, $db_password, $db_name);

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Retrieve form data
    $user_id = $_POST['user_id'];
    $credits = $_POST['credits'];

    // Update the credits for the specified user in the users table
    //$sql = "UPDATE users SET credits = '$credits' WHERE id = '$user_id'";
    $sql = "UPDATE users SET credits = credits + '$credits' WHERE id = '$user_id'";

    if (mysqli_query($connection, $sql)) {
        // Credits allocated successfully
        // Redirect to a success page
        header("Location: credits_allocated.php");
        exit();
    } else {
        // Error occurred while allocating credits
        $error_message = "Error: " . mysqli_error($connection);
    }
}

// Close the database connection
mysqli_close($connection);
?>
<?php include('../headerspecial.php'); ?>

<!-- HTML form for credit allocation -->
<form action="allocate_credits.php" method="POST">
  <?php
    $userID = $_GET['zoneID'];
    ?>
    <label for="zone">Zone:</label>
    <input type="text" name="zone" id="zone" value="<?php echo $_GET['zone'];?>" required readonly>
  <p>
    <input type="hidden" name="user_id" value="<?php echo $userID;?>">

    <label for="credits">Credits:</label>
    <input type="text" name="credits" id="credits" required>

    <button type="submit">Allocate Credits</button>
</form>

<?php
// Display error message, if any
if (isset($error_message)) {
    echo '<p>' . $error_message . '</p>';
}
?>
