<!-- Bootstrapモーダル -->
<div class="modal-header">
    <h5 class="modal-title" id="parkDetailLabel">{{ $park->park_name }}</h5>
    <button type="button" class="close" data-dismiss="modal" aria-label="閉じる">
        <span aria-hidden="true">&times;</span>
    </button>
</div>
<div class="modal-body">
    <!-- Googleマップ埋め込み -->
    <div style="width:100%; height:350px;">
        <iframe
            width="100%"
            height="350"
            frameborder="0"
            style="border:0"
            src="https://www.google.com/maps?q={{ $park->park_latitude }},{{ $park->park_longitude }}&hl=ja&z=16&output=embed"
            allowfullscreen>
        </iframe>
    </div>
    <!-- 駐輪場情報 -->
    <div class="mt-3 small">
        {{ $park->park_adrs }}
        <span class="d-inline-block" style="margin-top:0.5em;"></span>
        <span>
            【標準収容台数】
            @foreach($zoneStandardSum as $type => $count)
            {{ $type }}：{{ $count }}台　
            @endforeach
        </span>
    </div>
    <div class="mt-3">
        <span>{{ $park->price_memo }}</span>
    </div>
    <!-- 空き台数・契約情報 -->
    <div class="mt-3">
        @php
        $zonesByPtype = $zones->groupBy('ptype_id');
        @endphp
        @foreach($zonesByPtype as $ptypeId => $zonesGroup)
        <div class="mb-3">
            <strong>{{ $zonesGroup->first()->ptype_subject }}{{ $zonesGroup->first()->zone_name }}</strong>
            <div style="display: flex; gap: 1em;">
                @foreach($zonesGroup as $zone)
                @php
                $vacant = $vacancyData[$zone->psection_id . '_' . $zone->ptype_subject] ?? 0;
                $grace = $parkGracePeriod ?? null;
                $now = \Carbon\Carbon::now();

                // 猶予期間判定
                $isGracePeriod = false;
                if ($grace && $grace->update_grace_period_start_date && $grace->update_grace_period_start_time && $grace->update_grace_period_end_date && $grace->update_grace_period_end_time) {
                $now = \Carbon\Carbon::now();
                $year = $now->year;
                $month = $now->month;
                $startDay = (int)$grace->update_grace_period_start_date;
                $endDay = (int)$grace->update_grace_period_end_date;
                $startTime = $grace->update_grace_period_start_time;
                $endTime = $grace->update_grace_period_end_time;

                if ($startDay > $endDay) {
                // 月またぎ
                // 前月の開始日～今月の終了日
                $prevMonth = $now->copy()->subMonth();
                $startPrev = \Carbon\Carbon::createFromFormat('Y-m-d H:i', sprintf('%04d-%02d-%02d %s', $prevMonth->year, $prevMonth->month, $startDay, $grace->update_grace_period_start_time));
                $endCurr = \Carbon\Carbon::createFromFormat('Y-m-d H:i', sprintf('%04d-%02d-%02d %s', $year, $month, $endDay, $grace->update_grace_period_end_time));
                // 今月の開始日～翌月の終了日
                $startCurr = \Carbon\Carbon::createFromFormat('Y-m-d H:i', sprintf('%04d-%02d-%02d %s', $year, $month, $startDay, $grace->update_grace_period_start_time));
                $nextMonth = $month == 12 ? 1 : $month + 1;
                $nextYear = $month == 12 ? $year + 1 : $year;
                $endNext = \Carbon\Carbon::createFromFormat('Y-m-d H:i', sprintf('%04d-%02d-%02d %s', $nextYear, $nextMonth, $endDay, $grace->update_grace_period_end_time));
                $isGracePeriod = $now->between($startPrev, $endCurr) || $now->between($startCurr, $endNext);
                } else {
                // 同月
                $start = \Carbon\Carbon::createFromFormat('Y-m-d H:i', sprintf('%04d-%02d-%02d %s', $year, $month, $startDay, $grace->update_grace_period_start_time));
                $end = \Carbon\Carbon::createFromFormat('Y-m-d H:i', sprintf('%04d-%02d-%02d %s', $year, $month, $endDay, $grace->update_grace_period_end_time));
                $isGracePeriod = $now->between($start, $end);
                }
                }
                @endphp
                <div class="mb-2">
                    {{ $zone->psection_subject }}：空き {{ max(0, $vacant) }}台
                    @if($isGracePeriod)
                    @if($vacant > 0)
                    <button type="button" class="btn btn-success btn-sm btn-contract"
                        data-park-id="{{ $park->park_id }}"
                        data-psection-id="{{ $zone->psection_id }}"
                        data-ptype-id="{{ $zone->ptype_id }}">
                        定期契約
                    </button>
                    @else
                    <button type="button" class="btn btn-danger btn-sm btn_103 btn-reserve"
                        data-park-id="{{ $park->park_id }}"
                        data-psection-id="{{ $zone->psection_id }}"
                        data-ptype-id="{{ $zone->ptype_id }}">
                        空き待ち予約
                    </button>
                    @endif
                    @else
                    <button type="button" class="btn btn-danger btn-sm btn_103 btn-reserve"
                        data-park-id="{{ $park->park_id }}"
                        data-psection-id="{{ $zone->psection_id }}"
                        data-ptype-id="{{ $zone->ptype_id }}">
                        販売期間外（予約可）
                    </button>
                    @endif
                </div>
                @endforeach
            </div>
        </div>
        @endforeach
    </div>
</div>
<div class="modal-footer">
    <button type="button" class="btn btn-secondary" data-dismiss="modal">閉じる</button>
</div>