<?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();
        $user = authenticate_user($email, (string) ($_POST['password'] ?? ''));
        flash('success', '欢迎回来，当前账号 ' . ($user['user_code'] ?? '') . ' 已登录。');
        redirect($redirectTarget);
    } catch (Throwable $exception) {
        flash('danger', $exception->getMessage());
        redirect(login_page_url($redirectTarget, ['email' => $email]));
    }
}

render_layout_start('账号登录', '登录页，使用已验证邮箱与密码进入前台 5 个页面。', 'auth');
?>
<section class="app-page-section auth-page-shell">
  <article class="app-card gradient-card hero-card">
    <div class="tiny-eyebrow">Step 4 / 4</div>
    <h1 class="app-hero-title compact-title">账号登录</h1>
    <p class="app-hero-copy mb-0">这是开户链路的最后一步。登录成功后，你就能进入首页、任务、VIP、钱包、我的 5 个主页面。</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="login-email">邮箱地址</label>
          <input class="form-control" id="login-email" type="email" name="email" placeholder="example@email.com" value="<?= h($prefillEmail) ?>" required>
        </div>
        <div>
          <label class="form-label" for="login-password">密码</label>
          <input class="form-control" id="login-password" type="password" name="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">首页</span>
            <span class="list-meta-line">查看真实账户余额、最近任务和快捷入口。</span>
          </span>
        </div>
        <div class="app-list-item static-row">
          <span>
            <span class="list-title-strong">任务 / VIP / 钱包 / 我的</span>
            <span class="list-meta-line">这些页面都已经可以打开，任务和 VIP 也有真实数据库逻辑。</span>
          </span>
        </div>
        <div class="app-list-item static-row">
          <span>
            <span class="list-title-strong">以后还能继续扩展</span>
            <span class="list-meta-line">比如充值、提现、转账、邀请、安全中心和客服系统。</span>
          </span>
        </div>
      </div>

      <div class="auth-link-grid mt-3">
        <a class="btn btn-outline-light" href="<?= h(register_page_url($redirectTarget, $prefillEmail !== '' ? ['email' => $prefillEmail] : [])) ?>">没有账号，去注册</a>
        <a class="btn btn-outline-light" href="<?= h(start_page_url($redirectTarget)) ?>">返回启动页</a>
      </div>
    </article>
  </div>
</section>
<?php render_layout_end(); ?>
