<?php
declare(strict_types=1);
require_once __DIR__ . '/bootstrap.php';

$error = '';
if (request_method() === 'POST') {
    if (!verify_csrf($_POST['csrf_token'] ?? null)) {
        $error = 'Неверный CSRF токен.';
    } elseif (admin_login(post_text('username'), post_text('password'))) {
        redirect('/admin/index.php');
    } else {
        $error = 'Неверный логин или пароль.';
    }
}
?><!DOCTYPE html>
<html lang="ru">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width,initial-scale=1">
  <title>Вход в CMS</title>
  <link rel="stylesheet" href="/admin/assets/admin.css?v=7">
</head>
<body style="display:grid;place-items:center;min-height:100vh">
  <form class="panel" method="post" style="width:min(420px,calc(100% - 24px))">
    <h1>Вход в CMS</h1>
    <?php if ($error): ?><div class="notice notice--error"><?= e($error) ?></div><?php endif; ?>
    <input type="hidden" name="csrf_token" value="<?= e(csrf_token()) ?>">
    <label><span>Логин</span><input name="username" value="admin" autocomplete="username"></label>
    <label><span>Пароль</span><input type="password" name="password" autocomplete="current-password"></label>
    <button class="button" type="submit" style="width:100%;margin-top:12px">Войти</button>
  </form>
</body>
</html>
