<?php
require_once 'includes/db_connect.php';
if (!isset($_SESSION["user_id"]) || !isset($_POST['amount']) || (float)$_POST['amount'] <= 0) { header("location: add_balance.php"); exit; }
$amount = (float)$_POST['amount'];
$user_id = $_SESSION['user_id'];

if (isset($_POST['action']) && $_POST['action'] == 'confirm') {
    $conn->begin_transaction();
    try {
        $conn->query("UPDATE users SET balance = balance + $amount WHERE id = $user_id");
        $desc = "Auto balance added via bKash Gateway (Simulated)";
        $conn->query("INSERT INTO transactions (user_id, type, amount, description) VALUES ($user_id, 'Add Balance', $amount, '$desc')");
        $conn->commit();
        header("Location: payment_success.php?amount=" . $amount);
        exit;
    } catch (Exception $e) { $conn->rollback(); die("Something went wrong."); }
}
?>
<!DOCTYPE html><html lang="bn"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Confirm Payment</title><link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet"></head>
<body class="bg-light d-flex align-items-center justify-content-center" style="height: 100vh;"><div class="card text-center" style="width: 350px;">
    <div class="card-body p-4">
        <img src="https://i.ibb.co/d2W3n5R/bkash.png" width="80" class="mb-3">
        <h4>bKash Payment Simulation</h4>
        <p class="text-muted">বাস্তব জগতে এই পেজটি বিকাশের নিজস্ব পেমেন্ট পেজ হবে।</p>
        <p class="fs-4"><strong>পরিমাণ: ৳<?php echo number_format($amount, 2); ?></strong></p>
        <form method="POST">
            <input type="hidden" name="amount" value="<?php echo $amount; ?>">
            <input type="hidden" name="action" value="confirm">
            <div class="d-grid"><button type="submit" class="btn btn-danger btn-lg">পেমেন্ট সম্পন্ন করতে এখানে ক্লিক করুন (সিমুলেশন)</button></div>
        </form>
        <a href="add_balance.php" class="btn btn-link mt-2">বাতিল করুন</a>
    </div>
</div></body></html>