<?php
function mFmt(array $e, string $col, $val, array $refs) {
  $f = $e['fields'][$col] ?? [];
  $t = $f['type'] ?? 'text';
  if ($t === 'bool')  return ((int)$val === 1) ? badge('Ya','success') : badge('Tidak','secondary');
  if ($t === 'ref')   return e($refs[$col][(int)$val] ?? '-');
  if ($t === 'enum')  return e($f['options'][$val] ?? ($val ?: '-'));
  if ($t === 'money') return rph($val);
  if ($t === 'int')   return $val === null || $val === '' ? '-' : nf($val,0);
  if ($t === 'decimal') return $val === null || $val === '' ? '-' : nf($val,2);
  return e(truncate((string)$val, 44) ?: '-');
}
?>
<div class="d-flex flex-wrap gap-2 mb-3">
  <a class="btn btn-sm btn-light" href="<?= url('/master') ?>"><i class="bi bi-arrow-left me-1"></i>Data Master</a>
  <?php if (can('master','create')): ?><a class="btn btn-sm btn-primary" href="<?= url('/master/'.$key.'/tambah') ?>"><i class="bi bi-plus-lg me-1"></i>Tambah <?= e($entity['label']) ?></a><?php endif; ?>
  <?php if (can('master','export')): ?><a class="btn btn-sm btn-outline-primary" href="<?= url('/master/'.$key.'/ekspor',['format'=>'xlsx']) ?>"><i class="bi bi-file-earmark-excel me-1"></i>Ekspor Excel</a><?php endif; ?>
  <form class="ms-auto d-flex gap-2" method="get">
    <input type="search" class="form-control form-control-sm" name="cari" value="<?= attr($cari) ?>" placeholder="Cari…" style="min-width:220px">
    <button class="btn btn-sm btn-light"><i class="bi bi-search"></i></button>
  </form>
</div>
<div class="card">
  <div class="card-header"><i class="bi bi-<?= e($entity['icon']) ?> me-1"></i><?= e($entity['label']) ?>
    <span class="text-body-secondary fw-normal ms-1">(<?= nf($p['total']) ?>)</span></div>
  <div class="table-wrap"><table class="table table-hover table-sticky mb-0">
    <thead><tr>
      <?php foreach ($entity['list'] as $col): $f = $entity['fields'][$col] ?? []; ?>
        <th class="<?= in_array($f['type'] ?? '', ['money','int','decimal'], true) ? 'num' : '' ?>"><?= e($f['label'] ?? $col) ?></th>
      <?php endforeach; ?>
      <th class="ctr">Aksi</th>
    </tr></thead>
    <tbody>
    <?php if (!$p['data']): ?><tr><td colspan="<?= count($entity['list'])+1 ?>"><div class="empty-state"><i class="bi bi-inbox"></i>Belum ada data.</div></td></tr><?php endif; ?>
    <?php foreach ($p['data'] as $row): ?>
      <tr>
        <?php foreach ($entity['list'] as $col): $f = $entity['fields'][$col] ?? []; ?>
          <td class="<?= in_array($f['type'] ?? '', ['money','int','decimal'], true) ? 'num' : '' ?>"><?= mFmt($entity, $col, $row[$col] ?? null, $refs) ?></td>
        <?php endforeach; ?>
        <td class="ctr nowrap">
          <?php if (can('master','edit')): ?><a class="btn btn-sm btn-light py-0 px-1" href="<?= url('/master/'.$key.'/'.(int)$row['id'].'/ubah') ?>"><i class="bi bi-pencil"></i></a><?php endif; ?>
          <?php if (can('master','delete')): ?>
            <form method="post" action="<?= url('/master/'.$key.'/'.(int)$row['id'].'/hapus') ?>" class="d-inline"><?= csrf() ?>
              <button class="btn btn-sm btn-light py-0 px-1 text-danger" data-konfirmasi="Hapus data ini?"><i class="bi bi-trash"></i></button></form><?php endif; ?>
        </td>
      </tr>
    <?php endforeach; ?>
    </tbody></table></div>
  <?= \App\Core\View::partial('partials/pagination', ['p' => $p]) ?>
</div>
