<?php
// Login screen. Standalone layout (no sidebar/nav — nothing to navigate to
// until you're signed in).
require_once __DIR__ . '/includes/auth.php';
require_once __DIR__ . '/includes/visitor_log.php';
log_visit();

if (!empty($_SESSION['user_id'])) {
    header('Location: index.php');
    exit;
}

$error = null;
$usernameValue = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $usernameValue = trim($_POST['username'] ?? '');
    $password = (string) ($_POST['password'] ?? '');
    $remember = !empty($_POST['remember']);

    if (!csrf_verify($_POST['csrf_token'] ?? null)) {
        $error = 'Your session expired — please try again.';
    } elseif ($usernameValue === '' || $password === '') {
        $error = 'Wrong username or password.';
    } else {
        $result = attempt_login($usernameValue, $password);
        if ($result['ok']) {
            login_session($result['user']);
            if ($remember) {
                create_remember_me((int) $result['user']['id']);
            }
            purge_expired_tokens();
            header('Location: index.php');
            exit;
        }
        $error = $result['error'];
    }
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>月账 · Monthly Ledger — Log in</title>
<meta name="theme-color" content="#5a5bd8">
<link rel="icon" href="assets/icons/favicon.ico" sizes="any">
<link rel="apple-touch-icon" href="assets/icons/apple-touch-icon.png?v=<?= e(asset_version('assets/icons/apple-touch-icon.png')) ?>">
<link rel="manifest" href="manifest.json?v=<?= e(asset_version('manifest.json')) ?>">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-title" content="Monthly Ledger">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.3/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-icons/1.11.3/font/bootstrap-icons.min.css">
<link rel="stylesheet" href="assets/css/app.css?v=<?= e(asset_version('assets/css/app.css')) ?>">
</head>
<body>
<div class="container" style="min-height:100vh;">
  <div class="row justify-content-center align-items-center" style="min-height:100vh;">
    <div class="col-11 col-sm-8 col-md-5 col-lg-4">
      <div class="card-x p-4 p-lg-5">
        <div class="brand justify-content-center mb-4 p-0">
          <span class="brand-badge"><i class="bi bi-wallet2"></i></span>
          <span class="brand-name">月账 <span class="fw-normal text-secondary">· Monthly Ledger</span></span>
        </div>

        <?php if ($error): ?>
          <div class="alert alert-danger py-2 small" role="alert"><?= e($error) ?></div>
        <?php endif; ?>

        <form method="post" novalidate>
          <?= csrf_field() ?>

          <label class="form-label small fw-semibold" for="username">Username</label>
          <input class="form-control mb-3" id="username" name="username" type="text"
                 autocomplete="username" required value="<?= e($usernameValue) ?>">

          <label class="form-label small fw-semibold" for="password">Password</label>
          <input class="form-control mb-3" id="password" name="password" type="password"
                 autocomplete="current-password" required>

          <div class="form-check mb-4">
            <input class="form-check-input" id="remember" name="remember" type="checkbox" checked>
            <label class="form-check-label small" for="remember">Stay signed in on this device</label>
          </div>

          <button class="btn-accent w-100" type="submit">Log in</button>
        </form>
      </div>
    </div>
  </div>
</div>
</body>
</html>
