<?php
$statusOpts = ['all'=>'ทุกสถานะ','active'=>'ปกติ','suspended'=>'ระงับ'];
$typeOpts   = ['all'=>'ทุกประเภท','official'=>'Official Store','standard'=>'Standard Store'];
?>
<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">จัดการผู้ขาย &amp; ร้านค้า (Sellers &amp; Stores)</h4>
    <p class="text-muted small mb-0">ค้นหา ตรวจสอบข้อมูลร้านค้า ตรวจสอบสถานะ Official และกำกับดูแลผู้ขาย</p>
  </div>
</div>

<!-- ── Filter Bar ──────────────────────────────────────── -->
<div class="admin-filter-bar mb-3">
  <form method="GET" class="row g-2 align-items-end">
    <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="ชื่อร้านค้า, store-slug, เจ้าของ, email...">
      </div>
    </div>
    <div class="col-6 col-md-3">
      <label class="form-label small fw-semibold text-muted mb-1">สถานะร้านค้า</label>
      <select name="status" class="form-select form-select-sm">
        <?php foreach ($statusOpts as $v => $l): ?>
        <option value="<?= $v ?>" <?= ($filters['status'] ?? '') === $v ? 'selected' : '' ?>><?= $l ?></option>
        <?php endforeach; ?>
      </select>
    </div>
    <div class="col-6 col-md-2">
      <label class="form-label small fw-semibold text-muted mb-1">ประเภท</label>
      <select name="store_type" class="form-select form-select-sm">
        <?php foreach ($typeOpts as $v => $l): ?>
        <option value="<?= $v ?>" <?= ($filters['store_type'] ?? '') === $v ? 'selected' : '' ?>><?= $l ?></option>
        <?php endforeach; ?>
      </select>
    </div>
    <div class="col-12 col-md-2 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['status'] ?? 'all') !== 'all' || ($filters['store_type'] ?? 'all') !== 'all'): ?>
      <a href="<?= url('/admin/sellers') ?>" 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-shop 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>ร้านค้า</th>
          <th>เจ้าของร้าน (Owner)</th>
          <th class="text-center">สินค้า</th>
          <th class="text-center">ออเดอร์</th>
          <th>ประเภท</th>
          <th>สถานะ</th>
          <th>สร้างเมื่อ</th>
          <th class="text-end">การจัดการ</th>
        </tr>
      </thead>
      <tbody>
        <?php if (empty($stores)): ?>
        <tr>
          <td colspan="8" class="text-center text-muted py-4">ไม่พบร้านค้าตามเงื่อนไขที่ค้นหา</td>
        </tr>
        <?php else: foreach ($stores as $s): ?>
        <tr>
          <td>
            <div class="d-flex align-items-center gap-2">
              <div class="rounded-circle border overflow-hidden bg-light d-flex align-items-center justify-content-center flex-shrink-0" style="width: 36px; height: 36px;">
                <?php if ($s['logo']): ?>
                  <img src="<?= store_image_url($s['logo'], 'logo') ?>" class="w-100 h-100 object-fit-cover" alt="">
                <?php else: ?>
                  <i class="bi bi-shop text-muted small"></i>
                <?php endif; ?>
              </div>
              <div>
                <div class="fw-semibold text-dark"><?= e($s['store_name']) ?></div>
                <div class="text-muted small font-monospace">/shop/<?= e($s['store_slug']) ?></div>
              </div>
            </div>
          </td>
          <td>
            <div class="small fw-medium text-dark"><?= e($s['first_name'] . ' ' . $s['last_name']) ?></div>
            <div class="text-muted small">@<?= e($s['owner_username']) ?></div>
          </td>
          <td class="text-center font-monospace small"><?= number_format((int)$s['total_products']) ?></td>
          <td class="text-center font-monospace small"><?= number_format((int)$s['total_orders']) ?></td>
          <td>
            <?php if (!empty($s['verified_at'])): ?>
              <span class="status-pill status-official"><i class="bi bi-patch-check-fill me-1"></i>Official</span>
            <?php else: ?>
              <span class="status-pill status-secondary">Standard</span>
            <?php endif; ?>
          </td>
          <td>
            <?php if ($s['status'] === 'active'): ?>
              <span class="status-pill status-active">ปกติ</span>
            <?php elseif ($s['status'] === 'suspended'): ?>
              <span class="status-pill status-suspended">ระงับ</span>
            <?php else: ?>
              <span class="status-pill status-secondary"><?= e($s['status']) ?></span>
            <?php endif; ?>
          </td>
          <td class="text-muted small"><?= date('d/m/Y', strtotime($s['created_at'])) ?></td>
          <td class="text-end">
            <a href="<?= url('/admin/sellers/' . (int)$s['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($stores)): ?>
      <div class="text-center text-muted py-3 small">ไม่พบข้อมูลร้านค้า</div>
    <?php else: foreach ($stores as $s): ?>
      <div class="admin-mobile-card">
        <div class="d-flex justify-content-between align-items-start mb-2">
          <div class="d-flex align-items-center gap-2">
            <div class="rounded-circle border overflow-hidden bg-light flex-shrink-0" style="width: 40px; height: 40px;">
              <?php if ($s['logo']): ?><img src="<?= store_image_url($s['logo'], 'logo') ?>" class="w-100 h-100 rounded-circle object-fit-cover" alt=""><?php else: ?><i class="bi bi-shop text-muted small"></i><?php endif; ?>
            </div>
            <div>
              <div class="fw-bold text-dark"><?= e($s['store_name']) ?></div>
              <div class="text-muted small">@<?= e($s['owner_username']) ?></div>
            </div>
          </div>
          <?php if ($s['status'] === 'active'): ?><span class="status-pill status-active">ปกติ</span><?php else: ?><span class="status-pill status-suspended">ระงับ</span><?php endif; ?>
        </div>
        <div class="d-flex justify-content-between align-items-center pt-2 border-top">
          <span class="text-muted small">สินค้า: <?= (int)$s['total_products'] ?> &bull; ออเดอร์: <?= (int)$s['total_orders'] ?></span>
          <a href="<?= url('/admin/sellers/' . (int)$s['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>

