@extends('layouts.app')
@section('content')
<main>
    <header class="alert alert-success">
        @if($isRegularContract)
        <h4 class="container">新規定期契約 > 空き駐輪場を確認する</h4>
        @else
        <h4 class="container">駐輪場検索 > 駐輪場選択</h4>
        @endif
    </header>
    <section id="" class="container mt20 mb20">
        <div class="row">
            <div class="col-12 col-lg-10 offset-0 offset-lg-1 mt20 mb20">
                <div class="card border-success">
                    <div class="card-header border-success">
                        <h5 class="card-title text-success">駐輪場をお選びください。</h5>
                    </div>
                    <div class="card-body">
                        <form class="row form" method="GET" action="">
                            <div class="w-100 alert alert-success">
                                <h6><a class="text-success" data-toggle="collapse" href="#search-option" role="button"
                                        aria-expanded="false" aria-controls="search-option">絞込み条件を追加する</a></h6>
                                <div class="collapse row" id="search-option">
                                    <div class="col-3">市町村名</div>
                                    <div class="col-9 mb10">
                                        <select id="city-select" name="city_id" class="form-control form-control-lg" onchange="this.form.submit()">
                                            <option value="">市町村を選択してください</option>
                                            @foreach($cities as $city)
                                            <option value="{{ $city->city_id }}" @if(request('city_id')==$city->city_id) selected @endif>{{ $city->city_name }}</option>
                                            @endforeach
                                        </select>
                                    </div>
                                    <div class="col-3">駅名</div>
                                    <div class="col-9 mb10">
                                        <select id="station-select" name="station_neighbor_station" class="form-control form-control-lg" onchange="this.form.submit()">
                                            <option value="">駅名を選択してください</option>
                                            @foreach($stations as $station)
                                            <option value="{{ $station->station_neighbor_station }}" @if(request('station_neighbor_station')==$station->station_neighbor_station) selected @endif>{{ $station->station_neighbor_station }}</option>
                                            @endforeach
                                        </select>
                                    </div>
                                    <div class="col-3">駐輪場名</div>
                                    <div class="col-9 mb10">
                                        <select id="park-select" name="park_id" class="form-control form-control-lg" onchange="this.form.submit()">
                                            <option value="">全て</option>
                                            @foreach($parks as $park)
                                            <option value="{{ $park->park_id }}" @if(request('park_id')==$park->park_id) selected @endif>{{ $park->park_name }}</option>
                                            @endforeach
                                        </select>
                                    </div>
                                </div>
                            </div>
                        </form>
                        <table id="searchTable" class="tablesorter table table-striped">
                            <thead>
                                <tr>
                                    <th class="header">
                                        駐輪場名
                                        <a href="{{ request()->fullUrlWithQuery(['sort' => 'park_ruby', 'order' => 'asc', 'page' => 1]) }}" class="text-success"><i class="bi bi-caret-up-fill"></i></a>
                                        <a href="{{ request()->fullUrlWithQuery(['sort' => 'park_ruby', 'order' => 'desc', 'page' => 1]) }}" class="text-success"><i class="bi bi-caret-down-fill"></i></a>
                                    </th>
                                    <th class="header">
                                        市町村名
                                        <a href="{{ request()->fullUrlWithQuery(['sort' => 'city_id', 'order' => 'asc', 'page' => 1]) }}" class="text-success"><i class="bi bi-caret-up-fill"></i></a>
                                        <a href="{{ request()->fullUrlWithQuery(['sort' => 'city_id', 'order' => 'desc', 'page' => 1]) }}" class="text-success"><i class="bi bi-caret-down-fill"></i></a>
                                    </th>
                                    <th class="header">
                                        駅名
                                        <a href="{{ request()->fullUrlWithQuery(['sort' => 'station_name_ruby', 'order' => 'asc', 'page' => 1]) }}" class="text-success"><i class="bi bi-caret-up-fill"></i></a>
                                        <a href="{{ request()->fullUrlWithQuery(['sort' => 'station_name_ruby', 'order' => 'desc', 'page' => 1]) }}" class="text-success"><i class="bi bi-caret-down-fill"></i></a>
                                    </th>
                                    <th class="header">自転車</th>
                                    <th class="header">原付</th>
                                    <th class="header">自動二輪</th>
                                    <th class="header">自動車</th>
                                </tr>
                            </thead>
                            <tbody>
                                @forelse($parks_table as $row)
                                <tr>
                                    <td><a href="javascript:void(0);" class="btn-popup text-primary" data-park-id="{{ $row->park_id }}">
                                            {{ $row->park_name }}
                                        </a></td>
                                    <td>{{ $row->city_name }}</td>
                                    <td>{{ $row->station_neighbor_station }}</td>
                                    {{-- 自転車・原付・自動二輪・自動車列 --}}
                                    @foreach(['自転車', '原付', '自動二輪', '自動車'] as $vehicle)
                                    <td>
                                        @php
                                        $zonesForType = ($zones[$row->park_id] ?? collect())->where('psection_subject', $vehicle);
                                        @endphp
                                        @forelse ($zonesForType as $zone)
                                        @php
                                        $reserveCount = ($reserve[$row->park_id] ?? collect())
                                        ->where('psection_id', $zone->psection_id)
                                        ->count();
                                        $vacancy = $zone->zone_tolerance - $zone->zone_number - $reserveCount;
                                        // 猶予期間判定
                                        $grace = $city_grace_periods[$row->city_id] ?? null;
                                        $now = \Carbon\Carbon::now();
                                        $inGrace = 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) {
                                        $year = $now->year;
                                        $month = $now->month;
                                        $startDay = (int)$grace->update_grace_period_start_date;
                                        $endDay = (int)$grace->update_grace_period_end_date;
                                        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));
                                        $inGrace = $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));
                                        $inGrace = $now->between($start, $end);
                                        }
                                        }
                                        @endphp
                                        @if ($vacancy > 0 && $inGrace)
                                        <button class="btn btn-block btn-sm btn-outline-success btn_82-table btn-popup" data-park-id="{{ $row->park_id }}">定期契約</button>
                                        @elseif (!$inGrace)
                                        <button class="btn btn-block btn-sm btn-outline-danger btn_103-table btn-popup" data-park-id="{{ $row->park_id }}">販売期間外</button>
                                        @elseif ($vacancy <= 0 && $inGrace)
                                        <button class="btn btn-block btn-sm btn-outline-danger btn_103-table btn-popup" data-park-id="{{ $row->park_id }}">空き待ち申込</button>
                                        @endif
                                        @empty
                                        <span class="text-muted"></span>
                                        @endforelse
                                    </td>
                                    @endforeach
                                </tr>
                                @empty
                                <tr>
                                    <td colspan="7" class="text-center">該当する駐輪場はありません。</td>
                                </tr>
                                @endforelse
                            </tbody>
                        </table>

                        @php
                        $totalPages = ceil($parks_table_total / $parks_table_perPage);
                        $currentPage = $parks_table_page;
                        @endphp
                        <nav aria-label="searchTable-pager">
                            <ul class="pagination justify-content-center">
                                <li class="page-item {{ $currentPage <= 1 ? 'disabled' : '' }}">
                                    <a class="page-link text-success" href="{{ request()->fullUrlWithQuery(['page' => $currentPage - 1]) }}" aria-label="前">
                                        <span aria-hidden="true">&laquo;</span>
                                        <span class="sr-only">前</span>
                                    </a>
                                </li>
                                @for($i = 1; $i <= $totalPages; $i++)
                                    <li class="page-item {{ $i == $currentPage ? 'active' : '' }}">
                                    <a class="page-link text-success" href="{{ request()->fullUrlWithQuery(['page' => $i]) }}">{{ $i }}</a>
                                    </li>
                                    @endfor
                                    <li class="page-item {{ $currentPage >= $totalPages ? 'disabled' : '' }}">
                                        <a class="page-link text-success" href="{{ request()->fullUrlWithQuery(['page' => $currentPage + 1]) }}" aria-label="次">
                                            <span aria-hidden="true">»</span>
                                            <span class="sr-only">次</span>
                                        </a>
                                    </li>
                            </ul>
                        </nav>

                    </div>
                </div>
            </div>
        </div>
    </section>
</main>
<div id="modalArea"></div>
<div class="modal fade" id="popup-modal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content" id="popup-content">
            <!-- Ajaxで詳細HTMLを挿入 -->
        </div>
    </div>
</div>
@endsection