<?php
/**
 * 邮箱验证（注册后或个人中心重发）
 */
require_login();
$me = current_user();
if (!headers_sent()) {
    header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
    header('Pragma: no-cache');
}
$email = trim((string) ($me['email'] ?? ''));
// 统一从数据库读取真实状态，避免个人中心与验证页出现「已验证 / 未验证」不一致。
$verified = email_verified_fresh($me);
$verifyRequired = setting('email_verify_required', '0') === '1';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    check_csrf();
    $act = input('act');
    if ($act === 'send') {
        if ($email === '') {
            flash('请先在个人资料中填写邮箱', 'err');
        } elseif ($verified) {
            flash('当前邮箱已验证，无需重复发送验证码', 'ok');
            redirect(page_url('verify_email'));
        } elseif (security_rate_limited('email_code', 3, 900, $email)) {
            flash('验证码发送过于频繁，请稍后再试', 'err');
        } else {
            security_event('email_code', $email);
            list($ok, $msg) = issue_email_code((int) $me['id'], $email, 'verify_email');
            flash($ok ? '验证码已发送，请查收邮箱' : ('发送失败：' . $msg), $ok ? 'ok' : 'err');
        }
    } elseif ($act === 'verify') {
        $code = trim((string) ($_POST['code'] ?? ''));
        if ($verified) {
            flash('当前邮箱已验证，无需重复验证', 'ok');
            redirect(page_url('verify_email'));
        }
        if (consume_email_code((int) $me['id'], $code, 'verify_email')) {
            DB::run('UPDATE ' . DB::table('users') . ' SET email_verified = 1 WHERE id = ?', [(int) $me['id']]);
            unset($_SESSION['verify_email_uid']);
            flash('邮箱验证成功');
            redirect(page_url('profile'));
        }
        flash('验证码错误或已过期', 'err');
        redirect(page_url('verify_email'));
    }
    redirect(page_url('verify_email'));
}

$GLOBALS['PAGE'] = 'verify_email';
render_header('邮箱验证');
?>
<section class="auth-wrap">
  <div class="auth-card">
    <h1>邮箱验证</h1>
    <p class="auth-sub"><?= $verified ? '当前账号邮箱已验证。' : ($verifyRequired ? '验证邮箱后，才能正常使用找回密码和重要通知。' : '当前邮箱尚未验证；本站未开启强制邮箱验证，你仍可正常使用，完成验证可提高账号安全。') ?></p>
    <?php if ($email === ''): ?>
      <div class="notice">你的账号还没有填写邮箱，请先到个人资料中填写。</div>
      <a class="btn-primary block" href="<?= e(page_url('profile')) ?>">去个人资料</a>
    <?php elseif ($verified): ?>
      <div class="notice ok"><?= e($email) ?> 已验证</div>
      <a class="btn-primary block" href="<?= e(page_url('profile')) ?>">返回个人中心</a>
    <?php else: ?>
      <div class="notice">当前邮箱未验证，验证码将发送到 <?= e($email) ?></div>
      <form method="post" class="auth-form">
        <?= csrf_field() ?>
        <input type="hidden" name="act" value="verify">
        <label>6 位验证码</label>
        <input type="text" name="code" maxlength="6" inputmode="numeric" required placeholder="请输入邮箱中的验证码">
        <button class="btn-primary block" type="submit">完成验证</button>
      </form>
      <form method="post" class="auth-form">
        <?= csrf_field() ?>
        <input type="hidden" name="act" value="send">
        <button class="btn-ghost block" type="submit">重新发送验证码</button>
      </form>
    <?php endif; ?>
    <div class="auth-links"><a href="<?= e(page_url('profile')) ?>">返回个人中心</a></div>
  </div>
</section>
<?php render_footer(); ?>
