@extends('layouts.app')
@section('title', '駐輪場管理者マスタ')

@section('content')

<style>
.sticky-col {
  position: sticky;
  left: 1;
  background: #faebd7;
  z-index: 10;
  border-right: 1px solid #dcdcdc !important;
  box-shadow: 2px 0 0 #dcdcdc;
}
table {
  border-collapse: collapse !important;
}
.sticky-col {
  left: 0 !important;
}
th.sticky-col, td.sticky-col {
  border-right: 1px solid #dcdcdc !important;
  background: #faebd7;
}
th.sticky-col + th, td.sticky-col + td {
  border-left: 1px solid #dcdcdc !important;
  background: #fff;
}
table th:last-child,
table td:last-child {
  border-right: 1px solid #dcdcdc !important;
}
</style>

<!-- 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('managers') }}" 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('managers_add') }}'">新規</button>
            <button type="button" id="delete" class="btn btn-sm btn-default mr10">削除</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="form 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>
            @endif
        </div>

        <!-- ▼ 単一テーブル構成 -->
        <div class="col-lg-12 mb20">
            <div class="sample03-wrapper">
                <form action="{{ route('managers_delete') }}" method="POST" id="form_delete">
                    @csrf
                    <div style="overflow-x: auto;">
                        <table class="table table-bordered dataTable text-nowrap" style="min-width:3000px;">

                            <thead class="thead-light">
                                <tr>
                                    {{-- ★ チェック + 編集ボタン列 --}}
                                    <!-- ヘッダー -->
                                <th class="text-left sticky-col" style="width:120px;">
                                    <input type="checkbox" onclick="$('input[name*=\'pk\']').prop('checked', this.checked);">
                                </th>
                                <th class="sorting {{ ($sort=='manager_id') ? ($sort_type=='asc'?'sorting_asc':'sorting_desc') : '' }}" sort="manager_id" ><span>駐輪場管理者ID</span></th>
                                <th class="sorting {{ ($sort=='manager_name') ? ($sort_type=='asc'?'sorting_asc':'sorting_desc') : '' }}" sort="manager_name"><span>駐輪場管理者名</span></th>
                                <th><span>種別</span></th>
                                <th class="sorting {{ ($sort=='manager_parkid') ? ($sort_type=='asc'?'sorting_asc':'sorting_desc') : '' }}" sort="manager_parkid"><span>所属駐車場ID</span></th>
                                <th class="sorting {{ ($sort=='manager_device1') ? ($sort_type=='asc'?'sorting_asc':'sorting_desc') : '' }}" sort="manager_device1"><span>管理デバイス1</span></th>
                                <th><span>管理デバイス2</span></th>
                                <th><span>メールアドレス</span></th>
                                <th class="sorting {{ ($sort=='manager_tel') ? ($sort_type=='asc'?'sorting_asc':'sorting_desc') : '' }}" sort="manager_tel"><span>電話番号</span></th>
                                <th class="sorting {{ ($sort=='manager_alert1') ? ($sort_type=='asc'?'sorting_asc':'sorting_desc') : '' }}" sort="manager_alert1"><span>アラート1送信</span></th>
                                <th class="sorting {{ ($sort=='manager_alert2') ? ($sort_type=='asc'?'sorting_asc':'sorting_desc') : '' }}" sort="manager_alert2"><span>アラート2送信</span></th>
                                <th class="sorting {{ ($sort=='manager_quit_flag') ? ($sort_type=='asc'?'sorting_asc':'sorting_desc') : '' }}" sort="manager_quit_flag"><span>退職フラグ</span></th>
                                <th><span>退職日</span></th>
                            </tr>
                        </thead>
                        <tbody class="bg-white">
                            @foreach($list as $item)
                                <tr>
                                    {{-- チェック＋編集ボタン --}}
                                    <td class="align-middle sticky-col" style="background-color:#faebd7;">
                                        <div class="d-flex align-items-center">
                                            <input type="checkbox" class="m-0 checkbox" name="pk[]" value="{{ $item->manager_id }}">
                                            <a href="{{ route('managers_edit', ['id' => $item->manager_id]) }}" class="btn btn-sm btn-default ml10">編集</a>
                                        </div>
                                    </td>

                                    <td class="sm-item text-left align-middle">{{ $item->manager_id }}</td>
                                    <td class="sm-item text-left align-middle">{{ $item->manager_name }}</td>
                                    <td class="sm-item text-left align-middle">{{ $item->manager_type }}</td>
                                    <td class="sm-item text-left align-middle">{{ !empty($item->getPark()) ? $item->getPark()->park_name : '' }}</td>
                                    <td class="sm-item text-left align-middle">{{ !empty($item->getDevice1()) ? $item->getDevice1()->device_subject : '' }}</td>
                                    <td class="sm-item text-left align-middle">{{ !empty($item->getDevice2()) ? $item->getDevice2()->device_subject : '' }}</td>
                                    <td class="sm-item text-left align-middle">{{ $item->manager_mail }}</td>
                                    <td class="sm-item text-left align-middle">{{ $item->manager_tel }}</td>
                                    <td class="sm-item text-left align-middle">{{ $item->manager_alert1 }}</td>
                                    <td class="sm-item text-left align-middle">{{ $item->manager_alert2 }}</td>
                                    <td class="sm-item text-left align-middle">{{ $item->getManagerQuitFlagDisplay() }}</td>
                                    <td class="sm-item text-left align-middle">
                                        @if($item->manager_quitday)
                                            <span>
                                                {{ mb_substr($item->manager_quitday, 0, 10) }}
                                            </span>
                                        @endif
                                    </td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                </form>
            <!-- </div> -->
        </div>
        <!-- ▲ 単一テーブル構成ここまで -->
    </div>
</section>

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

<script>
    // 全選択
    document.getElementById('checkbox_all')?.addEventListener('change', function () {
        const checks = document.querySelectorAll('#form_delete .checkbox');
        checks.forEach(ch => ch.checked = this.checked);
    });
</script>
@endsection

