<?php
/** @var object $kind */
/** @var list<object> $list */
/** @var bool $canManage */
/** @var array<int,bool> $rowCanEdit */
$canManage = ! empty($canManage);
$rowCanEdit = $rowCanEdit ?? [];
$showActionsCol = $canManage;
$rowStartNo = 1;
if (isset($pager) && $pager !== null) {
    $rowStartNo = (int) ($pager->getCurrentPage() - 1) * (int) $pager->getPerPage() + 1;
}
?>
<div class="space-y-3">
  <?= view('components/print_header', ['printTitle' => '세부코드 - ' . esc($kind->ck_name)]) ?>
  <section class="border border-gray-300 rounded bg-control-panel p-2">
    <div class="flex flex-wrap items-center justify-between gap-y-2">
      <div class="flex flex-wrap items-center gap-2 text-sm">
        <a href="<?= base_url('bag/code-kinds') ?>" class="text-blue-600 hover:underline">&larr; 기본코드 종류</a>
        <span class="text-gray-400">|</span>
        <span class="font-bold text-gray-700"><?= $canManage ? '세부코드 관리' : '세부코드 조회' ?> — <?= esc($kind->ck_name) ?> (<?= esc($kind->ck_code) ?>)</span>
      </div>
      <div class="flex items-center gap-2">
        <button type="button" onclick="window.print()" class="no-print rounded border border-gray-300 px-3 py-1 text-sm text-gray-600 hover:bg-gray-50">인쇄</button>
        <?php if ($canManage): ?>
        <a href="<?= base_url('admin/code-details/' . (int) $kind->ck_idx . '/create') ?>" class="rounded border border-transparent bg-[#243a5e] px-3 py-1.5 text-sm text-white shadow hover:opacity-90">세부코드 등록</a>
        <?php endif; ?>
      </div>
    </div>
  </section>
  <div class="border border-gray-300 rounded-lg p-4 overflow-auto">
    <table class="data-table w-full">
      <thead>
        <tr>
          <th class="w-16 text-center">번호</th>
          <th class="w-24 text-center">코드</th>
          <th>코드명</th>
          <th class="w-24 text-center">범위</th>
          <th class="w-20 text-center">정렬순서</th>
          <th class="w-20 text-center">상태</th>
          <th>등록일</th>
          <?php if ($showActionsCol): ?>
          <th class="w-28 text-center">작업</th>
          <?php endif; ?>
        </tr>
      </thead>
      <tbody>
        <?php $rowNo = $rowStartNo; foreach ($list as $row): ?>
        <?php
          $isPlatform = (($row->cd_source ?? 'platform') === 'platform' && (int) ($row->cd_lg_idx ?? 0) === 0);
          $scopeLabel = $isPlatform ? '공통' : '지자체';
        ?>
        <tr>
          <td class="text-center"><?= (string) $rowNo ?></td>
          <td class="text-center font-mono"><?= esc($row->cd_code) ?></td>
          <td><?= esc($row->cd_name) ?></td>
          <td class="text-center text-xs"><?= esc($scopeLabel) ?></td>
          <td class="text-center"><?= (int) $row->cd_sort ?></td>
          <td class="text-center"><?= (int) $row->cd_state === 1 ? '사용' : '미사용' ?></td>
          <td class="text-left"><?= esc($row->cd_regdate ?? '') ?></td>
          <?php if ($showActionsCol): ?>
          <td class="text-center text-sm">
            <?php if (! empty($rowCanEdit[$row->cd_idx])): ?>
            <a href="<?= base_url('admin/code-details/edit/' . (int) $row->cd_idx) ?>" class="text-blue-600 hover:underline">수정</a>
            <form action="<?= base_url('admin/code-details/delete/' . (int) $row->cd_idx) ?>" method="POST" class="ml-1 inline" onsubmit="return confirm('이 세부코드를 삭제하시겠습니까?');">
              <?= csrf_field() ?>
              <button type="submit" class="text-red-600 hover:underline">삭제</button>
            </form>
            <?php else: ?>
            <span class="text-gray-400">—</span>
            <?php endif; ?>
          </td>
          <?php endif; ?>
        </tr>
        <?php $rowNo++; ?>
        <?php endforeach; ?>
      </tbody>
    </table>
  </div>
  <?php if (isset($pager) && $pager !== null): ?>
    <div class="mt-3"><?= $pager->links() ?></div>
  <?php endif; ?>
</div>
