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

$user = current_user();
$previewMode = !$user;
$profileUser = $user ?: [
    'email' => 'example@email.com',
    'user_code' => '100086',
    'vip_level' => 3,
];
$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,
    ];
$menuItems = [
    ['title' => '我的资产', 'desc' => '查看余额与资金流水', 'href' => 'wallet.php', 'icon' => 'wallet'],
    ['title' => '我的任务', 'desc' => '查看任务大厅与订单', 'href' => 'task.php', 'icon' => 'tasks'],
    ['title' => 'VIP 中心', 'desc' => '选择更高返佣等级', 'href' => 'vip.php', 'icon' => 'vip'],
    ['title' => '邀请好友', 'desc' => '后续可扩展邀请返佣', 'href' => 'index.php', 'icon' => 'spark'],
    ['title' => '安全中心', 'desc' => '当前账户与系统状态', 'href' => 'healthz.php', 'icon' => 'lock'],
    ['title' => '联系客服', 'desc' => '下一步接入内置 IM', 'href' => '#support-panel', 'icon' => 'support'],
];

render_layout_start('我的', '个人中心页，展示账号信息、常用菜单入口与客服/后台预览。', 'profile');
?>
<section class="app-page-section">
  <article class="app-card gradient-card hero-card">
    <div class="d-flex align-items-center gap-3">
      <div class="profile-avatar-large"><?= h(app_user_initial($profileUser)) ?></div>
      <div class="flex-grow-1 min-w-0">
        <div class="tiny-eyebrow">我的（个人中心）</div>
        <h1 class="app-section-title mb-1"><?= h(app_user_name($profileUser)) ?></h1>
        <div class="list-meta-line"><?= h((string) $profileUser['email']) ?> · ID <?= h((string) $profileUser['user_code']) ?></div>
      </div>
      <span class="vip-chip-large"><?= h(vip_info((int) $profileUser['vip_level'])['name']) ?></span>
    </div>
    <?php if ($previewMode): ?>
      <div class="app-inline-note mt-3">当前是“我的”页面视觉预览。登录后会显示你的真实账户资料与任务状态。</div>
    <?php endif; ?>
  </article>
</section>

<section class="app-page-section">
  <div class="mini-stat-grid three-cols">
    <article class="app-card stat-card">
      <div class="mini-label">总资产</div>
      <div class="stat-value"><?= h(number_format((float) $stats['total_balance'], 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((string) $stats['active_orders']) ?></div>
      <div class="stat-meta">任务</div>
    </article>
    <article class="app-card stat-card">
      <div class="mini-label">已完成</div>
      <div class="stat-value"><?= h((string) $stats['completed_orders']) ?></div>
      <div class="stat-meta">任务</div>
    </article>
  </div>
</section>

<section class="app-page-section">
  <article class="app-card">
    <div class="section-heading-app mb-3">
      <div class="tiny-eyebrow">常用功能</div>
      <h2 class="app-section-title">菜单入口</h2>
    </div>
    <div class="app-list-stack">
      <?php foreach ($menuItems as $item): ?>
        <a class="app-list-item" href="<?= h((string) $item['href']) ?>">
          <span class="menu-item-left">
            <span class="task-platform-icon accent-blue small-menu-icon"><?= app_icon_svg((string) $item['icon']) ?></span>
            <span>
              <span class="list-title-strong"><?= h((string) $item['title']) ?></span>
              <span class="list-meta-line"><?= h((string) $item['desc']) ?></span>
            </span>
          </span>
          <span class="task-row-arrow"><?= app_icon_svg('chevron-right') ?></span>
        </a>
      <?php endforeach; ?>
      <?php if ($user): ?>
        <a class="app-list-item" href="logout.php">
          <span class="menu-item-left">
            <span class="task-platform-icon accent-pink small-menu-icon"><?= app_icon_svg('withdraw') ?></span>
            <span>
              <span class="list-title-strong">退出登录</span>
              <span class="list-meta-line">安全退出当前账号</span>
            </span>
          </span>
          <span class="task-row-arrow"><?= app_icon_svg('chevron-right') ?></span>
        </a>
      <?php endif; ?>
    </div>
  </article>
</section>

<section class="app-page-section" id="support-panel">
  <article class="app-card">
    <div class="section-heading-app mb-3">
      <div class="tiny-eyebrow">客服 / 后台</div>
      <h2 class="app-section-title">下一步建议</h2>
    </div>
    <div class="app-inline-note mb-3">你前面确认过要做“内置 IM 客服”。这一步我先把入口样式放好了；下一步我可以继续帮你把“联系客服 → 建会话 → 发消息 → 后台客服工作台”接成真实页面。</div>
    <div class="d-grid gap-2">
      <a class="btn btn-gradient" href="operations.php">先看后台工作台预览</a>
      <a class="btn btn-outline-light" href="healthz.php">查看系统健康检查</a>
    </div>
  </article>
</section>
<?php render_layout_end(); ?>
