<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

// ✅ Load config
$configPath = $_SERVER['DOCUMENT_ROOT'] . "/pay/config.php";
if (!file_exists($configPath)) {
    die("❌ Config file not found.");
}

$config = include($configPath);

// ✅ Database connection
$conn = new mysqli($config['db_host'], $config['db_user'], $config['db_pass'], $config['db_name']);
if ($conn->connect_error) {
    die("❌ Database connection failed: " . $conn->connect_error);
}

// ✅ Process POST request
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $order_no = isset($_POST['order_no']) ? trim($_POST['order_no']) : '';
    $utr_no = isset($_POST['utr_no']) ? trim($_POST['utr_no']) : '';

    if (empty($order_no) || empty($utr_no)) {
        die("⚠️ Order number or UTR number is missing.");
    }

    // ✅ Prepare query
    $query = "UPDATE transactions SET utr_number = ?, status = 'pending' WHERE order_no = ?";
    $stmt = $conn->prepare($query);

    if (!$stmt) {
        die("❌ Prepare failed: " . $conn->error);
    }

    $stmt->bind_param("ss", $utr_no, $order_no);

    if ($stmt->execute()) {
        // ✅ Redirect to Thank You page
        header("Location: thankyou.php?order=$order_no&utr=$utr_no");
        exit();
    } else {
        echo "<h3>❌ Error submitting UTR: " . $stmt->error . "</h3>";
    }

    $stmt->close();
    $conn->close();
} else {
    echo "❌ Invalid request method.";
}
?>
