@extends('layouts.app')
@section('title', 'シール発行履歴')

@section('content')
<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-2">
        <h3 class="m-0 text-dark">シール発行履歴</h3>
        <nav aria-label="breadcrumb" class="mb-0" style="background: transparent;">
            <ol class="breadcrumb px-2 py-2 mb-0" style="background: transparent;">
                <li class="breadcrumb-item"><a href="{{ route('home') }}">ホーム</a></li>
                <li class="breadcrumb-item active" aria-current="page">シール発行履歴</li>
            </ol>
        </nav>
    </div>

    <!-- 絞り込みフィルター -->
    <div class="card mb-3">
        <div class="card-body">
            <form method="GET" action="{{ route('seals') }}">
                <div class="row mb-3 align-items-center">
                    <label class="col-sm-2 col-form-label fw-bold">シール発行駐輪場</label>
                    <div class="col-sm-4">
                        <select name="park_id" class="form-select">
                            <option value="">全て</option>
                            @foreach($parks as $park)
                                <option value="{{ $park->park_id }}"

                                    {{ (string)request('park_id') === (string)$park->park_id ? 'selected' : '' }}>
                                    {{ $park->park_name }}
                                </option>
                            @endforeach
                        </select>
                    </div>
                </div>

                <div class="row mb-3 align-items-center">
                    <label class="col-sm-2 col-form-label fw-bold">エリア</label>
                    <div class="col-sm-4">
                        <select name="park_id" class="form-select">
                            <option value="">全て</option>
                            @foreach($parks as $park)
                                <option value="{{ $park->park_id }}"

                                    {{ (string)request('park_id') === (string)$park->park_id ? 'selected' : '' }}>
                                    {{ $park->park_name }}
                                </option>
                            @endforeach
                        </select>
                    </div>
                </div>




                <div class="row mb-3 align-items-center">
                    <label class="col-sm-2 col-form-label fw-bold py-0">発行日</label>
                    <div class="col-sm-10">
                        <div class="row align-items-center mb-0">
                            <div class="col-auto">
                                <div class="form-check form-check-inline me-2">
                                    <input class="form-check-input" type="radio" name="period_type" id="period_type_range" value="range" {{ request('period_type', 'range') == 'range' ? 'checked' : '' }}>
                                    <label class="form-check-label" for="period_type_range">範囲指定</label>
                                </div>
                            </div>
                            <div class="col-auto ms-3">
                                <div class="d-flex align-items-center">
                                    <input type="date" name="seal_day_from" id="seal_day_from" class="form-control" style="width:160px;" value="{{ request('seal_day_from') }}">
                                    <span class="mx-3 fw-bold">～</span>
                                    <input type="date" name="seal_day_to" id="seal_day_to" class="form-control" style="width:160px;" value="{{ request('seal_day_to') }}">
                                </div>
                            </div>
                        </div>
                        <div class="row align-items-center mt-2">
                            <div class="col-auto">
                                <div class="form-check form-check-inline me-2">
                                    <input class="form-check-input" type="radio" name="period_type" id="period_type_recent" value="recent" {{ request('period_type') == 'recent' ? 'checked' : '' }}>
                                    <label class="form-check-label" for="period_type_recent">直近Nヶ月等</label>
                                </div>
                            </div>
                            <div class="col-auto ms-3">
                                <select name="recent_period" id="recent_period" class="form-select">
                                    <option value="">全て</option>
                                    <option value="12m" {{ request('recent_period') == '12m' ? 'selected' : '' }}>直近12ヶ月</option>
                                    <option value="6m"  {{ request('recent_period') == '6m'  ? 'selected' : '' }}>直近6ヶ月</option>
                                    <option value="3m"  {{ request('recent_period') == '3m'  ? 'selected' : '' }}>直近3ヶ月</option>
                                    <option value="2m"  {{ request('recent_period') == '2m'  ? 'selected' : '' }}>直近2ヶ月</option>
                                    <option value="1m"  {{ request('recent_period') == '1m'  ? 'selected' : '' }}>直近1ヶ月</option>
                                    <option value="1w"  {{ request('recent_period') == '1w'  ? 'selected' : '' }}>直近1週間</option>
                                </select>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="row">
                    <div class="col-sm-12 text-end">
                        <button type="submit" class="btn btn-default me-2">絞り込み</button>
                        <a href="{{ route('seals') }}" class="btn btn-default">解除</a>
                        <button type="submit" class="btn btn-default me-2">CSV出力</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
        <div class="ms-auto">
            {{ $list->appends(request()->except('page'))->links('pagination') }}
        </div>
    <!-- データテーブル -->
    <div id="seals-table-wrapper" class="table-responsive">
        <table class="table table-bordered table-hover table-sm dataTable">
            <thead class="thead-light">
                <tr>
                    <th class="sorting {{ ($sort=='seal_issueid') ? ($sortType=='asc'?'sorting_asc':'sorting_desc') : '' }}" sort="seal_issueid" style="cursor:pointer;">
                        シール発行履歴ID
                    </th>
                    <th class="sorting {{ ($sort=='park_id') ? ($sortType=='asc'?'sorting_asc':'sorting_desc') : '' }}" sort="park_id" style="cursor:pointer;">
                        シール発行駐輪場
                    </th>
                    <th class="sorting {{ ($sort=='contract_id') ? ($sortType=='asc'?'sorting_asc':'sorting_desc') : '' }}" sort="contract_id" style="cursor:pointer;">
                        定期契約ID
                    </th>
                    <th>車種区分ID</th>
                    <th class="sorting {{ ($sort=='seal_day') ? ($sortType=='asc'?'sorting_asc':'sorting_desc') : '' }}" sort="seal_day" style="cursor:pointer;">
                        発行日
                    </th>
                    <th class="sorting {{ ($sort=='contract_seal_issue') ? ($sortType=='asc'?'sorting_asc':'sorting_desc') : '' }}" sort="contract_seal_issue" style="cursor:pointer;">
                        発行回数
                    </th>
                    <th>理由</th>
                </tr>
            </thead>
            <tbody class="bg-white">
                @foreach($list as $item)
                <tr>
                    <td>{{ $item->seal_issueid }}</td>
                    <td>{{ $item->park_name ?? $item->park_id }}</td>
                    <td>{{ $item->contract_id }}</td>
                    <td>{{ $item->psection_subject ?? $item->psection_id }}</td>
                    <td>{{ $item->seal_day }}</td>
                    <td>{{ $item->contract_seal_issue }}</td>
                    <td>{{ $item->seal_reason }}</td>
                </tr>
                @endforeach
            </tbody>
        </table>
    </div>
    <div class="d-flex align-items-center mb-2">
@push('scripts')
<script>
// タグ発行キュー処理と同じソート挙動
document.querySelectorAll('th.sorting').forEach(th => {
    th.addEventListener('click', function(){
        const url = new URL(window.location.href);
        const current = url.searchParams.get('sort') || '';
        const currentType = url.searchParams.get('sort_type') || '';
        const nextCol = this.getAttribute('sort');
        let nextType = 'asc';
        if (current === nextCol) {
            nextType = (currentType === 'asc') ? 'desc' : 'asc';
        }
        url.searchParams.set('sort', nextCol);
        url.searchParams.set('sort_type', nextType);
        window.location.href = url.toString();
    });
});

    function togglePeriodInputs() {
        const isRange = document.getElementById('period_type_range').checked;
        document.getElementById('seal_day_from').disabled = !isRange;
        document.getElementById('seal_day_to').disabled = !isRange;
        document.getElementById('recent_period').disabled = isRange;
    }
    document.getElementById('period_type_range').addEventListener('change', togglePeriodInputs);
    document.getElementById('period_type_recent').addEventListener('change', togglePeriodInputs);
    window.addEventListener('DOMContentLoaded', togglePeriodInputs);
</script>
@endpush
    </div>
</div>
@endsection