@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_confirm');
    $operators = $operators ?? collect();
    $categoryOptions = $categoryOptions ?? [];
@endphp

<div class="content-header">
    <div class="container-fluid">
        <div class="row mb-2 align-items-center">
            <div class="col-sm-6">
                <h1 class="m-0 text-dark">編集</h1>
            </div>
            <div class="col-sm-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

        <form method="post"
              id="user-edit-form"
              action="{{ $isEdit ? route('users_edit', ['seq' => $user->user_seq]) : route('users_add') }}"
              enctype="multipart/form-data">
            @csrf

            <div class="card shadow-sm">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h3 class="card-title mb-0 font-weight-bold">基本情報</h3>
                    <div class="btn-group">
                        <button type="submit" class="btn btn-success btn-sm px-4">登録</button>
                        @if ($isEdit && $hasDeleteRoute)
                            <button type="button" id="btn-quit-top" class="btn btn-outline-danger btn-sm px-4">退会</button>
                        @endif
                    </div>
                </div>

                <div class="card-body p-0">
                    <table class="table table-form mb-0">
                        <tbody>
                            <tr>
                                <th>利用者連番</th>
                                <td><input type="text" class="form-control form-control-sm bg-secondary-light" value="{{ $isEdit ? $user->user_seq : '新規' }}" readonly></td>
                            </tr>
                            <tr>
                                <th>利用者ID<span class="text-danger ml-1">*</span></th>
                                <td><input type="text" name="user_id" class="form-control form-control-sm" value="{{ $value('user_id') }}" required></td>
                            </tr>
                            <tr>
                                <th>会員ID</th>
                                <td><input type="text" name="member_id" class="form-control form-control-sm" value="{{ $value('member_id') }}"></td>
                            </tr>
                            <tr>
                                <th>氏名<span class="text-danger ml-1">*</span></th>
                                <td><input type="text" name="user_name" class="form-control form-control-sm" value="{{ $value('user_name') }}" required></td>
                            </tr>
                            <tr>
                                <th>フリガナ</th>
                                <td><input type="text" name="user_phonetic" class="form-control form-control-sm" value="{{ $value('user_phonetic') }}"></td>
                            </tr>
                            <tr>
                                <th>パスワード</th>
                                <td>
                                    <input type="password" name="user_pass" class="form-control form-control-sm" autocomplete="new-password">
                                    <small class="form-text text-muted">未入力の場合は変更されません。</small>
                                </td>
                            </tr>
                            <tr>
                                <th>メインメールアドレス</th>
                                <td><input type="email" name="user_primemail" class="form-control form-control-sm" value="{{ $value('user_primemail') }}" placeholder="name@example.com"></td>
                            </tr>
                            <tr>
                                <th>予備メールアドレス</th>
                                <td><input type="email" name="user_submail" class="form-control form-control-sm" value="{{ $value('user_submail') }}" placeholder="name@example.com"></td>
                            </tr>
                            <tr>
                                <th>携帯電話番号</th>
                                <td><input type="text" name="user_mobile" class="form-control form-control-sm" value="{{ $value('user_mobile') }}" placeholder="080-0000-0000"></td>
                            </tr>
                            <tr>
                                <th>固定電話番号</th>
                                <td><input type="text" name="user_homephone" class="form-control form-control-sm" value="{{ $value('user_homephone') }}" placeholder="03-0000-0000"></td>
                            </tr>
                            <tr>
                                <th>性別</th>
                                <td>
                                    @foreach (['男性','女性','未入力'] as $gender)
                                        @php $id = 'user_gender_'.$gender; @endphp
                                        <div class="custom-control custom-radio custom-control-inline">
                                            <input type="radio" id="{{ $id }}" name="user_gender" value="{{ $gender }}" class="custom-control-input" {{ $value('user_gender') === $gender ? 'checked' : '' }}>
                                            <label class="custom-control-label" for="{{ $id }}">{{ $gender }}</label>
                                        </div>
                                    @endforeach
                                </td>
                            </tr>
                            <tr>
                                <th>生年月日</th>
                                <td><input type="date" name="user_birthdate" class="form-control form-control-sm" value="{{ $value('user_birthdate') }}"></td>
                            </tr>
                            <tr>
                                <th>年齢</th>
                                <td><input type="number" name="user_age" class="form-control form-control-sm" value="{{ $value('user_age') }}"></td>
                            </tr>
                            <tr>
                                <th>学校名</th>
                                <td><input type="text" name="user_school" class="form-control form-control-sm" value="{{ $value('user_school') }}"></td>
                            </tr>
                            <tr>
                                <th>卒業予定</th>
                                <td><input type="date" name="user_graduate" class="form-control form-control-sm" value="{{ $value('user_graduate') }}"></td>
                            </tr>
                            <tr>
                                <th>勤務先名</th>
                                <td><input type="text" name="user_workplace" class="form-control form-control-sm" value="{{ $value('user_workplace') }}"></td>
                            </tr>
                            <tr>
                                <th>区民区分</th>
                                <td><input type="text" name="ward_residents" class="form-control form-control-sm" value="{{ $value('ward_residents') }}"></td>
                            </tr>
                            <tr>
                                <th>本人確認書類</th>
                                <td><input type="text" name="user_idcard" class="form-control form-control-sm" value="{{ $value('user_idcard') }}"></td>
                            </tr>
                            <tr>
                                <th>本人確認チェック</th>
                                <td>
                                    @foreach ([0 => '未チェック', 1 => '手動チェックOK'] as $flag => $label)
                                        @php $id = 'user_idcard_chk_flag_'.$flag; @endphp
                                        <div class="custom-control custom-radio custom-control-inline">
                                            <input type="radio" id="{{ $id }}" name="user_idcard_chk_flag" value="{{ $flag }}" class="custom-control-input" {{ (string)$value('user_idcard_chk_flag','0') === (string)$flag ? 'checked' : '' }}>
                                            <label class="custom-control-label" for="{{ $id }}">{{ $label }}</label>
                                        </div>
                                    @endforeach
                                </td>
                            </tr>
                            <tr>
                                <th>本人確認日時</th>
                                <td><input type="date" name="user_chk_day" class="form-control form-control-sm" value="{{ $value('user_chk_day') }}"></td>
                            </tr>
                            <tr>
                                <th>本人確認オペレータ</th>
                                <td>
                                    <select name="ope_id" class="form-control form-control-sm">
                                        <option value="">本人確認オペレータID</option>
                                        @foreach ($operators as $operator)
                                            <option value="{{ $operator->ope_id }}" {{ (string) old('ope_id', $isEdit ? (string) ($user->ope_id ?? '') : '') === (string) $operator->ope_id ? 'selected' : '' }}>
                                                {{ $operator->ope_name }}
                                            </option>
                                        @endforeach
                                    </select>
                                </td>
                            </tr>
                            <tr>
                                <th>タグ／QRフラグ</th>
                                <td>
                                    <div class="custom-control custom-radio custom-control-inline">
                                        <input type="radio" id="tag_flag_qr" name="tag_qr_flag" value="1" class="custom-control-input" {{ $value('tag_qr_flag','0') === '1' ? 'checked' : '' }}>
                                        <label class="custom-control-label" for="tag_flag_qr">QR</label>
                                    </div>
                                    <div class="custom-control custom-radio custom-control-inline">
                                        <input type="radio" id="tag_flag_tag" name="tag_qr_flag" value="0" class="custom-control-input" {{ $value('tag_qr_flag','0') === '0' ? 'checked' : '' }}>
                                        <label class="custom-control-label" for="tag_flag_tag">タグ</label>
                                    </div>
                                </td>
                            </tr>
                            <tr>
                                <th>タグシリアル</th>
                                <td><input type="text" name="user_tag_serial" class="form-control form-control-sm" value="{{ $value('user_tag_serial') }}"></td>
                            </tr>
                            <tr>
                                <th>タグシリアル（64進）</th>
                                <td><input type="text" name="user_tag_serial_64" class="form-control form-control-sm" value="{{ $value('user_tag_serial_64') }}"></td>
                            </tr>
                            <tr>
                                <th>QRコード</th>
                                <td><input type="text" name="qr_code" class="form-control form-control-sm" value="{{ $value('qr_code') }}"></td>
                            </tr>
                            <tr>
                                <th>AID</th>
                                <td><input type="text" name="user_aid" class="form-control form-control-sm" value="{{ $value('user_aid') }}"></td>
                            </tr>
                            <tr>
                                <th>設置場所QRID</th>
                                <td><input type="text" name="user_place_qrid" class="form-control form-control-sm" value="{{ $value('user_place_qrid') }}"></td>
                            </tr>
                            <tr>
                                <th>利用者分類</th>
                                <td>
                                    <select name="user_categoryid" class="form-control form-control-sm">
                                        <option value="">選択してください</option>
                                        @foreach ($categoryOptions as $id => $label)
                                            <option value="{{ $id }}"
                                                {{ (string) old('user_categoryid', $value('user_categoryid')) === (string) $id ? 'selected' : '' }}>
                                                {{ $label }}
                                            </option>
                                        @endforeach
                                    </select>
                                </td>
                            </tr>
                            <tr>
                                <th>タグ発行数</th>
                                <td><input type="number" name="user_tag_issue" class="form-control form-control-sm" value="{{ $value('user_tag_issue') }}"></td>
                            </tr>
                            <tr>
                                <th>タグ発行許可</th>
                                <td>
                                    <div class="custom-control custom-radio custom-control-inline">
                                        <input type="radio" id="issue_permission_ok" name="issue_permission" value="1" class="custom-control-input" {{ $value('issue_permission','1') === '1' ? 'checked' : '' }}>
                                        <label class="custom-control-label" for="issue_permission_ok">許可</label>
                                    </div>
                                    <div class="custom-control custom-radio custom-control-inline">
                                        <input type="radio" id="issue_permission_ng" name="issue_permission" value="0" class="custom-control-input" {{ $value('issue_permission','1') === '0' ? 'checked' : '' }}>
                                        <label class="custom-control-label" for="issue_permission_ng">発行不可</label>
                                    </div>
                                </td>
                            </tr>
                            <tr>
                                <th>退会フラグ</th>
                                <td>
                                    <div class="custom-control custom-radio custom-control-inline">
                                        <input type="radio" id="quit_flag_yes" name="user_quit_flag" value="1" class="custom-control-input" {{ $value('user_quit_flag','0') === '1' ? 'checked' : '' }}>
                                        <label class="custom-control-label" for="quit_flag_yes">退会済</label>
                                    </div>
                                    <div class="custom-control custom-radio custom-control-inline">
                                        <input type="radio" id="quit_flag_no" name="user_quit_flag" value="0" class="custom-control-input" {{ $value('user_quit_flag','0') === '0' ? 'checked' : '' }}>
                                        <label class="custom-control-label" for="quit_flag_no">在籍</label>
                                    </div>
                                </td>
                            </tr>
                            <tr>
                                <th>退会日</th>
                                <td><input type="date" name="user_quitday" class="form-control form-control-sm" value="{{ $value('user_quitday') }}"></td>
                            </tr>
                            <tr>
                                <th>現住所 郵便番号</th>
                                <td><input type="text" name="user_regident_zip" class="form-control form-control-sm" value="{{ $value('user_regident_zip') }}"></td>
                            </tr>
                            <tr>
                                <th>現住所 都道府県</th>
                                <td><input type="text" name="user_regident_pre" class="form-control form-control-sm" value="{{ $value('user_regident_pre') }}"></td>
                            </tr>
                            <tr>
                                <th>現住所 市区町村</th>
                                <td><input type="text" name="user_regident_city" class="form-control form-control-sm" value="{{ $value('user_regident_city') }}"></td>
                            </tr>
                            <tr>
                                <th>現住所 番地・建物名</th>
                                <td><input type="text" name="user_regident_add" class="form-control form-control-sm" value="{{ $value('user_regident_add') }}"></td>
                            </tr>
                            <tr>
                                <th>連絡先 郵便番号</th>
                                <td><input type="text" name="user_relate_zip" class="form-control form-control-sm" value="{{ $value('user_relate_zip') }}"></td>
                            </tr>
                            <tr>
                                <th>連絡先 都道府県</th>
                                <td><input type="text" name="user_relate_pre" class="form-control form-control-sm" value="{{ $value('user_relate_pre') }}"></td>
                            </tr>
                            <tr>
                                <th>連絡先 市区町村</th>
                                <td><input type="text" name="user_relate_city" class="form-control form-control-sm" value="{{ $value('user_relate_city') }}"></td>
                            </tr>
                            <tr>
                                <th>連絡先 番地・建物名</th>
                                <td><input type="text" name="user_relate_add" class="form-control form-control-sm" value="{{ $value('user_relate_add') }}"></td>
                            </tr>
                            <tr>
                                <th>本人確認書類ファイル1</th>
                                <td><input type="file" name="id_photo1" class="form-control-file"></td>
                            </tr>
                            <tr>
                                <th>本人確認書類ファイル2</th>
                                <td><input type="file" name="id_photo2" class="form-control-file"></td>
                            </tr>
                            <tr>
                                <th>備考</th>
                                <td><textarea name="user_remarks" rows="4" class="form-control form-control-sm">{{ $value('user_remarks') }}</textarea></td>
                            </tr>
                        </tbody>
                    </table>
                </div>

                <div class="card-footer d-flex justify-content-between align-items-center flex-wrap">
                    <a href="{{ route('users') }}" class="btn btn-outline-secondary">戻る</a>
                    <div class="btn-group">
                        @if ($isEdit && $hasDeleteRoute)
                            <button type="button" class="btn btn-outline-danger" id="btn-quit-bottom">退会</button>
                        @endif
                        <button type="submit" class="btn btn-success">登録</button>
                    </div>
                </div>
            </div>
        </form>

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

@push('scripts')
@if ($isEdit && $hasDeleteRoute)
<script>
    (function () {
        const confirmQuit = function () {
            if (window.confirm('この利用者を退会処理します。よろしいですか？')) {
                document.getElementById('quitForm').submit();
            }
        };
        const btnTop = document.getElementById('btn-quit-top');
        const btnBottom = document.getElementById('btn-quit-bottom');
        if (btnTop) { btnTop.addEventListener('click', confirmQuit); }
        if (btnBottom) { btnBottom.addEventListener('click', confirmQuit); }
    })();
</script>
@endif
@endpush

<style>
    .table-form {
        width: 100%;
        border-collapse: separate;
        border-spacing: 0;
    }
    .table-form th,
    .table-form td {
        border-bottom: 1px solid #e2e8f0;
        padding: 14px 18px;
        vertical-align: middle;
    }
    .table-form th {
        width: 22%;
        background: #f1f5f9;
        font-weight: 600;
        color: #334155;
    }
    .table-form td {
        background: #ffffff;
    }
    .table-form .form-control-sm,
    .table-form textarea.form-control-sm {
        background: #ffffff;
    }
    .bg-secondary-light {
        background: #f8fafc !important;
    }
    .card-footer .btn-group .btn + .btn {
        margin-left: .5rem;
    }
</style>
@endsection
