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

$err = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    csrf_check();
    [$ok, $msg] = register_user($_POST['username'] ?? '', $_POST['email'] ?? '', $_POST['password'] ?? '');
    if ($ok) {
        // login automático tras registrarse
        login_user($_POST['username'], $_POST['password'], client_ip());
        flash_set('ok', '¡Cuenta creada! Bienvenido, ' . $_POST['username'] . '.');
        redirect(BASE . '/panel/');
    }
    $err = $msg;
}

$page_title = 'Crear cuenta';
require __DIR__ . '/includes/header.php';
?>
<div class="authwrap">
  <div class="authcard">
    <h1 data-i18n="regTitle">Crear cuenta</h1>
    <p class="sub" data-i18n="regSub">Regístrate para descargar plugins y dejar reseñas.</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="regUser">Usuario</label>
        <input class="input" name="username" maxlength="32" required value="<?= e($_POST['username'] ?? '') ?>" placeholder="3-32 letras, números o _"></div>
      <div class="field"><label data-i18n="regEmail">Correo</label>
        <input class="input" type="email" name="email" maxlength="190" required value="<?= e($_POST['email'] ?? '') ?>" placeholder="tucorreo@ejemplo.com"></div>
      <div class="field"><label data-i18n="regPass">Contraseña</label>
        <input class="input" type="password" name="password" minlength="8" required placeholder="Mínimo 8 caracteres"></div>
      <button class="btn primary block" type="submit" data-i18n="regBtn">Crear cuenta</button>
    </form>
    <div class="alt"><span data-i18n="regAlt">¿Ya tienes cuenta?</span> <a href="<?= e(BASE) ?>/login.php" data-i18n="regAltLink">Entra aquí</a></div>
  </div>
</div>
<?php require __DIR__ . '/includes/footer.php'; ?>
