<?php
header('Content-Type: text/html');
require 'db.php'; // Include your database connection script

// Retrieve user_id from the query parameters
$user_id = isset($_GET['user_id']) ? intval($_GET['user_id']) : 0;

// Check if the user_id is valid
if ($user_id > 0) {
    // Retrieve user information from the database
    $sql = "SELECT name, email, phone FROM users WHERE id = $user_id";
    $result = $conn->query($sql);

    if ($result && $result->num_rows > 0) {
        $user = $result->fetch_assoc();
        ?>
        <!DOCTYPE html>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title>Add Money to Wallet</title>
            <style>
                body {
                    font-family: Arial, sans-serif;
                    display: flex;
                    justify-content: center;
                    align-items: center;
                    height: 100vh;
                    background-color: #f7f7f7;
                }
                .container {
                    background-color: #fff;
                    padding: 20px;
                    border-radius: 10px;
                    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
                    max-width: 400px;
                    width: 100%;
                }
                h2 {
                    margin-bottom: 20px;
                    color: #333;
                }
                label {
                    display: block;
                    margin-bottom: 5px;
                    font-weight: bold;
                }
                input[type="text"], input[type="number"] {
                    width: 100%;
                    padding: 10px;
                    margin-bottom: 20px;
                    border: 1px solid #ccc;
                    border-radius: 5px;
                }
                button {
                    width: 100%;
                    padding: 10px;
                    background-color: #007bff;
                    color: #fff;
                    border: none;
                    border-radius: 5px;
                    font-size: 16px;
                    cursor: pointer;
                }
                button:hover {
                    background-color: #0056b3;
                }
            </style>
        </head>
        <body>
            <div class="">
                <h2>Add Money to Wallet</h2>
                <form action="https://sandbox.payhere.lk/pay/checkout" method="post">
                    <input type="hidden" name="merchant_id" value="YOUR_MERCHANT_ID">    <!-- Replace YOUR_MERCHANT_ID with your actual PayHere merchant ID -->
                    <input type="hidden" name="return_url" value="https://yourwebsite.com/return_url">
                    <input type="hidden" name="cancel_url" value="https://yourwebsite.com/cancel_url">
                    <input type="hidden" name="notify_url" value="https://yourwebsite.com/notify_url">
                    <input type="hidden" name="order_id" value="<?php echo time(); ?>">
                    <input type="hidden" name="items" value="Wallet Recharge">
                    <label for="name">Name</label>
                    <input type="text" id="name" name="first_name" value="<?php echo $user['name']; ?>" readonly>
                    <label for="email">Email</label>
                    <input type="text" id="email" name="email" value="<?php echo $user['email']; ?>" readonly>
                    <label for="phone">Phone</label>
                    <input type="text" id="phone" name="phone" value="<?php echo $user['phone']; ?>" readonly>
                    <label for="amount">Amount</label>
                    <input type="number" id="amount" name="amount" required>
                    <input type="hidden" name="currency" value="LKR">
                    <button type="submit">Proceed to Pay</button>
                </form>
            </div>
        </body>
        </html>
        <?php
    } else {
        echo "Invalid user ID or user not found.";
    }
} else {
    echo "Invalid user ID.";
}

$conn->close();
?>
