<?php
declare(strict_types=1);
require __DIR__ . '/auth.php';
send_security_headers();
$language = site_language();
$isEnglish = $language === 'en';

if (current_user() !== null) {
    redirect_to('/account.php');
}

$email = '';
$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    require_valid_csrf();
    $email = normalize_email((string) ($_POST['email'] ?? ''));
    $password = (string) ($_POST['password'] ?? '');
    $confirmation = (string) ($_POST['password_confirmation'] ?? '');

    if (rate_limited('register', $email, 5, 3600)) {
        $error = site_text('注册尝试次数过多，请稍后再试。', 'Too many registration attempts. Please try again later.');
    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL) || strlen($email) > 254) {
        $error = site_text('请输入有效的电子邮箱。', 'Enter a valid email address.');
    } elseif (strlen($password) < 8 || strlen($password) > 128) {
        $error = site_text('密码长度需要为 8 至 128 个字符。', 'Password must be 8 to 128 characters long.');
    } elseif ($password !== $confirmation) {
        $error = site_text('两次输入的密码不一致。', 'The passwords do not match.');
    } else {
        record_attempt('register', $email);
        try {
            $statement = database()->prepare('INSERT INTO users(email, password_hash) VALUES(:email, :password_hash)');
            $statement->execute([
                'email' => $email,
                'password_hash' => password_hash($password, PASSWORD_DEFAULT),
            ]);
            establish_login((int) database()->lastInsertId());
            redirect_to('/account.php');
        } catch (PDOException $exception) {
            if ($exception->getCode() === '23000') {
                $error = site_text('该邮箱已经注册，请直接登录。', 'That email is already registered. Please sign in.');
            } else {
                throw $exception;
            }
        }
    }
}
?>
<!doctype html>
<html lang="<?= $isEnglish ? 'en' : 'zh-CN' ?>">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width,initial-scale=1">
  <title><?= escape_html(site_text('注册', 'Create an account')) ?> - <?= escape_html(site_text('米环科技（长春）有限公司', 'Mihuan Technology (Changchun) Co., Ltd.')) ?></title>
  <link rel="icon" href="/site-icon-20260917.png?v=1">
  <link rel="stylesheet" href="/auth.css?v=3">
</head>
<body>
  <header class="topbar">
    <a class="brand" href="/"><span><?= escape_html(site_text('米环科技', 'Mihuan Technology')) ?></span><span><?= escape_html(site_text('（长春）', '(Changchun)')) ?></span><span><?= escape_html(site_text('有限公司', 'Co., Ltd.')) ?></span></a>
    <nav class="language-toggle" aria-label="Language">
      <a href="<?= escape_html(site_language_href('zh')) ?>"<?= $isEnglish ? '' : ' aria-current="page"' ?>>中文</a>
      <span aria-hidden="true">/</span>
      <a href="<?= escape_html(site_language_href('en')) ?>"<?= $isEnglish ? ' aria-current="page"' : '' ?>>English</a>
    </nav>
  </header>
  <main class="auth-main">
    <section class="auth-panel" aria-labelledby="page-title">
      <h1 id="page-title"><?= escape_html(site_text('注册', 'Create an account')) ?></h1>
      <p class="lead"><?= escape_html(site_text('创建你的米环科技账户。', 'Create your Mihuan Technology account.')) ?></p>
      <?php if ($error !== ''): ?><p class="error" role="alert"><?= escape_html($error) ?></p><?php endif; ?>
      <form method="post" action="/register.php">
        <input type="hidden" name="csrf_token" value="<?= escape_html(csrf_token()) ?>">
        <input type="hidden" name="lang" value="<?= escape_html($language) ?>">
        <label for="email"><?= escape_html(site_text('电子邮箱', 'Email')) ?></label>
        <input id="email" name="email" type="email" autocomplete="email" maxlength="254" required value="<?= escape_html($email) ?>">
        <label for="password"><?= escape_html(site_text('密码', 'Password')) ?></label>
        <input id="password" name="password" type="password" autocomplete="new-password" minlength="8" maxlength="128" required>
        <label for="password-confirmation"><?= escape_html(site_text('确认密码', 'Confirm password')) ?></label>
        <input id="password-confirmation" name="password_confirmation" type="password" autocomplete="new-password" minlength="8" maxlength="128" required>
        <button type="submit"><?= escape_html(site_text('注册', 'Create account')) ?></button>
      </form>
      <p class="switch"><?= escape_html(site_text('已有账户？', 'Already have an account?')) ?> <a href="/login.php?lang=<?= escape_html($language) ?>"><?= escape_html(site_text('登录', 'Sign in')) ?></a></p>
    </section>
  </main>
</body>
</html>
