<?php
$ry = (int) ($reportYear ?? (int) date('Y'));
$lg = $currentLg ?? null;
$lgSido = $lg !== null ? trim((string) ($lg->lg_sido ?? '')) : '';
$lgGugun = $lg !== null ? trim((string) ($lg->lg_gugun ?? '')) : '';
$lgName = $lg !== null ? trim((string) ($lg->lg_name ?? '')) : '';
$scopeLabel = $lgSido !== '' && $lgGugun !== ''
    ? $lgSido . ' ' . $lgGugun
    : ($lgName !== '' ? $lgName : '—');
$exportUrl = mgmt_url('designated-shops/district-new-cancel/export') . '?' . http_build_query(['year' => $ry]);
?>
<?= view('components/print_header', ['printTitle' => '지정 판매소 신규/취소 현황 (' . $ry . '년)']) ?>
<style>
.gbms-dnc-wrap { max-width: 100%; }
.gbms-dnc-table { border-collapse: collapse; width: 100%; font-size: 13px; }
.gbms-dnc-table th,
.gbms-dnc-table td {
  border: 1px solid #7a8aa0;
  padding: 6px 10px;
  text-align: center;
}
.gbms-dnc-table thead th {
  background: linear-gradient(180deg, #e8eef6 0%, #d4dee9 100%);
  font-weight: 700;
  color: #1a2a3a;
}
.gbms-dnc-table thead th.gbms-sub {
  background: #dce6f0;
  font-weight: 600;
}
.gbms-dnc-table tbody td.text-left { text-align: left; }
.gbms-dnc-table thead th.gbms-region-head { text-align: left; }
.gbms-dnc-table tbody tr.gbms-total td {
  font-weight: 700;
  border: 2px solid #c62828;
  background: #fff8f8;
}
.gbms-dnc-caption {
  font-size: 13px;
  font-weight: 700;
  margin: 8px 0 6px;
  color: #1a2a3a;
}
.gbms-unit-pill {
  display: inline-block;
  margin-left: 8px;
  padding: 2px 8px;
  font-size: 11px;
  font-weight: 600;
  color: #0d47a1;
  background: #e3f2fd;
  border: 1px solid #90caf9;
  border-radius: 2px;
}
.gbms-tip {
  position: relative;
  display: inline-flex;
  flex-wrap: nowrap;
  align-items: center;
  gap: 4px;
}
.gbms-help {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 1rem;
  height: 1rem;
  padding: 0 2px;
  border: 1px solid #5c6f85;
  border-radius: 999px;
  background: #fef9c3;
  color: #111827;
  font-size: 11px;
  font-weight: 700;
  line-height: 1;
  font-family: Arial, sans-serif;
  user-select: none;
  cursor: help;
}
.gbms-help::after {
  content: attr(data-tip);
  position: absolute;
  left: 50%;
  top: calc(100% + 6px);
  transform: translateX(-50%);
  display: none;
  min-width: 12rem;
  max-width: 14rem;
  padding: 6px 8px;
  border: 1px solid #8fa0b3;
  border-radius: 4px;
  background: #1f2937;
  color: #fff;
  font-size: 11px;
  font-weight: 500;
  line-height: 1.35;
  text-align: left;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.15);
  z-index: 30;
}
.gbms-help:hover::after,
.gbms-help:focus::after {
  display: block;
}
@media print {
  .gbms-dnc-table { font-size: 11px; }
  .gbms-help { display: none !important; }
}
</style>

<section class="border-b border-gray-300 p-2 shrink-0 bg-control-panel no-print">
  <div class="flex flex-wrap items-center justify-between gap-y-2">
    <span class="text-sm font-bold text-gray-800">[지정 판매소 신규/취소 현황]</span>
    <div class="flex items-center gap-2">
      <a href="<?= esc($exportUrl) ?>" class="border border-btn-excel-border text-btn-excel-text px-3 py-1 rounded-sm text-sm hover:bg-green-50 transition">엑셀저장</a>
      <button type="button" onclick="window.print()" class="border border-btn-print-border text-gray-600 px-3 py-1 rounded-sm text-sm hover:bg-gray-50 transition">인쇄</button>
      <a href="<?= mgmt_url('designated-shops') ?>" class="border border-gray-300 text-gray-600 px-3 py-1 rounded-sm text-sm hover:bg-gray-50">목록</a>
    </div>
  </div>
</section>

<section class="p-3 bg-white border-b border-gray-200 no-print">
  <form method="get" action="<?= mgmt_url('designated-shops/district-new-cancel') ?>" class="flex flex-wrap items-end gap-4">
    <div>
      <label class="block text-xs text-gray-600 mb-0.5">조회년도</label>
      <select name="year" class="border border-gray-400 rounded px-2 py-1.5 text-sm min-w-[7rem] bg-white">
        <?php foreach (($yearChoices ?? []) as $y): ?>
        <option value="<?= (int) $y ?>" <?= $ry === (int) $y ? 'selected' : '' ?>><?= (int) $y ?>년</option>
        <?php endforeach; ?>
      </select>
    </div>
    <div class="flex-1 min-w-[12rem]">
      <span class="block text-xs text-gray-600 mb-0.5">군·구 (소속 지자체)</span>
      <div class="border border-gray-300 rounded px-3 py-1.5 text-sm bg-gray-50 text-gray-800 font-medium">
        <?= esc($scopeLabel) ?>
      </div>
    </div>
    <span class="gbms-unit-pill self-end mb-0.5">단위: 판매소</span>
    <button type="submit" class="bg-btn-search text-white px-5 py-1.5 rounded-sm text-sm font-medium shadow-sm hover:opacity-90">조회</button>
  </form>
  <p class="text-xs text-gray-500 mt-2">
    종전·현행은 각각 <?= $ry - 1 ?>-12-31·<?= $ry ?>-12-31 기준 정상(지정일 이전·비정상 전환일 이후) 건수이며, 지정·취소는 <?= $ry ?>년 달력연도 기준입니다. 구·군 행은 효과 지자체의 기본코드(구 코드) 순서로 표시됩니다.
  </p>
</section>

<div class="mx-2 mt-3 mb-4 gbms-dnc-wrap">
  <div class="gbms-dnc-caption">지정 판매소 신규/취소 현황 조회 내역</div>
  <div class="overflow-x-auto border border-gray-400 rounded-lg bg-white">
    <table class="gbms-dnc-table">
      <thead>
        <tr>
          <th rowspan="2" class="min-w-[6rem] gbms-region-head">군·구</th>
          <th rowspan="2">
            <span class="gbms-tip">
              종전
              <span class="gbms-help" tabindex="0" aria-label="종전 설명" data-tip="전년도 12월 31일 기준 정상 상태 판매소 수">?</span>
            </span>
            <br><span class="text-xs font-normal">(전년도말)</span>
          </th>
          <th colspan="2">사용</th>
          <th rowspan="2">
            <span class="gbms-tip">
              현행
              <span class="gbms-help" tabindex="0" aria-label="현행 설명" data-tip="조회년도 12월 31일 기준 정상 상태 판매소 수">?</span>
            </span>
            <br><span class="text-xs font-normal">(금년도말)</span>
          </th>
        </tr>
        <tr>
          <th class="gbms-sub">
            <span class="gbms-tip">
              지정
              <span class="gbms-help" tabindex="0" aria-label="지정 설명" data-tip="조회년도 내 지정일이 속한 신규 지정 건수">?</span>
            </span>
          </th>
          <th class="gbms-sub">
            <span class="gbms-tip">
              취소
              <span class="gbms-help" tabindex="0" aria-label="취소 설명" data-tip="조회년도 내 폐업/해지 전환일이 속한 건수">?</span>
            </span>
          </th>
        </tr>
      </thead>
      <tbody>
        <?php foreach (($districtRows ?? []) as $row): ?>
        <tr>
          <td class="text-left font-medium"><?= esc($row->region_label) ?></td>
          <td><?= number_format((int) $row->prev_end) ?></td>
          <td><?= number_format((int) $row->designated_y) ?></td>
          <td><?= number_format((int) $row->cancelled_y) ?></td>
          <td><?= number_format((int) $row->curr_end) ?></td>
        </tr>
        <?php endforeach; ?>
        <?php if (empty($districtRows)): ?>
        <tr>
          <td colspan="5" class="text-center text-gray-500 py-8">표시할 구·군 또는 지정판매소 데이터가 없습니다.</td>
        </tr>
        <?php endif; ?>
        <?php if (! empty($districtRows) && isset($districtTotal)): ?>
        <tr class="gbms-total">
          <td class="text-left"><?= esc($districtTotal->region_label) ?></td>
          <td><?= number_format((int) $districtTotal->prev_end) ?></td>
          <td><?= number_format((int) $districtTotal->designated_y) ?></td>
          <td><?= number_format((int) $districtTotal->cancelled_y) ?></td>
          <td><?= number_format((int) $districtTotal->curr_end) ?></td>
        </tr>
        <?php endif; ?>
      </tbody>
    </table>
  </div>
</div>
