@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('payments') }}" 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('payments_add') }}'">新規</button>
      <button type="button" class="btn btn-sm btn-default mr10" id="delete">削除</button>
      
      <div class="d-flex justify-content-end">
        {{ $payments->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="table-responsive">
        <form action="{{ route('payments_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=='payment_id') ? ($sort_type=='asc'?'sorting_asc':'sorting_desc') : '' }}" sort="payment_id">
                  <span>決済情報ID</span>
                </th>
                <th><span>登録日時</span></th>
                <th class="sorting {{ ($sort=='updated_at') ? ($sort_type=='asc'?'sorting_asc':'sorting_desc') : '' }}" sort="updated_at">
                  <span>更新日時</span>
                </th>
                <th class="sorting {{ ($sort=='payment_companyname') ? ($sort_type=='asc'?'sorting_asc':'sorting_desc') : '' }}" sort="payment_companyname">
                  <span>事業者名</span>
                </th>
                <th><span>お問合せ先住所</span></th>
                <th><span>払い込み内容</span></th>
                <th><span>フリースペース記載名1</span></th>
                <th><span>フリースペース記載名2</span></th>
                <th><span>ご案内タイトル</span></th>
                <th><span>お客様ご案内</span></th>
                <th class="sorting {{ ($sort=='payment_inquiryname') ? ($sort_type=='asc'?'sorting_asc':'sorting_desc') : '' }}" sort="payment_inquiryname">
                  <span>問い合わせ担当</span>
                </th>                                
                <th class="sorting {{ ($sort=='payment_inquirytel') ? ($sort_type=='asc'?'sorting_asc':'sorting_desc') : '' }}" sort="payment_inquirytel">
                  <span>問合せ先ＴＥＬ</span>
                </th>

                <th class="sorting {{ ($sort=='payment_time') ? ($sort_type=='asc'?'sorting_asc':'sorting_desc') : '' }}" sort="payment_time">
                  <span>電話受付時間</span>
                </th>
                <!-- <th><span>更新オペレータID</span></th> -->

              </tr>
            </thead>
            <tbody class="bg-white">
              @foreach($payments as $payment)
                <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="{{ $payment->payment_id }}">
                        <a href="{{ route('payments_edit', ['id' => $payment->payment_id]) }}" class="btn btn-sm btn-default ml10">編集</a>
                    </div>
                  </td>
                  <td class="sm-item text-right align-middle">{{ $payment->payment_id }}</td>
                  <td class="sm-item text-left align-middle">{{ optional($payment->created_at)->format('Y-m-d H:i') }}</td>
                  <td class="sm-item text-left align-middle">{{ optional($payment->updated_at)->format('Y-m-d H:i') }}</td>

                  <td class="sm-item text-left align-middle">{{ $payment->payment_companyname }}</td>
                  <td class="sm-item text-left align-middle">{{ $payment->payment_add }}</td>
                  <td class="sm-item text-left align-middle">{{ $payment->payment_detail }}</td>
                  <td class="sm-item text-left align-middle">{{ $payment->payment_space1 }}</td>
                  <td class="sm-item text-left align-middle">{{ $payment->payment_space2 }}</td>
                  <td class="sm-item text-left align-middle">{{ $payment->payment_title }}</td>
                  <td class="sm-item text-left align-middle">{{ $payment->payment_guide }}</td>

                  <td class="sm-item text-left align-middle">{{ $payment->payment_inquiryname }}</td>
                  <td class="sm-item text-left align-middle">{{ $payment->payment_inquirytel }}</td>
                  <td class="sm-item text-left align-middle">{{ $payment->payment_time }}</td>

                  <!-- <td class="sm-item text-right align-middle">{{ $payment->operator_id }}</td> -->

                </tr>
              @endforeach
            </tbody>
          </table>
        </form>
      </div>
    </div>
    <!-- ▲ 単一テーブル構成ここまで ----------------------------------------- -->

  </div>
</section>

{{-- ソートのJS --}}
@push('scripts')
<script>
  
  // ヘッダクリックでソート
  document.querySelectorAll('th.sorting').forEach(th => {
    th.addEventListener('click', function(){
      const form = document.getElementById('list-form');
      const current = "{{ $sort ?? '' }}";
      const currentType = "{{ $sort_type ?? '' }}";
      const nextCol = this.getAttribute('sort');
      let nextType = 'asc';
      if (current === nextCol) {
        nextType = (currentType === 'asc') ? 'desc' : 'asc';
      }
      form.querySelector('[name=sort]').value = nextCol;
      form.querySelector('[name=sort_type]').value = nextType;
      form.submit();
    });
  });
</script>
@endpush
@endsection
