<?php
require_once '_auth.php';

$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $uname = trim($_POST['username'] ?? '');
    $upass = $_POST['password'] ?? '';
    if ($uname === ADMIN_USER && password_verify($upass, ADMIN_PASS)) {
        $_SESSION['adm_logged_in'] = true;
        $_SESSION['adm_user'] = $uname;
        header('Location: index.php');
        exit;
    } else {
        $error = 'Username atau password salah.';
    }
}

if (adm_is_logged_in()) {
    header('Location: index.php');
    exit;
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Login — Travelica Admin</title>
  <link rel="stylesheet" href="../assets/css/admin.css" />
  <link rel="stylesheet" href="assets/css/admin.css" />
</head>
<body>
<div class="adm-login-page">
  <div class="adm-login-card">
    <img src="../assets/images/logo.webp" alt="Travelica" class="adm-login-logo" />
    <h1 class="adm-login-title">Admin Panel</h1>
    <p class="adm-login-sub">Masuk untuk mengelola konten Travelica</p>

    <?php if ($error): ?>
    <div class="adm-error-msg"><?= htmlspecialchars($error) ?></div>
    <?php endif; ?>

    <form method="POST" class="adm-login-form">
      <div class="adm-form-group">
        <label class="adm-form-label" for="username">Username</label>
        <input type="text" id="username" name="username" class="adm-form-input"
               placeholder="admin" autocomplete="username"
               value="<?= htmlspecialchars($_POST['username'] ?? '') ?>" required />
      </div>
      <div class="adm-form-group">
        <label class="adm-form-label" for="password">Password</label>
        <input type="password" id="password" name="password" class="adm-form-input"
               placeholder="••••••••" autocomplete="current-password" required />
      </div>
      <button type="submit" class="btn btn-primary" style="margin-top:4px;justify-content:center;padding:13px;">
        Masuk
      </button>
    </form>
  </div>
</div>
</body>
</html>
