{{-- 利用者マスタ 共通フォーム --}}
<form method="post"
      id="form_edit"
      action="{{ $isEdit ? route('users_edit', ['seq' => $user->user_seq]) : route('users_add') }}"
      enctype="multipart/form-data">
  @csrf

  <div class="card">
    <div class="card-header">
      <h3 class="card-title">基本情報</h3>
    </div>

    <div class="card-body">

      {{-- 利用者連番（表示のみ） --}}
      <div class="form-group row">
        <label class="col-md-2 col-form-label">利用者連番</label>
        <div class="col-md-10">
          <input type="text" class="form-control form-control-sm"
                 value="{{ $isEdit ? $user->user_seq : '新規' }}" readonly>
        </div>
      </div>

      {{-- 利用者ID --}}
      <div class="form-group row">
        <label class="col-md-2 col-form-label">利用者ID</label>
        <div class="col-md-10">
          <input type="text" name="user_id" class="form-control form-control-sm"
                 value="{{ $value('user_id') }}" readonly>
        </div>
      </div>

      {{-- 会員ID
      <div class="form-group row">
        <label class="col-md-2 col-form-label">会員ID <span class="text-danger">*</span></label>
        <div class="col-md-10">
          <input type="text" name="member_id" class="form-control form-control-sm"
                 value="{{ $value('member_id') }}" placeholder="会員ID">
        </div>
      </div>--}}

      {{-- パスワード --}}
      <div class="form-group row">
        <label class="col-md-2 col-form-label">パスワード</label>
        <div class="col-md-10">
          <input type="text" name="user_pass" class="form-control form-control-sm"
                 value="{{ $value('user_pass') }}" readonly>
        </div>
      </div>

      {{-- 手動登録フラグ --}}
      <div class="form-group row">
        <label class="col-md-2 col-form-label">
          手動登録フラグ <span class="text-danger">*</span>
        </label>
        <div class="col-md-10 d-flex align-items-center">
          <div class="form-check mr-3">
            <input class="form-check-input" type="radio" name="user_manual_regist_flag"
                   id="user_manual_regist_flag_1" value="1"
                   {{ old('user_manual_regist_flag', '0') == '1' ? 'checked' : '' }}>
            <label for="user_manual_regist_flag_1" class="form-check-label">はい</label>
          </div>
          <div class="form-check">
            <input class="form-check-input" type="radio" name="user_manual_regist_flag"
                   id="user_manual_regist_flag_0" value="0"
                   {{ old('user_manual_regist_flag', '0') == '0' ? 'checked' : '' }}>
            <label for="user_manual_regist_flag_0" class="form-check-label">いいえ</label>
          </div>
        </div>

        @error('user_manual_regist_flag')
        <div class="col-md-10 offset-md-2">
          <small class="text-danger">{{ $message }}</small>
        </div>
        @enderror
      </div>

      {{-- 郵送必要フラグ --}}
      <div class="form-group row">
        <label class="col-md-2 col-form-label">
          郵送必要フラグ <span class="text-danger">*</span>
        </label>
        <div class="col-md-10 d-flex align-items-center">
          <div class="form-check mr-3">
            <input class="form-check-input" type="radio" name="user_mailing_flag"
                   id="user_mailing_flag_1" value="1"
                   {{ old('user_mailing_flag', '0') == '1' ? 'checked' : '' }}>
            <label for="user_mailing_flag_1" class="form-check-label">はい</label>
          </div>
          <div class="form-check">
            <input class="form-check-input" type="radio" name="user_mailing_flag"
                   id="user_mailing_flag_0" value="0"
                   {{ old('user_mailing_flag', '0') == '0' ? 'checked' : '' }}>
            <label for="user_mailing_flag_0" class="form-check-label">いいえ</label>
          </div>
        </div>

        @error('user_mailing_flag')
        <div class="col-md-10 offset-md-2">
          <small class="text-danger">{{ $message }}</small>
        </div>
        @enderror
      </div>

      {{-- 旧定期契約番号 --}}
      <div class="form-group row">
        <label class="col-md-2 col-form-label">旧定期契約番号</label>
        <div class="col-md-10">
          <input type="text" name="user_tag_serial" value="{{ old('user_tag_serial') }}"
                 class="form-control" placeholder="旧定期契約番号">
        </div>
      </div>

      {{-- タグシリアル --}}
      <div class="form-group row">
        <label class="col-md-2 col-form-label">タグシリアル</label>
        <div class="col-md-10">
          <input type="text" name="user_tag_serial" value="{{ old('user_tag_serial') }}"
                 class="form-control" placeholder="タグシリアル">
        </div>
      </div>

      {{-- タグシリアル(64) --}}
      <div class="form-group row">
        <label class="col-md-2 col-form-label">タグシリアル(64)</label>
        <div class="col-md-10">
          <input type="text" name="user_tag_serial_64" value="{{ old('user_tag_serial_64') }}"
                 class="form-control" placeholder="タグシリアル(64)">
        </div>
      </div>

      {{-- QRコード --}}
      <div class="form-group row">
        <label class="col-md-2 col-form-label">QRコード</label>
        <div class="col-md-10">
          <input type="text" name="qr_code" value="{{ old('qr_code') }}" class="form-control">
        </div>
      </div>

      {{-- タグ／ＱＲフラグ --}}
      <div class="form-group row">
        <label class="col-md-2 col-form-label">タグ／ＱＲフラグ<span class="text-danger">*</span></label>
        <div class="col-md-10 d-flex align-items-center">
          <div class="form-check mr-3">
            <input class="form-check-input" type="radio" name="tag_qr_flag"
                   id="tag_qr_tag" value="0"
                   {{ old('tag_qr_flag', '0') == '0' ? 'checked' : '' }}>
            <label for="tag_qr_tag" class="form-check-label">QR</label>
          </div>
          <div class="form-check mr-3">
            <input class="form-check-input" type="radio" name="tag_qr_flag"
                   id="tag_qr_qr" value="1"
                   {{ old('tag_qr_flag', '0') == '1' ? 'checked' : '' }}>
            <label for="tag_qr_qr" class="form-check-label">タグ</label>
          </div>
        </div>
      </div>

      {{-- AID / 設置場所QR --}}
      <div class="form-group row">
        <label class="col-md-2 col-form-label">AID</label>
        <div class="col-md-10">
          <input type="text" name="user_aid" value="{{ old('user_aid') }}" class="form-control">
        </div>
      </div>

      {{--<div class="form-group row">
        <label class="col-md-2 col-form-label">居場所通知用QRID</label>
        <div class="col-md-10">
          <input type="text" name="user_place_qrid" value="{{ old('user_place_qrid') }}" class="form-control">
        </div>
      </div>--}}

      {{-- 利用者分類 --}}
      <div class="form-group row">
        <label class="col-md-2 col-form-label">分類名１(一般、学生)</label>
        <div class="col-md-10">
          <input type="text" name="user_categoryid" value="{{ old('user_categoryid') }}" class="form-control">
        </div>
      </div>

      <div class="form-group row">
        <label class="col-md-2 col-form-label">分類名２(区民、非区民)</label>
        <div class="col-md-10">
          <input type="text" name="user_categoryid" value="{{ old('user_categoryid') }}" class="form-control">
        </div>
      </div>

      <div class="form-group row">
        <label class="col-md-2 col-form-label">分類名３(減免種別)</label>
        <div class="col-md-10">
          <input type="text" name="user_categoryid" value="{{ old('user_categoryid') }}" class="form-control">
        </div>
      </div>

      {{-- 利用者名 --}}
      <div class="form-group row">
        <label class="col-md-2 col-form-label">利用者名 <span class="text-danger">*</span></label>
        <div class="col-md-10">
          <input type="text" name="user_name" class="form-control form-control-sm"
                 value="{{ $value('user_name') }}" required>
        </div>
      </div>

      {{-- フリガナ --}}
      <div class="form-group row">
        <label class="col-md-2 col-form-label">フリガナ</label>
        <div class="col-md-10">
          <input type="text" name="user_phonetic" class="form-control form-control-sm"
                 value="{{ $value('user_phonetic') }}">
        </div>
      </div>

      {{-- 性別 --}}
      <div class="form-group row">
        <label class="col-md-2 col-form-label">性別</label>
        <div class="col-md-10">
      
          <div class="form-check form-check-inline">
            <input class="form-check-input"
                   type="radio"
                   name="user_gender"
                   id="gender_male"
                   value="1"
                   {{ $value('user_gender') === '1' ? 'checked' : '' }}>
            <label class="form-check-label" for="gender_male">男</label>
          </div>
      
          <div class="form-check form-check-inline">
            <input class="form-check-input"
                   type="radio"
                   name="user_gender"
                   id="gender_female"
                   value="2"
                   {{ $value('user_gender') === '2' ? 'checked' : '' }}>
            <label class="form-check-label" for="gender_female">女</label>
          </div>
      
          <div class="form-check form-check-inline">
            <input class="form-check-input"
                   type="radio"
                   name="user_gender"
                   id="gender_unknown"
                   value="0"
                   {{ $value('user_gender') === '0' || $value('user_gender') === null ? 'checked' : '' }}>
            <label class="form-check-label" for="gender_unknown">未入力</label>
          </div>
      
        </div>
      </div>

      {{-- 生年月日 --}}
      <div class="form-group row">
        <label class="col-md-2 col-form-label">生年月日</label>
        <div class="col-md-10">
          <input type="date" name="user_birthdate" class="form-control form-control-sm"
                 value="{{ $value('user_birthdate') }}">
        </div>
      </div>

      {{-- 年齢 --}}
      <div class="form-group row">
        <label class="col-md-2 col-form-label">年齢</label>
        <div class="col-md-10">
      
          @php
            $age = null;
            if ($value('user_birthdate')) {
                $age = \Carbon\Carbon::parse($value('user_birthdate'))
                          ->age;
            }
          @endphp
      
          <input type="text"
                 class="form-control form-control-sm"
                 value="{{ $age !== null ? $age : '' }}"
                 readonly>
      
        </div>
      </div>

      {{-- 自宅電話番号 --}}
      <div class="form-group row">
        <label class="col-md-2 col-form-label">自宅電話番号</label>
        <div class="col-md-10">
          <input type="text" name="user_homephone" class="form-control form-control-sm"
                 value="{{ $value('user_homephone') }}" placeholder="000-0000-0000">
        </div>
      </div>

      {{-- 携帯電話番号 --}}
      <div class="form-group row">
        <label class="col-md-2 col-form-label">携帯電話番号</label>
        <div class="col-md-10">
          <input type="text" name="user_mobile" class="form-control form-control-sm"
                 value="{{ $value('user_mobile') }}" placeholder="080-0000-0000">
        </div>
      </div>

      {{-- メールアドレス --}}
      <div class="form-group row">
        <label class="col-md-2 col-form-label">メールアドレス</label>
        <div class="col-md-10">
          <input type="email" name="user_primemail" class="form-control form-control-sm"
                 value="{{ $value('user_primemail') }}" placeholder="name@example.com">
        </div>
      </div>

      {{-- 予備メールアドレス --}}
      <div class="form-group row">
        <label class="col-md-2 col-form-label">予備メールアドレス</label>
        <div class="col-md-10">
          <input type="email" name="user_submail" class="form-control form-control-sm"
                 value="{{ $value('user_submail') }}" placeholder="name@example.com">
        </div>
      </div>

      {{-- 居住所 --}}
      <div class="form-group row">
        <label class="col-md-2 col-form-label">居住所:郵便番号</label>
        <div class="col-md-10">
          <input type="text" name="user_regident_zip" class="form-control form-control-sm"
                 value="{{ $value('user_regident_zip') }}">
        </div>
      </div>
      <div class="form-group row">
        <label class="col-md-2 col-form-label">居住所:都道府県</label>
        <div class="col-md-10">
          <input type="text" name="user_regident_pre" class="form-control form-control-sm"
                 value="{{ $value('user_regident_pre') }}">
        </div>
      </div>
      <div class="form-group row">
        <label class="col-md-2 col-form-label">居住所:市区町村</label>
        <div class="col-md-10">
          <input type="text" name="user_regident_city" class="form-control form-control-sm"
                 value="{{ $value('user_regident_city') }}">
        </div>
      </div>
      <div class="form-group row">
        <label class="col-md-2 col-form-label">居住所:住所</label>
        <div class="col-md-10">
          <input type="text" name="user_regident_add" class="form-control form-control-sm"
                 value="{{ $value('user_regident_add') }}">
        </div>
      </div>

      {{-- 関連住所 --}}
      <div class="form-group row">
        <label class="col-md-2 col-form-label">関連住所:郵便番号</label>
        <div class="col-md-10">
          <input type="text" name="user_relate_zip" class="form-control form-control-sm"
                 value="{{ $value('user_relate_zip') }}">
        </div>
      </div>
      <div class="form-group row">
        <label class="col-md-2 col-form-label">関連住所:都道府県</label>
        <div class="col-md-10">
          <input type="text" name="user_relate_pre" class="form-control form-control-sm"
                 value="{{ $value('user_relate_pre') }}">
        </div>
      </div>
      <div class="form-group row">
        <label class="col-md-2 col-form-label">関連住所:市区町村</label>
        <div class="col-md-10">
          <input type="text" name="user_relate_city" class="form-control form-control-sm"
                 value="{{ $value('user_relate_city') }}">
        </div>
      </div>
      <div class="form-group row">
        <label class="col-md-2 col-form-label">関連住所:住所</label>
        <div class="col-md-10">
          <input type="text" name="user_relate_add" class="form-control form-control-sm"
                 value="{{ $value('user_relate_add') }}">
        </div>
      </div>

      {{-- 区民 --}}
      <div class="form-group row">
          <label class="col-md-2 col-form-label">区民</label>
          <div class="col-md-10">
              <select name="ward_residents" class="form-control form-control-sm">
                  <option value="1"
                      {{ old('ward_residents', $value('ward_residents')) == 1 ? 'selected' : '' }}>
                      居民
                  </option>
      
                  <option value="0"
                      {{ old('ward_residents', $value('ward_residents')) == 0 ? 'selected' : '' }}>
                      非居民
                  </option>
              </select>
          </div>
      </div>


      {{-- 勤務先 --}}
      <div class="form-group row">
        <label class="col-md-2 col-form-label">勤務先</label>
        <div class="col-md-10">
          <input type="text" name="user_workplace" class="form-control form-control-sm"
                 value="{{ $value('user_workplace') }}">
        </div>
      </div>

      {{-- 学校 --}}
      <div class="form-group row">
        <label class="col-md-2 col-form-label">学校</label>
        <div class="col-md-10">
          <input type="text" name="user_school" class="form-control form-control-sm"
                 value="{{ $value('user_school') }}">
        </div>
      </div>

      {{-- 卒業予定 --}}
      <div class="form-group row">
        <label class="col-md-2 col-form-label">卒業予定</label>
        <div class="col-md-10">
          <input type="date" name="user_graduate" class="form-control form-control-sm"
                 value="{{ $value('user_graduate') }}">
        </div>
      </div>

      {{-- 本人確認書類 --}}
      <div class="form-group row">
        <label class="col-md-2 col-form-label">本人確認書類</label>
        <div class="col-md-10">
          <input type="text" name="user_idcard" class="form-control form-control-sm"
                 value="{{ $value('user_idcard') }}">
        </div>
      </div>

      {{-- 本人確認チェック済（5状態） --}}
      @php
        $options = [
          0 => '未チェック',
          1 => '自動チェックOK',
          2 => '手動チェックOK',
          3 => '手動チェックNG',
          4 => '自動チェックNG',
        ];
        $current = (string) old('user_idcard_chk_flag', $isEdit ? (string)($user->user_idcard_chk_flag ?? '0') : '0');
      @endphp
      <div class="form-group row">
        <label class="col-md-2 col-form-label">本人確認チェック済 <span class="text-danger">*</span></label>
        <div class="col-md-10 d-flex flex-wrap">
          @foreach ($options as $flag => $label)
            @php $id = "user_idcard_chk_flag_{$flag}"; @endphp
            <div class="form-check mr-3">
              <input class="form-check-input" type="radio" name="user_idcard_chk_flag" id="{{ $id }}" value="{{ $flag }}"
                     {{ (string)$flag === $current ? 'checked' : '' }}>
              <label class="form-check-label" for="{{ $id }}">{{ $label }}</label>
            </div>
          @endforeach
        </div>
      </div>

      {{-- 本人確認日時 --}}
      <div class="form-group row">
        <label class="col-md-2 col-form-label">本人確認日時</label>
        <div class="col-md-10">
          <input type="date" name="user_chk_day" class="form-control form-control-sm"
                 value="{{ $value('user_chk_day') }}">
        </div>
      </div>

      {{-- 本人確認オペレータ --}}
      <div class="form-group row">
        <label class="col-md-2 col-form-label">本人確認オペレータ</label>
        <div class="col-md-10">
          <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>
        </div>
      </div>

      {{-- タグ発行関連 --}}
      <div class="form-group row">
        <label class="col-md-2 col-form-label">タグ発行数</label>
        <div class="col-md-10">
          <input type="number" name="user_tag_issue" class="form-control form-control-sm"
                 value="{{ $value('user_tag_issue') }}">
        </div>
      </div>

      <div class="form-group row">
        <label class="col-md-2 col-form-label">タグ発行許可<span class="text-danger">*</span></label>
        <div class="col-md-10 d-flex flex-wrap">
          <div class="form-check mr-3">
            <input class="form-check-input" type="radio" name="issue_permission"
                   id="issue_permission_ok" value="1"
                   {{ $value('issue_permission','1') === '1' ? 'checked' : '' }}>
            <label class="form-check-label" for="issue_permission_ok">発行許可</label>
          </div>
          <div class="form-check mr-3">
            <input class="form-check-input" type="radio" name="issue_permission"
                   id="issue_permission_ng" value="0"
                   {{ $value('issue_permission','1') === '0' ? 'checked' : '' }}>
            <label class="form-check-label" for="issue_permission_ng">発行済</label>
          </div>
        </div>
      </div>

      {{-- 退会 --}}
      <div class="form-group row">
        <label class="col-md-2 col-form-label">退会フラグ<span class="text-danger">*</span></label>
        <div class="col-md-10 d-flex flex-wrap">
          <div class="form-check mr-3">
            <input class="form-check-input" type="radio" name="user_quit_flag"
                   id="quit_flag_yes" value="1"
                   {{ $value('user_quit_flag','0') === '1' ? 'checked' : '' }}>
            <label class="form-check-label" for="quit_flag_yes">はい</label>
          </div>
          <div class="form-check mr-3">
            <input class="form-check-input" type="radio" name="user_quit_flag"
                   id="quit_flag_no" value="0"
                   {{ $value('user_quit_flag','0') === '0' ? 'checked' : '' }}>
            <label class="form-check-label" for="quit_flag_no">いいえ</label>
          </div>
        </div>
      </div>

      <div class="form-group row">
        <label class="col-md-2 col-form-label">退会日</label>
        <div class="col-md-10">
          <input type="date" name="user_quitday" class="form-control form-control-sm"
                 value="{{ $value('user_quitday') }}">
        </div>
      </div>

      @php
        use Illuminate\Support\Facades\Storage;
        $currentPhoto1 = $isEdit ? ($user->photo_filename1 ?? '') : '';
        $currentPhoto2 = $isEdit ? ($user->photo_filename2 ?? '') : '';
      @endphp

      {{-- 本人確認写真（1） --}}
      <div class="form-group row align-items-start">
        <label class="col-md-2 col-form-label">本人確認写真ファイル1</label>
        <div class="col-md-10">
          <div class="d-flex flex-wrap align-items-center">
            <div class="mr-3 mb-2">
              <input type="file" name="id_photo1" id="id_photo1" class="form-control-file">
              <input type="hidden" name="delete_photo1" id="delete_photo1" value="0">
            </div>

            @if ($currentPhoto1)
              <div class="d-flex align-items-center mb-2">
                <div class="mr-3">
                  <a href="{{ Storage::url($currentPhoto1) }}" target="_blank">{{ basename($currentPhoto1) }}</a>
                  <div class="text-muted small">現在登録済み</div>
                </div>
                <button type="button" class="btn btn-default" id="btn-delete-photo1">削除</button>
              </div>
            @else
              <div class="text-muted small mb-2">現在登録なし</div>
            @endif
          </div>
        </div>
      </div>

      {{-- 本人確認写真（2） --}}
      <div class="form-group row align-items-start">
        <label class="col-md-2 col-form-label">本人確認写真ファイル2</label>
        <div class="col-md-10">
          <div class="d-flex flex-wrap align-items-center">
            <div class="mr-3 mb-2">
              <input type="file" name="id_photo2" id="id_photo2" class="form-control-file">
              <input type="hidden" name="delete_photo2" id="delete_photo2" value="0">
            </div>

            @if ($currentPhoto2)
              <div class="d-flex align-items-center mb-2">
                <div class="mr-3">
                  <a href="{{ Storage::url($currentPhoto2) }}" target="_blank">{{ basename($currentPhoto2) }}</a>
                  <div class="text-muted small">現在登録済み</div>
                </div>
                <button type="button" class="btn btn-default" id="btn-delete-photo2">削除</button>
              </div>
            @else
              <div class="text-muted small mb-2">現在登録なし</div>
            @endif
          </div>
        </div>
      </div>

      {{-- 備考 --}}
      <div class="form-group row">
        <label class="col-md-2 col-form-label">備考</label>
        <div class="col-md-10">
          <textarea name="user_remarks" rows="4" class="form-control">{{ old('user_remarks') }}</textarea>
        </div>
      </div>

            <div class="form-group row">
              <label class="col-md-2 col-form-label">親権者氏名</label>
              <div class="col-md-10">
                <input type="text" name="user_chk_opeid" value="{{ old('user_chk_opeid') }}" class="form-control">
              </div>
            </div>

            <div class="form-group row">
              <label class="col-md-2 col-form-label">親権者フリガナ</label>
              <div class="col-md-10">
                <input type="text" name="user_chk_opeid" value="{{ old('user_chk_opeid') }}" class="form-control">
              </div>
            </div>

            <div class="form-group row">
              <label class="col-md-2 col-form-label">親権者生年月日</label>
              <div class="col-md-10">
                <input type="text" name="user_chk_opeid" value="{{ old('user_chk_opeid') }}" class="form-control">
              </div>
            </div>

            <div class="form-group row">
              <label class="col-md-2 col-form-label">親権者住所</label>
              <div class="col-md-10">
                <input type="text" name="user_chk_opeid" value="{{ old('user_chk_opeid') }}" class="form-control">
              </div>
            </div>

            <div class="form-group row">
              <label class="col-md-2 col-form-label">親権者電話番号</label>
              <div class="col-md-10">
                <input type="text" name="user_chk_opeid" value="{{ old('user_chk_opeid') }}" class="form-control">
              </div>
            </div>

            <div class="form-group row">
              <label class="col-md-2 col-form-label">親権者予備電話番号</label>
              <div class="col-md-10">
                <input type="text" name="user_chk_opeid" value="{{ old('user_chk_opeid') }}" class="form-control">
              </div>
            </div>

            <div class="form-group row">
              <label class="col-md-2 col-form-label">親権者メールアドレス</label>
              <div class="col-md-10">
                <input type="text" name="user_chk_opeid" value="{{ old('user_chk_opeid') }}" class="form-control">
              </div>
            </div>

            <div class="form-group row">
              <label class="col-md-2 col-form-label">通知方法</label>
              <div class="col-md-10">
                <input type="text" name="user_chk_opeid" value="{{ old('user_chk_opeid') }}" class="form-control">
              </div>
            </div>

            <div class="form-group row">
              <label class="col-md-2 col-form-label">個人情報同意フラグ</label>
              <div class="col-md-10">
                <input type="text" name="user_chk_opeid" value="{{ old('user_chk_opeid') }}" class="form-control">
              </div>
            </div>

    <div class="mt-3">
      <button type="button" class="btn btn-success" id="register_edit">登録</button>

      {{-- 削除ボタン（別フォームを呼び出す） --}}
      <button type="button" class="btn btn-danger ml-2" id="delete_edit">削除</button>
      <a href="{{ route('users', keepUserListQuery()) }}" class="btn btn-secondary ml-2">戻る</a>
      @if ($isEdit && $hasDeleteRoute)
        <button type="button" class="btn btn-warning ml-2" id="quitForm">退会</button>
      @endif
    </div>

      
    </div> {{-- /.card-body --}}
  </div> {{-- /.card --}}
</form>
