<?php
declare(strict_types=1);

/** @var list<array<string,mixed>> $tableRows */
/** @var string $date */
/** @var string $monthStart */
/** @var int $saIdx */
/** @var string $catFilter */
/** @var list<object> $agencies */
/** @var array<string,string> $catLabels */
/** @var bool $hasBsFee */
/** @var string $lgName */
/** @var string $agencyLabel */
/** @var string $catLabelFilter */
/** @var list<string> $printExtraLines */

$exportParams = array_filter([
    'date' => $date ?? '',
    'sa_idx' => (int) ($saIdx ?? 0),
    'cat' => (string) ($catFilter ?? ''),
    'export' => '1',
], static fn ($v): bool => $v !== '' && $v !== null);
$excelUrl = mgmt_url('reports/daily-summary?' . http_build_query($exportParams));
?>
<?= view('components/print_header', [
    'printTitle' => '일계표',
    'printExtraLines' => $printExtraLines ?? [],
]) ?>

<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-700">일계표</span>
    <div class="flex flex-wrap gap-2">
      <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="<?= esc($excelUrl, 'attr') ?>" class="inline-flex items-center border border-green-600 text-green-700 px-3 py-1 rounded-sm text-sm hover:bg-green-50 transition">엑셀저장</a>
    </div>
  </div>
</section>

<section class="p-3 bg-white border-b border-gray-200 no-print">
  <form method="GET" action="<?= mgmt_url('reports/daily-summary') ?>" class="flex flex-wrap items-end gap-3 text-sm">
    <div>
      <label class="block text-gray-600 mb-0.5">조회일자</label>
      <input type="date" name="date" value="<?= esc($date ?? '') ?>" class="border border-gray-300 rounded px-2 py-1 text-sm"/>
    </div>
    <div>
      <label class="block text-gray-600 mb-0.5">대행소</label>
      <select name="sa_idx" class="border border-gray-300 rounded px-2 py-1 text-sm min-w-[12rem] max-w-[20rem]">
        <option value="0">전체</option>
        <?php foreach ($agencies ?? [] as $agency): ?>
          <?php $aid = (int) ($agency->sa_idx ?? 0); ?>
          <option value="<?= esc((string) $aid) ?>" <?= (int) ($saIdx ?? 0) === $aid ? 'selected' : '' ?>>
            <?= esc(trim((string) ($agency->sa_name ?? ''))) ?>
          </option>
        <?php endforeach; ?>
      </select>
    </div>
    <div>
      <label class="block text-gray-600 mb-0.5">구분</label>
      <select name="cat" class="border border-gray-300 rounded px-2 py-1 text-sm min-w-[10rem]">
        <option value="" <?= ($catFilter ?? '') === '' ? 'selected' : '' ?>>전체</option>
        <?php foreach (($catLabels ?? []) as $ck => $lab): ?>
          <option value="<?= esc($ck, 'attr') ?>" <?= ($catFilter ?? '') === $ck ? 'selected' : '' ?>><?= esc($lab) ?></option>
        <?php endforeach; ?>
      </select>
    </div>
    <button type="submit" class="bg-btn-search text-white px-4 py-1.5 rounded-sm text-sm">조회</button>
  </form>
</section>

<section class="p-3 bg-white">
  <style>
    @media print {
      .daily-summary-screen-title { display: none !important; }
    }
  </style>
  <div class="mb-2 text-center daily-summary-screen-title no-print">
    <h1 class="text-lg font-bold m-0">일계표</h1>
    <p class="text-sm text-gray-700 m-1"><?= esc(trim(($lgName ?? '') . ' · 조회일: ' . ($date ?? '') . ' · 대행소: ' . ($agencyLabel ?? '') . ' · 구분: ' . ($catLabelFilter ?? ''))) ?></p>
    <p class="text-xs text-gray-500 m-0">누계(월): <?= esc(($monthStart ?? '') . ' ~ ' . ($date ?? '')) ?> · (단위: 매 / 원)</p>
  </div>

  <div class="border border-gray-300 rounded-lg p-4 overflow-auto">
    <table class="w-full data-table text-sm" id="daily-summary-table">
      <thead>
        <tr>
          <th rowspan="2" class="align-middle text-left">구분</th>
          <th rowspan="2" class="align-middle text-left">봉투종류</th>
          <th colspan="4" class="text-center border-l border-gray-300">일계</th>
          <th colspan="4" class="text-center border-l border-gray-300">누계(월)</th>
        </tr>
        <tr>
          <th class="text-right border-l border-gray-300">수량</th>
          <th class="text-right">판매금액</th>
          <th class="text-right">수수료</th>
          <th class="text-right">징수액</th>
          <th class="text-right border-l border-gray-300">수량</th>
          <th class="text-right">판매금액</th>
          <th class="text-right">수수료</th>
          <th class="text-right">징수액</th>
        </tr>
      </thead>
      <tbody class="text-right">
        <?php foreach ($tableRows ?? [] as $r): ?>
          <?php
            $kind = (string) ($r['kind'] ?? 'data');
            $trClass = $kind === 'subtotal' ? 'bg-gray-50 font-semibold' : ($kind === 'grand' ? 'bg-amber-50 font-bold' : '');
            $fmtFee = static function (float $v) use ($hasBsFee): string {
              if (! $hasBsFee) {
                return '—';
              }
              return $v != 0.0 ? number_format((int) round($v)) : '';
            };
          ?>
          <tr class="<?= esc($trClass, 'attr') ?>">
            <?php if ($kind === 'grand'): ?>
              <td colspan="2" class="text-center"><?= esc((string) ($r['bag_name'] ?? '합 계')) ?></td>
            <?php else: ?>
              <td class="text-left pl-2"><?= esc((string) ($r['cat_label'] ?? '')) ?></td>
              <td class="text-left pl-2"><?= esc((string) ($r['bag_name'] ?? '')) ?></td>
            <?php endif; ?>
            <td class="border-l border-gray-200 tabular-nums"><?= number_format((int) ($r['d_qty'] ?? 0)) ?></td>
            <td class="tabular-nums"><?= number_format((int) round((float) ($r['d_amt'] ?? 0))) ?></td>
            <td class="tabular-nums"><?= $fmtFee((float) ($r['d_fee'] ?? 0)) ?></td>
            <td class="tabular-nums"><?= number_format((int) round((float) ($r['d_levy'] ?? 0))) ?></td>
            <td class="border-l border-gray-200 tabular-nums"><?= number_format((int) ($r['m_qty'] ?? 0)) ?></td>
            <td class="tabular-nums"><?= number_format((int) round((float) ($r['m_amt'] ?? 0))) ?></td>
            <td class="tabular-nums"><?= $fmtFee((float) ($r['m_fee'] ?? 0)) ?></td>
            <td class="tabular-nums"><?= number_format((int) round((float) ($r['m_levy'] ?? 0))) ?></td>
          </tr>
        <?php endforeach; ?>
        <?php if (($tableRows ?? []) === []): ?>
          <tr>
            <td colspan="10" class="text-center text-gray-400 py-6">조회된 데이터가 없습니다.</td>
          </tr>
        <?php endif; ?>
      </tbody>
    </table>
  </div>
</section>
