<div class="card">
  <div style="display:flex; justify-content:space-between; align-items:center;
              margin-bottom:16px; flex-wrap:wrap; gap:8px;">
    <span style="font-weight:600; color:#555;">
      Total: <strong><?= count($stores) ?></strong> toko
      <?php if ($is_admin): ?>
        <span class="badge badge-primary" style="margin-left:6px;">Semua User</span>
      <?php endif; ?>
    </span>
    <a href="<?= site_url('store/create') ?>" class="btn btn-primary">+ Tambah Toko</a>
  </div>

  <table id="tblStore" class="display nowrap" style="width:100%">
    <thead>
      <tr>
        <th>#</th>
        <th class="all">Nama Toko</th>
        <th>Alamat</th>
        <th>Telepon</th>
        <?php if ($is_admin): ?><th>Pemilik</th><?php endif; ?>
        <th>Footer</th>
        <th class="all">Aksi</th>
      </tr>
    </thead>
    <tbody>
      <?php if (empty($stores)): ?>
        <tr>
          <td colspan="<?= $is_admin ? 7 : 6 ?>"
              style="text-align:center; color:#999; padding:40px;">
            Belum ada toko.
            <a href="<?= site_url('store/create') ?>" style="color:var(--primary);">
              Tambah sekarang →
            </a>
          </td>
        </tr>
      <?php else: ?>
        <?php foreach ($stores as $i => $s): ?>
          <tr>
            <td><?= $i + 1 ?></td>
            <td><strong><?= htmlspecialchars($s->name) ?></strong></td>
            <td><?= htmlspecialchars($s->address ?: '-') ?></td>
            <td><?= htmlspecialchars($s->phone ?: '-') ?></td>
            <?php if ($is_admin): ?>
              <td>
                <span class="badge badge-primary" style="font-size:11px;">
                  <?= htmlspecialchars($s->owner_name ?? '-') ?>
                </span>
              </td>
            <?php endif; ?>
            <td style="font-size:12px; color:#666; max-width:180px;">
              <?= htmlspecialchars(mb_substr($s->footer_text ?? '-', 0, 40)) ?>
              <?= mb_strlen($s->footer_text ?? '') > 40 ? '…' : '' ?>
            </td>
            <td>
              <div style="display:flex; gap:4px; justify-content:center;">
                <a href="<?= site_url('store/edit/' . $s->id) ?>"
                   class="btn btn-warning btn-sm">✏️ Edit</a>
                <a href="<?= site_url('store/delete/' . $s->id) ?>"
                   class="btn btn-danger btn-sm"
                   onclick="return confirm('Hapus toko <?= htmlspecialchars(addslashes($s->name)) ?>?')">🗑</a>
              </div>
            </td>
          </tr>
        <?php endforeach; ?>
      <?php endif; ?>
    </tbody>
  </table>
</div>

<script>
$(document).ready(function () {
  var colAksi = <?= $is_admin ? 6 : 5 ?>;

  $('#tblStore').DataTable({
    responsive: true,
    language: {
      search:            '',
      searchPlaceholder: 'Cari toko...',
      lengthMenu:        'Tampilkan _MENU_ baris',
      info:              'Menampilkan _START_–_END_ dari _TOTAL_ data',
      infoEmpty:         'Tidak ada data',
      infoFiltered:      '(dari _MAX_ total)',
      zeroRecords:       'Toko tidak ditemukan',
      emptyTable:        'Belum ada toko',
      paginate: { first:'«', last:'»', next:'›', previous:'‹' },
    },
    order:      [[1, 'asc']],
    pageLength: 10,
    lengthMenu: [[5, 10, 25, 50, -1], [5, 10, 25, 50, 'Semua']],
    columnDefs: [
      { orderable: false, targets: colAksi },
      { responsivePriority: 1, targets: 1 },
      { responsivePriority: 2, targets: colAksi },
    ],
    dom: '<"dt-top"lf>rt<"dt-bottom"ip>',
  });
});
</script>
