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

$user = current_user();
$previewMode = !$user;
$wallet = $user
    ? wallet_snapshot((int) $user['id'])
    : ['available_balance' => 5560.25, 'frozen_balance' => 120.00];
$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,
    ];
$logs = $user ? get_wallet_logs((int) $user['id'], 8) : [
    ['entry_type' => 'task_credit', 'amount' => 1000.00, 'frozen_amount' => 0.00, 'note' => '任务佣金到账', 'created_at' => gmdate('Y-m-d H:i:s')],
    ['entry_type' => 'task_reject', 'amount' => 0.00, 'frozen_amount' => -50.00, 'note' => '未通过审核，释放冻结金额', 'created_at' => gmdate('Y-m-d H:i:s', time() - 3600)],
    ['entry_type' => 'task_freeze', 'amount' => 0.00, 'frozen_amount' => 100.00, 'note' => '任务提交后冻结佣金', 'created_at' => gmdate('Y-m-d H:i:s', time() - 7200)],
];
$available = (float) $wallet['available_balance'];
$fee = $available > 0 ? min(1.00, $available) : 1.00;
$estimated = max(0, $available - $fee);
$quickActions = [
    ['label' => '充值', 'icon' => 'deposit', 'href' => '#deposit-panel'],
    ['label' => '提现', 'icon' => 'withdraw', 'href' => '#withdraw-panel'],
    ['label' => '转账', 'icon' => 'swap', 'href' => '#transfer-panel'],
    ['label' => '记录', 'icon' => 'history', 'href' => '#history-panel'],
];

render_layout_start('钱包', '钱包页，展示可用余额、冻结余额、提现卡片与资金流水记录。', 'wallet');
?>
<section class="app-page-section">
  <article class="app-card gradient-card hero-card">
    <div class="tiny-eyebrow">我的钱包</div>
    <h1 class="app-hero-title compact-title">资金总览</h1>
    <div class="balance-card mt-3">
      <div class="mini-label">可用余额（USDT）</div>
      <div class="balance-value"><?= h(number_format((float) $wallet['available_balance'], 2)) ?></div>
      <div class="balance-sub">总余额 <?= h(number_format((float) $stats['total_balance'], 2)) ?> · 冻结 <?= h(number_format((float) $wallet['frozen_balance'], 2)) ?></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="mini-stat-grid three-cols">
    <article class="app-card stat-card">
      <div class="mini-label">可用余额</div>
      <div class="stat-value"><?= h(number_format((float) $wallet['available_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(number_format((float) $wallet['frozen_balance'], 2)) ?></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['month_earnings'], 2)) ?></div>
      <div class="stat-meta">本月收益</div>
    </article>
  </div>
</section>

<section class="app-page-section" id="deposit-panel">
  <article class="app-card">
    <div class="section-heading-app mb-3">
      <div class="tiny-eyebrow">充值</div>
      <h2 class="app-section-title">USDT 充值说明</h2>
    </div>
    <div class="detail-meta-grid">
      <div>
        <div class="mini-label">网络</div>
        <div class="detail-mini-value">TRC20</div>
      </div>
      <div>
        <div class="mini-label">到账方式</div>
        <div class="detail-mini-value">人工审核</div>
      </div>
      <div>
        <div class="mini-label">处理时间</div>
        <div class="detail-mini-value">5 - 15 分钟</div>
      </div>
    </div>
    <div class="app-inline-note mt-3">第一版先保留参考图中的充值入口样式，下一步我可以继续帮你接“充值 / 提现 → 客服工单 / 内置 IM”。</div>
  </article>
</section>

<section class="app-page-section" id="withdraw-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="withdraw-card-shell">
      <div class="wallet-field-row">
        <span>提现地址</span>
        <span>USDT-TRC20</span>
      </div>
      <div class="wallet-field-input">请输入 USDT 钱包地址</div>
      <div class="wallet-field-row mt-3">
        <span>提现金额</span>
        <span>全部</span>
      </div>
      <div class="wallet-field-input"><?= h(number_format($available > 0 ? $available : 1000, 2)) ?> USDT</div>
      <div class="wallet-summary-list mt-3">
        <div><span>手续费</span><strong><?= h(number_format($fee, 2)) ?> USDT</strong></div>
        <div><span>实际到账</span><strong><?= h(number_format($estimated, 2)) ?> USDT</strong></div>
      </div>
      <a class="btn btn-gradient w-100 mt-3" href="profile.php#support-panel">联系客服提现</a>
    </div>
  </article>
</section>

<section class="app-page-section" id="transfer-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">这一块我先按照你发的设计图放了入口和卡片结构。下一步可以继续接“转账表单 + 审核记录 + 钱包扣减逻辑”。</div>
  </article>
</section>

<section class="app-page-section" id="history-panel">
  <article class="app-card">
    <div class="section-heading-app mb-3">
      <div class="tiny-eyebrow">资金流水</div>
      <h2 class="app-section-title">最近记录</h2>
    </div>
    <?php if ($logs): ?>
      <div class="app-list-stack">
        <?php foreach ($logs as $log): ?>
          <?php
          $delta = (float) $log['amount'] + (float) $log['frozen_amount'];
          $deltaClass = $delta > 0 ? 'is-positive' : ($delta < 0 ? 'is-negative' : '');
          ?>
          <div class="app-list-item static-row">
            <span>
              <span class="list-title-strong"><?= h(wallet_entry_label((string) $log['entry_type'])) ?></span>
              <span class="list-meta-line"><?= h((string) $log['note']) ?> · <?= h(format_datetime((string) $log['created_at'])) ?></span>
            </span>
            <span class="ledger-value-inline <?= h($deltaClass) ?>"><?= h(format_delta($delta)) ?> USDT</span>
          </div>
        <?php endforeach; ?>
      </div>
    <?php else: ?>
      <div class="empty-tip">目前还没有资金流水。先完成一次任务提交与审核，就会在这里看到变化。</div>
    <?php endif; ?>
  </article>
</section>
<?php render_layout_end(); ?>
