<?php
require_once __DIR__ . '/../includes/lang.php';
require_once __DIR__ . '/../config/db.php';

session_start();
if (isset($_SESSION['user_id'])) { header('Location: ../dashboard.php'); exit; }

$dir  = t('dir');
$code = t('lang_code');
$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = trim($_POST['username'] ?? '');
    $password = $_POST['password'] ?? '';

    if ($username && $password) {
        // Check users table
        $stmt = db()->prepare("SELECT id, username, password_hash, role, status FROM users WHERE username = ? LIMIT 1");
        $stmt->execute([$username]);
        $user = $stmt->fetch();

        if ($user && password_verify($password, $user['password_hash'])) {
            if ($user['status'] === 'suspended') {
                $error = t('err_suspended');
            } else {
                session_regenerate_id(true);
                $_SESSION['user_id']   = $user['id'];
                $_SESSION['username']  = $user['username'];
                $_SESSION['role']      = $user['role'];
                header('Location: ../dashboard.php');
                exit;
            }
        } else {
            $error = t('err_login');
        }
    } else {
        $error = t('err_required');
    }
}
?>
<!DOCTYPE html>
<html lang="<?= htmlspecialchars($code) ?>" dir="<?= htmlspecialchars($dir) ?>">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title><?= htmlspecialchars(t('login')) ?> — <?= htmlspecialchars(t('site_name')) ?></title>
  <link rel="icon" href="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'><text y='.9em' font-size='90'>🌴</text></svg>">
  <link href="https://fonts.googleapis.com/css2?family=Cairo:wght@400;600;700;800;900&display=swap" rel="stylesheet">
  <link rel="stylesheet" href="../css/style.css">
  <style>
    body { background: var(--cream); }
    .auth-card { animation: fadeUp .45s ease both; }
    @keyframes fadeUp { from { opacity:0; transform:translateY(24px); } to { opacity:1; transform:none; } }
  </style>
</head>
<body>

<!-- Lang switcher -->
<div style="position:fixed;top:14px;inset-inline-end:14px;z-index:100;display:flex;gap:4px">
  <?php foreach(['ar','en','fr'] as $l): ?>
  <a href="<?= lang_url($l) ?>" style="background:<?= $code===$l?'var(--g-main)':'var(--white)' ?>;color:<?= $code===$l?'var(--white)':'var(--muted)' ?>;padding:5px 10px;border-radius:100px;font-size:.72rem;font-weight:700;font-family:var(--font);border:1.5px solid var(--border)"><?= $l==='ar'?'ع':strtoupper($l) ?></a>
  <?php endforeach; ?>
</div>

<div class="auth-wrap">
  <div class="auth-card">

    <!-- Logo -->
    <div style="text-align:center;margin-bottom:24px">
      <div style="font-size:2.2rem;font-weight:900;color:var(--g-deep)"><?= htmlspecialchars(t('site_name')) ?><span style="color:var(--gold)">.</span></div>
    </div>

    <h1 class="auth-title"><?= htmlspecialchars(t('login')) ?></h1>

    <?php if ($error): ?>
      <div class="error-box">⚠️ <?= htmlspecialchars($error) ?></div>
    <?php endif; ?>

    <form method="POST" novalidate>
      <div class="form-group">
        <input type="text" name="username" class="form-input"
               placeholder="<?= htmlspecialchars(t('username')) ?>"
               value="<?= htmlspecialchars($_POST['username'] ?? '') ?>"
               autocomplete="username" required autofocus>
      </div>
      <div class="form-group">
        <input type="password" name="password" class="form-input"
               placeholder="<?= htmlspecialchars(t('password')) ?>"
               autocomplete="current-password" required>
      </div>

      <button type="submit" class="btn btn-green"><?= htmlspecialchars(t('login_btn')) ?></button>
    </form>

    <!-- Guest explore -->
    <div style="margin:16px 0;text-align:center">
      <a href="../index.php<?= $code!=='ar'?'?lang='.$code:'' ?>"
         style="display:inline-flex;align-items:center;gap:6px;background:var(--gold-pale);color:var(--gold);border:1.5px solid var(--gold-lt);border-radius:var(--r);padding:12px 20px;font-size:.88rem;font-weight:700;font-family:var(--font);width:100%;justify-content:center">
        🧭 <?= htmlspecialchars(t('explore_btn')) ?>
      </a>
    </div>

    <div class="auth-link-row">
      <?= htmlspecialchars(t('no_account')) ?>
      <a href="register.php<?= $code!=='ar'?'?lang='.$code:'' ?>"><?= htmlspecialchars(t('register')) ?></a>
    </div>

    <div style="text-align:center;margin-top:12px">
      <a href="../index.php<?= $code!=='ar'?'?lang='.$code:'' ?>" class="btn-ghost" style="font-size:.82rem;color:var(--muted);font-family:var(--font)">
        ← <?= htmlspecialchars(t('go_home')) ?>
      </a>
    </div>

  </div>
</div>

</body>
</html>
