<!-- Modal -->
<div class="modal fade" id="teacherModal2{{$vehicle->id.$w.$i}}" tabindex="-1" aria-labelledby="teacherModal2{{$vehicle->id.$w.$i}}Label" aria-hidden="true" data-backdrop="static" >
    <div class="modal-dialog modal-dialog-centered">

        @if(date('Y-m-d')==$week[$w])
            @if(numberOfStudent($arrValue2[$w][$vehicle->id][$i])==1) <!-- มีผู้เรียนแค่ 1 คนในช่วงเวลา -->
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title-card" id="teacherModal2{{$vehicle->id.$w.$i}}Label">ระบุผู้สอน</h5>
                </div>
                <div class="modal-body">
                    <div class="row mb-3">
                        <div class="col-12 ">
                            <label id="vehicle-name" class="modal-label-card float-left">{{$vehicle->type ." คันที่ : ".$vehicle->number . " , " . $vehicle->brand." ".$vehicle->model. " [ ".$vehicle->registration_class." ".$vehicle->registration_number." ]"}}</label>
                        </div>
                    </div>

                    <div class="row mt-2">
                        <div class="col-12 ">
                            <label id="student-name{{$vehicle->id.$w.$i}}" class="modal-label-card float-left">ผู้เรียน : {{$arrValue2[$w][$vehicle->id][$i]}}</label>
                        </div>
                    </div>
                    <div class="row mt-4">
                        <div class="col-12">
                            <label id="module{{$vehicle->id.$w.$i}}" class="modal-label-card float-left">วิชา : {{ $arrModule2[$w][$vehicle->id][$i] }}</label>
                        </div>
                    </div>
                    <div class="row mt-2">
                        <div class="col-12">
                            <label id="drive-time{{$vehicle->id.$w.$i}}" class="modal-label-card float-left">เวลา : {{formatTimeFromDB($arrSchedule2[$w][$vehicle->id][$i]) . " ถึง " . getEndTimeFromHour($arrSchedule2[$w][$vehicle->id][$i], ($arrSpan2[$w][$vehicle->id][$i])/2 ) ." น."}}</label>
                        </div>
                    </div>
                    <form action="{{ route('calendar.booking') }}"
                            id="teacherBookFrm2{{$vehicle->id.$w.$i}}"
                            novalidate class="teacherBookFrm {{ ($errors->any()) ? 'was-validated' : 'needs-validation' }}"
                            method="post"
                    >
                    @csrf
                    <input type="hidden" name="vehicleBookingID" id="vehicle-booking{{$vehicle->id.$w.$i}}" value="{{$arrBooking2[$w][$vehicle->id][$i]}}">
                    <input type="hidden" name="purpose" value="เรียนและสอบ">
                    <div class="row mt-4">
                        <div class="col-12">
                            <div class="form-group">
                                <label for="teacher" class="modal-label-card float-left mb-2">ผู้สอน @if(date('Y-m-d')==$week[$w])<span class="text-red">*</span>@endif</label>
                                <select id="teacher{{$vehicle->id.$w.$i}}" name="teacher" data-size="10" data-container="body" class="selectpicker show-menu-arrow form-control" required>
                                    <option value="">--เลือก--</option>
                                    @foreach ($teachers as $indexKey => $teacher)
                                    <option value="{{ $teacher->id }}" @if($teacher->id == $arrTeacher2[$w][$vehicle->id][$i]) selected @endif>{{ $teacher->title.$teacher->firstname. " " .$teacher->lastname }}</option>
                                    @endforeach
                                </select>
                                @error('teacher{{$vehicle->id.$w.$i}}')
                                <div class="text-danger">{{ $message }}</div>
                                @enderror
                            </div>
                        </div>
                    </div>
                </div> <!-- modal-body -->
                <div class="modal-footer">
                    <button type="button" class="btn btn-outline-secondary" data-dismiss="modal">ปิด</button>
                    <button type="submit" id="bookBtn{{$vehicle->id.$w.$i}}" class="btn btn-success" onclick="document.getElementById('teacherBookFrm2{{$vehicle->id.$w.$i}}').submit();">ตกลง</button>
                    </form>
                        @if(check_cancel_booking($arrSchedule2[$w][$vehicle->id][$i])) <!--เวลาปัจจุบัน(ทำรายยกเลิกได้) ก่อนเรียน > 4 ชม. -->
                            <form action="{{ route('calendar.cancel_booking') }}"
                                method="post"
                                {{-- onsubmit="confirmalert(event, {{ $vehicle->id.$w.$i }}, 'ยกเลิกตารางเรียน')" --}}
                            >
                                @csrf
                                @method('DELETE')
                                <input type="hidden" name="vehicleBookingID" id="vehicle-cancle-booking{{$vehicle->id.$w.$i}}" value="{{$arrBooking2[$w][$vehicle->id][$i]}}">
                                <div class="clearfix float-right" >
                                    <button type="submit" id="cancelBtn{{$vehicle->id.$w.$i}}" class="btn btn-danger" >ยกเลิกนัด</button>
                                </div>
                            </form>
                        @endif
                </div>
            </div>
            @else   <!-- ผู้เรียนมากกว่า 1 คนในเวลาเดียวกัน -->
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title-card" id="teacherModal2{{$vehicle->id.$w.$i}}Label">ระบุผู้สอน</h5>
                </div>
                <div class="modal-body">
                    <div class="row mb-3">
                        <div class="col-12 ">
                            <label id="vehicle-name" class="modal-label-card float-left">{{$vehicle->type ." คันที่ : ".$vehicle->number . " , " . $vehicle->brand." ".$vehicle->model. " [ ".$vehicle->registration_class." ".$vehicle->registration_number." ]"}}</label>
                        </div>
                    </div>
                    @if(check_cancel_booking($arrSchedule2[$w][$vehicle->id][$i])) <!--เวลาปัจจุบัน(ทำรายยกเลิกได้) ก่อนเรียน > 4 ชม. -->
                        @for($n = 0; $n < numberOfStudent($arrValue2[$w][$vehicle->id][$i]); $n++)
                            <form id="frm{{$vehicle->id.$w.$i.$n}}"
                                action="{{ route('calendar.cancel_booking') }}"
                                method="post"
                                {{-- onsubmit="confirmalert(event, {{ $vehicle->id.$w.$i }}, 'ยกเลิกตารางเรียน')" --}}
                            >
                            @csrf
                            @method('DELETE')
                                <input type="hidden" name="date" id="date-booking{{$vehicle->id.$w.$i}}" value="{{$week[$w]}}">
                                <div class="row mt-2">
                                    <input type="hidden" name="vehicleBookingID" id="vehicle-booking{{$vehicle->id.$w.$i.$n}}" value="{{ getVehicleBookingAtN($arrBooking2[$w][$vehicle->id][$i], $n) }}">
                                    <div class="col-9">
                                        <label id="student-name{{$n}}" class="modal-label-card float-left">ผู้เรียน : {{ getStudentAtN($arrValue2[$w][$vehicle->id][$i], $n)}}</label>
                                    </div>
                                    @if(date('Y-m-d')<$week[$w])
                                    <div class="col-3">
                                        <button type="submit" id="cancelBtn{{$vehicle->id.$w.$i.$n}}" class="btn btn-sm btn-danger">ยกเลิกนัด</button>
                                    </div>
                                    @endif
                                </div>
                            </form>
                            <hr style="margin-top: -5px;"/>
                        @endfor
                    @else <!-- เลยเวลา 4 ชม. ไม่สามารถยกเลิกนัดได้ ทำได้แค่ book ผู้สอน -->
                        @for($n = 0; $n < numberOfStudent($arrValue2[$w][$vehicle->id][$i]); $n++)
                            <div class="row mt-2">
                                <div class="col-9">
                                    <label id="student-name{{$n}}" class="modal-label-card float-left">ผู้เรียน : {{ getStudentAtN($arrValue2[$w][$vehicle->id][$i], $n)}}</label>
                                </div>
                                <hr>
                            </div>
                        @endfor
                    @endif
                    <div class="row mt-4">
                        <div class="col-12">
                            <label id="module{{$vehicle->id.$w.$i}}" class="modal-label-card float-left">วิชา : {{ $arrModule2[$w][$vehicle->id][$i] }}</label>
                        </div>
                    </div>
                    <div class="row mt-2">
                        <div class="col-12">
                            <label id="drive-time{{$vehicle->id.$w.$i}}" class="modal-label-card float-left">เวลา : {{formatTimeFromDB($arrSchedule2[$w][$vehicle->id][$i]) . " ถึง " . getEndTimeFromHour($arrSchedule2[$w][$vehicle->id][$i], ($arrSpan2[$w][$vehicle->id][$i])/2 ) ." น."}}</label>
                        </div>
                    </div>
                    <form action="{{ route('calendar.booking') }}"
                        id="teacherBookFrm22{{$vehicle->id.$w.$i}}"
                        novalidate class="teacherBookFrm {{ ($errors->any()) ? 'was-validated' : 'needs-validation' }}"
                        method="post">
                        @csrf
                        <input type="hidden" name="vehicleBookingID" id="vehicle-booking{{$vehicle->id.$w.$i}}" value="{{$arrBooking2[$w][$vehicle->id][$i]}}">
                    <div class="row mt-4">
                        <div class="col-12">
                            <div class="form-group">
                                <label for="teacher" class="modal-label-card float-left mb-2">ผู้สอน @if(date('Y-m-d')==$week[$w])<span class="text-red">*</span>@endif</label>
                                <select id="teacher{{$vehicle->id.$w.$i}}" name="teacher" data-size="10" data-container="body" class="selectpicker show-menu-arrow form-control teacher-select" @if(date('Y-m-d')>$week[$w]) disabled @endif required>
                                    <option value="">--เลือก--</option>
                                    @foreach ($teachers as $indexKey => $teacher)
                                    <option value="{{ $teacher->id }}" @if($teacher->id == $arrTeacher2[$w][$vehicle->id][$i]) selected @endif>{{ $teacher->title.$teacher->firstname. " " .$teacher->lastname }}</option>
                                    @endforeach
                                </select>
                                @error('teacher{{$vehicle->id.$w.$i}}')
                                <div class="text-danger">{{ $message }}</div>
                                @enderror
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-outline-secondary" data-dismiss="modal">ปิด</button>
                    <button type="submit" id="bookBtn{{$vehicle->id.$w.$i}}" onclick="document.getElementById('teacherBookFrm22{{$vehicle->id.$w.$i}}').submit();" class="btn btn-success">ตกลง</button>
                </div>
                    </form>
            </div>
            @endif
        @elseif(date('Y-m-d') < $week[$w]) <!-- ถ้ายังไม่ถึงวันนัดให้ยกเลิกนัดได้อย่างเดียว -->
            @if(numberOfStudent($arrValue2[$w][$vehicle->id][$i])==1) <!-- มีผู้เรียนแค่ 1 คนในช่วงเวลา -->
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title-card" id="teacherModal2{{$vehicle->id.$w.$i}}Label">ยกเลิกตารางเรียน</h5>
                </div>
                <div class="modal-body">
                    <div class="row mb-3">
                        <div class="col-12 ">
                            <label id="vehicle-name" class="modal-label-card float-left">{{$vehicle->type ." คันที่ : ".$vehicle->number . " , " . $vehicle->brand." ".$vehicle->model. " [ ".$vehicle->registration_class." ".$vehicle->registration_number." ]"}}</label>
                        </div>
                    </div>
                    <div class="row mt-2">
                        <input type="hidden" name="vehicleBookingID" id="vehicle-booking{{$vehicle->id.$w.$i}}" value="{{$arrBooking2[$w][$vehicle->id][$i]}}">
                        <div class="col-12 ">
                            <label id="student-name{{$vehicle->id.$w.$i}}" class="modal-label-card float-left">ผู้เรียน : {{$arrValue2[$w][$vehicle->id][$i]}}</label>
                        </div>
                    </div>
                    <div class="row mt-4">
                        <div class="col-12">
                            <label id="module{{$vehicle->id.$w.$i}}" class="modal-label-card float-left">วิชา : {{ $arrModule2[$w][$vehicle->id][$i] }}</label>
                        </div>
                    </div>
                    <div class="row mt-2">
                        <div class="col-12">
                            <label id="drive-time{{$vehicle->id.$w.$i}}" class="modal-label-card float-left">เวลา : {{formatTimeFromDB($arrSchedule2[$w][$vehicle->id][$i]) . " ถึง " . getEndTimeFromHour($arrSchedule2[$w][$vehicle->id][$i], ($arrSpan2[$w][$vehicle->id][$i])/2 ) ." น."}}</label>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <form id="frm{{$vehicle->id.$w.$i}}"
                        action="{{ route('calendar.cancel_booking') }}"
                        method="post"
                        {{-- onsubmit="confirmalert(event, {{ $vehicle->id.$w.$i }}, 'ยกเลิกตารางเรียน')" --}}
                    >
                        @csrf
                        @method('DELETE')
                        <input type="hidden" name="date" id="date-booking{{$vehicle->id.$w.$i}}" value="{{$week[$w]}}">
                        <input type="hidden" name="vehicleBookingID" id="vehicle-booking{{$vehicle->id.$w.$i}}" value="{{$arrBooking2[$w][$vehicle->id][$i]}}">
                        <button type="button" class="btn btn-outline-secondary mr-2" data-dismiss="modal">ปิด</button>
                        <div class="clearfix float-right" >
                            <button type="submit" id="cancelBtn{{$vehicle->id.$w.$i}}" class="btn btn-danger" >ยกเลิกนัด</button>
                        </div>
                    </form>
                </div>
            </div>
            @else <!-- ผู้เรียนมากกว่า 1 คนในเวลาเดียวกัน -->
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title-card" id="teacherModal2{{$vehicle->id.$w.$i}}Label">ยกเลิกตารางเรียน</h5>
                </div>
                <div class="modal-body">
                    @for($n = 0; $n < numberOfStudent($arrValue2[$w][$vehicle->id][$i]); $n++)
                    <form id="frm{{$vehicle->id.$w.$i.$n}}"
                        action="{{ route('calendar.cancel_booking') }}"
                        method="post"
                        {{-- onsubmit="confirmalert(event, {{ $vehicle->id.$w.$i }}, 'ยกเลิกตารางเรียน')" --}}
                    >
                        @csrf
                        @method('DELETE')
                        <div class="row mt-3">
                            <input type="hidden" name="date" id="date-booking{{$vehicle->id.$w.$i}}" value="{{$week[$w]}}">
                            <input type="hidden" name="vehicleBookingID" id="vehicle-booking{{$vehicle->id.$w.$i.$n}}" value="{{ getVehicleBookingAtN($arrBooking2[$w][$vehicle->id][$i], $n) }}">
                            <div class="col-9">
                                <label id="student-name{{$n}}" class="modal-label-card float-left">ผู้เรียน : {{ getStudentAtN($arrValue2[$w][$vehicle->id][$i], $n)}}</label>
                            </div>
                            <div class="col-3">
                                <button type="submit" id="cancelBtn{{$vehicle->id.$w.$i.$n}}" class="btn btn-sm btn-danger" style="margin-top:-8px;">ยกเลิกนัด</button>
                            </div>
                        </div>
                    </form>
                    <hr style="margin-top: -5px;"/>
                    @endfor
                    <div class="row mt-4">
                        <div class="col-12">
                            <label id="module{{$vehicle->id.$w.$i}}" class="modal-label-card float-left">วิชา : {{ $arrModule2[$w][$vehicle->id][$i] }}</label>
                        </div>
                    </div>
                    <div class="row mt-2">
                        <div class="col-12">
                            <label id="drive-time{{$vehicle->id.$w.$i}}" class="modal-label-card float-left">เวลา : {{formatTimeFromDB($arrSchedule2[$w][$vehicle->id][$i]) . " ถึง " . getEndTimeFromHour($arrSchedule2[$w][$vehicle->id][$i], ($arrSpan2[$w][$vehicle->id][$i])/2 ) ." น."}}</label>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-outline-secondary" data-dismiss="modal">ปิด</button>
                </div>
            </div>
            @endif
        @else <!-- รายการของวันที่ผ่านมาแล้ว -->
            <div class="modal-content">
                <div class="modal-body">
                    <div class="row mb-3">
                        <div class="col-12 ">
                            <label class="modal-label-card float-left">{{$vehicle->type ." คันที่ : ".$vehicle->number . " , " . $vehicle->brand." ".$vehicle->model. " [ ".$vehicle->registration_class." ".$vehicle->registration_number." ]"}}</label>
                        </div>
                    </div>
                    @if(numberOfStudent($arrValue2[$w][$vehicle->id][$i])==1) <!-- มีผู้เรียนแค่ 1 คนในช่วงเวลา -->
                        <div class="row mt-2">
                            <div class="col-12 ">
                                <label class="modal-label-card float-left">ผู้เรียน : {{$arrValue2[$w][$vehicle->id][$i]}}</label>
                            </div>
                        </div>
                    @else
                        @for($n = 0; $n < numberOfStudent($arrValue2[$w][$vehicle->id][$i]); $n++)
                            <div class="row mt-2">
                                <div class="col-12">
                                    <label id="student-name{{$n}}" class="modal-label-card float-left">ผู้เรียน : {{ getStudentAtN($arrValue2[$w][$vehicle->id][$i], $n)}}</label>
                                </div>
                            </div>
                        @endfor
                    @endif
                    <div class="row mt-4">
                        <div class="col-12">
                            <label id="module{{$vehicle->id.$w.$i}}" class="modal-label-card float-left">วิชา : {{ $arrModule2[$w][$vehicle->id][$i] }}</label>
                        </div>
                    </div>
                    <div class="row mt-2">
                        <div class="col-12">
                            <label class="modal-label-card float-left">เวลา : {{formatTimeFromDB($arrSchedule2[$w][$vehicle->id][$i]) . " ถึง " . getEndTimeFromHour($arrSchedule2[$w][$vehicle->id][$i], ($arrSpan2[$w][$vehicle->id][$i])/2 ) ." น."}}</label>
                        </div>
                    </div>
                    <div class="row mt-4">
                        <div class="col-12">
                            <label class="modal-label-card float-left">ผู้สอน : {{ getTeacherName($teachers, $arrTeacher2[$w][$vehicle->id][$i])}}</label>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-outline-secondary" data-dismiss="modal">ปิด</button>
                </div>
            </div>
        @endif
    </div>
</div>
<!-- End modal -->
