@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('terms') }}" 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 d-flex justify-content-between">
            <div>
                <button type="button" class="btn btn-sm btn-default mr10" onclick="location.href='{{ route('terms_add') }}'">新規</button>
                <button type="button" class="btn btn-sm btn-default mr10" id="delete">削除</button>
            </div>
            <div style="margin-top:20px;">
                {{ $terms->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" role="alert">
                    <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
                    {{ session('success') }}
                </div>
            @elseif(session('error'))
                <div class="alert alert-danger alert-dismissible" role="alert">
                    <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
                    {{ session('error') }}
                </div>
            @endif
        </div>

        <!-- ▼ 単一テーブル構成 -->
        <div class="col-lg-12 mb20">
            <div class="table-responsive">
                <form action="{{ route('terms_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=='terms_id') ? ($sort_type=='asc'?'sorting_asc':'sorting_desc') : '' }}" sort="terms_id">
                                    <span>利用規約ID</span>
                                </th>
                                <th class="sorting {{ ($sort=='city_id') ? ($sort_type=='asc'?'sorting_asc':'sorting_desc') : '' }}" sort="city_id">
                                    <span>市区ID</span>
                                </th>
                                <th><span>使用中</span></th>
                                <th><span>リビジョン</span></th>
                                <th><span>規約内容</span></th>
                                <th><span>備考</span></th>
                                <th class="sorting {{ ($sort=='start_date') ? ($sort_type=='asc'?'sorting_asc':'sorting_desc') : '' }}" sort="start_date">
                                    <span>使用開始日</span>
                                </th>
                            </tr>
                        </thead>
                        <tbody class="bg-white">
                            @foreach($terms as $term)
                                <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="{{ $term->terms_id }}">
                                            <a href="{{ route('terms_edit', ['id' => $term->terms_id]) }}" class="btn btn-sm btn-default ml10">編集</a>
                                        </div>
                                    </td>
                                    <td class="sm-item text-left align-middle">{{ $term->terms_id }}</td>
                                    <td class="sm-item text-left align-middle">{{ $term->city_id }}</td>
                                    <td class="sm-item text-left align-middle">{{ $term->use_flag ? '○' : '' }}</td>
                                    <td class="sm-item text-left align-middle">{{ $term->terms_revision }}</td>
                                    <td class="sm-item text-left align-middle">
                                        {{ \Illuminate\Support\Str::limit($term->terms_text, 100) }}
                                    </td>
                                    <td class="sm-item text-left align-middle">{{ $term->memo }}</td>
                                    <td class="sm-item text-left align-middle">{{ \Carbon\Carbon::parse($term->start_date)->format('Y-m-d') }}</td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                </form>
            </div>
        </div>
        <!-- ▲ 単一テーブル構成ここまで -->
    </div>
</section>
@endsection
