{{-- resources/views/admin/reserves/edit.blade.php --}}
@extends('layouts.app')
@section('title', '定期予約マスタ - 編集')

@section('content')
<style>
  .rv-edit, .rv-edit .card, .rv-edit .form-control, .rv-edit .btn, .rv-edit .breadcrumb{
    font-family:"Noto Sans JP","Hiragino Kaku Gothic ProN","Meiryo",system-ui,-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial,sans-serif;
    font-size:13px; line-height:1.45;
  }
  .rv-form .field{display:flex;align-items:center;margin-bottom:.7rem;}
  .rv-form .label{flex:0 0 180px;margin:0;color:#333;font-weight:600;white-space:nowrap;}
  .rv-form .input{flex:1 1 auto;min-width:260px;}
  .rv-form .form-control{height:calc(2.0rem + 2px);padding:.25rem .5rem;}
  /* 目标图没有灰色表头，这里直接隐藏 */
  .card .card-header{display:none;}
  .rv-toolbar .btn+.btn{margin-left:.4rem;}
  .help-text{color:#666;font-size:12px;}
</style>

@php
  // 只读显示用：从现有 options 推断名称（不改控制器也能显示）
  $userName = '';
  if (!empty($row->user_id ?? null) && !empty($userOptions[$row->user_id] ?? null)) {
      // $userOptions 形如 "12345 山田太郎" → 去掉开头ID只留名字
      $userName = trim(preg_replace('/^\s*\d+\s*/', '', $userOptions[$row->user_id]));
  }
  $parkName = '';
  if (!empty($row->park_id ?? null) && !empty($parkOptions[$row->park_id] ?? null)) {
      $parkName = trim(preg_replace('/^\s*\d+\s*/', '', $parkOptions[$row->park_id]));
  }
@endphp

<div class="rv-edit">
  {{-- ヘッダ --}}
  <div class="content-header">
    <div class="container-fluid">
      <div class="d-flex justify-content-between align-items-center mb-2">
        <h1 class="m-0 text-dark">編集</h1>
      </div>

      <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('reserves') }}">定期予約マスタ</a></li>
        <li class="breadcrumb-item active">編集</li>
      </ol>

      @if(session('success'))
        <div class="alert alert-success py-2 px-3 my-2">{{ session('success') }}</div>
      @endif
      @if($errors->any())
        <div class="alert alert-danger py-2 px-3 my-2">
          <ul class="mb-0">@foreach($errors->all() as $e)<li>{{ $e }}</li>@endforeach</ul>
        </div>
      @endif
    </div>
  </div>

  {{-- 本体 --}}
  <section class="content">
    <div class="container-fluid">
      <div class="card rv-form">
        {{-- GET と同一ルートで POST 更新 --}}
        <form id="edit-form" method="post" action="{{ route('reserves_edit', ['reserve_id'=>$row->reserve_id]) }}">
          @csrf

          <div class="card-body">
            <div class="row">
              {{-- 左カラム（目标图顺序） --}}
              <div class="col-lg-6">
                {{-- 定期予約ID（読み取り） --}}
                <div class="field">
                  <label class="label">定期予約ID</label>
                  <div class="input"><input type="text" class="form-control" value="{{ $row->reserve_id }}" readonly></div>
                </div>

                {{-- 定期契約ID --}}
                <div class="field">
                  <label class="label">定期契約ID</label>
                  <div class="input">
                    <input type="text" name="contract_id" class="form-control"
                           value="{{ old('contract_id', $row->contract_id ?? '') }}"
                           placeholder="定期契約ID">
                  </div>
                </div>

                {{-- 利用者分類ID（先頭「全て」） --}}
                <div class="field">
                  <label class="label">利用者分類ID</label>
                  <div class="input">
                    <select name="user_categoryid" class="form-control">
                      <option value="">全て</option>
                      @if(!empty($userTypeOptions))
                        @foreach($userTypeOptions as $k=>$v)
                          <option value="{{ $k }}" {{ (string)old('user_categoryid', $row->user_categoryid ?? '')===(string)$k?'selected':'' }}>
                            {{ $v }}
                          </option>
                        @endforeach
                      @endif
                    </select>
                  </div>
                </div>

                {{-- 予約日時 --}}
                <div class="field">
                  <label class="label">予約日時</label>
                  <div class="input">
                    <input type="text" name="reserve_date" class="form-control"
                           placeholder="yyyy/mm/dd hh:mm:ss"
                           value="{{ old('reserve_date', $row->reserve_date ?? '') }}">
                  </div>
                </div>

                {{-- 利用者名（読み取り）※目標画面に合わせて表示のみ --}}
                <div class="field">
                  <label class="label">利用者名</label>
                  <div class="input"><input type="text" class="form-control" value="{{ $userName }}" readonly></div>
                </div>

                {{-- 利用者ID --}}
                <div class="field">
                  <label class="label">利用者ID</label>
                  <div class="input">
                    <select name="user_id" class="form-control">
                      <option value="">選択してください</option>
                      @if(!empty($userOptions))
                        @foreach($userOptions as $k=>$v)
                          <option value="{{ $k }}" {{ (string)old('user_id', $row->user_id ?? '')===(string)$k?'selected':'' }}>
                            {{ $v }}
                          </option>
                        @endforeach
                      @endif
                    </select>
                  </div>
                </div>

                {{-- 駐輪場（読み取り） --}}
                <div class="field">
                  <label class="label">駐輪場</label>
                  <div class="input"><input type="text" class="form-control" value="{{ $parkName }}" readonly></div>
                </div>

                {{-- 駐輪場ID --}}
                <div class="field">
                  <label class="label">駐輪場ID</label>
                  <div class="input">
                    <select name="park_id" class="form-control">
                      <option value="">選択してください</option>
                      @if(!empty($parkOptions))
                        @foreach($parkOptions as $k=>$v)
                          <option value="{{ $k }}" {{ (string)old('park_id', $row->park_id ?? '')===(string)$k?'selected':'' }}>
                            {{ $v }}
                          </option>
                        @endforeach
                      @endif
                    </select>
                  </div>
                </div>

                {{-- 駐輪場所ID --}}
                <div class="field">
                  <label class="label">駐輪場所ID</label>
                  <div class="input">
                    <select name="price_parkplaceid" class="form-control">
                      <option value="">選択してください</option>
                      @if(!empty($parkplaceOptions))
                        @foreach($parkplaceOptions as $k=>$v)
                          <option value="{{ $k }}" {{ (string)old('price_parkplaceid', $row->price_parkplaceid ?? '')===(string)$k?'selected':'' }}>
                            {{ $v }}
                          </option>
                        @endforeach
                      @endif
                    </select>
                  </div>
                </div>

                {{-- 車種区分ID --}}
                <div class="field">
                  <label class="label">車種区分ID</label>
                  <div class="input">
                    <select name="psection_id" class="form-control">
                      <option value="">選択してください</option>
                      @if(!empty($psectionOptions))
                        @foreach($psectionOptions as $k=>$v)
                          <option value="{{ $k }}" {{ (string)old('psection_id', $row->psection_id ?? '')===(string)$k?'selected':'' }}>
                            {{ $v }}
                          </option>
                        @endforeach
                      @endif
                    </select>
                  </div>
                </div>

                {{-- 駐輪分類ID --}}
                <div class="field">
                  <label class="label">駐輪分類ID</label>
                  <div class="input">
                    <select name="ptype_id" class="form-control">
                      <option value="">選択してください</option>
                      @if(!empty($ptypeOptions))
                        @foreach($ptypeOptions as $k=>$v)
                          <option value="{{ $k }}" {{ (string)old('ptype_id', $row->ptype_id ?? '')===(string)$k?'selected':'' }}>
                            {{ $v }}
                          </option>
                        @endforeach
                      @endif
                    </select>
                  </div>
                </div>
              </div>

              {{-- 右カラム（目标图顺序） --}}
              <div class="col-lg-6">
                {{-- 減免措置 --}}
                <div class="field">
                  <label class="label">減免措置</label>
                  <div class="input">
                    @php $reduction = old('reduction', $row->reduction ?? ''); @endphp
                    <label class="mr-3"><input type="radio" name="reduction" value="1" {{ (string)$reduction==='1'?'checked':'' }}> あり</label>
                    <label><input type="radio" name="reduction" value="0" {{ (string)$reduction==='0'?'checked':'' }}> なし</label>
                  </div>
                </div>

                {{-- 自動リマインド日 --}}
                <div class="field">
                  <label class="label">自動リマインド日</label>
                  <div class="input">
                    <input type="text" name="auto_remind_date" class="form-control" placeholder="yyyy/mm/dd"
                           value="{{ old('auto_remind_date', $row->auto_remind_date ?? '') }}">
                  </div>
                </div>

                {{-- 手動リマインド日 --}}
                <div class="field">
                  <label class="label">手動リマインド日</label>
                  <div class="input">
                    <input type="text" name="manual_remind_date" class="form-control" placeholder="yyyy/mm/dd"
                           value="{{ old('manual_remind_date', $row->manual_remind_date ?? '') }}">
                  </div>
                </div>

                {{-- ８００Ｍ以内フラグ --}}
                <div class="field">
                  <label class="label">８００Ｍ以内フラグ</label>
                  <div class="input">
                    @php $m800 = old('within_800m_flag', $row->within_800m_flag ?? ''); @endphp
                    <label class="mr-3"><input type="radio" name="within_800m_flag" value="1" {{ (string)$m800==='1'?'checked':'' }}> ８００M以内</label>
                    <label><input type="radio" name="within_800m_flag" value="0" {{ (string)$m800==='0'?'checked':'' }}> ８００M以内ではない</label>
                  </div>
                </div>

                {{-- 解約日 --}}
                <div class="field">
                  <label class="label">解約日</label>
                  <div class="input">
                    <input type="text" name="contract_cancelday" class="form-control" placeholder="yyyy/mm/dd"
                           value="{{ old('contract_cancelday', $row->contract_cancelday ?? '') }}">
                  </div>
                </div>

                {{-- 有効フラグ --}}
                <div class="field">
                  <label class="label">有効フラグ</label>
                  <div class="input">
                    @php $valid = old('valid_flag', $row->valid_flag ?? ''); @endphp
                    <label class="mr-3"><input type="radio" name="valid_flag" value="1" {{ (string)$valid==='1'?'checked':'' }}> 有効</label>
                    <label><input type="radio" name="valid_flag" value="0" {{ (string)$valid==='0'?'checked':'' }}> 無効</label>
                  </div>
                </div>

                {{-- 空き待ちメール送信日時 --}}
                <div class="field">
                  <label class="label">空き待ちメール送信日時</label>
                  <div class="input">
                    <input type="text" name="mail_sent_at" class="form-control" placeholder="yyyy/mm/dd hh:mm:ss"
                           value="{{ old('mail_sent_at', $row->mail_sent_at ?? '') }}">
                  </div>
                </div>

                {{-- 手動通知 --}}
                <div class="field">
                  <label class="label">手動通知</label>
                  <div class="input">
                    @php $mnotice = old('manual_notice', $row->manual_notice ?? ''); @endphp
                    <label class="mr-3"><input type="radio" name="manual_notice" value="0" {{ (string)$mnotice==='0'?'checked':'' }}> 手動通知</label>
                    <label><input type="radio" name="manual_notice" value="1" {{ (string)$mnotice==='1'?'checked':'' }}> メール通知</label>
                  </div>
                </div>

                {{-- 手動通知方法 --}}
                <div class="field">
                  <label class="label">手動通知方法</label>
                  <div class="input">
                    @php $mhow = old('manual_notice_method', $row->manual_notice_method ?? ''); @endphp
                    <label class="mr-3"><input type="radio" name="manual_notice_method" value="tel"  {{ $mhow==='tel'?'checked':'' }}> 電話</label>
                    <label><input type="radio" name="manual_notice_method" value="post" {{ $mhow==='post'?'checked':'' }}> 郵送</label>
                    <div class="help-text mt-1">※ 必要に応じて選択肢は調整してください。</div>
                  </div>
                </div>

                {{-- 空き待ち順 --}}
                <div class="field">
                  <label class="label">空き待ち順</label>
                  <div class="input">
                    <input type="number" name="waitlist_order" class="form-control" min="0" step="1"
                           value="{{ old('waitlist_order', $row->waitlist_order ?? '') }}">
                  </div>
                </div>
              </div>
            </div>
          </div>

          {{-- 下部操作（保留：目标图底部有登録/削除） --}}
          <div class="card-footer d-flex justify-content-start">
            <button type="submit" class="btn btn-success">登録</button>
            <button type="button" id="btnDeleteBottom" class="btn btn-danger ml-2">削除</button>
            <a href="{{ route('reserves') }}" class="btn btn-default ml-2">戻る</a>
          </div>
        </form>

        {{-- 削除POST（confirmed + ids[]） --}}
        <form id="del-form" method="post" action="{{ route('reserves_delete') }}" style="display:none;">
          @csrf
          <input type="hidden" name="confirmed" value="1">
          <input type="hidden" name="ids[]" value="{{ $row->reserve_id }}">
        </form>
      </div>
    </div>
  </section>
</div>

<script>
  (function(){
    function del(){ if(confirm('この予約を削除します。よろしいですか？')) document.getElementById('del-form').submit(); }
    var t=document.getElementById('btnDeleteTop'), b=document.getElementById('btnDeleteBottom');
    if(t) t.addEventListener('click', del);
    if(b) b.addEventListener('click', del);
  })();
</script>
@endsection
