<?php
$typeMap = [
  'product' => ['status-preparing', 'สินค้า (Product)'],
  'seller'  => ['status-official', 'ร้านค้า (Seller)'],
  'order'   => ['status-active', 'คำสั่งซื้อ (Order)'],
  'review'  => ['status-warning', 'รีวิว (Review)'],
];

$statusMap = [
  'pending'   => ['status-pending', 'รอดำเนินการ (Pending)'],
  'submitted' => ['status-pending', 'รอดำเนินการ (Pending)'],
  'reviewing' => ['status-preparing', 'กำลังตรวจสอบ (Reviewing)'],
  'resolved'  => ['status-active', 'แก้ไขแล้ว (Resolved)'],
  'rejected'  => ['status-danger', 'ปฏิเสธ (Rejected)'],
];
?>
<div class="d-flex flex-column flex-md-row justify-content-between align-items-md-center gap-2 mb-3">
  <div>
    <h4 class="fw-bold mb-1 text-dark">รายงานปัญหาจากผู้ใช้งาน (Reports &amp; Disputes)</h4>
    <p class="text-muted small mb-0">คิวตรวจสอบข้อร้องเรียน รายงานสินค้าผิดกฎหมาย รายงานพฤติกรรมร้านค้า และรีวิวที่ไม่เหมาะสม</p>
  </div>
</div>

<!-- ── Status Counter Badges ───────────────────────────── -->
<div class="d-flex flex-wrap gap-2 mb-3">
  <?php
  $statusTabs = ['all'=>'ทั้งหมด', 'pending'=>'รอดำเนินการ', 'reviewing'=>'กำลังตรวจ', 'resolved'=>'แก้ไขแล้ว', 'rejected'=>'ปฏิเสธ'];
  foreach ($statusTabs as $k => $l):
    $cnt = $k === 'all' ? array_sum($counts ?? []) : ($counts[$k] ?? 0);
  ?>
  <a href="?status=<?= $k ?>&report_type=<?= e($filters['report_type'] ?? 'all') ?>"
     class="btn btn-sm <?= ($filters['status'] ?? 'all') === $k ? 'btn-primary' : 'btn-white border text-muted' ?>" style="font-size: 0.8rem;">
    <?= $l ?> <span class="badge bg-light text-dark border ms-1"><?= $cnt ?></span>
  </a>
  <?php endforeach; ?>
</div>

<!-- ── Filter Bar ──────────────────────────────────────── -->
<div class="admin-filter-bar mb-3">
  <form method="GET" class="row g-2 align-items-end">
    <input type="hidden" name="status" value="<?= e($filters['status'] ?? 'all') ?>">
    <div class="col-md-5">
      <label class="form-label small fw-semibold text-muted mb-1">ค้นหารายงาน</label>
      <div class="input-group input-group-sm">
        <span class="input-group-text bg-light"><i class="bi bi-search text-muted"></i></span>
        <input type="text" name="q" value="<?= e($filters['q'] ?? '') ?>" class="form-control" placeholder="ชื่อผู้รายงาน, ข้อความรายงาน...">
      </div>
    </div>
    <div class="col-6 col-md-4">
      <label class="form-label small fw-semibold text-muted mb-1">ประเภทที่ถูกรายงาน</label>
      <select name="report_type" class="form-select form-select-sm">
        <option value="all">ทุกประเภท</option>
        <option value="product" <?= ($filters['report_type'] ?? '') === 'product' ? 'selected' : '' ?>>สินค้า (Product)</option>
        <option value="seller" <?= ($filters['report_type'] ?? '') === 'seller' ? 'selected' : '' ?>>ร้านค้า (Seller)</option>
        <option value="order" <?= ($filters['report_type'] ?? '') === 'order' ? 'selected' : '' ?>>คำสั่งซื้อ (Order)</option>
        <option value="review" <?= ($filters['report_type'] ?? '') === 'review' ? 'selected' : '' ?>>รีวิว (Review)</option>
      </select>
    </div>
    <div class="col-6 col-md-3 d-flex gap-2">
      <button type="submit" class="btn btn-primary btn-sm w-100"><i class="bi bi-funnel me-1"></i>ค้นหา</button>
      <?php if (!empty($filters['q']) || ($filters['report_type'] ?? 'all') !== 'all' || ($filters['status'] ?? 'all') !== 'all'): ?>
      <a href="<?= url('/admin/reports') ?>" class="btn btn-outline-secondary btn-sm" title="ล้างการกรอง"><i class="bi bi-x-lg"></i></a>
      <?php endif; ?>
    </div>
  </form>
</div>

<!-- ── Data Table Container ────────────────────────────── -->
<div class="admin-table-container">
  <div class="admin-table-header">
    <div class="d-flex align-items-center gap-2">
      <i class="bi bi-flag text-primary"></i>
      <span class="fw-bold text-dark" style="font-size: 0.9rem;">รายการรายงานปัญหา</span>
      <span class="badge bg-light text-muted border ms-1"><?= number_format($total ?? 0) ?> รายการ</span>
    </div>
  </div>

  <div class="table-responsive d-none d-md-block">
    <table class="admin-table">
      <thead>
        <tr>
          <th width="70">ID</th>
          <th>ผู้รายงาน</th>
          <th>ประเภทเป้าหมาย</th>
          <th>รายละเอียดรายงาน</th>
          <th>สถานะ</th>
          <th>ผู้ดูแล / ผู้สรุป</th>
          <th>วันที่ส่ง</th>
          <th class="text-end">การจัดการ</th>
        </tr>
      </thead>
      <tbody>
        <?php if (empty($reports)): ?>
        <tr>
          <td colspan="8" class="text-center text-muted py-4">ไม่พบรายงานปัญหาตามเงื่อนไขที่เลือก</td>
        </tr>
        <?php else: foreach ($reports as $r): ?>
        <tr>
          <td class="font-monospace small text-muted">#<?= (int)$r['id'] ?></td>
          <td>
            <div class="fw-semibold text-dark small"><?= e(trim(($r['reporter_first_name'] ?? '') . ' ' . ($r['reporter_last_name'] ?? ''))) ?></div>
            <div class="text-muted small">@<?= e($r['reporter_username'] ?? '—') ?></div>
          </td>
          <td>
            <?php
            [$tpClass, $tpLabel] = $typeMap[$r['report_type']] ?? ['status-secondary', $r['report_type']];
            ?>
            <span class="status-pill <?= $tpClass ?>">
              <?= $tpLabel ?> #<?= (int)$r['target_id'] ?>
            </span>
          </td>
          <td>
            <div class="text-dark small fw-medium" style="max-width: 260px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;" title="<?= e($r['description']) ?>">
              <?= e($r['description']) ?>
            </div>
          </td>
          <td>
            <?php
            [$stClass, $stLabel] = $statusMap[$r['status']] ?? ['status-secondary', $r['status']];
            ?>
            <span class="status-pill <?= $stClass ?>"><?= $stLabel ?></span>
          </td>
          <td class="small text-muted">
            <?= !empty($r['assigned_admin_username']) ? '@' . e($r['assigned_admin_username']) : '<span class="text-muted">—</span>' ?>
          </td>
          <td class="text-muted small"><?= date('d/m/Y H:i', strtotime($r['created_at'])) ?></td>
          <td class="text-end">
            <a href="<?= url('/admin/reports/' . (int)$r['id']) ?>" class="btn btn-outline-primary btn-sm py-1 px-2" style="font-size: 0.75rem;">
              <i class="bi bi-search me-1"></i>ตรวจสอบ
            </a>
          </td>
        </tr>
        <?php endforeach; endif; ?>
      </tbody>
    </table>
  </div>

  <!-- Mobile Card List View -->
  <div class="d-md-none p-3">
    <?php if (empty($reports)): ?>
      <div class="text-center text-muted py-3 small">ไม่พบรายงานปัญหา</div>
    <?php else: foreach ($reports as $r): ?>
      <div class="admin-mobile-card">
        <div class="d-flex justify-content-between align-items-start mb-2">
          <div>
            <div class="fw-bold text-dark">#<?= (int)$r['id'] ?> &bull; <?= e(mb_substr($r['description'], 0, 40)) ?><?= mb_strlen($r['description']) > 40 ? '...' : '' ?></div>
            <div class="text-muted small">โดย @<?= e($r['reporter_username'] ?? '—') ?></div>
          </div>
          <?php [$stClass, $stLabel] = $statusMap[$r['status']] ?? ['status-secondary', $r['status']]; ?>
          <span class="status-pill <?= $stClass ?>"><?= $stLabel ?></span>
        </div>
        <div class="d-flex justify-content-between align-items-center pt-2 border-top">
          <span class="text-muted small"><?= date('d/m/Y', strtotime($r['created_at'])) ?></span>
          <a href="<?= url('/admin/reports/' . (int)$r['id']) ?>" class="btn btn-outline-primary btn-sm py-1 px-2" style="font-size: 0.75rem;">
            ตรวจสอบ &rarr;
          </a>
        </div>
      </div>
    <?php endforeach; endif; ?>
  </div>

  <!-- Pagination -->
  <?php if (($total_pages ?? 1) > 1): ?>
  <div class="p-3 border-top d-flex justify-content-center">
    <nav>
      <ul class="pagination pagination-sm mb-0">
        <?php for ($i = 1; $i <= $total_pages; $i++): ?>
        <li class="page-item <?= $i === $page ? 'active' : '' ?>">
          <a class="page-link" href="?<?= http_build_query(array_merge($filters, ['page' => $i])) ?>"><?= $i ?></a>
        </li>
        <?php endfor; ?>
      </ul>
    </nav>
  </div>
  <?php endif; ?>
</div>

