<?php

require_once __DIR__ . '/../config.php';
require_once __DIR__ . '/../lang.php';

// ─────────────────────────────────────────────
// Already Logged In
// ─────────────────────────────────────────────
if (isAdmin()) {
    redirect('index.php');
}

// ─────────────────────────────────────────────
// Login Logic
// ─────────────────────────────────────────────
$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    $password = trim($_POST['password'] ?? '');

    if ($password === ADMIN_PASSWORD) {

        $_SESSION['savoria_admin'] = true;

        redirect('index.php');
    }

    $error = t('wrong_password');
}

$dir = $isRTL ? 'rtl' : 'ltr';

?>

<!DOCTYPE html>

<html lang="<?= $LANG ?>" dir="<?= $dir ?>">

<head>

<meta charset="UTF-8">

<meta name="viewport"
      content="width=device-width, initial-scale=1.0">

<title>
  SAVORIA — <?= t('login_admin') ?>
</title>

<link rel="stylesheet"
      href="../assets/css/style.css">

</head>

<body style="
  min-height:100vh;
  display:flex;
  align-items:center;
  justify-content:center;
  background:var(--bg);
  padding:20px;
">

<!-- LOGIN CARD -->
<div class="card"
     style="
       width:100%;
       max-width:380px;
       padding:42px;
     ">

  <!-- HEADER -->
  <div style="
    text-align:center;
    margin-bottom:30px;
  ">

    <div class="nav-logo"
         style="
           font-size:2.2rem;
           margin-bottom:10px;
           display:block;
         ">

      SAVORIA

    </div>

    <span style="
      font-size:10px;
      letter-spacing:.25em;
      text-transform:uppercase;
      color:var(--fg40);
    ">

      <?= t('login_admin') ?>

    </span>

  </div>

  <!-- ERROR -->
  <?php if ($error): ?>

  <div class="alert alert-error"
       style="margin-bottom:18px">

    <?= e($error) ?>

  </div>

  <?php endif; ?>

  <!-- FORM -->
  <form method="POST">

    <div class="form-group">

      <label class="form-label">

        <?= t('password') ?>

      </label>

      <input
        class="form-input"
        type="password"
        name="password"
        required
        autofocus
        autocomplete="off"
      >

    </div>

    <button type="submit"
            class="btn btn-primary"
            style="
              width:100%;
              justify-content:center;
              margin-top:8px;
            ">

      <?= t('login_btn') ?>

    </button>

  </form>

  <!-- FOOTER -->
  <div style="
    margin-top:24px;
    text-align:center;
  ">

    <a href="?lang=<?= $LANG === 'en' ? 'ar' : 'en' ?>"
       style="
         font-size:11px;
         color:var(--fg40);
         text-decoration:none;
       ">

      <?= $LANG === 'en' ? 'عربي' : 'EN' ?>

    </a>

    <span style="
      color:var(--fg20);
      margin:0 10px;
    ">

      •

    </span>

    <a href="../index.php"
       style="
         font-size:11px;
         color:var(--fg40);
         text-decoration:none;
       ">

      <?= $isRTL ? 'العودة للموقع' : 'Back to Website' ?>

    </a>

  </div>

</div>

</body>

</html>