{{-- アラート（成功） --}}
@if(Session::has('success'))
    <div class="alert alert-success alert-dismissible" role="alert">
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
        {{ Session::get('success') }}
    </div>
@endif

{{-- アラート（エラー：Session） --}}
@if(Session::has('error'))
    <div class="alert alert-danger alert-dismissible" role="alert">
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
        {!! Session::get('error') !!}
    </div>
@endif

{{-- アラート（任意エラー） --}}
@if(isset($errorMsg))
    <div class="alert alert-danger alert-dismissible" role="alert">
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
        @if(is_array($errorMsg))
            <ul class="mb-0">
                @foreach($errorMsg as $msg)
                    <li>{{ $msg }}</li>
                @endforeach
            </ul>
        @else
            {!! $errorMsg !!}
        @endif
    </div>
@endif

{{-- バリデーションエラー --}}
@if($errors->any())
    <div class="alert alert-danger alert-dismissible">
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
        <h4><i class="icon fa fa-ban"></i> {{ __('入力内容に不備があります:') }}</h4>
        <ul>
            @foreach($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

@php
    // datetime-local 表示用 (YYYY-MM-DDTHH:MM)
    $contractCreatedAtVal = old('contract_created_at');
    if ($contractCreatedAtVal === null) {
        $dt = $record->contract_created_at ?? null;
        $contractCreatedAtVal = $dt ? \Carbon\Carbon::parse($dt)->format('Y-m-d\TH:i') : '';
    }

    $reserveDateVal = old('reserve_date');
    if ($reserveDateVal === null) {
        $dt = $record->reserve_date ?? null;
        $reserveDateVal = $dt ? \Carbon\Carbon::parse($dt)->format('Y-m-d\TH:i') : '';
    }

    $sentDateVal = old('sent_date');
    if ($sentDateVal === null) {
        $dt = $record->sent_date ?? null;
        $sentDateVal = $dt ? \Carbon\Carbon::parse($dt)->format('Y-m-d\TH:i') : '';
    }

    // 800m_flag は先頭が数字のため、プロパティアクセス不可の可能性あり
    $flag800m = old('800m_flag', $record->{'800m_flag'} ?? '0');

    // 手動通知
    $manualFlag = old('reserve_manual', $record->reserve_manual ?? '0');

    // 手動通知方法
    $noticeMethod = old('reserve_notice', $record->reserve_notice ?? '');
@endphp

<div class="card-body">
    <div class="row">

        {{-- ▼ 定期予約ID（編集時のみ表示） --}}
        @if(!empty($isEdit))
            <div class="form-group col-3">
                <label>{{ __('予約ID') }}</label>
            </div>
            <div class="form-group col-9">
                <div class="input-group">
                    <input type="text"
                           class="form-control form-control-lg bg-light"
                           value="{{ old('reserve_id', $record->reserve_id ?? '') }}"
                           readonly>
                </div>
            </div>
        @endif

        {{-- ▼ 定期契約ID --}}
        <div class="form-group col-3">
            <label>{{ __('定期契約ID') }}</label>
        </div>
        <div class="form-group col-9">
            <div class="input-group">
                <input type="number"
                       name="contract_id"
                       class="form-control form-control-lg"
                       placeholder="{{ __('定期契約ID') }}"
                       value="{{ old('contract_id', $record->contract_id ?? '') }}">
            </div>
        </div>

        {{-- ▼ 利用者分類ID（user_categoryid） --}}
        <div class="form-group col-3">
            <label>{{ __('利用者分類') }}</label>
        </div>
        <div class="form-group col-9">
            <div class="input-group">
                <select name="user_categoryid" class="form-control form-control-lg">
                    <option value="">{{ __('全て') }}</option>
                    @foreach(($userTypes ?? []) as $id => $name)
                        <option value="{{ $id }}"
                            {{ (string)old('user_categoryid', $record->user_categoryid ?? '') === (string)$id ? 'selected' : '' }}>
                            {{ $name }}
                        </option>
                    @endforeach
                </select>
            </div>
        </div>

        {{-- ▼ 定期契約日時 --}}
        <div class="form-group col-3">
            <label>{{ __('定期契約日時') }}</label>
        </div>
        <div class="form-group col-9">
            <div class="input-group">
                <input type="datetime-local"
                       name="contract_created_at"
                       class="form-control form-control-lg"
                       value="{{ $contractCreatedAtVal }}">
            </div>
        </div>

        {{-- ▼ 利用者ID --}}
        <div class="form-group col-3">
            <label>{{ __('利用者ID') }}</label>
        </div>
        <div class="form-group col-9">
            <div class="input-group">
                <input type="number"
                       name="user_id"
                       class="form-control form-control-lg"
                       placeholder="{{ __('利用者ID') }}"
                       value="{{ old('user_id', $record->user_id ?? '') }}">
            </div>
        </div>

        {{-- ▼ 駐輪場ID --}}
        <div class="form-group col-3">
            <label class="form-label required">{{ __('駐輪場') }}</label>
        </div>
        <div class="form-group col-9">
            <div class="input-group">
                <select name="park_id" class="form-control form-control-lg">
                    <option value="">{{ __('全て') }}</option>

                    @foreach(($parks ?? []) as $parkId => $parkName)
                        <option value="{{ $parkId }}"
                            {{ (string)old('park_id', $record->park_id ?? '') === (string)$parkId ? 'selected' : '' }}>
                            {{ $parkName }}
                        </option>
                    @endforeach
                </select>
            </div>
        </div>


        {{-- ▼ 駐輪場所ID（price_parkplaceid） --}}
        <div class="form-group col-3">
            <label>{{ __('駐輪場所ID') }}</label>
        </div>
        <div class="form-group col-9">
            <div class="input-group">
                <select name="price_parkplaceid" class="form-control form-control-lg">
                    <option value="">{{ __('全て') }}</option>

                    @foreach(($prices ?? []) as $id => $name)
                        <option value="{{ $id }}"
                            {{ (string)old('price_parkplaceid', $record->price_parkplaceid ?? '') === (string)$id ? 'selected' : '' }}>
                            {{ $name }}
                        </option>
                    @endforeach
                </select>
            </div>
        </div>

        {{-- ▼ 車種区分ID（psection_id） --}}
        <div class="form-group col-3">
            <label>{{ __('車種区分ID') }}</label>
        </div>
        <div class="form-group col-9">
            <div class="input-group">
                <select name="psection_id" class="form-control form-control-lg">
                    <option value="">{{ __('全て') }}</option>

                    @foreach(($psections ?? []) as $psectionId => $psectionName)
                        <option value="{{ $psectionId }}"
                            {{ (string)old('psection_id', $record->psection_id ?? '') === (string)$psectionId ? 'selected' : '' }}>
                            {{ $psectionName }}
                        </option>
                    @endforeach
                </select>
            </div>
        </div>

        {{-- ▼ 駐輪分類ID（ptype_id） --}}
        <div class="form-group col-3">
            <label class="form-label required">{{ __('駐輪分類ID') }}</label>
        </div>
        <div class="form-group col-9">
            <div class="input-group">
                <select name="ptype_id" class="form-control form-control-lg">
                    <option value="">{{ __('全て') }}</option>

                    @foreach(($ptypes ?? []) as $ptypeId => $ptypeName)
                        <option value="{{ $ptypeId }}"
                            {{ (string)old('ptype_id', $record->ptype_id ?? '') === (string)$ptypeId ? 'selected' : '' }}>
                            {{ $ptypeName }}
                        </option>
                    @endforeach
                </select>
            </div>
        </div>

        {{-- ▼ 予約日時 --}}
        <div class="form-group col-3">
            <label class="form-label required">{{ __('予約日時') }}</label>
        </div>
        <div class="form-group col-9">
            <div class="input-group">
                <input type="datetime-local"
                       name="reserve_date"
                       class="form-control form-control-lg"
                       value="{{ $reserveDateVal }}">
            </div>
        </div>        

        {{-- ▼ 減免措置 --}}
        <div class="form-group col-3">
            <label>{{ __('減免措置') }}</label>
        </div>
        <div class="form-group col-9">
            @php
                // 初期表示：なし
                $reductionFlag = old('reserve_reduction', $record->reserve_reduction ?? '0');
            @endphp

            <div class="row">
                <div class="col-2 offset-1 form-check">
                    <label class="radio-inline mb-0">
                        <input type="radio"
                            name="reserve_reduction"
                            class="minimal"
                            value="1"
                            {{ (string)$reductionFlag === '1' ? 'checked' : '' }}>
                        {{ __('あり') }}
                    </label>
                </div>

                <div class="col-2 form-check">
                    <label class="radio-inline mb-0">
                        <input type="radio"
                            name="reserve_reduction"
                            class="minimal"
                            value="0"
                            {{ (string)$reductionFlag === '0' ? 'checked' : '' }}>
                        {{ __('なし') }}
                    </label>
                </div>
            </div>
        </div>
        
        {{-- ▼ 自動リマインド日 --}}
        <div class="form-group col-3">
            <label>{{ __('自動リマインド日') }}</label>
        </div>
        <div class="form-group col-9">
            <div class="input-group">
                <input type="date"
                       name="reserve_auto_remind"
                       class="form-control form-control-lg"
                       value="{{ old('reserve_auto_remind', $record->reserve_auto_remind ?? '') }}">
            </div>
        </div>
                
        {{-- ▼ 手動リマインド日 --}}
        <div class="form-group col-3">
            <label>{{ __('手動リマインド日') }}</label>
        </div>
        <div class="form-group col-9">
            <div class="input-group">
                <input type="date"
                       name="reserve_manual_remind"
                       class="form-control form-control-lg"
                       value="{{ old('reserve_manual_remind', $record->reserve_manual_remind ?? '') }}">
            </div>
        </div>

        <div class="form-group col-3">
            <label>{{ __('800M以内フラグ') }}</label>
        </div>
        <div class="form-group col-9">
            @php
                $flag800m = old('800m_flag', $record->{'800m_flag'} ?? '0');
            @endphp

            <div class="row">
                <div class="col-2 offset-1 form-check">
                    <label class="radio-inline mb-0">
                        <input type="radio" name="800m_flag" class="minimal" value="1"
                            {{ (string)$flag800m === '1' ? 'checked' : '' }}>
                        {{ __('800M以内') }}
                    </label>
                </div>

                <div class="col-2 form-check">
                    <label class="radio-inline mb-0">
                        <input type="radio" name="800m_flag" class="minimal" value="0"
                            {{ (string)$flag800m === '0' ? 'checked' : '' }}>
                        {{ __('800M以内ではない') }}
                    </label>
                </div>
            </div>
        </div>

        {{-- ▼ 解約日 --}}
        <div class="form-group col-3">
            <label>{{ __('解約日') }}</label>
        </div>
        <div class="form-group col-9">
            <div class="input-group">
                <input type="date"
                       name="reserve_cancelday"
                       class="form-control form-control-lg"
                       value="{{ old('reserve_cancelday', $record->reserve_cancelday ?? '') }}">
            </div>
        </div>

        {{-- ▼ 有効フラグ --}}
        <div class="form-group col-3">
            <label>{{ __('有効フラグ') }}</label>
        </div>
        <div class="form-group col-9">
            @php
                $validFlag = old('valid_flag', $record->valid_flag ?? '1'); // 初期：有効
            @endphp

            <div class="row">
                <div class="col-2 offset-1 form-check">
                    <label class="radio-inline mb-0">
                        <input type="radio" name="valid_flag" class="minimal" value="1"
                            {{ (string)$validFlag === '1' ? 'checked' : '' }}>
                        {{ __('有効') }}
                    </label>
                </div>

                <div class="col-2 form-check">
                    <label class="radio-inline mb-0">
                        <input type="radio" name="valid_flag" class="minimal" value="0"
                            {{ (string)$validFlag === '0' ? 'checked' : '' }}>
                        {{ __('無効') }}
                    </label>
                </div>
            </div>
        </div>
        
        {{-- ▼ 手動通知 --}}
        <div class="form-group col-3">
            <label>{{ __('手動通知') }}</label>
        </div>
        <div class="form-group col-9">
            @php
                // 画面2の表示に合わせて：初期は「メール通知(0)」
                $manualFlag = old('reserve_manual', $record->reserve_manual ?? '0');
            @endphp

            <div class="row">
                <div class="col-2 offset-1 form-check">
                    <label class="radio-inline mb-0">
                        <input type="radio" name="reserve_manual" class="minimal" value="1"
                            {{ (string)$manualFlag === '1' ? 'checked' : '' }}>
                        {{ __('手動通知') }}
                    </label>
                </div>

                <div class="col-3 form-check">
                    <label class="radio-inline mb-0">
                        <input type="radio" name="reserve_manual" class="minimal" value="0"
                            {{ (string)$manualFlag === '0' ? 'checked' : '' }}>
                        {{ __('メール通知') }}
                    </label>
                </div>
            </div>
        </div>

        {{-- ▼ 手動通知方法 --}}
        <div class="form-group col-3">
            <label>{{ __('手動通知方法') }}</label>
        </div>
        <div class="form-group col-9">
            @php
                // 初期：電話
                $noticeMethod = old('reserve_notice', $record->reserve_notice ?? 'tel');
            @endphp

            <div class="row">
                <div class="col-2 offset-1 form-check">
                    <label class="radio-inline mb-0">
                        <input type="radio" name="reserve_notice" class="minimal" value="tel"
                            {{ (string)$noticeMethod === 'tel' ? 'checked' : '' }}>
                        {{ __('電話') }}
                    </label>
                </div>

                <div class="col-2 form-check">
                    <label class="radio-inline mb-0">
                        <input type="radio" name="reserve_notice" class="minimal" value="post"
                            {{ (string)$noticeMethod === 'post' ? 'checked' : '' }}>
                        {{ __('郵送') }}
                    </label>
                </div>
            </div>
        </div>

        {{-- ▼ メール送信日時 --}}
        <div class="form-group col-3">
            <label>{{ __('メール送信日時') }}</label>
        </div>
        <div class="form-group col-9">
            <div class="input-group">
                <input type="datetime-local"
                       name="sent_date"
                       class="form-control form-control-lg"
                       value="{{ $sentDateVal }}">
            </div>
        </div>

        {{-- ▼ 空き待ち順 --}}
        <div class="form-group col-3">
            <label>{{ __('空き待ち順') }}</label>
        </div>
        <div class="form-group col-9">
            <div class="input-group">
                <input type="number"
                       name="reserve_order"
                       class="form-control form-control-lg"
                       placeholder="{{ __('空き待ち順') }}"
                       min="0"
                       value="{{ old('reserve_order', $record->reserve_order ?? '') }}">
            </div>
        </div>

        {{-- ▼ 予約区分（通常/仮） --}}
        <div class="form-group col-3">
            <label>{{ __('予約区分') }}</label>
        </div>
        <div class="form-group col-9">
            @php
                // 初期：通常予約
                $reserveType = old('reserve_type', $record->reserve_type ?? '0');
            @endphp

            <div class="row">
                <div class="col-2 offset-1 form-check">
                    <label class="radio-inline mb-0">
                        <input type="radio" name="reserve_type" class="minimal" value="0"
                            {{ (string)$reserveType === '0' ? 'checked' : '' }}>
                        {{ __('通常予約') }}
                    </label>
                </div>

                <div class="col-2 form-check">
                    <label class="radio-inline mb-0">
                        <input type="radio" name="reserve_type" class="minimal" value="1"
                            {{ (string)$reserveType === '1' ? 'checked' : '' }}>
                        {{ __('仮予約') }}
                    </label>
                </div>
            </div>
        </div>      

        <!-- {{-- ▼ オペレータID --}}
        <div class="form-group col-3">
            <label>{{ __('オペレータID') }}</label>
        </div>
        <div class="form-group col-9">
            <div class="input-group">
                <input type="number"
                       name="ope_id"
                       class="form-control form-control-lg"
                       placeholder="{{ __('オペレータID') }}"
                       value="{{ old('ope_id', $record->ope_id ?? '') }}">
            </div>
        </div> -->

    </div>    

    {{-- ▼ 下部ボタン --}}
    <div class="row mt-4">
        <div class="form-group col-md-10 d-flex align-items-center gap-2 justify-content-start">

            {{-- 登録ボタン --}}
            @if(!empty($isEdit))
                <button type="button" id="register_edit" class="btn btn-lg btn-success mr-2 js-confirm-submit">
                    {{ __('登録') }}
                </button>
            @else
                <button type="button" id="register" class="btn btn-lg btn-success mr-2 register js-confirm-submit">
                    {{ __('登録') }}
                </button>
            @endif

            {{-- 削除ボタン（編集時のみ表示） --}}
            @if(!empty($isEdit))
                <button type="button" id="delete_edit" class="btn btn-lg btn-danger js-inline-delete mr-2">
                    {{ __('削除') }}
                </button>
            @endif

            {{-- 戻るボタン（一覧状態保持） --}}
            <button type="button"
                    id="btn_back"
                    class="btn btn-lg btn-secondary mr-2"
                    data-back-url="{{ request('back') }}">
                {{ __('戻る') }}
            </button>

        </div>
    </div>
</div>
