@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"><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">
        {{-- 並び替え用 hidden --}}
        <form action="{{ route('jurisdiction_parkings') }}" 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 justify-content-between">
            <div>
                <button type="button" class="btn btn-sm btn-default mr10" onclick="location.href='{{ route('jurisdiction_parkings_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>
                <!-- CSVインポート -->
                <form action="{{ route('jurisdiction_parkings_import') }}" 
                    method="POST" enctype="multipart/form-data" 
                    style="display:inline-block">
                    @csrf
                    <input type="file" name="csv_file" accept=".csv" class="d-none" id="csvInput"
                        onchange="this.form.submit()">
                    <button type="button" class="btn btn-sm btn-default mr10"
                            onclick="document.getElementById('csvInput').click()">
                        {{ __('インポート') }}
                    </button>
                </form>
            </div>
            <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('success'))
                <div class="alert alert-success alert-dismissible">{{ session('success') }}</div>
            @elseif(session('error'))
                <div class="alert alert-danger alert-dismissible">{{ session('error') }}</div>
            @endif
        </div>

        <!-- ▼ 単一テーブル構成 ----------------------------------------- -->
        <div class="col-lg-12 mb20">
            <div class="table-responsive">
                <form action="{{ route('jurisdiction_parkings_delete') }}" method="POST" id="form_delete">
                    @csrf
                    <table class="table table-bordered dataTable text-nowrap">
                        <thead class="thead-light">
                            <tr>
                                {{-- ★ チェック + 編集 用の1列 --}}
                                <th style="width:140px;" class="text-left">
                                    <input type="checkbox" onclick="$('input[name*=\'pk\']').prop('checked', this.checked);">
                                </th>
                                <th class="sorting {{ ($sort=='jurisdiction_parking_id') ? ($sort_type=='asc'?'sorting_asc':'sorting_desc') : '' }}" sort="jurisdiction_parking_id"><span>管轄駐輪場ID</span></th>
                                <th class="sorting {{ ($sort=='jurisdiction_parking_name') ? ($sort_type=='asc'?'sorting_asc':'sorting_desc') : '' }}" sort="jurisdiction_parking_name"><span>管轄名</span></th>
                                <th><span>オペレーター（エリアマネージャ）</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="pk[]" value="{{ $item->jurisdiction_parking_id }}">
                                            <a href="{{ route('jurisdiction_parkings_edit', ['id' => $item->jurisdiction_parking_id]) }}" class="btn btn-sm btn-default ml-2">編集</a>
                                        </div>
                                    </td>

                                    <td class="sm-item text-left align-middle">{{ $item->jurisdiction_parking_id }}</td>
                                    <td class="sm-item text-left align-middle">{{ $item->jurisdiction_parking_name }}</td>
                                    <td class="sm-item text-left align-middle">{{ $item->ope->ope_name ?? '' }}</td>
                                    <td class="sm-item text-left align-middle">{{ $item->park->park_name ?? '' }}</td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                </form>
            </div>
        </div>
        <!-- ▲ 単一テーブル構成ここまで ----------------------------------------- -->
    </div>
</section>

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