<?php
require_once 'class.gspay.php';

$gspay = new GSPay();
$response = [];
$action = $_POST['action'] ?? null;

if ($_SERVER['REQUEST_METHOD'] === 'POST' && $action) {
    switch ($action) {
        case 'generate_idr':
            $tx_id = $_POST['tx_id'] ?: 'TX' . time();
            $username = $_POST['username'] ?: 'user_test';
            $amount = (int)$_POST['amount'];
            $channel = $_POST['channel'] ?: null;
            $response = $gspay->generatePaymentIDR($tx_id, $username, $amount, $channel);
            break;

        case 'check_status_idr':
            $tx_id = $_POST['tx_id'];
            $response = $gspay->getPaymentStatus($tx_id);
            break;

        case 'make_payout':
            $tx_id = $_POST['tx_id'] ?: 'WD' . time();
            $username = $_POST['username'];
            $acc_name = $_POST['acc_name'];
            $acc_number = $_POST['acc_number'];
            $amount = (int)$_POST['amount'];
            $bank = $_POST['bank_target'];
            $desc = $_POST['description'];
            $response = $gspay->makePayout($tx_id, $username, $acc_name, $acc_number, $amount, $bank, $desc);
            break;

        case 'check_status_payout':
            $tx_id = $_POST['tx_id'];
            $response = $gspay->getPayoutStatus($tx_id);
            break;

        case 'check_balance':
            $response = $gspay->getBalance();
            break;

        case 'generate_usdt':
            $tx_id = $_POST['tx_id'] ?: 'USDT' . time();
            $username = $_POST['username'];
            $amount = (float)$_POST['amount'];
            $response = $gspay->generatePaymentUSDT($tx_id, $username, $amount);
            break;
    }
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Test GSPay API Integration</title>
    <style>
        body { font-family: sans-serif; background-color: #1e1e1e; color: #f1f1f1; padding: 20px; }
        .container { max-width: 800px; margin: auto; background-color: #252526; padding: 20px; border-radius: 12px; box-shadow: 0 4px 12px rgba(0,0,0,0.5); }
        h1, h2 { text-align: center; color: #00bcd4; }
        .form-section { background-color: #2d2d2d; padding: 15px; border-radius: 8px; margin-bottom: 20px; border-left: 5px solid #00bcd4; }
        label { display: block; margin: 10px 0 5px; color: #ccc; font-size: 14px; }
        input, select { width: 100%; padding: 10px; border-radius: 5px; border: 1px solid #555; background: #3c3c3c; color: #fff; box-sizing: border-box; }
        button { width: 100%; padding: 12px; margin-top: 15px; border: none; border-radius: 5px; cursor: pointer; font-weight: bold; transition: 0.3s; }
        .btn-blue { background-color: #00bcd4; color: white; }
        .btn-green { background-color: #4CAF50; color: white; }
        .btn-orange { background-color: #ff9800; color: white; }
        .response-box { background-color: #1a1a1a; padding: 15px; border-radius: 5px; overflow-x: auto; border: 1px solid #444; }
        pre { white-space: pre-wrap; word-wrap: break-word; color: #a6e22e; margin: 0; }
        .grid { display: grid; grid-template-columns: 1fr 1fr; gap: 15px; }
    </style>
</head>
<body>

<div class="container">
    <h1>GSPay API Tester</h1>

    <div class="form-section">
        <h2>Operator Balance</h2>
        <form method="POST">
            <input type="hidden" name="action" value="check_balance">
            <button type="submit" class="btn-orange">Check Current Balance</button>
        </form>
    </div>

    <div class="grid">
        <div class="form-section">
            <h2>Deposit IDR</h2>
            <form method="POST">
                <input type="hidden" name="action" value="generate_idr">
                <label>Username</label>
                <input type="text" name="username" placeholder="Username" required>
                <label>Amount (IDR)</label>
                <input type="number" name="amount" placeholder="10000" required>
                <label>Channel (Optional)</label>
                <select name="channel">
                    <option value="">User Selection Page</option>
                    <option value="QRIS">QRIS</option>
                    <option value="DANA">DANA</option>
                </select>
                <button type="submit" class="btn-green">Generate Payment URL</button>
            </form>
        </div>

        <div class="form-section">
            <h2>Check IDR Status</h2>
            <form method="POST">
                <input type="hidden" name="action" value="check_status_idr">
                <label>Transaction ID</label>
                <input type="text" name="tx_id" required>
                <button type="submit" class="btn-blue">Check Status</button>
            </form>
        </div>
    </div>

    <div class="form-section">
        <h2>Withdrawal / Payout</h2>
        <form method="POST">
            <input type="hidden" name="action" value="make_payout">
            <div class="grid">
                <div>
                    <label>Player Username</label>
                    <input type="text" name="username" required>
                    <label>Bank Target (Code)</label>
                    <input type="text" name="bank_target" placeholder="BCA / MANDIRI" required>
                    <label>Amount</label>
                    <input type="number" name="amount" required>
                </div>
                <div>
                    <label>Account Name</label>
                    <input type="text" name="acc_name" required>
                    <label>Account Number</label>
                    <input type="text" name="acc_number" required>
                    <label>Description</label>
                    <input type="text" name="description" placeholder="Optional">
                </div>
            </div>
            <button type="submit" class="btn-green">Process Payout</button>
        </form>
    </div>

    <div class="form-section">
        <h2>Deposit USDT (TRC20)</h2>
        <form method="POST">
            <input type="hidden" name="action" value="generate_usdt">
            <label>Username</label>
            <input type="text" name="username" required>
            <label>Amount (USDT)</label>
            <input type="number" step="0.01" name="amount" placeholder="10.00" required>
            <button type="submit" class="btn-green">Generate USDT Address</button>
        </form>
    </div>

    <?php if ($response): ?>
    <div class="form-section" style="border-left-color: #4CAF50;">
        <h2 style="color: #4CAF50;">API Response</h2>
        <div class="response-box">
            <pre><?= htmlspecialchars(json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)) ?></pre>
        </div>
        <?php if(isset($response['payment_url'])): ?>
            <div style="margin-top: 15px; text-align: center;">
                <a href="<?= $response['payment_url'] ?>" target="_blank" style="color: #00bcd4; font-weight: bold; text-decoration: none; border: 1px solid #00bcd4; padding: 10px; border-radius: 5px;">Buka Link Pembayaran</a>
            </div>
        <?php endif; ?>
    </div>
    <?php endif; ?>

</div>

</body>
</html>