<?php
require __DIR__ . '/../_boot.php';
require __DIR__ . '/../partials/nav.php';
if (!$user || $user['role_name'] !== 'super_admin') { header('Location: dashboard.php'); exit; }
$filterUserId = (int)($_GET['user_id'] ?? 0);
?>
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Security Center | Pyrk Admin</title>
  <link rel="stylesheet" href="../assets/css/app.css">
</head>
<body>
<?php pyrk_render_nav('admin'); ?>
<main class="container enterprise-page security-page">
  <section class="page-hero admin-hero dashboard-hero enterprise-hero security-hero">
    <div>
      <p class="eyebrow">Enterprise security</p>
      <h1>Security center</h1>
      <p class="muted">Block/unblock accounts, monitor login attempts, review login IPs and manage session lockout settings.</p>
    </div>
    <div class="hero-actions">
      <a class="btn btn-light" href="users.php">System users</a>
      <a class="btn btn-light" href="members.php">Members</a>
    </div>
  </section>

  <section class="grid three compact-stats security-stats">
    <div class="stat stat-pro"><span>Failed attempt lock</span><strong id="settingFailedStat">3</strong><small>wrong passwords</small></div>
    <div class="stat stat-pro"><span>Lock duration</span><strong id="settingLockStat">60</strong><small>minutes</small></div>
    <div class="stat stat-pro"><span>Auto logout</span><strong id="settingSessionStat">180</strong><small>minutes</small></div>
  </section>

  <section class="grid admin-main-grid security-grid">
    <aside class="panel sticky-panel security-settings-panel">
      <div class="section-head">
        <div>
          <p class="eyebrow">Security settings</p>
          <h2>Login rules</h2>
        </div>
      </div>
      <form id="securitySettingsForm" class="stack-form">
        <label>Lock account after wrong attempts
          <input name="max_failed_attempts" id="maxFailedAttempts" type="number" min="1" max="20" value="3" required>
          <small class="muted">Recommended: 3.</small>
        </label>
        <label>Temporary lock duration in minutes
          <input name="lockout_minutes" id="lockoutMinutes" type="number" min="1" max="1440" value="60" required>
          <small class="muted">Example: 60 = one hour.</small>
        </label>
        <label>Auto logout after inactivity in minutes
          <input name="session_timeout_minutes" id="sessionTimeoutMinutes" type="number" min="5" max="1440" value="180" required>
          <small class="muted">180 minutes = 3 hours. You can change this here.</small>
        </label>
        <button class="btn" type="submit">Save security settings</button>
        <p id="securitySettingsMsg" class="notice"></p>
      </form>
    </aside>

    <section class="panel enterprise-table-panel">
      <div class="section-head crm-section-head">
        <div>
          <p class="eyebrow">Account controls</p>
          <h2>Block / unblock users and members</h2>
        </div>
        <div class="inline-controls search-controls">
          <input id="accountSearch" placeholder="Search name, email, mobile or IP">
          <select id="accountRoleFilter"><option value="">All roles</option></select>
          <select id="accountStatusFilter"><option value="">All status</option><option value="active">Active</option><option value="inactive">Inactive</option><option value="blocked">Blocked</option></select>
        </div>
      </div>
      <div class="table-wrap pro-table-wrap security-table-wrap">
        <table class="pro-table security-table">
          <thead><tr><th>Account</th><th>Role</th><th>Status</th><th>Login security</th><th>Last login</th><th></th></tr></thead>
          <tbody id="securityAccountsBody"></tbody>
        </table>
      </div>
    </section>
  </section>

  <section class="panel enterprise-table-panel security-logs-panel">
    <div class="section-head crm-section-head">
      <div>
        <p class="eyebrow">Login audit</p>
        <h2>Login activity</h2>
        <p class="muted small">Shows login time, IP address, browser/device, success/failure, logout time and logout reason.</p>
      </div>
      <div class="inline-controls search-controls">
        <input id="loginSearch" placeholder="Search email, IP, name or reason">
        <select id="loginSuccessFilter"><option value="">All attempts</option><option value="1">Success only</option><option value="0">Failed only</option></select>
        <select id="loginLimit"><option value="50">50</option><option value="100" selected>100</option><option value="200">200</option><option value="300">300</option></select>
        <button class="btn btn-light btn-small" type="button" id="refreshSecurityBtn">Refresh</button>
      </div>
    </div>
    <div class="table-wrap pro-table-wrap security-table-wrap">
      <table class="pro-table security-log-table">
        <thead><tr><th>Time</th><th>Account</th><th>Status</th><th>IP</th><th>Device / browser</th><th>Logout</th></tr></thead>
        <tbody id="loginLogsBody"></tbody>
      </table>
    </div>
  </section>
</main>
<script>
window.PYRK_API_BASE='../../api';
window.PYRK_CSRF='<?= htmlspecialchars($csrf) ?>';
window.PYRK_SECURITY_USER_ID=<?= $filterUserId ?>;
</script>
<script src="../assets/js/api.js"></script>
<script src="../assets/js/admin_security.js"></script>
</body>
</html>
