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

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    try {
        verify_csrf_or_fail();
        $action = (string) ($_POST['action'] ?? '');
        if ($action === 'review_order') {
            $orderId = (int) ($_POST['order_id'] ?? 0);
            $decision = (string) ($_POST['decision'] ?? 'reject');
            $reviewed = review_task_order($orderId, $decision, (string) ($_POST['review_note'] ?? ''));
            flash('success', '订单 #' . ($reviewed['id'] ?? $orderId) . ' 已' . ($decision === 'approve' ? '审核通过并入账。' : '驳回并释放冻结金额。'));
            redirect('operations.php');
        }
    } catch (Throwable $exception) {
        flash('danger', $exception->getMessage());
        redirect('operations.php');
    }
}

$stats = get_operator_stats();
$pendingOrders = get_operator_orders('pending_review', 20);
$recentOrders = get_recent_reviewed_orders(12);
render_layout_start('运营审核台', '任务待审核队列与资金联动。', 'ops');
?>
<section class="mb-4 mb-lg-5">
  <div class="section-heading d-flex align-items-center justify-content-between flex-wrap gap-3 mb-3">
    <div>
      <div class="eyebrow">Ops board</div>
      <h1 class="section-title">任务审核台</h1>
    </div>
    <div class="supporting-copy">这是第一版后台切片：用于验证审核动作、冻结佣金与到账流水联动。</div>
  </div>
  <div class="stats-grid four-up">
    <article class="metric-card">
      <div class="metric-label">今日新增用户</div>
      <div class="metric-value"><?= h((string) $stats['today_users']) ?></div>
    </article>
    <article class="metric-card">
      <div class="metric-label">待审核订单</div>
      <div class="metric-value"><?= h((string) $stats['pending_reviews']) ?></div>
    </article>
    <article class="metric-card">
      <div class="metric-label">已通过订单</div>
      <div class="metric-value"><?= h((string) $stats['approved_orders']) ?></div>
    </article>
    <article class="metric-card">
      <div class="metric-label">累计入账</div>
      <div class="metric-value"><?= h(format_usdt($stats['credited_amount'])) ?></div>
    </article>
  </div>
</section>

<section class="mb-4 mb-lg-5">
  <div class="section-heading mb-3">
    <div class="eyebrow">Pending queue</div>
    <h2 class="section-title">待审核任务</h2>
  </div>
  <div class="row g-3">
    <?php if ($pendingOrders): ?>
      <?php foreach ($pendingOrders as $order): ?>
        <div class="col-12">
          <article class="panel ops-card h-100">
            <div class="d-flex justify-content-between gap-3 flex-wrap mb-3">
              <div>
                <div class="list-title"><?= h($order['task_title']) ?> · <?= h($order['platform_name']) ?></div>
                <div class="supporting-copy">用户 <?= h($order['email']) ?> · ID <?= h($order['user_code']) ?> · <?= h(vip_info((int) $order['vip_level'])['name']) ?></div>
              </div>
              <?= render_status_badge((string) $order['status']) ?>
            </div>
            <div class="task-meta-grid mb-3">
              <div>
                <div class="metric-label">提交时间</div>
                <div class="task-meta-value"><?= h(format_datetime((string) $order['submitted_at'])) ?></div>
              </div>
              <div>
                <div class="metric-label">领取时间</div>
                <div class="task-meta-value"><?= h(format_datetime((string) $order['claimed_at'])) ?></div>
              </div>
              <div>
                <div class="metric-label">任务佣金</div>
                <div class="task-meta-value"><?= h(format_usdt($order['reward_usdt'])) ?></div>
              </div>
              <div>
                <div class="metric-label">用户 VIP</div>
                <div class="task-meta-value"><?= h(vip_info((int) $order['vip_level'])['name']) ?></div>
              </div>
            </div>
            <div class="field-panel mb-3">
              <div class="metric-label">用户完成备注</div>
              <div class="field-value pre-wrap"><?= h((string) $order['proof_note']) ?></div>
            </div>
            <form method="post" class="d-grid gap-3">
              <input type="hidden" name="csrf_token" value="<?= h(csrf_token()) ?>">
              <input type="hidden" name="action" value="review_order">
              <input type="hidden" name="order_id" value="<?= h((string) $order['id']) ?>">
              <div>
                <label class="form-label" for="review-note-<?= h((string) $order['id']) ?>">审核说明</label>
                <textarea class="form-control" id="review-note-<?= h((string) $order['id']) ?>" name="review_note" rows="3" placeholder="例如：内容验证通过 / 备注不足，需重做"></textarea>
              </div>
              <div class="d-flex flex-wrap gap-2">
                <button class="btn btn-light btn-sm px-3" type="submit" name="decision" value="approve" data-confirm="确认审核通过并将佣金转入可用余额？">审核通过并入账</button>
                <button class="btn btn-outline-danger btn-sm px-3" type="submit" name="decision" value="reject" data-confirm="确认驳回并释放冻结金额？">驳回并释放冻结</button>
                <a class="btn btn-outline-light btn-sm px-3" href="order.php?id=<?= h((string) $order['id']) ?>&amp;ops=1">用户订单视图</a>
              </div>
            </form>
          </article>
        </div>
      <?php endforeach; ?>
    <?php else: ?>
      <div class="col-12">
        <div class="panel empty-state">
          <div class="empty-title">暂无待审核订单</div>
          <p class="mb-0">先用前台账号领取并提交一个任务，再回到这里执行审核。</p>
        </div>
      </div>
    <?php endif; ?>
  </div>
</section>

<section class="mb-4">
  <div class="section-heading mb-3">
    <div class="eyebrow">History</div>
    <h2 class="section-title">最近已处理订单</h2>
  </div>
  <div class="panel p-0 overflow-hidden">
    <?php if ($recentOrders): ?>
      <div class="list-shell">
        <?php foreach ($recentOrders as $order): ?>
          <div class="list-row">
            <div>
              <div class="list-title">#<?= h((string) $order['id']) ?> · <?= h($order['task_title']) ?></div>
              <div class="supporting-copy"><?= h($order['email']) ?> · <?= h(format_datetime((string) $order['reviewed_at'])) ?></div>
            </div>
            <div class="d-flex align-items-center gap-3 ms-auto">
              <div class="text-end">
                <div class="list-amount"><?= h(format_usdt($order['reward_usdt'])) ?></div>
              </div>
              <?= render_status_badge((string) $order['status']) ?>
            </div>
          </div>
        <?php endforeach; ?>
      </div>
    <?php else: ?>
      <div class="empty-state">
        <div class="empty-title">暂无历史审核记录</div>
        <p class="mb-0">处理第一笔任务后，这里会形成后台审核时间线。</p>
      </div>
    <?php endif; ?>
  </div>
</section>
<?php render_layout_end(); ?>
