@extends('layouts.app')

@section('title', '市区マスタ')

@section('content')
    {{-- ▼ フラッシュメッセージ --}}
    @if(session('success'))
        <div class="alert alert-success">{{ session('success') }}</div>
    @endif
    @if(session('error'))
        <div class="alert alert-danger">{{ session('error') }}</div>
    @endif

    <!-- Content Header (Page 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="{{ url('/home') }}">ホーム</a></li>
                        <li class="breadcrumb-item active">市区マスタ</li>
                    </ol>
                </div>
            </div>
        </div>
    </div>

    <!-- Main content -->
    <section class="content">
        <div class="container-fluid">
            <form id="list-form" method="GET" action="{{ route('city') }}">
                <input type="hidden" name="sort" value="{{ $sort }}">
                <input type="hidden" name="sort_type" value="{{ $sort_type }}">

            </form>
            <div class="d-flex flex-column flex-md-row align-items-md-center justify-content-between mb-3 gap-2">
                <div>
                    <a href="{{ route('city_add') }}" class="btn btn-sm btn-default">新規</a>
                    <button type="button" id="delete" class="btn btn-sm btn-default">削除</button>
                </div>
                @if ($list->count() > 0)
                        <div class="mt-2 mt-md-0 ms-md-3">
                        {{ $list->appends([
                    'sort' => $sort,
                    'sort_type' => $sort_type,
                    ] + request()->except('page'))->links('pagination::bootstrap-4') }}
                    </div>
                @endif
            </div>

            @if ($list->count() > 0)
                <form id="form_delete" method="POST" action="{{ route('city_delete') }}">
                    @csrf
                    <div class="table-responsive">
                        <table class="table table-bordered table-hover">
                            <thead class="thead-light">
                                <tr>
                                    <th><input type="checkbox" class="js-check-all"></th>
                                    <th class="sorting{{ $sort === 'city_id' ? ($sort_type === 'asc' ? '_asc' : '_desc') : '' }}"
                                        sort="city_id">
                                        市区ID

                                        @if($sort === 'city_id')
                                            <i class="fa fa-arrow-up ms-1" style="font-size: 0.7em; color: {{ $sort && $sort_type ? 'black' : '#ccc' }}"></i>
                                            <i class="fa fa-arrow-down ms-1" style="font-size: 0.7em; color: {{ $sort && !$sort_type ? 'black' : '#ccc' }}"></i>
                                        @endif
                                    </th>
                                    <th class="sorting{{ $sort === 'city_name' ? ($sort_type === 'asc' ? '_asc' : '_desc') : '' }}"
                                        sort="city_name">
                                        市区名
                                        @if($sort === 'city_name')
                                            <i class="fa fa-arrow-up ms-1" style="font-size: 0.7em; color: {{ $sort && $sort_type ? 'black' : '#ccc' }}"></i>
                                            <i class="fa fa-arrow-down ms-1" style="font-size: 0.7em; color: {{ $sort && !$sort_type ? 'black' : '#ccc' }}"></i>
                                        @endif
                                    </th>
                                    <th>印字レイアウトファイル</th>
                                    <th>顧客M入力不要フィールドID</th>
                                    <th>備考</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach ($list as $city)
                                    <tr>
                                        <td style="background: #faebd7; width: 110px; padding: 0 4px;">
                                            <input type="checkbox" name="pk[]" value="{{ $city->city_id }}">
                                            <a href="{{ route('city_edit', ['id' => $city->city_id]) }}"
                                                class="btn btn-sm btn-default ms-1">編集</a>
                                        </td>
                                        <td>{{ $city->city_id }}</td>
                                        <td>{{ $city->city_name }}</td>
                                        <td>{{ $city->print_layout }}</td>
                                        <td>{{ $city->city_user }}</td>
                                        <td>{{ $city->city_remarks }}</td>
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                </form>
            @else
                <div class="alert alert-info mt-4">表示する市区データがありません。</div>
            @endif
        </div>
    </section>
@endsection

@push('scripts')
    <script>
        (function ($) {
            $(function () {
                var $form = $('#form_delete');
                if (!$form.length) { return; }

                $(document)
                    .off('change.cityCheckAll', '.js-check-all')
                    .on('change.cityCheckAll', '.js-check-all', function () {
                        var checked = $(this).prop('checked');
                        $form.find('input[name="pk[]"]').prop('checked', checked);
                    });

                $(document)
                    .off('click.cityDeleteGuard', '#delete')
                    .on('click.cityDeleteGuard', '#delete', function (event) {
                        if (!$form.find('input[name="pk[]"]:checked').length) {
                            event.preventDefault();
                            event.stopImmediatePropagation();
                            window.alert('削除対象を選択してください。');
                        }
                    });
            });
        })(window.jQuery);

        $(document).on('click', 'th[sort]', function () {
            const $form = $('#list-form');
            const sortKey = $(this).attr('sort');
            const currentSort = $form.find('input[name="sort"]').val();
            const currentType = $form.find('input[name="sort_type"]').val();

            if (currentSort === sortKey) {
                // 切换方向
                const newType = currentType === 'asc' ? 'desc' : 'asc';
                $form.find('input[name="sort_type"]').val(newType);
            } else {
                // 新字段，默认升序
                $form.find('input[name="sort"]').val(sortKey);
                $form.find('input[name="sort_type"]').val('asc');
            }

            $form.submit();
        });
    </script>
@endpush