@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>

<!-- Main Content -->
<section class="content">
    <div class="container-fluid">

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

        <div class="container-fluid mb20">
            <button type="button" class="btn btn-sm btn-default mr10" onclick="location.href='{{ route('regular_types_add') }}'">新規</button>
            <button type="button" class="btn btn-sm btn-default mr10" id="delete">削除</button>
            <!-- <button type="submit" class="btn btn-sm btn-default mr10" form="form_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">
            {{-- ▼ フラッシュメッセージ --}}
            @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 mb20">
            <div class="table-responsive">
                <form action="{{ route('regular_types_delete') }}" method="POST" id="form_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>
                                {{-- ソート --}}
                                <th class="sorting {{ ($sort=='regular_type_id') ? ($sort_type=='asc'?'sorting_asc':'sorting_desc') : '' }}" sort="regular_type_id"><span>定期種別ID</span></th>
                                <th><span>市区名</span></th>
                                <th><span>定期種別1</span></th>
                                <th><span>定期種別2</span></th>
                                <th><span>定期種別3</span></th>
                                <th><span>定期種別6</span></th>
                                <th><span>定期種別12</span></th>
                                <th><span>備考</span></th>
                            </tr>
                        </thead>
                        <tbody class="bg-white">
                            @php
                                $rc = \App\Models\RegularType::RegularClass;
                            @endphp
                            @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="pk[]" value="{{ $item->regular_type_id }}">
                                            <a href="{{ route('regular_types_edit', ['id' => $item->regular_type_id]) }}" class="btn btn-sm btn-default ml10">編集</a>
                                        </div>
                                    </td>

                                    <td class="sm-item text-left align-middle">{{ $item->regular_type_id }}</td>
                                    <td class="sm-item text-left align-middle">{{ $item->getCity()?->city_name ?? '-' }}</td>
                                    <td class="sm-item text-left align-middle">{{ __($rc[$item->regular_class_1] ?? '-') }}</td>
                                    <td class="sm-item text-left align-middle">{{ __($rc[$item->regular_class_2] ?? '-') }}</td>
                                    <td class="sm-item text-left align-middle">{{ __($rc[$item->regular_class_3] ?? '-') }}</td>
                                    <td class="sm-item text-left align-middle">{{ __($rc[$item->regular_class_6] ?? '-') }}</td>
                                    <td class="sm-item text-left align-middle">{{ __($rc[$item->regular_class_12] ?? '-') }}</td>
                                    <td class="sm-item text-left align-middle">{{ $item->memo ?? '-' }}</td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                </form>
            </div>
        </div>
        <!-- ▲ 単一テーブル構成ここまで -->
    </div>
</section>

<form action="{{ route('regular_types_export') }}" method="GET" id="form_export"></form>
@endsection
