@extends('layouts.app')

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

@section('content')
    <!-- 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 active">市区マスタ</li>
                    </ol>
                </div>
            </div>
        </div>
    </div>

    <section class="content">
        <div class="container-fluid">

            {{-- 並び替え用 hidden --}}
            <form method="GET" action="{{ route('city') }}" id="list-form">
                <input type="hidden" name="sort" id="sort" value="{{ $sort ?? '' }}">
                <input type="hidden" name="sort_type" id="sort_type" value="{{ $sort_type ?? '' }}">
            </form>

            {{-- ▼ ボタンエリア --}}
            <div class="col-lg-12 mb-3 px-0">
                <button type="button"
                        class="btn btn-sm btn-primary mr10"
                        onclick="location.href='{{ route('city.add', ['back' => request()->fullUrl()]) }}'">
                    新規
                </button>

                <button type="button" class="btn btn-sm btn-danger mr10" id="delete">削除</button>
            </div>

            {{-- ▼ ページネーション --}}
            <div class="col-lg-12 px-0">
                <div class="d-flex flex-column align-items-end mb-2 text-sm">
                    {{-- 件数表示（上） --}}
                    <div class="text-dark text-sm mb-1">
                        @if ($list->total() > 0)
                            全 {{ $list->total() }} 件中 {{ $list->firstItem() }}〜{{ $list->lastItem() }} 件を表示
                        @else
                            全0件
                        @endif
                    </div>

                    {{-- ページネーション（下） --}}
                    <div>
                        {{ $list->appends([
                            'sort'      => $sort ?? '',
                            'sort_type' => $sort_type ?? '',
                        ] + request()->except('page'))->links('pagination') }}
                    </div>
                </div>
            </div>

            {{-- ▼ フラッシュメッセージ --}}
            <div class="form col-lg-12 px-0">
                @if(Session::has('success'))
                    <div class="alert alert-success alert-dismissible">
                        <button type="button" class="close" data-dismiss="alert">×</button>
                        {{ Session::get('success') }}
                    </div>
                @elseif(Session::has('error'))
                    <div class="alert alert-danger alert-dismissible">
                        <button type="button" class="close" data-dismiss="alert">×</button>
                        {!! Session::get('error') !!}
                    </div>
                @elseif(!empty($errorMsg))
                    <div class="alert alert-danger alert-dismissible">
                        <button type="button" class="close" data-dismiss="alert">×</button>
                        @if(is_array($errorMsg))
                            <ul class="mb-0">
                                @foreach($errorMsg as $msg)
                                    <li>{{ $msg }}</li>
                                @endforeach
                            </ul>
                        @else
                            {!! $errorMsg !!}
                        @endif
                    </div>
                @endif
            </div>

            {{-- ▼ テーブル --}}
            <div class="col-lg-12 mb20 px-0">
                <div class="table-responsive">
                    <form id="form_delete" method="POST" action="{{ route('city.delete') }}">
                        @csrf

                        <table class="table table-bordered dataTable text-nowrap">
                            <thead class="thead-light">
                                <tr>
                                    <th style="width:140px;">
                                        <input type="checkbox"
                                               class="js-check-all"
                                               onclick="$('input[name*=\'pk\']').prop('checked', this.checked);">
                                    </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 ?? '') === 'city_name' ? (($sort_type ?? '') === 'asc' ? 'sorting_asc' : 'sorting_desc') : '' }}"
                                        sort="city_name">
                                        <span>市区名</span>
                                    </th>

                                    <th><span>印字レイアウトファイル</span></th>
                                    <th><span>備考</span></th>
                                </tr>
                            </thead>

                            <tbody class="bg-white">
                                @foreach ($list as $city)
                                    <tr>
                                        <td style="background-color:#faebd7;">
                                            <div class="d-flex align-items-center">
                                                <input type="checkbox" name="pk[]" value="{{ $city->city_id }}">
                                                <a href="{{ route('city.edit', ['id' => $city->city_id, 'back' => request()->fullUrl()]) }}"
                                                   class="btn btn-sm btn-outline-primary ml10">
                                                    編集
                                                </a>
                                            </div>
                                        </td>

                                        <td>{{ $city->city_id }}</td>
                                        <td>{{ $city->city_name }}</td>
                                        <td>{{ $city->print_layout }}</td>
                                        <td>{{ $city->city_remarks }}</td>
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </form>
                </div>
            </div>

        </div>
    </section>
@endsection
