<?php
require_once __DIR__ . '/../includes/functions.php';
startSecureSession();
requireAdmin();

$message = '';
$messageType = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    csrfVerify();
    $action = $_POST['action'] ?? '';

    if ($action === 'save_site_settings') {
        $allowReg = isset($_POST['allow_registration']) ? 1 : 0;
        $allowLogin = isset($_POST['allow_login']) ? 1 : 0;

        updateAdminSettings([
            'allow_registration' => $allowReg,
            'allow_login' => $allowLogin
        ]);

        $message = 'Site access & registration settings updated successfully!';
        $messageType = 'success';
    }
}

$settings = getAdminSettings();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<?php $pageTitle = "Site Settings — Admin Control Panel"; include __DIR__ . '/admin-style.php'; ?>
<style>
.toggle-card {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 18px 20px;
    background: var(--surface-card);
    border: 1px solid var(--color-hairline);
    border-radius: var(--r-md);
    margin-bottom: 16px;
    transition: all 0.2s ease;
}
.toggle-card:hover {
    border-color: var(--blue-200);
}
.toggle-info {
    flex: 1;
    padding-right: 20px;
}
.toggle-title {
    font-size: 14px;
    font-weight: 700;
    color: var(--color-ink);
    margin-bottom: 4px;
    display: flex;
    align-items: center;
    gap: 8px;
}
.toggle-desc {
    font-size: 12px;
    color: var(--color-ink-mute);
    line-height: 1.4;
}

/* iOS/Stripe style Toggle Switch */
.switch {
    position: relative;
    display: inline-block;
    width: 48px;
    height: 26px;
    flex-shrink: 0;
}
.switch input {
    opacity: 0;
    width: 0;
    height: 0;
}
.slider {
    position: absolute;
    cursor: pointer;
    inset: 0;
    background-color: #cbd5e1;
    transition: .25s ease;
    border-radius: 34px;
}
.slider:before {
    position: absolute;
    content: "";
    height: 20px;
    width: 20px;
    left: 3px;
    bottom: 3px;
    background-color: white;
    transition: .25s ease;
    border-radius: 50%;
    box-shadow: 0 1px 3px rgba(0,0,0,0.2);
}
input:checked + .slider {
    background-color: #10b981;
}
input:checked + .slider:before {
    transform: translateX(22px);
}
.status-pill {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    font-size: 11px;
    font-weight: 700;
    padding: 2px 8px;
    border-radius: 99px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}
.status-pill.active {
    background: #dcfce7;
    color: #15803d;
}
.status-pill.inactive {
    background: #fee2e2;
    color: #dc2626;
}
</style>
</head>
<body>
<?php include __DIR__ . '/admin-nav.php'; ?>
    <div class="wrap" style="max-width: 800px;">

      <div style="margin-bottom:24px;">
        <h1 style="font-size:22px;font-weight:700;color:var(--color-ink);letter-spacing:-0.5px;display:flex;align-items:center;gap:8px;">
          <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="#2563eb" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-sliders"><line x1="4" x2="4" y1="21" y2="14"/><line x1="4" x2="4" y1="10" y2="3"/><line x1="12" x2="12" y1="21" y2="12"/><line x1="12" x2="12" y1="8" y2="3"/><line x1="20" x2="20" y1="21" y2="16"/><line x1="20" x2="20" y1="12" y2="3"/><line x1="2" x2="6" y1="14" y2="14"/><line x1="10" x2="14" y1="8" y2="8"/><line x1="18" x2="22" y1="16" y2="16"/></svg> Site Settings
        </h1>
        <p style="font-size:13px;color:var(--color-ink-mute);margin-top:4px;">Control global site access policies, merchant registration toggles, and login permissions.</p>
      </div>

      <?php if ($message): ?>
        <div class="callout callout-<?= $messageType === 'success' ? 'success' : 'error' ?>" style="margin-bottom:20px;">
          <?= htmlspecialchars($message) ?>
        </div>
      <?php endif; ?>

      <form method="POST">
        <input type="hidden" name="action" value="save_site_settings">
        <input type="hidden" name="csrf_token" value="<?= csrfToken() ?>">

        <!-- REGISTRATION TOGGLE CARD -->
        <div class="toggle-card">
          <div class="toggle-info">
            <div class="toggle-title">
              <svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="#2563eb" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M16 21v-2a4 4 0 0 0-4-4H6a4 4 0 0 0-4 4v2"/><circle cx="9" cy="7" r="4"/><line x1="19" x2="19" y1="8" y2="14"/><line x1="16" x2="22" y1="11" y2="11"/></svg>
              Allow Merchant Registrations
              <span class="status-pill <?= !empty($settings['allow_registration']) ? 'active' : 'inactive' ?>">
                <?= !empty($settings['allow_registration']) ? 'ENABLED' : 'DISABLED' ?>
              </span>
            </div>
            <div class="toggle-desc">When turned ON, new merchants can register accounts via <code>/register.php</code>. When turned OFF, registration attempts will be blocked.</div>
          </div>
          <label class="switch">
            <input type="checkbox" name="allow_registration" value="1" <?= !empty($settings['allow_registration']) ? 'checked' : '' ?>>
            <span class="slider"></span>
          </label>
        </div>

        <!-- LOGIN TOGGLE CARD -->
        <div class="toggle-card">
          <div class="toggle-info">
            <div class="toggle-title">
              <svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="#2563eb" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M15 3h4a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2h-4"/><polyline points="10 17 15 12 10 7"/><line x1="15" x2="3" y1="12" y2="12"/></svg>
              Allow Merchant Logins
              <span class="status-pill <?= !empty($settings['allow_login']) ? 'active' : 'inactive' ?>">
                <?= !empty($settings['allow_login']) ? 'ENABLED' : 'DISABLED' ?>
              </span>
            </div>
            <div class="toggle-desc">When turned ON, merchants can sign in to their dashboard. When turned OFF, merchant logins are paused. <i>(Super Admin can always log in).</i></div>
          </div>
          <label class="switch">
            <input type="checkbox" name="allow_login" value="1" <?= !empty($settings['allow_login']) ? 'checked' : '' ?>>
            <span class="slider"></span>
          </label>
        </div>

        <div style="margin-top: 24px; text-align: right;">
          <button type="submit" class="btn btn-primary" style="padding: 11px 28px; font-size: 14px; font-weight: 600;">
            <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M19 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h11l5 5v11a2 2 0 0 1-2 2z"/><polyline points="17 21 17 13 7 13 7 21"/><polyline points="7 3 7 8 15 8"/></svg>
            Save Site Settings
          </button>
        </div>
      </form>

    </div>
  </main>
</div>
<script>if (window.lucide) { lucide.createIcons(); }</script>
</body>
</html>
