@extends('layouts.app')
@section('title', '定期予約登録')

@section('content')
<style>
  /* 画面全体のフォント／サイズを統一（やや小さめ） */
  .rv-add,
  .rv-add .card,
  .rv-add .form-control,
  .rv-add .btn,
  .rv-add .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;
  }
  /* 1行＝左ラベル／右入力 */
  .rv-add .field{display:flex;align-items:center;margin-bottom:.7rem;}
  .rv-add .label{flex:0 0 170px;margin:0;color:#333;font-weight:600;white-space:nowrap;}
  .rv-add .input{flex:1 1 auto;}
  .rv-add .form-control{height:calc(2.0rem + 2px);padding:.25rem .5rem;}
  .rv-add .help{color:#888;font-size:12px;}
</style>

<div class="rv-add">
  {{-- パンくず＆見出し --}}
  <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="{{ route('reserves') }}">定期予約マスタ</a></li>
            <li class="breadcrumb-item active">新規</li>
          </ol>
        </div>
      </div>
      @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">
        <div class="card-header d-flex justify-content-start gap-2">
          <button type="submit" class="btn btn-sm btn-default register js-confirm-submit">登録</button>
          <a href="{{ route('reserves') }}" class="btn btn-sm btn-default ml-2">戻る</a>
        </div>

        <form id="rvAddForm" method="post" action="{{ route('reserves_add') }}">
          @csrf
          <div class="card-body">

            {{-- 画面上部（ID系） --}}

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

            <div class="field">
              <label class="label">利用者分類ID</label>
              <div class="input">
                <select name="user_categoryid" class="form-control">
                  <option value="">全て</option>
                  @foreach($userTypes as $type)
                    <option value="{{ $type->user_categoryid }}"
                      {{ old('user_categoryid') == $type->user_categoryid ? 'selected' : '' }}>
                      {{ $type->display_name }}
                    </option>
                  @endforeach
                </select>
              </div>
            </div>

            <div class="field">
              <label class="label">利用者登録日時</label>
              <div class="input">
                <input type="datetime-local" class="form-control" name="user_created_at" value="{{ old('user_created_at') }}">
              </div>
            </div>

            {{-- 基本情報 --}}
            <div class="field">
              <label class="label">利用者ID <span class="text-danger">*</span></label>
              <div class="input">
                <input type="number" class="form-control" name="user_id" value="{{ old('user_id') }}" required placeholder="利用者ID">
              </div>
            </div>

            <div class="field">
              <label class="label">駐輪場ID <span class="text-danger">*</span></label>
              <div class="input">
                <select name="park_id" class="form-control" required>
                  <option value="">駐輪場ID</option>
                  @foreach($parks as $park)
                     <option value="{{ $park->park_id }}" {{ old('park_id') == $park->park_id ? 'selected' : '' }}>
                       {{ $park->park_name }}
                     </option>
                  @endforeach
                </select>
              </div>
            </div>
 
            <div class="field">
              <label class="label">駐輪場所ID</label>
              <div class="input">
                <select name="price_parkplaceid" class="form-control">
                  <option value="">駐輪場所ID</option>
                  @foreach($priceOptions as $price)
                    <option value="{{ $price->price_parkplaceid }}" {{ old('price_parkplaceid') == $price->price_parkplaceid ? 'selected' : '' }}>
                      {{ $price->price_parkplaceid }} {{ $price->prine_name }}
                    </option>
                  @endforeach
                </select>
              </div>
            </div>

            <div class="field">
              <label class="label">車種区分ID</label>
              <div class="input">
                <input type="number" class="form-control" name="psection_id" value="{{ old('psection_id') }}" placeholder="車種区分ID">
              </div>
            </div>

            <div class="field">
              <label class="label">駐輪分類ID</label>
              <div class="input">
                <input type="number" class="form-control" name="ptype_id" value="{{ old('ptype_id') }}" placeholder="駐輪分類ID">
              </div>
            </div>

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

            {{-- ラジオ系 --}}
            <div class="field">
              <label class="label">減免措置</label>
              <div class="input">
                <div class="form-check form-check-inline">
                  <input class="form-check-input" type="radio" name="reduction_flag" id="reduction1" value="1" {{ old('reduction_flag','0')==='1'?'checked':'' }}>
                  <label class="form-check-label" for="reduction1">あり</label>
                </div>
                <div class="form-check form-check-inline ml-3">
                  <input class="form-check-input" type="radio" name="reduction_flag" id="reduction0" value="0" {{ old('reduction_flag','0')==='0'?'checked':'' }}>
                  <label class="form-check-label" for="reduction0">なし</label>
                </div>
              </div>
            </div>

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

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

            <div class="field">
              <label class="label">８００Ｍ以内フラグ</label>
              <div class="input">
                <div class="form-check form-check-inline">
                  <input class="form-check-input" type="radio" name="within_800m_flag" id="m800_1" value="1" {{ old('within_800m_flag','0')==='1'?'checked':'' }}>
                  <label class="form-check-label" for="m800_1">８００M以内</label>
                </div>
                <div class="form-check form-check-inline ml-3">
                  <input class="form-check-input" type="radio" name="within_800m_flag" id="m800_0" value="0" {{ old('within_800m_flag','0')==='0'?'checked':'' }}>
                  <label class="form-check-label" for="m800_0">８００M内ではない</label>
                </div>
              </div>
            </div>

            {{-- 期間・状態 --}}
            <div class="field">
              <label class="label">解約日</label>
              <div class="input">
                <input type="date" class="form-control" name="reserve_cancelday" value="{{ old('reserve_cancelday') }}" placeholder="yyyy/mm/dd">
              </div>
            </div>

            <div class="field">
              <label class="label">有効フラグ</label>
              <div class="input">
                <div class="form-check form-check-inline">
                  <input class="form-check-input" type="radio" name="valid_flag" id="valid1" value="1" {{ old('valid_flag','1')==='1'?'checked':'' }}>
                  <label class="form-check-label" for="valid1">有効</label>
                </div>
                <div class="form-check form-check-inline ml-3">
                  <input class="form-check-input" type="radio" name="valid_flag" id="valid0" value="0" {{ old('valid_flag')==='0'?'checked':'' }}>
                  <label class="form-check-label" for="valid0">無効</label>
                </div>
              </div>
            </div>

            {{-- 通知 --}}
            <div class="field">
              <label class="label">手動通知</label>
              <div class="input">
                <div class="form-check form-check-inline">
                  <input class="form-check-input" type="radio" name="manual_notice" id="mn1" value="1" {{ old('manual_notice','0')==='1'?'checked':'' }}>
                  <label class="form-check-label" for="mn1">手動通知</label>
                </div>
                <div class="form-check form-check-inline ml-3">
                  <input class="form-check-input" type="radio" name="manual_notice" id="mn2" value="2" {{ old('manual_notice')==='2'?'checked':'' }}>
                  <label class="form-check-label" for="mn2">メール通知</label>
                </div>
              </div>
            </div>

            <div class="field">
              <label class="label">手動通知方法</label>
              <div class="input">
                <div class="form-check form-check-inline">
                  <input class="form-check-input" type="radio" name="manual_notice_method" id="mm_tel" value="tel" {{ old('manual_notice_method','tel')==='tel'?'checked':'' }}>
                  <label class="form-check-label" for="mm_tel">電話</label>
                </div>
                <div class="form-check form-check-inline ml-3">
                  <input class="form-check-input" type="radio" name="manual_notice_method" id="mm_mail" value="post" {{ old('manual_notice_method')==='post'?'checked':'' }}>
                  <label class="form-check-label" for="mm_mail">郵送</label>
                </div>
              </div>
            </div>

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

            <div class="field">
              <label class="label">空き待ち順</label>
              <div class="input">
                <input type="number" class="form-control" name="waiting_order" value="{{ old('waiting_order') }}" placeholder="空き待ち順">
              </div>
            </div>

            <div class="mt-4 d-flex gap-2">
              <button type="submit" class="btn btn-sm btn-default register js-confirm-submit">登録</button>
              <a href="{{ route('reserves') }}" class="btn btn-sm btn-default ml-2">戻る</a>
            </div>
          </div>
        </form>
      </div>
    </div>
  </section>
</div>

@push('scripts')
<script>
  (function ($) {
    'use strict';
    $(function () {
      $(document).off('click.rvConfirm', '.js-confirm-submit').on('click.rvConfirm', '.js-confirm-submit', function (e) {
        e.preventDefault();
        const $button = $(this);
        $.confirm({
          title: '確認ダイアログ',
          content: '！登録してよろしいですか？　はい/いいえ',
          buttons: {
            yes: {
              text: 'はい',
              btnClass: 'btn-primary',
              action: function () {
                let $form = $button.closest('form');
                if (!$form.length) {
                  const formId = $button.attr('form');
                  $form = formId ? $('#' + formId) : $('form').first();
                }
                $form.trigger('submit');
              }
            },
            no: { text: 'いいえ' }
          }
        });
      });
    });
  })(jQuery);
</script>
@endpush
@endsection
