{{-- 휴가/근태신청/사유서 통합 모달 --}}
<div id="leave-modal" class="hidden fixed inset-0 z-50 flex items-center justify-center">
    {{-- 배경 오버레이 --}}
    <div class="fixed inset-0 bg-black/50" onclick="closeLeaveModal()"></div>

    {{-- 모달 본문 --}}
    <div class="relative bg-white rounded-xl shadow-2xl w-full max-w-lg mx-4 max-h-[90vh] overflow-y-auto">
        <div class="flex items-center justify-between px-6 py-4 border-b border-gray-200">
            <h3 class="text-lg font-semibold text-gray-800">
                <span id="modal-type-label">신청</span>
            </h3>
            <button onclick="closeLeaveModal()" class="p-1 hover:bg-gray-100 rounded-lg transition-colors">
                <svg class="w-5 h-5 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"/>
                </svg>
            </button>
        </div>

        <form id="leave-form" onsubmit="event.preventDefault(); submitLeave();">
            <input type="hidden" id="modal-leave-type" name="leave_type">

            <div class="px-6 py-4 space-y-4">
                {{-- 신청자 --}}
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">신청자 <span class="text-red-500">*</span></label>
                    <select id="modal-user-id" name="user_id" required onchange="onUserChange()"
                            class="w-full px-3 py-2 text-sm border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
                        <option value="">선택하세요</option>
                        @foreach($employees as $emp)
                        <option value="{{ $emp->user_id }}">{{ $emp->display_name }}</option>
                        @endforeach
                    </select>
                </div>

                {{-- 잔여연차 (연차 차감 대상만) --}}
                <div id="balance-info-row" style="display:none;">
                    <label class="block text-sm font-medium text-gray-700 mb-1">잔여연차</label>
                    <p id="balance-remaining" class="text-sm text-blue-600 font-medium">-</p>
                </div>

                {{-- 시작일 --}}
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">
                        <span class="reason-label">날짜</span> <span class="text-red-500">*</span>
                    </label>
                    <input type="date" id="modal-start-date" name="start_date" required
                           class="w-full px-3 py-2 text-sm border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
                </div>

                {{-- 종료일 --}}
                <div id="end-date-row">
                    <label class="block text-sm font-medium text-gray-700 mb-1">종료일 <span class="text-red-500">*</span></label>
                    <input type="date" id="modal-end-date" name="end_date"
                           class="w-full px-3 py-2 text-sm border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
                </div>

                {{-- 일수 --}}
                <div id="days-info">
                    <p class="text-xs text-gray-500">일수는 영업일 기준 자동 계산됩니다.</p>
                </div>

                {{-- 사유 --}}
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">사유</label>
                    <textarea id="modal-reason" name="reason" rows="3" placeholder="사유를 입력하세요..."
                              class="w-full px-3 py-2 text-sm border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 resize-none"></textarea>
                </div>

                {{-- 결재선 선택 --}}
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">결재선</label>
                    <select id="modal-approval-line" name="approval_line_id"
                            class="w-full px-3 py-2 text-sm border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
                        <option value="">기본결재선 사용</option>
                        @foreach($approvalLines as $line)
                        <option value="{{ $line->id }}" @if($line->is_default) selected @endif>{{ $line->name }}</option>
                        @endforeach
                    </select>
                </div>
            </div>

            <div class="flex items-center justify-end gap-2 px-6 py-4 border-t border-gray-200 bg-gray-50 rounded-b-xl">
                <button type="button" onclick="closeLeaveModal()"
                        class="px-4 py-2 text-sm font-medium text-gray-700 bg-white border border-gray-300 rounded-lg hover:bg-gray-50 transition-colors">
                    취소
                </button>
                <button type="submit"
                        class="px-4 py-2 text-sm font-medium text-white bg-blue-600 rounded-lg hover:bg-blue-700 transition-colors">
                    신청 (결재 자동 생성)
                </button>
            </div>
        </form>
    </div>
</div>
