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

$opsMode = (string) ($_GET['ops'] ?? '') === '1';
$orderId = (int) ($_GET['id'] ?? 0);
$user = $opsMode ? current_user() : require_user();
$order = get_order_by_id($orderId, $opsMode ? null : (int) $user['id']);
if (!$order) {
    http_response_code(404);
    render_layout_start('订单不存在', '未找到指定订单。', 'tasks');
    ?>
    <div class="panel">
      <div class="eyebrow">404</div>
      <h1 class="section-title">订单不存在</h1>
      <p class="supporting-copy mb-3">请返回概览查看你的任务记录。</p>
      <a class="btn btn-light btn-sm" href="index.php">返回概览</a>
    </div>
    <?php
    render_layout_end();
    exit;
}

$wallet = wallet_snapshot((int) $order['user_id']);
$task = task_by_slug((string) $order['task_slug']);
$unlockTimestamp = strtotime((string) $order['claimed_at']) + (int) $order['countdown_seconds'];
render_layout_start('订单 #' . $orderId, '订单详情、审核结果与钱包联动。', $opsMode ? 'ops' : 'tasks');
?>
<section class="mb-4">
  <div class="row g-4">
    <div class="col-lg-7">
      <article class="panel h-100">
        <div class="d-flex align-items-start justify-content-between gap-3 flex-wrap mb-3">
          <div>
            <div class="eyebrow">Order detail</div>
            <h1 class="section-title mb-2">订单 #<?= h((string) $order['id']) ?> · <?= h($order['task_title']) ?></h1>
            <div class="supporting-copy"><?= h($order['platform_name']) ?> · <?= h($order['task_type']) ?></div>
          </div>
          <?= render_status_badge((string) $order['status']) ?>
        </div>

        <div class="stats-grid three-up mb-4">
          <div class="metric-card compact">
            <div class="metric-label">佣金</div>
            <div class="metric-value"><?= h(format_usdt($order['reward_usdt'])) ?></div>
          </div>
          <div class="metric-card compact">
            <div class="metric-label">VIP 限制</div>
            <div class="metric-value">VIP<?= h((string) $order['vip_required']) ?></div>
          </div>
          <div class="metric-card compact">
            <div class="metric-label">倒计时</div>
            <div class="metric-value"><?= h((string) $order['countdown_seconds']) ?> 秒</div>
          </div>
        </div>

        <div class="timeline-list mb-4">
          <div class="timeline-item">
            <div class="timeline-index">1</div>
            <div>
              <div class="list-title">已领取</div>
              <div class="supporting-copy"><?= h(format_datetime($order['claimed_at'])) ?></div>
            </div>
          </div>
          <div class="timeline-item<?= !empty($order['submitted_at']) ? '' : ' is-dimmed' ?>">
            <div class="timeline-index">2</div>
            <div>
              <div class="list-title">已提交审核</div>
              <div class="supporting-copy"><?= h(format_datetime($order['submitted_at'])) ?></div>
            </div>
          </div>
          <div class="timeline-item<?= !empty($order['reviewed_at']) ? '' : ' is-dimmed' ?>">
            <div class="timeline-index">3</div>
            <div>
              <div class="list-title">审核完成</div>
              <div class="supporting-copy"><?= h(format_datetime($order['reviewed_at'])) ?></div>
            </div>
          </div>
        </div>

        <?php if ((string) $order['status'] === 'claimed'): ?>
          <div class="countdown-card mb-3">
            <div class="metric-label">剩余倒计时</div>
            <div class="countdown-value" data-countdown="<?= h((string) $unlockTimestamp) ?>"><?= h((string) max(0, (int) ($unlockTimestamp - time()))) ?> 秒</div>
            <div class="supporting-copy">完成后回到任务详情页提交完成备注。</div>
          </div>
          <a class="btn btn-light btn-sm" href="task.php?task=<?= h((string) $order['task_slug']) ?>">返回任务详情提交</a>
        <?php elseif ((string) $order['status'] === 'pending_review'): ?>
          <div class="notice-card notice-warning mb-3">
            <div class="notice-title">佣金冻结中</div>
            <p class="mb-0">该订单已提交审核，<?= h(format_usdt($order['reward_usdt'])) ?> 已计入冻结余额，等待运营审批后转入可用余额。</p>
          </div>
        <?php elseif ((string) $order['status'] === 'approved'): ?>
          <div class="notice-card mb-3">
            <div class="notice-title">佣金已入账</div>
            <p class="mb-0">审核通过后，佣金已转入可用余额，可在钱包页查看流水。</p>
          </div>
        <?php elseif ((string) $order['status'] === 'rejected'): ?>
          <div class="notice-card notice-muted mb-3">
            <div class="notice-title">任务未通过审核</div>
            <p class="mb-0">冻结佣金已释放。你可以返回任务大厅领取其他任务继续测试流程。</p>
          </div>
        <?php endif; ?>

        <?php if (!empty($order['proof_note'])): ?>
          <div class="field-panel mt-3">
            <div class="metric-label">完成备注</div>
            <div class="field-value pre-wrap"><?= h((string) $order['proof_note']) ?></div>
          </div>
        <?php endif; ?>

        <?php if (!empty($order['review_note'])): ?>
          <div class="field-panel mt-3">
            <div class="metric-label">审核说明</div>
            <div class="field-value pre-wrap"><?= h((string) $order['review_note']) ?></div>
          </div>
        <?php endif; ?>
      </article>
    </div>
    <div class="col-lg-5">
      <article class="panel h-100">
        <div class="eyebrow">Wallet effect</div>
        <h2 class="panel-title">钱包联动结果</h2>
        <div class="stats-grid two-up mt-3">
          <div class="metric-card compact">
            <div class="metric-label">可提现余额</div>
            <div class="metric-value"><?= h(format_usdt($wallet['available_balance'])) ?></div>
          </div>
          <div class="metric-card compact">
            <div class="metric-label">冻结余额</div>
            <div class="metric-value"><?= h(format_usdt($wallet['frozen_balance'])) ?></div>
          </div>
        </div>
        <div class="notice-card mt-3">
          <div class="notice-title">下一步</div>
          <p class="mb-0">打开钱包查看资金流水，或进入运营台执行审核动作，体验“冻结 → 入账/释放”的完整链路。</p>
        </div>
        <div class="d-flex flex-wrap gap-2 mt-3">
          <a class="btn btn-light btn-sm px-3" href="wallet.php">查看钱包</a>
          <a class="btn btn-outline-light btn-sm px-3" href="operations.php">打开运营台</a>
          <?php if ($task): ?>
            <a class="btn btn-outline-light btn-sm px-3" href="task.php?task=<?= h((string) $task['slug']) ?>">返回任务</a>
          <?php endif; ?>
        </div>
      </article>
    </div>
  </div>
</section>
<?php render_layout_end(); ?>
