@extends('layouts.app')
@section('title', '契約許容市区マスタ')

@section('content')

@if ($errors->any())
    <div class="alert alert-danger">
        <ul class="mb-0">
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif
@if (session('success'))
    <div class="alert alert-success">
        {{ session('success') }}
    </div>
@endif

<!-- Content Header -->
<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"><a href="javascript:void(0);">[東京都|〇〇駐輪場]</a></li> -->
                    <li class="breadcrumb-item active">契約許容市区マスタ</li>
                </ol>
            </div>
        </div>
    </div>
</div>

<!-- Main Content -->
<section class="content">
    <div class="container-fluid">
        <!-- ▼ 検索条件 -->
        <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('contract_allowable_cities') }}" method="post" id="list-form">
                        @csrf
                        <input type="hidden" name="sort" id="sort" value="{{ $sort ?? '' }}">
                        <input type="hidden" name="sort_type" id="sort_type" value="{{ $sort_type ?? '' }}">

                        <div class="row">
                            <div class="form-group col-3">
                                <label>契約許容市区ID</label>
                                <input type="text" name="contract_allowable_city_id" class="form-control"
                                       value="{{ $inputs['contract_allowable_city_id'] ?? '' }}">
                            </div>
                            <div class="form-group col-3">
                                <label>市区ID</label>
                                <select name="city_id" class="form-control">
                                    <option value="">市区ID</option>
                                    @foreach($cityList as $id => $name)
                                        <option value="{{ $id }}" @if(($inputs['city_id'] ?? '') == $id) selected @endif>{{ $name }}</option>
                                    @endforeach
                                </select>
                            </div>
                            <div class="form-group col-3">
                                <label>許容市区名</label>
                                
                                <input type="text" name="contract_allowable_city_name" class="form-control"
                                       value="{{ $inputs['contract_allowable_city_name'] ?? '' }}">
                            </div>
                            <div class="form-group col-3">
                                <label>駐輪場ID</label>
                                <select name="park_id" class="form-control">
                                    <option value="">--</option>
                                    @foreach($parkList as $id => $name)
                                        <option value="{{ $id }}" @if(($inputs['park_id'] ?? '') == $id) selected @endif>{{ $name }}</option>
                                    @endforeach
                                </select>
                            </div>
                        </div>

                        <div class="form-group col-12 text-left">
                            <button type="submit" name="action" value="filter" class="btn btn-default">絞り込み</button>
                            <button type="submit" name="action" value="unlink" class="btn btn-default">解除</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
        <!-- ▲ 検索条件ここまで -->

        <!-- ▼ ボタン + ページネーション -->
        <div class="container-fluid mb20">
            <button type="button" class="btn btn-sm btn-default mr10" onclick="location.href='{{ route('contract_allowable_cities_add') }}'">新規</button>
            <button type="button" class="btn btn-sm btn-default mr10" id="delete">削除</button>
            <button type="button" class="btn btn-sm btn-default mr10" id="export_csv"
                    action="{{ route('contract_allowable_cities_export') }}">CSV出力</button>
            <div class="d-flex justify-content-end">
                {{ $list->appends(['sort' => $sort ?? '', 'sort_type' => $sort_type ?? ''])->links('pagination') }}
            </div>
        </div>

        <!-- ▼ 単一テーブル構成 -->
        <div class="col-lg-12 mb20">
            <div class="table-responsive">
                <form method="post" action="{{ route('contract_allowable_cities_delete') }}" id="form_delete">
                    @csrf
                    <table class="table table-bordered dataTable text-nowrap">
                        <thead class="thead-light">
                            <tr>
                                 <th style="width:140px; border-left:1px solid #dcdcdc;" class="text-left">
                                    <input type="checkbox" onclick="$('input[name*=\'pk\']').prop('checked', this.checked);">
                                </th>
                                <th class="sorting {{ ($sort=='contract_allowable_city_id') ? ($sort_type=='asc'?'sorting_asc':'sorting_desc') : '' }}" sort="contract_allowable_city_id"><span>契約許容市区ID</span></th>
                                <th class="sorting {{ ($sort=='city_id') ? ($sort_type=='asc'?'sorting_asc':'sorting_desc') : '' }}" sort="city_id"><span>市区ID</span></th>
                                <th class="sorting {{ ($sort=='contract_allowable_city_name') ? ($sort_type=='asc'?'sorting_asc':'sorting_desc') : '' }}" sort="contract_allowable_city_name"><span>許容市区名</span></th>
                                <th class="sorting {{ ($sort=='park_id') ? ($sort_type=='asc'?'sorting_asc':'sorting_desc') : '' }}" sort="park_id"><span>駐輪場ID</span></th>
                                <th><span>隣接区フラグ</span></th>
                            </tr>
                        </thead>
                        <tbody class="bg-white">
                            @foreach($list as $item)
                                <tr>
                                    <td class="align-middle" style="background-color:#faebd7;">
                                        <div class="d-flex align-items-center">
                                            <input type="checkbox" class="m-0 checkbox" name="id[]" value="{{ $item->contract_allowable_city_id }}">
                                            <a href="{{ route('contract_allowable_cities_edit', ['id' => $item->contract_allowable_city_id]) }}" class="btn btn-sm btn-default ml10">編集</a>
                                        </div>
                                    </td>
                                    <td class="sm-item text-left align-middle">{{ $item->contract_allowable_city_id }}</td>
                                    <td class="sm-item text-left align-middle">{{ $item->city_name }}</td>
                                    <td class="sm-item text-left align-middle">{{ $item->contract_allowable_city_name }}</td>
                                    <td>{{ $item->park_name }}</td>

                                    <!-- <td class="sm-item text-left align-middle">{{ $item->park_id }}</td> -->
                                    <td class="sm-item text-left align-middle">{{ $item->same_district_flag == 0 ? '隣接市' : 'その他' }}</td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                </form>
            </div>
        </div>
        <!-- ▲ 単一テーブル構成ここまで -->
    </div>
</section>
@endsection
