<div class="row g-3 mb-3">
  <?php foreach ($sources as $k => $s): ?>
    <div class="col-6 col-md-4 col-xl-3">
      <a class="card h-100 text-decoration-none text-body" href="<?= url('/pembuat-laporan/baru', ['sumber'=>$k]) ?>">
        <div class="card-body">
          <div class="d-flex align-items-center gap-2 mb-1">
            <i class="bi bi-<?= e($s['icon']) ?> fs-5 text-primary"></i>
            <strong style="font-size:13.5px"><?= e($s['label']) ?></strong>
          </div>
          <div class="small text-body-secondary"><?= e($s['deskripsi']) ?></div>
        </div>
      </a>
    </div>
  <?php endforeach; ?>
</div>

<div class="card">
  <div class="card-header">
    <i class="bi bi-bookmark-star me-1"></i>Template Laporan Tersimpan
    <span class="text-body-secondary fw-normal">(<?= nf(count($templates)) ?>)</span>
    <div class="card-actions"><a class="btn btn-sm btn-primary" href="<?= url('/pembuat-laporan/baru') ?>"><i class="bi bi-plus-lg me-1"></i>Laporan Baru</a></div>
  </div>
  <div class="table-wrap">
    <table class="table table-hover mb-0">
      <thead><tr><th>Nama Laporan</th><th>Sumber Data</th><th>Kolom</th><th>Pemilik</th><th class="ctr">Berbagi</th><th class="ctr">Aksi</th></tr></thead>
      <tbody>
      <?php if (!$templates): ?><tr><td colspan="6"><div class="empty-state"><i class="bi bi-file-earmark-plus"></i>Belum ada template laporan tersimpan.</div></td></tr><?php endif; ?>
      <?php foreach ($templates as $t): ?>
        <tr>
          <td><a class="text-decoration-none fw-semibold" href="<?= url('/pembuat-laporan/'.(int)$t['id']) ?>"><?= e($t['name']) ?></a>
            <?php if ((int)$t['is_system'] === 1): ?> <span class="badge bg-info-subtle text-info-emphasis">Bawaan</span><?php endif; ?>
            <?php if ($t['description']): ?><div class="small text-body-secondary"><?= e(truncate($t['description'],80)) ?></div><?php endif; ?></td>
          <td><?= e($sources[$t['data_source']]['label'] ?? $t['data_source']) ?></td>
          <td><?= nf($t['jumlah_kolom']) ?> kolom</td>
          <td><?= e($t['owner_name'] ?: 'Sistem') ?></td>
          <td class="ctr"><?= badge(['private'=>'Pribadi','shared'=>'Dibagikan','public'=>'Publik'][$t['visibility']] ?? $t['visibility'],
                        $t['visibility']==='public'?'success':($t['visibility']==='shared'?'info':'secondary')) ?></td>
          <td class="ctr nowrap">
            <a class="btn btn-sm btn-light py-0 px-1" href="<?= url('/pembuat-laporan/'.(int)$t['id']) ?>" title="Buka"><i class="bi bi-box-arrow-up-right"></i></a>
            <?php if (can('report_builder','create')): ?>
            <form method="post" action="<?= url('/pembuat-laporan/'.(int)$t['id'].'/duplikat') ?>" class="d-inline"><?= csrf() ?>
              <button class="btn btn-sm btn-light py-0 px-1" title="Duplikat"><i class="bi bi-files"></i></button></form>
            <?php endif; ?>
            <?php if (can('report_builder','delete') && (int)$t['is_system'] === 0): ?>
            <form method="post" action="<?= url('/pembuat-laporan/'.(int)$t['id'].'/hapus') ?>" class="d-inline"><?= csrf() ?>
              <button class="btn btn-sm btn-light py-0 px-1 text-danger" title="Hapus" data-konfirmasi="Hapus template laporan &quot;<?= attr($t['name']) ?>&quot;?"><i class="bi bi-trash"></i></button></form>
            <?php endif; ?>
          </td>
        </tr>
      <?php endforeach; ?>
      </tbody>
    </table>
  </div>
</div>
