<?php
declare(strict_types=1);
require_once __DIR__ . '/app/Helpers/bootstrap.php';
start_session();
ob_start(static function (string $html): string {
    if (language() === 'de') { return $html; }
    return strtr($html, ['<html lang="de">' => '<html lang="en">', 'Hauptadministrator · TripTale' => t('admin_login.title') . ' · TripTale', '>← Zurück<' => '>' . t('admin_login.back') . '<', '>Hauptadministrator<' => '>' . t('admin_login.title') . '<', '>Mit deinem persönlichen Passwort anmelden.<' => '>' . t('admin_login.intro') . '<', '>E-Mail-Adresse<input' => '>' . t('user_form.email') . '<input', '>Passwort<input' => '>' . t('admin_login.password') . '<input', '>Anmelden<' => '>' . t('admin_login.submit') . '<']);
});
if (logged_in_user() !== null) { header('Location: /dashboard.php'); exit; }
$error=null;
if ($_SERVER['REQUEST_METHOD']==='POST') {
    verify_csrf(); $email=strtolower(trim((string)($_POST['email']??''))); $password=(string)($_POST['password']??'');
    $query=db()->prepare('SELECT id,password_hash FROM '.table('users')." WHERE email=:email AND role='main_admin' AND status='active' AND deleted_at IS NULL LIMIT 1");
    $query->execute([':email'=>$email]); $account=$query->fetch();
    if (!$account || !$account['password_hash'] || !password_verify($password,$account['password_hash'])) { $error=t('admin_login.error'); }
    else { session_regenerate_id(true); $_SESSION['user_id']=(int)$account['id']; $_SESSION['login_expires_at']=time()+(14*24*60*60); header('Location: /dashboard.php'); exit; }
}
?><!doctype html><html lang="de"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Hauptadministrator · TripTale</title><style>body{font-family:system-ui,sans-serif;background:#f4f6f8;margin:0;padding:32px;color:#17202a}main{max-width:460px;margin:auto;background:#fff;padding:28px;border-radius:14px;box-shadow:0 4px 18px #0001}label{display:block;margin-top:14px;font-weight:700}input{box-sizing:border-box;width:100%;margin-top:6px;padding:11px;border:1px solid #b9c1c8;border-radius:7px;font:inherit}button,.button{display:inline-block;margin-top:22px;padding:12px 16px;border:0;border-radius:7px;background:#126e82;color:#fff;text-decoration:none;font-weight:700}.back{background:#e3ebee;color:#126e82}.error{padding:12px;border-radius:7px;background:#ffe6e3}@media(max-width:600px){body{padding:12px}main{padding:16px}}</style><link rel="stylesheet" href="/assets/triptale-ui.css?v=19"></head><body><main><a class="button back" href="/login.php">← Zurück</a><h1>Hauptadministrator</h1><p>Mit deinem persönlichen Passwort anmelden.</p><?php if($error): ?><p class="error"><?= htmlspecialchars($error,ENT_QUOTES,'UTF-8') ?></p><?php endif; ?><form method="post"><input type="hidden" name="csrf_token" value="<?= htmlspecialchars(csrf_token(),ENT_QUOTES,'UTF-8') ?>"><label>E-Mail-Adresse<input name="email" type="email" required autofocus></label><label>Passwort<input name="password" type="password" required autocomplete="current-password"></label><button type="submit">Anmelden</button></form></main></body></html>
