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

$redirectTarget = safe_redirect_target((string) ($_POST['redirect'] ?? $_GET['redirect'] ?? 'index.php'));
if (current_user()) {
    redirect($redirectTarget);
}

$prefillEmail = normalize_email((string) ($_GET['email'] ?? ''));

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

    try {
        verify_csrf_or_fail();

        $password = (string) ($_POST['password'] ?? '');
        $confirmPassword = (string) ($_POST['confirm_password'] ?? '');
        if ($password !== $confirmPassword) {
            throw new RuntimeException('两次输入的密码不一致。');
        }

        begin_signup_verification($email, $password);
        flash('success', '验证码已发送到你的邮箱，请在 10 分钟内完成验证。');
        redirect(verify_page_url($email, $redirectTarget));
    } catch (Throwable $exception) {
        flash('danger', $exception->getMessage());
        redirect(register_page_url($redirectTarget, ['email' => $email]));
    }
}

render_layout_start('邮箱注册', '邮箱注册页，提交邮箱与密码后发送 6 位验证码。', 'auth');
?>
<section class="app-page-section auth-page-shell">
  <article class="app-card gradient-card hero-card">
    <div class="tiny-eyebrow">Step 2 / 4</div>
    <h1 class="app-hero-title compact-title">邮箱注册</h1>
    <p class="app-hero-copy mb-0">这里不再直接创建账号。先填写邮箱和密码，系统会先发 6 位验证码，验证通过后才真正建号。</p>
  </article>
</section>

<section class="app-page-section auth-page-shell">
  <div class="auth-stack auth-page-grid">
    <article class="app-card auth-card-panel">
      <div class="auth-panel-head">
        <div>
          <div class="list-title-strong">填写注册信息</div>
          <div class="list-meta-line">验证码将发送到你的邮箱，账户创建会等到验证通过后再执行。</div>
        </div>
        <span class="tag-chip">注册</span>
      </div>

      <form method="post" class="auth-form-grid">
        <input type="hidden" name="csrf_token" value="<?= h(csrf_token()) ?>">
        <input type="hidden" name="redirect" value="<?= h($redirectTarget) ?>">
        <div>
          <label class="form-label" for="register-email">邮箱地址</label>
          <input class="form-control" id="register-email" type="email" name="email" placeholder="example@email.com" value="<?= h($prefillEmail) ?>" required>
        </div>
        <div>
          <label class="form-label" for="register-password">登录密码</label>
          <input class="form-control" id="register-password" type="password" name="password" placeholder="至少 6 位字符" required>
        </div>
        <div>
          <label class="form-label" for="register-password-confirm">确认密码</label>
          <input class="form-control" id="register-password-confirm" type="password" name="confirm_password" placeholder="再次输入密码" required>
        </div>
        <button class="btn btn-gradient" type="submit">发送验证码</button>
      </form>
    </article>

    <article class="app-card auth-card-panel">
      <div class="auth-panel-head">
        <div>
          <div class="list-title-strong">注册后会发生什么</div>
          <div class="list-meta-line">我把复杂流程翻成一句人话：先确认邮箱，再正式开户。</div>
        </div>
        <span class="tag-chip secondary-chip">说明</span>
      </div>

      <div class="app-list-stack">
        <div class="app-list-item static-row">
          <span>
            <span class="list-title-strong">第 1 步：发送 6 位验证码</span>
            <span class="list-meta-line">系统会用你填写的邮箱作为收件人，验证码有效期 10 分钟。</span>
          </span>
        </div>
        <div class="app-list-item static-row">
          <span>
            <span class="list-title-strong">第 2 步：验证通过后建号</span>
            <span class="list-meta-line">数据库会真实创建用户、6 位数字 ID、钱包与 VIP0。</span>
          </span>
        </div>
        <div class="app-list-item static-row">
          <span>
            <span class="list-title-strong">第 3 步：回到登录页</span>
            <span class="list-meta-line">邮箱验证通过后，再进入登录页使用密码登录。</span>
          </span>
        </div>
      </div>

      <div class="app-inline-note mt-3">验证码依赖当前 SMTP 环境。如果还没有配置好发信参数，这里会真实提示失败，方便你知道问题在邮件配置而不是页面。</div>

      <div class="auth-link-grid mt-3">
        <a class="btn btn-outline-light" href="<?= h(login_page_url($redirectTarget)) ?>">已有账号，去登录</a>
        <a class="btn btn-outline-light" href="<?= h(start_page_url($redirectTarget)) ?>">返回启动页</a>
      </div>
    </article>
  </div>
</section>
<?php render_layout_end(); ?>
