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

$redpay = new RedPay();
$response = null;
$action = $_POST['action'] ?? null;

if ($_SERVER['REQUEST_METHOD'] === 'POST' && $action) {
    try {
        switch ($action) {
            case 'qris':
                $response = $redpay->generateQRIS($_POST['amount'], $_POST['player']);
                break;
            case 'dana':
                $response = $redpay->generateDANA($_POST['amount'], $_POST['player']);
                break;
            case 'status':
                $response = $redpay->checkStatus($_POST['txid']);
                break;
            case 'withdraw':
                $response = $redpay->withdraw(
                    $_POST['amount'], 
                    $_POST['player'], 
                    $_POST['bank'], 
                    $_POST['acc_no'], 
                    $_POST['acc_name'], 
                    $_POST['desc']
                );
                break;
        }
    } catch (Exception $e) {
        $response = ['success' => false, 'message' => $e->getMessage()];
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>YERE API Tester</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
    /* Menargetkan semua input dan textarea */
.form-control::placeholder {
    color: #888; /* Ganti dengan warna yang Anda inginkan, misal abu-abu sedang */
    opacity: 1;  /* Penting untuk Firefox agar warna tidak pudar */
}

/* Opsional: Jika ingin warna placeholder berbeda saat input fokus */
.form-control:focus::placeholder {
    color: #666;
}
        :root { --redpay-primary: #f44336; --redpay-dark: #121212; --redpay-card: #1e1e1e; }
        body { background-color: var(--redpay-dark); color: #e0e0e0; font-family: 'Inter', sans-serif; }
        .navbar { background-color: #000; border-bottom: 2px solid var(--redpay-primary); }
        .card { background-color: var(--redpay-card); border: none; border-radius: 12px; transition: transform 0.2s; color: #fff; }
        .card:hover { transform: translateY(-5px); }
        .form-control, .form-select { background-color: #2d2d2d; border: 1px solid #444; color: #fff; }
        .form-control:focus { background-color: #333; color: #fff; border-color: var(--redpay-primary); box-shadow: none; }
        .btn-primary { background-color: var(--redpay-primary); border: none; }
        .btn-primary:hover { background-color: #d32f2f; }
        pre { background-color: #000; padding: 15px; border-radius: 8px; color: #a6e22e; border: 1px solid #333; font-size: 0.85rem; }
        .status-badge { font-size: 0.8rem; padding: 5px 12px; border-radius: 20px; }
        .nav-tabs .nav-link { color: #aaa; border: none; }
        .nav-tabs .nav-link.active { background: none; color: var(--redpay-primary); border-bottom: 3px solid var(--redpay-primary); }
    </style>
</head>
<body>

<nav class="navbar navbar-dark mb-4">
    <div class="container">
        <a class="navbar-brand" href="#"><i class="fa-solid fa-fire-re"></i> YEREPAY <strong>TESTER</strong></a>
        <span class="navbar-text text-uppercase small fw-bold">API Version 1.0</span>
    </div>
</nav>

<div class="container">
    <div class="row">
        <div class="col-lg-7">
            <ul class="nav nav-tabs mb-4" id="myTab" role="tablist">
                <li class="nav-item"><button class="nav-link active" data-bs-toggle="tab" data-bs-target="#tab-deposit">Deposit</button></li>
                <li class="nav-item"><button class="nav-link" data-bs-toggle="tab" data-bs-target="#tab-withdraw">Withdraw</button></li>
                <li class="nav-item"><button class="nav-link" data-bs-toggle="tab" data-bs-target="#tab-status">Check Status</button></li>
            </ul>

            <div class="tab-content">
                <div class="tab-pane fade show active" id="tab-deposit">
                    <div class="row g-3">
                        <div class="col-md-6">
                            <div class="card p-4">
                                <h5 class="mb-3 text-danger"><i class="fa-solid fa-qrcode"></i> QRIS Generate</h5>
                                <form method="POST">
                                    <input type="hidden" name="action" value="qris">
                                    <input type="text" name="player" class="form-control mb-2" placeholder="Player Username" required>
                                    <input type="number" name="amount" class="form-control mb-3" placeholder="Amount (min 10.000)" required>
                                    <button type="submit" class="btn btn-primary w-100">Generate QRIS</button>
                                </form>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="card p-4 border-info">
                                <h5 class="mb-3 text-info"><i class="fa-solid fa-wallet"></i> DANA Generate</h5>
                                <form method="POST">
                                    <input type="hidden" name="action" value="dana">
                                    <input type="text" name="player" class="form-control mb-2" placeholder="Player Username" required>
                                    <input type="number" name="amount" class="form-control mb-3" placeholder="Amount" required>
                                    <button type="submit" class="btn btn-info w-100 text-white">Generate DANA</button>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="tab-pane fade" id="tab-withdraw">
                    <div class="card p-4">
                        <h5 class="mb-3"><i class="fa-solid fa-money-bill-transfer"></i> Local Payout</h5>
                        <form method="POST">
                            <input type="hidden" name="action" value="withdraw">
                            <div class="row g-2">
                                <div class="col-md-6"><input type="text" name="player" class="form-control mb-2" placeholder="Player Username" required></div>
                                <div class="col-md-6"><input type="number" name="amount" class="form-control mb-2" placeholder="Amount" required></div>
                                <div class="col-md-4"><input type="text" name="bank" class="form-control mb-2" placeholder="Bank (BCA/BNI/..)" required></div>
                                <div class="col-md-8"><input type="text" name="acc_no" class="form-control mb-2" placeholder="Account Number" required></div>
                                <div class="col-12"><input type="text" name="acc_name" class="form-control mb-2" placeholder="Account Name" required></div>
                                <div class="col-12"><input type="text" name="desc" class="form-control mb-3" placeholder="Description (Optional)"></div>
                            </div>
                            <button type="submit" class="btn btn-success w-100">Process Withdrawal</button>
                        </form>
                    </div>
                </div>

                <div class="tab-pane fade" id="tab-status">
                    <div class="card p-4">
                        <h5 class="mb-3"><i class="fa-solid fa-magnifying-glass"></i> Transaction Status</h5>
                        <form method="POST">
                            <input type="hidden" name="action" value="status">
                            <label class="small text-secondary">Transaction ID (Format: QR- atau DN-)</label>
                            <input type="text" name="txid" class="form-control mb-3" placeholder="Contoh: QR-ABCDEFGHIJKLMNOP" required>
                            <button type="submit" class="btn btn-warning w-100 fw-bold">Fetch Status</button>
                        </form>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-lg-5 mt-4 mt-lg-0">
            <h5 class="mb-3"><i class="fa-solid fa-terminal"></i> Response Output</h5>
            <div class="card p-3 shadow-lg" style="min-height: 400px; border: 1px solid #333;">
                <?php if ($response): ?>
                    <pre><?php echo json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); ?></pre>
                    
                    <?php if (isset($response['data']['qris_image'])): ?>
                        <div class="text-center mt-3 bg-white p-3 rounded">
                            <img src="<?php echo $response['data']['qris_image']; ?>" class="img-fluid" style="max-width: 200px;">
                            <div class="text-dark small mt-2 fw-bold"><?php echo $response['data']['transaction_id']; ?></div>
                        </div>
                    <?php endif; ?>

                    <?php if (isset($response['data']['payment_url'])): ?>
                        <div class="text-center mt-3">
                            <a href="<?php echo $response['data']['payment_url']; ?>" target="_blank" class="btn btn-outline-info">Buka Link Pembayaran <i class="fa-solid fa-external-link"></i></a>
                        </div>
                    <?php endif; ?>

                <?php else: ?>
                    <div class="text-center text-secondary py-5">
                        <i class="fa-solid fa-code fa-3x mb-3"></i>
                        <p>Belum ada request dikirim.<br>Silakan isi form dan kirim data.</p>
                    </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>