<?php
define('TEMPLATE_PUBLIC_SKIP_LOGIN', true);

include __DIR__ . '/../include.php';
include __DIR__ . '/../include_work.php';

$logged_in = !empty($_SESSION['LINE_ID']) && !empty($_SESSION['user']);

if (!$logged_in) {
    header('Location: login.php');
    exit;
}

$batches = work_batches();
$batch = work_batch_by_ym($_GET['ym'] ?? null);
if (!$batch) {
    $page_title = 'サマリ';
    $active_nav = 'home';
    $ym_qs = '';
    include __DIR__ . '/inc/layout_head.php';
    echo '<p class="work-empty">作業データがありません。先に Excel を取り込んでください。</p>';
    include __DIR__ . '/inc/layout_foot.php';
    exit;
}

$current_ym = $batch['year_month'];
$ym_qs = $current_ym;
$stats = work_prefecture_stats((int)$batch['number']);
$progress = work_progress((int)$batch['number']);
$room_total = (int)($progress['room_total'] ?? 0);
$property_total = (int)($progress['property_total'] ?? 0);
$scheduled_count = (int)($progress['scheduled_count'] ?? 0);
$scheduled_rate = $room_total > 0 ? (int)round($scheduled_count / $room_total * 100) : 0;

$page_title = 'サマリ ' . work_ym_label($current_ym);
$active_nav = 'home';
include __DIR__ . '/inc/layout_head.php';
?>

<h1>サマリ</h1>
<p class="work-lead">お客さまから届いた部屋リストを取り込み、各県の作業件数を自動で集計しています。Excelのサマリを手集計する必要はありません。</p>

<?php
$tab_base = 'index.php';
$keep = [];
include __DIR__ . '/inc/month_tabs.php';
?>

<section class="work-stat-grid" aria-label="月次合計">
  <article class="work-stat-card">
    <p class="work-stat-card__label">物件</p>
    <p class="work-stat-card__value"><?php echo number_format($property_total); ?></p>
  </article>
  <article class="work-stat-card">
    <p class="work-stat-card__label">作業件数</p>
    <p class="work-stat-card__value"><?php echo number_format($room_total); ?></p>
  </article>
  <article class="work-stat-card">
    <p class="work-stat-card__label">予定入力</p>
    <p class="work-stat-card__value"><?php echo number_format($scheduled_count); ?></p>
    <p class="work-stat-card__sub"><?php echo $scheduled_rate; ?>%</p>
  </article>
</section>

<form class="work-search" action="properties.php" method="get" role="search">
  <input type="hidden" name="ym" value="<?php echo work_h($current_ym); ?>">
  <label class="visually-hidden" for="dash-q">物件・APNO・住所を検索</label>
  <i class="fa-solid fa-magnifying-glass" aria-hidden="true"></i>
  <input id="dash-q" type="search" name="q" placeholder="物件名・APNO・住所">
  <button type="submit" class="btn-primary">検索</button>
</form>

<section>
  <h2>各県の作業数</h2>
  <div class="table-responsive">
    <table class="table work-summary-table">
      <thead>
        <tr>
          <th>エリア</th>
          <th>作業件数</th>
          <th>物件数</th>
        </tr>
      </thead>
      <tbody>
        <?php foreach ($stats as $row): ?>
          <tr class="clickable-row" onclick="location.href='properties.php?ym=<?php echo work_h($current_ym); ?>&pref=<?php echo urlencode($row['prefecture']); ?>'">
            <td><?php echo work_h($row['prefecture']); ?></td>
            <td><?php echo number_format((int)$row['room_count']); ?></td>
            <td><?php echo number_format((int)$row['property_count']); ?></td>
          </tr>
        <?php endforeach; ?>
      </tbody>
      <tfoot>
        <tr>
          <th>合計</th>
          <th><?php echo number_format($room_total); ?></th>
          <th><?php echo number_format($property_total); ?></th>
        </tr>
      </tfoot>
    </table>
  </div>
</section>

<?php include __DIR__ . '/inc/layout_foot.php'; ?>
