@extends('layouts.app')

@section('title', 'ゾーンマスタ')

@section('content')
<div class="content-header">
    <div class="container-fluid">
        <div class="row mb-2">
            <div class="col-lg-6">
                <h1 class="m-0 text-dark">ゾーンマスタ</h1>
            </div>
            <div class="col-lg-6">
                <ol class="breadcrumb float-sm-right text-sm">
                    <li class="breadcrumb-item"><a href="{{ route('home') }}">ホーム</a></li>
                    <li class="breadcrumb-item active">ゾーンマスタ</li>
                </ol>
            </div>
        </div>
    </div>
</div>

<section class="content">
    
    {{-- ▼ フラッシュメッセージ --}}
    @if(Session::has('success'))
        <div class="alert alert-success alert-dismissible" role="alert">
            <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
            {{ Session::get('success') }}
        </div>
    @elseif(Session::has('error'))
        <div class="alert alert-danger alert-dismissible">
            <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
            <h4><i class="icon fa fa-ban"></i> {{__('入力内容に不備があります')}}:</h4>
            {!! Session::get('error') !!}
        </div>
    @elseif(isset($errorMsg))
        <div class="alert alert-danger alert-dismissible">
            <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
            <h4><i class="icon fa fa-ban"></i> {{__('入力内容に不備があります')}}:</h4>
            {!! $errorMsg !!}
        </div>
    @endif
         
    <!-- ▼ 絞り込みフィルター -->
    <div class="col-lg-12">
        <div class="card">
            <div class="card-header">
                <h3 class="card-title">絞り込みフィルター</h3>
            </div>
            <div class="card-body">
                <form action="{{ route('zones') }}" method="POST" id="list-form" class="form-horizontal">
                    @csrf
                    <input type="hidden" name="sort" value="{{ $sort ?? '' }}">
                    <input type="hidden" name="sort_type" value="{{ $sort_type ?? '' }}">

                    <table class="table table-borderless mb-0" style="width: 100%;">
                        <tbody>
                            <tr>
                                <th style="form-group col-2">駐輪場ID</th>
                                <td>
                                    <select name="park_id" class="form-control form-control-sm" style="width: 300px;">
                                        <option value="">駐輪場ID</option>
                                        @foreach($parkList as $id => $label)
                                            <option value="{{ $id }}" {{ old('park_id', $park_id ?? '') == $id ? 'selected' : '' }}>
                                                {{ $label }}
                                            </option>
                                        @endforeach
                                    </select>
                                </td>
                                <th style="form-group col-2">駐輪分類ID</th>
                                <td>
                                    <select name="ptype_id" class="form-control form-control-sm" style="width: 300px;">
                                        <option value="">駐輪分類ID</option>
                                        @foreach($ptypeList as $id => $label)
                                            <option value="{{ $id }}" {{ old('ptype_id', $ptype_id ?? '') == $id ? 'selected' : '' }}>
                                                {{ $label }}
                                            </option>
                                        @endforeach
                                    </select>
                                </td>
                            </tr>
                            <tr>
                                <th style="form-group col-2">車種区分ID</th>
                                <td>
                                    <select name="psection_id" class="form-control form-control-sm" style="width: 300px;">
                                        <option value="">車種区分ID</option>
                                        @foreach($psectionList as $id => $label)
                                            <option value="{{ $id }}" {{ old('psection_id', $psection_id ?? '') == $id ? 'selected' : '' }}>
                                                {{ $label }}
                                            </option>
                                        @endforeach
                                    </select>
                                </td>
                            </tr>
                        </tbody>
                    </table>

                    <div class="form-group col-12 text-left mt-3">
                        <button type="submit" name="action" value="filter" class="btn btn-default">絞り込み</button>
                        <button type="submit" name="action" value="reset" class="btn btn-default">解除</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <!-- ▲ 絞り込みフィルター -->

    <!-- ▼ ツールバー -->
<section class="content">
    <div class="container-fluid">
        {{-- ▼ アクションボタン --}}
        <div class="mb-3">
            <a href="{{ route('zones_add') }}" class="btn btn-sm btn-default">新規</a>
            <button type="button" class="btn btn-sm btn-default mr10" id="delete">削除</button>
        </div>
        
        {{-- ▼ ページネーション --}}
        <div class="d-flex justify-content-end">
            {{ $zones->appends(request()->all())->links('pagination') }}
        </div>
   
    </div>
    <!-- ▲ ツールバー -->

    <!-- ▼ 一覧テーブル -->
    <div class="col-lg-12 mb20">
        <div class="table-responsive">
            <form id="form_delete" method="POST" action="{{ route('zones_delete') }}">
                @csrf
                <table class="table table-bordered dataTable text-nowrap">
                    <thead class="thead-light">
                        <tr>
                            <!-- 全選択チェックボックス -->
                            <th style="width:140px;" class="text-left">
                                <input type="checkbox" onclick="$('input[name*=\'pk\']').prop('checked', this.checked);">
                            </th>

                            <!-- ゾーンID -->
                            <th class="sorting {{ ($sort=='zone_id') ? ($sort_type=='asc'?'sorting_asc':'sorting_desc') : '' }}" sort="zone_id">
                                <span>ゾーンID</span>
                            </th>

                            <!-- 駐輪場ID -->
                            <th class="sorting {{ ($sort=='park_id') ? ($sort_type=='asc'?'sorting_asc':'sorting_desc') : '' }}" sort="park_id">
                                <span>駐輪場ID</span>
                            </th>

                            <!-- 駐輪分類ID -->
                            <th class="sorting {{ ($sort=='ptype_id') ? ($sort_type=='asc'?'sorting_asc':'sorting_desc') : '' }}" sort="ptype_id">
                                <span>駐輪分類ID</span>
                            </th>

                            <!-- ゾーン名 -->
                            <th class="sorting {{ ($sort=='zone_name') ? ($sort_type=='asc'?'sorting_asc':'sorting_desc') : '' }}" sort="zone_name">
                                <span>ゾーン名</span>
                            </th>

                            <!-- ゾーン内現在契約台数 -->
                            <th class="sorting {{ ($sort=='zone_number') ? ($sort_type=='asc'?'sorting_asc':'sorting_desc') : '' }}" sort="zone_number">
                                <span>ゾーン内現在契約台数</span>
                            </th>

                            <!-- ゾーン内標準台数 -->
                            <th class="sorting {{ ($sort=='zone_standard') ? ($sort_type=='asc'?'sorting_asc':'sorting_desc') : '' }}" sort="zone_standard">
                                <span>ゾーン内標準台数</span>
                            </th>

                            <!-- ゾーン内許容台数 -->
                            <th class="sorting {{ ($sort=='zone_tolerance') ? ($sort_type=='asc'?'sorting_asc':'sorting_desc') : '' }}" sort="zone_tolerance">
                                <span>ゾーン内許容台数</span>
                            </th>

                            <!-- ゾーン順 -->
                            <th class="sorting {{ ($sort=='zone_sort') ? ($sort_type=='asc'?'sorting_asc':'sorting_desc') : '' }}" sort="zone_sort">
                                <span>ゾーン順</span>
                            </th>

                            <!-- 車種区分ID -->
                            <th class="sorting {{ ($sort=='psection_id') ? ($sort_type=='asc'?'sorting_asc':'sorting_desc') : '' }}" sort="psection_id">
                                <span>車種区分ID</span>
                            </th>
                        </tr>
                    </thead>

                    <tbody>
                    @foreach($zones as $item)
                        <tr class="bg-white">
                            <td class="align-middle" style="background-color:#faebd7;">
                                <div class="d-flex align-items-center">
                                    <input type="checkbox" class="m-0 checkbox" name="pk[]" value="{{ $item->zone_id }}">
                                    <a href="{{ route('zones_edit', ['id' => $item->zone_id]) }}" class="btn btn-sm btn-default ml10">編集</a>
                                </div>
                            </td>

                            <td>{{ $item->zone_id }}</td>
                            <td>{{ $item->park->park_name ?? '' }}</td>
                            <td>{{ $item->ptype->ptype_subject ?? '' }}</td>
                            <td>{{ $item->zone_name }}</td>
                            <td>{{ $item->zone_number }}</td>
                            <td>{{ $item->zone_standard }}</td>
                            <td>{{ $item->zone_tolerance }}</td>
                            <td>{{ $item->zone_sort }}</td>
                            <td>{{ $item->psection->psection_subject ?? '' }}</td>
                        </tr>
                    @endforeach
                    </tbody>
                </table>
            </form>
        </div>
    </div>
    <!-- ▲ 一覧テーブル -->
</section>

@push('scripts')
<script>
document.addEventListener('DOMContentLoaded', function () {
    // ---------------------
    // ソート機能
    // ---------------------
    document.querySelectorAll('th.sorting').forEach(th => {
        th.addEventListener('click', function() {
            const form = document.getElementById('list-form');
            const current = "{{ $sort ?? '' }}";
            const currentType = "{{ $sort_type ?? '' }}";
            const nextCol = this.getAttribute('sort');
            let nextType = 'asc';
            if (current === nextCol) {
                nextType = (currentType === 'asc') ? 'desc' : 'asc';
            }
            form.querySelector('[name=sort]').value = nextCol;
            form.querySelector('[name=sort_type]').value = nextType;
            form.submit();
        });
    });
});
</script>
@endpush

@endsection
