<?php
declare(strict_types=1);
/**
 * @var list<object> $list
 * @var string $start
 * @var string $end
 * @var string $action
 * @var string $table
 * @var string $user
 * @var array<string,string> $tableLabels
 * @var array<string,string> $actionLabels
 */
$actionBadge = static function (string $a): string {
    $map = [
        'create' => 'background:#dcfce7;color:#166534;',
        'update' => 'background:#dbeafe;color:#1e40af;',
        'delete' => 'background:#fee2e2;color:#991b1b;',
        'export' => 'background:#cffafe;color:#155e75;',
        'print'  => 'background:#f3e8ff;color:#6b21a8;',
    ];
    return $map[$a] ?? 'background:#f3f4f6;color:#374151;';
};
?>
<?= view('components/print_header', ['printTitle' => '활동 로그']) ?>

<section class="border-b border-gray-300 p-2 shrink-0 bg-control-panel no-print">
  <form method="GET" action="<?= base_url('admin/access/activity-logs') ?>" class="flex flex-wrap items-center gap-2 text-sm">
    <label class="font-bold text-gray-700">조회기간</label>
    <input type="date" name="start" class="border border-gray-300 rounded px-2 py-1 text-sm" value="<?= esc($start) ?>"/>
    <span>~</span>
    <input type="date" name="end" class="border border-gray-300 rounded px-2 py-1 text-sm" value="<?= esc($end) ?>"/>

    <select name="action" class="border border-gray-300 rounded px-2 py-1 text-sm min-w-[8rem]">
      <option value="">작업 전체</option>
      <?php foreach ($actionLabels as $k => $lbl): ?>
        <option value="<?= esc($k, 'attr') ?>" <?= $action === $k ? 'selected' : '' ?>><?= esc($lbl) ?></option>
      <?php endforeach; ?>
    </select>

    <select name="table" class="border border-gray-300 rounded px-2 py-1 text-sm max-w-[12rem]">
      <option value="">대상 전체</option>
      <?php foreach ($tableLabels as $k => $lbl): ?>
        <option value="<?= esc($k, 'attr') ?>" <?= $table === $k ? 'selected' : '' ?>><?= esc($lbl) ?></option>
      <?php endforeach; ?>
    </select>

    <input type="text" name="user" placeholder="사용자(아이디/이름)" class="border border-gray-300 rounded px-2 py-1 text-sm" value="<?= esc($user) ?>"/>

    <button type="submit" class="bg-btn-search text-white px-4 py-1.5 rounded-sm text-sm shadow hover:opacity-90 transition border border-transparent">조회</button>
    <a href="<?= base_url('admin/access/activity-logs') ?>" class="border border-gray-300 text-gray-600 px-3 py-1.5 rounded-sm text-sm hover:bg-gray-50">초기화</a>
  </form>
</section>

<div class="border border-gray-300 rounded-lg p-4 overflow-auto mt-2">
  <table class="w-full data-table text-sm">
    <thead>
      <tr>
        <th class="text-left">일시</th>
        <th class="text-left">사용자</th>
        <th class="text-center">작업</th>
        <th class="text-left">내용</th>
        <th class="text-center no-print">상세</th>
      </tr>
    </thead>
    <tbody>
      <?php foreach ($list as $row): ?>
        <?php
          $act = (string) ($row->al_action ?? '');
          $isLogin = ! empty($row->isLogin);
          $who = trim((string) ($row->mb_name ?? '') . (($row->mb_id ?? '') !== '' ? ' (' . $row->mb_id . ')' : ''));
          $badgeText = $isLogin ? '로그인' : ($actionLabels[$act] ?? $act);
          $badgeStyle = $isLogin ? 'background:#ede9fe;color:#5b21b6;' : $actionBadge($act);
          $changeList = $row->changeList ?? [];
        ?>
        <tr>
          <td class="text-left pl-2 whitespace-nowrap"><?= esc((string) ($row->al_regdate ?? '')) ?></td>
          <td class="text-left pl-2"><?= $who !== '' ? esc($who) : '<span class="text-gray-400">시스템</span>' ?></td>
          <td class="text-center">
            <span style="<?= $badgeStyle ?>display:inline-block;padding:1px 8px;border-radius:9999px;font-size:11px;font-weight:700;white-space:nowrap;">
              <?= esc($badgeText) ?>
            </span>
          </td>
          <td class="text-left pl-2">
            <span class="font-semibold text-gray-800"><?= esc((string) ($row->summaryText ?? '')) ?></span>
            <?php if ($changeList !== []): ?>
              <div class="text-xs text-gray-500 mt-0.5">
                <?php foreach (array_slice($changeList, 0, 4) as $c): ?>
                  <span class="inline-block mr-2">
                    <span class="text-gray-600"><?= esc($c['label']) ?>:</span>
                    <span class="text-gray-400"><?= esc($c['from']) ?></span>
                    →
                    <span class="text-gray-700"><?= esc($c['to']) ?></span>
                  </span>
                <?php endforeach; ?>
                <?php if (count($changeList) > 4): ?><span class="text-gray-400">외 <?= count($changeList) - 4 ?>건</span><?php endif; ?>
              </div>
            <?php endif; ?>
          </td>
          <td class="text-center no-print">
            <a href="<?= base_url('admin/access/activity-logs/' . (int) $row->al_idx) ?>" class="text-blue-700 hover:underline">보기</a>
          </td>
        </tr>
      <?php endforeach; ?>
      <?php if ($list === []): ?>
        <tr><td colspan="5" class="text-center text-gray-400 py-6">조회된 로그가 없습니다.</td></tr>
      <?php endif; ?>
    </tbody>
  </table>
</div>
<?php if (isset($pager)): ?><div class="mt-3 no-print"><?= $pager->links() ?></div><?php endif; ?>
