@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（city と同型：JS はここを submit） --}}
            <form method="GET" action="{{ route('usertypes.index') }}" id="list-form">
                <input type="hidden" name="sort" id="sort" value="{{ $sort ?? '' }}">
                <input type="hidden" name="sort_type" id="sort_type" value="{{ $sort_type ?? '' }}">

                {{-- フィルタ条件（並び替え時に条件が消えないように hidden で保持） --}}
                <input type="hidden" name="filter_sort_order" value="{{ request('filter_sort_order', $filter_sort_order ?? '') }}">
                <input type="hidden" name="filter_usertype_subject1" value="{{ request('filter_usertype_subject1', $filter_usertype_subject1 ?? '') }}">
                <input type="hidden" name="filter_usertype_subject2" value="{{ request('filter_usertype_subject2', $filter_usertype_subject2 ?? '') }}">
                <input type="hidden" name="filter_usertype_subject3" value="{{ request('filter_usertype_subject3', $filter_usertype_subject3 ?? '') }}">
            </form>

            <!-- ▼ 絞り込みフィルター -->
            <div class="col-lg-12 px-0">
                <div class="card">
                    <div class="card-header">
                        <h3 class="card-title">絞り込みフィルター</h3>
                    </div>

                    <div class="card-body">
                        {{-- city と同じ GET 運用 --}}
                        <form action="{{ route('usertypes.index') }}" method="GET" id="filter-form">
                            {{-- 現在の並び順も 유지（filter しても sort が消えない） --}}
                            <input type="hidden" name="sort" value="{{ $sort ?? '' }}">
                            <input type="hidden" name="sort_type" value="{{ $sort_type ?? '' }}">

                            {{-- 1行目：ソートオーダー / 分類名1 / 分類名2 --}}
                            <div class="row mb-3">
                                <div class="col-md-4">
                                    <div class="font-weight-bold mb-2">ソートオーダー</div>
                                    <input type="text"
                                           name="filter_sort_order"
                                           class="form-control"
                                           value="{{ request('filter_sort_order', $filter_sort_order ?? '') }}"
                                           placeholder="123456">
                                </div>

                                <div class="col-md-4">
                                    <div class="font-weight-bold mb-2">分類名1</div>
                                    <input type="text"
                                           name="filter_usertype_subject1"
                                           class="form-control"
                                           value="{{ request('filter_usertype_subject1', $filter_usertype_subject1 ?? '') }}"
                                           placeholder="キーワード...">
                                </div>

                                <div class="col-md-4">
                                    <div class="font-weight-bold mb-2">分類名2</div>
                                    <input type="text"
                                           name="filter_usertype_subject2"
                                           class="form-control"
                                           value="{{ request('filter_usertype_subject2', $filter_usertype_subject2 ?? '') }}"
                                           placeholder="キーワード...">
                                </div>
                            </div>

                            {{-- 2行目：分類名3 --}}
                            <div class="row mb-3">
                                <div class="col-md-4">
                                    <div class="font-weight-bold mb-2">分類名3</div>
                                    <input type="text"
                                           name="filter_usertype_subject3"
                                           class="form-control"
                                           value="{{ request('filter_usertype_subject3', $filter_usertype_subject3 ?? '') }}"
                                           placeholder="キーワード...">
                                </div>
                            </div>

                            {{-- ボタン --}}
                            <div class="mt-2">
                                <button type="submit" class="btn btn-success mr-2">
                                    絞り込み
                                </button>

                                {{-- 解除（sort/sort_type も初期化したいなら route('usertypes.index') のみにする） --}}
                                <a href="{{ route('usertypes.index') }}" class="btn btn-outline-secondary">
                                    解除
                                </a>
                            </div>

                        </form>
                    </div>
                </div>
            </div>
            <!-- ▲ 絞り込みフィルター -->

            {{-- ▼ ボタンエリア（city と同型） --}}
            <div class="col-lg-12 mb-3 px-0">
                <button type="button"
                        class="btn btn-sm btn-primary mr10"
                        onclick="location.href='{{ route('usertypes.create') }}?back={{ urlencode(request()->fullUrl()) }}'">
                    新規
                </button>

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

                <button type="submit" class="btn btn-sm btn-outline-success mr10" form="form_export">
                    CSV出力
                </button>
            </div>

            {{-- ▼ ページネーション（city と同型） --}}
            <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>

            {{-- ▼ フラッシュメッセージ（city と同型） --}}
            <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(isset($errorMsg))
                    <div class="alert alert-danger alert-dismissible">
                        <button type="button" class="close" data-dismiss="alert">×</button>
                        {!! $errorMsg !!}
                    </div>
                @endif
            </div>

            {{-- ▼ テーブル（city と同型） --}}
            <div class="col-lg-12 mb20 px-0">
                <div class="table-responsive">
                    <form id="form_delete" method="POST" action="{{ route('usertypes.destroy') }}">
                        @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 ?? '') === 'user_categoryid' ? (($sort_type ?? '') === 'asc' ? 'sorting_asc' : 'sorting_desc') : '' }}"
                                        sort="user_categoryid">
                                        <span>利用者分類ID</span>
                                    </th>

                                    <th class="sorting {{ ($sort ?? '') === 'sort_order' ? (($sort_type ?? '') === 'asc' ? 'sorting_asc' : 'sorting_desc') : '' }}"
                                        sort="sort_order">
                                        <span>ソートオーダー</span>
                                    </th>

                                    <th class="sorting {{ ($sort ?? '') === 'usertype_subject1' ? (($sort_type ?? '') === 'asc' ? 'sorting_asc' : 'sorting_desc') : '' }}"
                                        sort="usertype_subject1">
                                        <span>分類名1</span>
                                    </th>

                                    <th class="sorting {{ ($sort ?? '') === 'usertype_subject2' ? (($sort_type ?? '') === 'asc' ? 'sorting_asc' : 'sorting_desc') : '' }}"
                                        sort="usertype_subject2">
                                        <span>分類名2</span>
                                    </th>

                                    <th class="sorting {{ ($sort ?? '') === 'usertype_subject3' ? (($sort_type ?? '') === 'asc' ? 'sorting_asc' : 'sorting_desc') : '' }}"
                                        sort="usertype_subject3">
                                        <span>分類名3</span>
                                    </th>

                                    <th class="sorting {{ ($sort ?? '') === 'print_name' ? (($sort_type ?? '') === 'asc' ? 'sorting_asc' : 'sorting_desc') : '' }}"
                                        sort="print_name">
                                        <span>印字名</span>
                                    </th>

                                    <th><span>適用料率</span></th>
                                    <th><span>備考</span></th>
                                </tr>
                            </thead>

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

                                        <td>{{ $item->user_categoryid }}</td>
                                        <td>{{ $item->sort_order }}</td>
                                        <td>{{ $item->usertype_subject1 }}</td>
                                        <td>{{ $item->usertype_subject2 }}</td>
                                        <td>{{ $item->usertype_subject3 }}</td>
                                        <td>{{ $item->print_name }}</td>
                                        <td class="text-right">{{ $item->usertype_money }}</td>
                                        <td>{{ $item->usertype_remarks }}</td>
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </form>
                </div>
            </div>

            {{-- CSV（POST のまま） --}}
            <form action="{{ route('usertypes.export') }}" method="POST" id="form_export">
                @csrf
                {{-- 現在の検索条件を引き継ぐなら hidden を足す（必要なら） --}}
                <input type="hidden" name="sort" value="{{ $sort ?? '' }}">
                <input type="hidden" name="sort_type" value="{{ $sort_type ?? '' }}">
                <input type="hidden" name="filter_sort_order" value="{{ request('filter_sort_order', $filter_sort_order ?? '') }}">
                <input type="hidden" name="filter_usertype_subject1" value="{{ request('filter_usertype_subject1', $filter_usertype_subject1 ?? '') }}">
                <input type="hidden" name="filter_usertype_subject2" value="{{ request('filter_usertype_subject2', $filter_usertype_subject2 ?? '') }}">
                <input type="hidden" name="filter_usertype_subject3" value="{{ request('filter_usertype_subject3', $filter_usertype_subject3 ?? '') }}">
            </form>

        </div>
    </section>
@endsection
