<?php
require_once __DIR__ . '/includes/init.php';
if (is_logged_in()) redirect(BASE . '/panel/');

$err = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    csrf_check();
    [$ok, $msg] = login_user($_POST['login'] ?? '', $_POST['password'] ?? '', client_ip());
    if ($ok) {
        redirect(BASE . '/panel/');
    }
    $err = $msg;
}

$page_title = 'Entrar';
require __DIR__ . '/includes/header.php';
?>
<div class="authwrap">
  <div class="authcard">
    <h1 data-i18n="loginTitle">Entrar</h1>
    <p class="sub" data-i18n="loginSub">Accede a tus descargas y licencias.</p>
    <?php if ($err): ?><div class="alert alert-error"><?= e($err) ?></div><?php endif; ?>
    <form method="post" autocomplete="off">
      <?= csrf_field() ?>
      <div class="field"><label data-i18n="loginUser">Usuario o correo</label>
        <input class="input" name="login" required value="<?= e($_POST['login'] ?? '') ?>"></div>
      <div class="field"><label data-i18n="loginPass">Contraseña</label>
        <input class="input" type="password" name="password" required></div>
      <button class="btn primary block" type="submit" data-i18n="loginBtn">Entrar</button>
    </form>
    <div class="alt"><span data-i18n="loginAlt">¿No tienes cuenta?</span> <a href="<?= e(BASE) ?>/register.php" data-i18n="loginAltLink">Crea una</a></div>
  </div>
</div>
<?php require __DIR__ . '/includes/footer.php'; ?>
