<?php
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;
}
//$pdo = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_password);

// Assuming you have a database connection established
// 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);

function generateTransactionID() {
    $timestamp = time(); // Get current timestamp
    $randomString = bin2hex(random_bytes(4)); // Generate a random string

    return $timestamp . '-' . $randomString;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Retrieve form data
    $user_id = $_POST['user_id'];
    $zone = $_POST['zone'];
    $amount = $_POST['amount'];
    $payment_date = $_POST['payment_date'];
    $payment_method = $_POST['payment_method'];
    $transaction_id = $_POST['transaction_id'];

    // Prepare the SQL statement to update payment details
    $sql = "INSERT INTO payments (user_id, amount, payment_date, payment_method, transaction_id)
            VALUES ('$user_id', '$amount', '$payment_date', '$payment_method', '$transaction_id')";

    // Execute the SQL statement
    if (mysqli_query($connection, $sql)) {
        // Payment confirmed successfully
        // Update the credits for the specified user in the users table

        // Redirect to a success page
        header("Location: allocate_credits.php?zoneID={$user_id}&zone={$zone}");
        exit();
    } else {
        // Error occurred while processing payment confirmation
        $error_message = "Error: " . mysqli_error($connection);
    }
}

// Close the database connection
mysqli_close($connection);
?>
<?php include('../headerspecial.php'); ?>
<h2>Record Payment for Zone</h2> 
<!-- HTML form for payment confirmation -->
<form action="confirm_payment.php" method="POST">
  <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 $zoneID;?>">

    <label for="amount">Amount (Espees):</label>
    <input type="text" name="amount" id="amount" required>
<p>
    <label for="payment_date">Payment Date:</label>
    <input type="date" name="payment_date" id="payment_date" required>
<p>
    <label for="payment_method">Payment Method:</label>
    <select name="payment_method" id="payment_method" required>
        <!-- <option value="credit_card">Credit Card</option>-->
        <option value="bank_transfer">Bank Transfer</option>
      <!--   <option value="paypal">PayPal</option>-->
        <!-- Add other payment method options as needed -->
    </select>
<p>
    <label for="transaction_id">Transaction ID:</label>
    <input type="text" name="transaction_id" id="transaction_id" value="<?php
    $transaction_id = generateTransactionID();
    echo $transaction_id;?>" required readonly>
<p>
    <button type="submit">Confirm Payment</button>
</form>

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