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

if (admin_logged_in()) {
    header('Location: index.php');
    exit;
}

$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $user = trim((string)($_POST['username'] ?? ''));
    $pass = (string)($_POST['password'] ?? '');
    if ($user === '' || $pass === '') {
        $error = 'Enter username and password.';
    } else {
        try {
            $st = db()->prepare('SELECT id, password_hash, username FROM admins WHERE username = ? LIMIT 1');
            $st->execute([$user]);
            $row = $st->fetch();
            if ($row && password_verify($pass, (string)$row['password_hash'])) {
                $_SESSION['admin_id'] = (int)$row['id'];
                $_SESSION['admin_username'] = (string)($row['username'] ?? $user);
                header('Location: index.php');
                exit;
            }
        } catch (Throwable $e) {
            $error = 'Database error. Import sql/install.sql and check config.php.';
        }
        $error = $error ?: 'Invalid credentials.';
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Sign in — Bhatt Enterprises Admin</title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css" rel="stylesheet">
  <style>
    body { min-height: 100vh; background: linear-gradient(145deg, #0f172a 0%, #1e293b 50%, #0f172a 100%); }
    .login-card { border: none; border-radius: 1rem; box-shadow: 0 25px 50px -12px rgba(0,0,0,.35); }
    .login-brand { font-weight: 800; letter-spacing: -0.03em; color: #0f172a; }
    .login-accent { color: #d97706; }
  </style>
</head>
<body class="d-flex align-items-center py-5">
  <div class="container">
    <div class="row justify-content-center">
      <div class="col-md-5 col-lg-4">
        <div class="text-center text-white mb-4">
          <i class="bi bi-sun-fill login-accent fs-1"></i>
          <p class="small text-white-50 text-uppercase tracking-wide mb-0 mt-2">Bhatt Enterprises</p>
          <h1 class="h4 mt-1 fw-bold">Administration</h1>
        </div>
        <div class="card login-card">
          <div class="card-body p-4 p-md-5">
            <h2 class="login-brand h5 mb-1">Sign in</h2>
            <p class="text-muted small mb-4">Use your admin credentials to manage the website.</p>
            <?php if ($error): ?>
              <div class="alert alert-danger py-2 small"><?= htmlspecialchars($error, ENT_QUOTES, 'UTF-8') ?></div>
            <?php endif; ?>
            <form method="post" autocomplete="off">
              <div class="mb-3">
                <label class="form-label small fw-semibold text-secondary">Username</label>
                <div class="input-group">
                  <span class="input-group-text bg-light border-end-0"><i class="bi bi-person"></i></span>
                  <input type="text" name="username" class="form-control border-start-0 ps-0" required autofocus>
                </div>
              </div>
              <div class="mb-4">
                <label class="form-label small fw-semibold text-secondary">Password</label>
                <div class="input-group">
                  <span class="input-group-text bg-light border-end-0"><i class="bi bi-key"></i></span>
                  <input type="password" name="password" class="form-control border-start-0 ps-0" required>
                </div>
              </div>
              <button type="submit" class="btn btn-dark w-100 py-2 fw-semibold">Continue</button>
            </form>
          </div>
        </div>
        <p class="text-center text-white-50 small mt-4 mb-0">Default after SQL import: <kbd class="bg-dark border-secondary">admin</kbd> / <kbd class="bg-dark border-secondary">password</kbd></p>
        <p class="text-center mt-2"><a href="../index.php" class="text-white-50 small">← Back to website</a></p>
      </div>
    </div>
  </div>
</body>
</html>
