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

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    flash('warning', '首页认证入口已升级为独立页面，请从“启动页 / 注册 / 登录”继续。');
    redirect(start_page_url());
}

$user = current_user();
$previewMode = !$user;
$stats = $user
    ? get_dashboard_stats((int) $user['id'])
    : [
        'available_balance' => 5560.25,
        'frozen_balance' => 120.00,
        'total_balance' => 5680.25,
        'today_earnings' => 1000.00,
        'month_earnings' => 18560.20,
        'today_tasks' => 30,
        'active_orders' => 3,
        'completed_orders' => 12,
    ];
$recentOrders = $user ? get_user_orders((int) $user['id'], 4) : [];
$showcaseTasks = array_slice(array_values(task_catalog()), 0, 4);
$quickActions = [
    ['label' => '充值', 'icon' => 'deposit', 'href' => 'wallet.php#deposit-panel'],
    ['label' => '提现', 'icon' => 'withdraw', 'href' => 'wallet.php#withdraw-panel'],
    ['label' => '转账', 'icon' => 'swap', 'href' => 'wallet.php#transfer-panel'],
    ['label' => '记录', 'icon' => 'history', 'href' => 'wallet.php#history-panel'],
];
$taskShortcuts = [
    ['label' => '看视频', 'icon' => 'play', 'href' => 'task.php?category=video', 'accent' => 'accent-red'],
    ['label' => '点赞', 'icon' => 'heart', 'href' => 'task.php?category=like', 'accent' => 'accent-pink'],
    ['label' => '关注', 'icon' => 'spark', 'href' => 'task.php?category=social', 'accent' => 'accent-violet'],
    ['label' => '网站', 'icon' => 'globe', 'href' => 'task.php?category=web', 'accent' => 'accent-blue'],
    ['label' => '应用', 'icon' => 'download', 'href' => 'task.php?category=app', 'accent' => 'accent-orange'],
];
$onboardingSteps = [
    ['step' => '01', 'title' => '启动页', 'desc' => '先看平台介绍、流程说明和开户注册入口。'],
    ['step' => '02', 'title' => '邮箱注册', 'desc' => '填写邮箱和密码，系统准备发送 6 位验证码。'],
    ['step' => '03', 'title' => '邮箱验证码', 'desc' => '输入邮件里的 6 位数字，验证通过后再正式建号。'],
    ['step' => '04', 'title' => '账号登录', 'desc' => '用已验证邮箱登录，再进入首页 / 任务 / VIP / 钱包 / 我的。'],
];

render_layout_start('首页', '任务返佣平台首页，提供资产总览、快捷任务、VIP 入口与真实邮箱开户流程。', 'home');
?>
<section class="app-page-section">
  <article class="app-card gradient-card hero-card">
    <div class="d-flex align-items-start justify-content-between gap-3">
      <div>
        <div class="tiny-eyebrow">Hello, <?= h(app_user_name($user)) ?></div>
        <h1 class="app-hero-title">任务返佣平台</h1>
        <p class="app-hero-copy mb-0">做任务，赚佣金。先领取任务，再等待审核入账。</p>
      </div>
      <div class="hero-avatar"><?= h(app_user_initial($user)) ?></div>
    </div>

    <div class="balance-card mt-3">
      <div class="d-flex align-items-start justify-content-between gap-3">
        <div>
          <div class="mini-label">总资产（USDT）</div>
          <div class="balance-value"><?= h(number_format((float) $stats['total_balance'], 2)) ?></div>
          <div class="balance-sub">可用 <?= h(number_format((float) $stats['available_balance'], 2)) ?> · 冻结 <?= h(number_format((float) $stats['frozen_balance'], 2)) ?></div>
        </div>
        <span class="vip-chip-large"><?= h($user ? vip_info((int) $user['vip_level'])['name'] : 'VIP3') ?></span>
      </div>

      <div class="quick-action-grid mt-3">
        <?php foreach ($quickActions as $action): ?>
          <a class="quick-action" href="<?= h($action['href']) ?>">
            <span class="quick-action-icon"><?= app_icon_svg((string) $action['icon']) ?></span>
            <span class="quick-action-label"><?= h((string) $action['label']) ?></span>
          </a>
        <?php endforeach; ?>
      </div>
    </div>

    <?php if ($previewMode): ?>
      <div class="app-inline-note mt-3">当前显示的是参考图风格预览数据。登录后会显示你的真实余额、任务与流水。</div>
    <?php endif; ?>
  </article>
</section>

<section class="app-page-section">
  <div class="section-heading-app mb-3">
    <div class="tiny-eyebrow">今日数据</div>
    <h2 class="app-section-title">账户看板</h2>
  </div>
  <div class="mini-stat-grid three-cols">
    <article class="app-card stat-card">
      <div class="mini-label">今日任务</div>
      <div class="stat-value"><?= h((string) $stats['today_tasks']) ?>/30</div>
      <div class="stat-meta">已开始任务数</div>
    </article>
    <article class="app-card stat-card">
      <div class="mini-label">今日收益</div>
      <div class="stat-value"><?= h(number_format((float) $stats['today_earnings'], 2)) ?></div>
      <div class="stat-meta">USDT</div>
    </article>
    <article class="app-card stat-card">
      <div class="mini-label">本月总收益</div>
      <div class="stat-value"><?= h(number_format((float) $stats['month_earnings'], 2)) ?></div>
      <div class="stat-meta">USDT</div>
    </article>
  </div>
</section>

<section class="app-page-section">
  <div class="section-heading-app mb-3">
    <div class="tiny-eyebrow">快捷任务</div>
    <h2 class="app-section-title">常用入口</h2>
  </div>
  <article class="app-card">
    <div class="feature-shortcuts">
      <?php foreach ($taskShortcuts as $shortcut): ?>
        <a class="feature-shortcut" href="<?= h($shortcut['href']) ?>">
          <span class="feature-icon <?= h((string) $shortcut['accent']) ?>"><?= app_icon_svg((string) $shortcut['icon']) ?></span>
          <span><?= h((string) $shortcut['label']) ?></span>
        </a>
      <?php endforeach; ?>
    </div>
  </article>
</section>

<section class="app-page-section">
  <div class="section-heading-app mb-3">
    <div class="tiny-eyebrow">热门任务</div>
    <h2 class="app-section-title">推荐任务</h2>
  </div>
  <div class="app-task-list">
    <?php foreach ($showcaseTasks as $task): ?>
      <?php $meta = task_visual_meta($task); ?>
      <a class="app-card task-row-card" href="task.php?task=<?= h((string) $task['slug']) ?>">
        <span class="task-platform-icon <?= h((string) $meta['accent']) ?>"><?= app_icon_svg((string) $meta['icon']) ?></span>
        <span class="task-row-content">
          <span class="task-row-title"><?= h((string) $task['title']) ?></span>
          <span class="task-row-subtitle"><?= h((string) $meta['brand']) ?> · <?= h((string) $task['summary']) ?></span>
        </span>
        <span class="task-row-side">
          <span class="task-row-reward">+<?= h(number_format((float) $task['reward'], 2)) ?></span>
          <span class="task-row-arrow"><?= app_icon_svg('chevron-right') ?></span>
        </span>
      </a>
    <?php endforeach; ?>
  </div>
</section>

<?php if ($user): ?>
<section class="app-page-section">
  <div class="section-heading-app mb-3">
    <div class="tiny-eyebrow">最近动态</div>
    <h2 class="app-section-title">我的任务</h2>
  </div>
  <article class="app-card">
    <?php if ($recentOrders): ?>
      <div class="app-list-stack">
        <?php foreach ($recentOrders as $order): ?>
          <a class="app-list-item" href="order.php?id=<?= h((string) $order['id']) ?>">
            <span>
              <span class="list-title-strong">#<?= h((string) $order['id']) ?> · <?= h((string) $order['task_title']) ?></span>
              <span class="list-meta-line"><?= h(format_datetime((string) $order['claimed_at'])) ?> · <?= h(format_usdt($order['reward_usdt'])) ?></span>
            </span>
            <span><?= render_status_badge((string) $order['status']) ?></span>
          </a>
        <?php endforeach; ?>
      </div>
    <?php else: ?>
      <div class="empty-tip">你还没有任务记录。先进入“任务”页领取第一笔佣金。</div>
    <?php endif; ?>
  </article>
</section>
<?php else: ?>
<section class="app-page-section" id="auth">
  <div class="section-heading-app mb-3">
    <div class="tiny-eyebrow">邮箱入口</div>
    <h2 class="app-section-title">真实开户流程</h2>
  </div>
  <article class="app-card auth-card-panel">
    <div class="auth-panel-head">
      <div>
        <div class="list-title-strong">4 步完成邮箱开户</div>
        <div class="list-meta-line">现在已经拆成独立页面：启动页 → 邮箱注册 → 验证码 → 登录，避免跳过验证码直接建号。</div>
      </div>
      <span class="tag-chip">真实流程</span>
    </div>

    <div class="onboarding-flow-grid">
      <?php foreach ($onboardingSteps as $step): ?>
        <div class="onboarding-step-card">
          <div class="step-kicker">STEP <?= h((string) $step['step']) ?></div>
          <div class="list-title-strong mt-2"><?= h((string) $step['title']) ?></div>
          <div class="list-meta-line mt-1"><?= h((string) $step['desc']) ?></div>
        </div>
      <?php endforeach; ?>
    </div>

    <div class="auth-link-grid mt-3">
      <a class="btn btn-gradient" href="<?= h(start_page_url()) ?>">打开启动页</a>
      <a class="btn btn-outline-light" href="<?= h(register_page_url()) ?>">邮箱注册</a>
      <a class="btn btn-outline-light" href="<?= h(login_page_url()) ?>">已有账号登录</a>
    </div>

    <div class="app-inline-note mt-3">验证码会发送到你填写的邮箱；如果当前 SMTP 还没配置，页面会真实提示发送失败，不会假装成功。</div>
  </article>
</section>
<?php endif; ?>
<?php render_layout_end(); ?>
