<?php
declare(strict_types=1);
require_once __DIR__ . '/core.php';

$message = '';
$messageType = 'ok';
$lookup = null;

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $pin = (string) ($_POST['pin'] ?? '');
    $id = preg_replace('/\D/', '', (string) ($_POST['user_id'] ?? ''));
    $action = (string) ($_POST['admin_action'] ?? 'lookup');
    if (!hash_equals(AVIATOR_ADMIN_PIN, $pin)) {
        $message = 'Invalid admin PIN.';
        $messageType = 'err';
    } elseif (strlen($id) !== 6) {
        $message = 'Enter a valid 6 digit user ID.';
        $messageType = 'err';
    } else {
        if ($action === 'lookup') {
            $lookup = aviator_find_visitor_by_id($id);
            if (!$lookup) {
                $message = 'User ID not found.';
                $messageType = 'err';
            }
        } else {
            $amount = aviator_amount($_POST['amount'] ?? 0);
            $limit = aviator_amount($_POST['restrict_limit'] ?? 0);
            $result = aviator_update_visitor_by_id($id, function (&$v) use ($action, $amount, $limit): void {
                if ($action === 'add_balance') {
                    $v['balance'] = round((float) $v['balance'] + $amount, 2);
                    $v['total_added'] = round((float) ($v['total_added'] ?? 0) + $amount, 2);
                    if ($limit > 0) {
                        $v['restrict_limit'] = $limit;
                    }
                } elseif ($action === 'set_balance') {
                    $v['balance'] = $amount;
                    if ($limit > 0) {
                        $v['restrict_limit'] = $limit;
                    }
                } elseif ($action === 'set_limit') {
                    $v['restrict_limit'] = $limit;
                } elseif ($action === 'restrict') {
                    $v['status'] = 'RESTRICTED';
                } elseif ($action === 'activate') {
                    $v['status'] = 'ACTIVE';
                } elseif ($action === 'clear_limit') {
                    $v['restrict_limit'] = 0;
                    $v['status'] = 'ACTIVE';
                }
            });
            if ($result['ok'] ?? false) {
                $lookup = $result['visitor'];
                $message = 'Account updated successfully.';
            } else {
                $message = $result['message'] ?? 'Update failed.';
                $messageType = 'err';
            }
        }
    }
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Balance Admin</title>
<style>
body{margin:0;background:#07080a;color:#fff;font-family:Arial,sans-serif;padding:20px}.wrap{max-width:900px;margin:0 auto}.card{background:#15171b;border:1px solid #2b3039;border-radius:22px;padding:20px;margin-bottom:16px;box-shadow:0 20px 60px rgba(0,0,0,.35)}h1{margin:0 0 8px}.muted{color:#9aa3af;line-height:1.5}.grid{display:grid;grid-template-columns:repeat(2,1fr);gap:12px}@media(max-width:700px){.grid{grid-template-columns:1fr}}input,select,button{width:100%;box-sizing:border-box;border-radius:14px;border:1px solid #333944;background:#0d0f12;color:#fff;padding:13px;font-size:15px}button{background:#238636;border-color:#2ea043;font-weight:800;cursor:pointer}.danger{background:#b4232d;border-color:#ef4444}.warn{background:#a36500;border-color:#f59e0b}.blue{background:#155da9;border-color:#2f8df2}.msg{padding:12px;border-radius:14px;margin-top:12px}.ok{background:#102a19;border:1px solid #2f8f46;color:#b9ffc7}.err{background:#321316;border:1px solid #aa2530;color:#ffd2d2}.table{width:100%;border-collapse:collapse;margin-top:12px}.table td{padding:10px;border-bottom:1px solid #272b33}.table td:first-child{color:#9aa3af}a{color:#65b7ff}</style>
</head>
<body>
<div class="wrap">
<div class="card">
<h1>Balance Admin</h1>
<p class="muted">Add/set demo balance by 6 digit ID. If balance reaches or crosses the restriction limit, the account status becomes <b>RESTRICTED</b> and the main page will block betting.</p>
<?php if ($message): ?><div class="msg <?= $messageType ?>"><?= aviator_h($message) ?></div><?php endif; ?>
<form method="post">
<div class="grid">
<input type="password" name="pin" placeholder="Admin PIN" required>
<input type="text" name="user_id" placeholder="6 digit user ID" maxlength="6" required value="<?= aviator_h((string)($_POST['user_id'] ?? '')) ?>">
<select name="admin_action">
<option value="lookup">Lookup only</option>
<option value="add_balance">Add balance</option>
<option value="set_balance">Set balance</option>
<option value="set_limit">Set restriction limit</option>
<option value="restrict">Restrict account</option>
<option value="activate">Activate account</option>
<option value="clear_limit">Clear limit + activate</option>
</select>
<input type="number" step="0.01" name="amount" placeholder="Amount, example 500">
<input type="number" step="0.01" name="restrict_limit" placeholder="Restriction limit, example 1000">
<button type="submit">Submit</button>
</div>
</form>
<p class="muted"><a href="index.php">Open main page</a> · <a href="deposit.php">Open deposit</a></p>
</div>
<?php if ($lookup): ?>
<div class="card">
<h2>User Details</h2>
<table class="table">
<tr><td>ID</td><td><?= aviator_h((string)$lookup['id']) ?></td></tr>
<tr><td>Status</td><td><?= aviator_h((string)$lookup['status']) ?></td></tr>
<tr><td>Balance</td><td><?= number_format((float)$lookup['balance'], 2) ?> <?= AVIATOR_CURRENCY ?></td></tr>
<tr><td>Restriction Limit</td><td><?= number_format((float)$lookup['restrict_limit'], 2) ?> <?= AVIATOR_CURRENCY ?></td></tr>
<tr><td>Total Added</td><td><?= number_format((float)$lookup['total_added'], 2) ?> <?= AVIATOR_CURRENCY ?></td></tr>
<tr><td>Total Bet</td><td><?= number_format((float)$lookup['total_bet'], 2) ?> <?= AVIATOR_CURRENCY ?></td></tr>
<tr><td>Total Win</td><td><?= number_format((float)$lookup['total_win'], 2) ?> <?= AVIATOR_CURRENCY ?></td></tr>
<tr><td>Created</td><td><?= aviator_h((string)$lookup['created_at']) ?></td></tr>
<tr><td>Updated</td><td><?= aviator_h((string)$lookup['updated_at']) ?></td></tr>
</table>
</div>
<?php endif; ?>
</div>
</body>
</html>
