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

// Handle Logout
if (isset($_GET['logout'])) {
    $_SESSION = [];
    if (ini_get("session.use_cookies")) {
        $params = session_get_cookie_params();
        setcookie(session_name(), '', time() - 42000,
            $params["path"], $params["domain"],
            $params["secure"], $params["httponly"]
        );
    }
    session_destroy();
    header('Location: /admin/login.php');
    exit;
}

if (isAdminLoggedIn()) { header('Location: /admin/'); exit; }

$errors = [];
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $email = trim($_POST['email'] ?? '');
    $pass  = trim($_POST['password'] ?? '');
    $admin = Database::fetchOne("SELECT * FROM admins WHERE email=?", [$email]);
    if ($admin && password_verify($pass, $admin['password'])) {
        $_SESSION['admin_id']   = $admin['id'];
        $_SESSION['admin_name'] = $admin['name'];
        $_SESSION['admin_role'] = $admin['role'];
        Database::execute("UPDATE admins SET last_login=NOW() WHERE id=?", [$admin['id']]);
        logActivity('admin', $admin['id'], 'Admin Login');
        header('Location: /admin/');
        exit;
    } else {
        $errors[] = 'Invalid credentials.';
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Admin Login — Rig N Dig</title>
<script src="https://cdn.tailwindcss.com"></script>
<style>
  body { background: #0a0e1a; color: #e8eaed; font-family: 'Segoe UI', sans-serif; }
  input { background: #0a0e1a; border: 1px solid #2a3147; color: #e8eaed; padding: 10px 14px; border-radius: 8px; width: 100%; outline: none; }
  input:focus { border-color: #ff6b00; }
  .btn { background: #ff6b00; color: #fff; padding: 12px; border-radius: 8px; font-weight: 600; cursor: pointer; border: none; width: 100%; }
  .btn:hover { background: #e55a00; }
</style>
</head>
<body class="min-h-screen flex items-center justify-center">
<div class="w-full max-w-sm">
  <div class="text-center mb-8">
    <div class="text-2xl font-black mb-1" style="font-family:Rajdhani,sans-serif"><span style="color:#ff6b00">RIG</span> N <span style="color:#ff6b00">DIG</span></div>
    <div class="text-sm" style="color:#6b7280">Admin Panel</div>
  </div>
  <?php foreach ($errors as $e): ?>
  <div style="background:#3b0000;border:1px solid #dc2626;color:#fca5a5;padding:12px;border-radius:8px;margin-bottom:16px;font-size:14px;"><?= e($e) ?></div>
  <?php endforeach; ?>
  <div style="background:#1a1f2e;border:1px solid #2a3147;border-radius:12px;padding:24px;">
    <form method="POST">
      <div style="margin-bottom:16px">
        <label style="font-size:12px;color:#6b7280;display:block;margin-bottom:6px">Email</label>
        <input type="email" name="email" required>
      </div>
      <div style="margin-bottom:24px; position:relative;">
        <label style="font-size:12px;color:#6b7280;display:block;margin-bottom:6px">Password</label>
        <input type="password" name="password" id="password" required style="padding-right: 40px;">
        <button type="button" onclick="togglePass()" style="position:absolute; right:10px; bottom:10px; background:none; border:none; color:#6b7280; cursor:pointer; font-size:18px;">👁️</button>
      </div>
      <button type="submit" class="btn">Sign In to Admin Panel</button>
    </form>
  </div>
  <script>
    function togglePass() {
      const p = document.getElementById('password');
      p.type = p.type === 'password' ? 'text' : 'password';
    }
  </script>
  <div class="text-center mt-4" style="font-size:12px;color:#4b5563">
    <a href="/" style="color:#6b7280">← Back to Website</a>
  </div>
</div>
</body>
</html>
