<?php
session_start();
if (isset($_SESSION['tahwissa_admin'])) { header('Location: index.php'); exit; }
require_once __DIR__ . '/../config/db.php';
$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = trim($_POST['username'] ?? '');
    $password = $_POST['password'] ?? '';
    if ($username && $password) {
        $stmt = db()->prepare("SELECT id, password_hash FROM admins WHERE username = ? LIMIT 1");
        $stmt->execute([$username]);
        $admin = $stmt->fetch();
        if ($admin && password_verify($password, $admin['password_hash'])) {
            session_regenerate_id(true);
            $_SESSION['tahwissa_admin'] = $admin['id'];
            db()->prepare("UPDATE admins SET last_login=NOW() WHERE id=?")->execute([$admin['id']]);
            header('Location: index.php'); exit;
        }
    }
    $error = 'اسم المستخدم أو كلمة المرور غير صحيحة.';
}
?>
<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>تسجيل دخول الإدارة — تحويسة</title>
  <link href="https://fonts.googleapis.com/css2?family=Cairo:wght@400;600;700;800&display=swap" rel="stylesheet">
  <style>
    *,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
    :root{--g:#2D6A4F;--gd:#1B4332;--gp:#D8F3DC;--gold:#C9A84C;--cream:#FAFAF5;--border:#E0EBE4;--muted:#6B7C74;--error:#DC2626;--err-bg:#FEE2E2}
    body{font-family:'Cairo',sans-serif;background:linear-gradient(160deg,var(--gd),var(--g));min-height:100vh;display:flex;align-items:center;justify-content:center;padding:20px}
    .card{background:#fff;border-radius:24px;padding:40px 32px;width:100%;max-width:400px;box-shadow:0 20px 60px rgba(0,0,0,.2)}
    .logo{text-align:center;font-size:1.9rem;font-weight:900;color:var(--gd);margin-bottom:4px}
    .logo span{color:var(--gold)}
    .sub{text-align:center;font-size:.82rem;color:var(--muted);margin-bottom:28px}
    label{display:block;font-size:.75rem;font-weight:700;color:var(--muted);text-transform:uppercase;letter-spacing:.08em;margin-bottom:5px}
    input{width:100%;padding:13px 16px;border:1.5px solid var(--border);border-radius:10px;font-family:inherit;font-size:.95rem;outline:none;transition:.2s;margin-bottom:14px;background:var(--cream)}
    input:focus{border-color:var(--g);box-shadow:0 0 0 3px rgba(45,106,79,.12);background:#fff}
    .btn{width:100%;padding:14px;background:var(--g);color:#fff;border:none;border-radius:10px;font-family:inherit;font-size:1rem;font-weight:800;cursor:pointer;transition:.2s}
    .btn:hover{background:var(--gd)}
    .err{background:var(--err-bg);color:var(--error);border-radius:10px;padding:12px;font-size:.88rem;font-weight:600;margin-bottom:16px;text-align:center}
    .back{display:block;text-align:center;margin-top:18px;font-size:.82rem;color:var(--muted);text-decoration:none}
    .back:hover{color:var(--g)}
  </style>
</head>
<body>
  <div class="card">
    <div class="logo">تحويسة<span>.</span></div>
    <p class="sub">لوحة تحكم الإدارة</p>
    <?php if ($error): ?><div class="err">⚠️ <?= htmlspecialchars($error) ?></div><?php endif; ?>
    <form method="POST">
      <label>اسم المستخدم</label>
      <input type="text" name="username" value="<?= htmlspecialchars($_POST['username']??'') ?>" autocomplete="username" required autofocus>
      <label>كلمة المرور</label>
      <input type="password" name="password" autocomplete="current-password" required>
      <button type="submit" class="btn">دخول ←</button>
    </form>
    <a href="../index.php" class="back">← العودة للموقع</a>
  </div>
</body>
</html>
