@extends('layouts.app')

@section('title', '利用者マスタ｜編集')

@section('content')
@php
    /** @var \Illuminate\Support\ViewErrorBag $errors */
    $isEdit = isset($user);
    $value = static function (string $key, $default = '') use ($isEdit, $user) {
        return old($key, $isEdit ? ($user->{$key} ?? $default) : $default);
    };
    $hasDeleteRoute = Route::has('users_delete');
    $operators = $operators ?? collect();
    $categoryOptions = $categoryOptions ?? [];
@endphp

<div class="content-header">
  <div class="container-fluid">
    <div class="row mb-2">
      <div class="col-lg-6">
        <h1 class="m-0 text-dark">{{ $isEdit ? '編集' : '新規' }}</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="{{ route('users') }}">利用者マスタ</a></li>
          <li class="breadcrumb-item active">{{ $isEdit ? '編集' : '新規' }}</li>
        </ol>
      </div>
    </div>
  </div>
</div>

<section class="content">
  <div class="container-fluid">

    {{-- 成功／エラー表示 --}}
    @if (session('status'))
      <div class="alert alert-success">{{ session('status') }}</div>
    @endif
    @if ($errors->any())
      <div class="alert alert-danger">
        <ul class="mb-0">
          @foreach ($errors->all() as $e)
            <li>{{ $e }}</li>
          @endforeach
        </ul>
      </div>
    @endif

    {{-- 共通フォーム部分 --}}
    @include('admin.users._form', [
        'isEdit' => $isEdit,
        'user' => $user ?? null,
        'value' => $value,
        'operators' => $operators,
        'categoryOptions' => $categoryOptions,
    ])

    {{-- ▼ 削除用フォーム（非表示） --}}
    <form id="form_delete" action="{{ route('users_delete') }}" method="post" class="d-none">
      @csrf
      <input type="hidden" name="user_seq" value="{{ $user->user_seq ?? '' }}">
    </form>

    @if ($isEdit && $hasDeleteRoute)
      <form method="post" action="{{ route('users_delete') }}" id="quitForm" class="d-none">
        @csrf
        <input type="hidden" name="user_seq" value="{{ $user->user_seq ?? '' }}">
      </form>
    @endif

  </div>
</section>

{{-- jQuery & jquery-confirm --}}
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-confirm@3.3.4/css/jquery-confirm.min.css">
<script src="https://cdn.jsdelivr.net/npm/jquery-confirm@3.3.4/js/jquery-confirm.min.js"></script>

@push('scripts')
{{-- ▼ 退会ボタン確認ダイアログ --}}
@if ($isEdit && $hasDeleteRoute)
<script>
$(function () {
    $('#quitForm').on('click', function (e) { 
        e.preventDefault();

        $.confirm({
            title: '退会確認',
            content: 'この利用者を退会処理します。<br>よろしいですか？',
            type: 'red', // 可选: 'blue', 'green', 'orange', 'red'
            buttons: {
                はい: {
                    text: 'はい',
                    btnClass: 'btn-danger',
                    keys: ['enter'],
                    action: function () {
                                      // ✅ 退会専用 hidden フィールド追加
                $('<input>').attr({
                    type: 'hidden',
                    name: 'quit_action',
                    value: '1'
                }).appendTo('#form_edit');
                        $('#form_edit').submit();
                    }
                },
                いいえ: {
                    text: 'いいえ',
                    btnClass: 'btn-default',
                    action: function () {}
                }
            }
        });
    });
});
</script>
@endif



{{-- ▼ 本人確認写真削除処理 --}}
<script>
(function () {
  const hookDelete = function(btnId, hiddenId, fileId, label) {
    const btn = document.getElementById(btnId);
    if (!btn) return;
    btn.addEventListener('click', function () {
      if (window.confirm(label + ' を削除します。よろしいですか？')) {
        const h = document.getElementById(hiddenId);
        if (h) h.value = '1';
        const f = document.getElementById(fileId);
        if (f) f.value = '';
        btn.classList.add('disabled');
        btn.textContent = '削除予定';
      }
    });
  };

  hookDelete('btn-delete-photo1', 'delete_photo1', 'id_photo1', '本人確認写真ファイル1');
  hookDelete('btn-delete-photo2', 'delete_photo2', 'id_photo2', '本人確認写真ファイル2');
})();

</script>
@endpush

<style>
  .bg-secondary-light { background: #f8fafc !important; }
  .form-check { display: inline-flex; align-items: center; }
  .form-check + .form-check { margin-left: 1rem; }
</style>


@endsection
