{{--
    공문서 전용 폼
    Props:
        $tenantInfo (array) - 테넌트(회사) 정보
--}}
@php
    $tenantInfo = $tenantInfo ?? [];
@endphp

<div id="official-letter-form-container" style="display: none;" class="mb-4">
    <input type="hidden" id="ol-company-name" value="{{ $tenantInfo['company_name'] ?? '' }}">
    <input type="hidden" id="ol-business-num" value="{{ $tenantInfo['business_num'] ?? '' }}">
    <input type="hidden" id="ol-ceo-name" value="{{ $tenantInfo['ceo_name'] ?? '' }}">
    <input type="hidden" id="ol-company-address" value="{{ $tenantInfo['address'] ?? '' }}">
    <input type="hidden" id="ol-phone" value="{{ $tenantInfo['phone'] ?? '' }}">
    <input type="hidden" id="ol-fax" value="{{ $tenantInfo['fax'] ?? '' }}">
    <input type="hidden" id="ol-email" value="{{ $tenantInfo['email'] ?? '' }}">

    <div class="space-y-4">
        {{-- 1. 문서 정보 --}}
        <div class="border border-gray-200 rounded-lg overflow-hidden">
            <div class="bg-gray-50 px-4 py-2 border-b border-gray-200">
                <h3 class="text-sm font-semibold text-gray-700">1. 문서 정보</h3>
            </div>
            <div class="p-4 space-y-3">
                <div class="flex gap-4 flex-wrap">
                    <div style="flex: 1 1 250px; max-width: 300px;">
                        <label class="block text-xs font-medium text-gray-500 mb-1">문서번호</label>
                        <input type="text" id="ol-doc-number" placeholder="예: 2026030601"
                               class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-blue-500">
                    </div>
                    <div style="flex: 0 0 180px;">
                        <label class="block text-xs font-medium text-gray-500 mb-1">일자 <span class="text-red-500">*</span></label>
                        <input type="date" id="ol-doc-date" value="{{ now()->format('Y-m-d') }}"
                               class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-blue-500">
                    </div>
                </div>
            </div>
        </div>

        {{-- 2. 수신/참조/제목 --}}
        <div class="border border-gray-200 rounded-lg overflow-hidden">
            <div class="bg-gray-50 px-4 py-2 border-b border-gray-200">
                <h3 class="text-sm font-semibold text-gray-700">2. 수신 정보</h3>
            </div>
            <div class="p-4 space-y-3">
                <div class="flex gap-4 flex-wrap">
                    <div style="flex: 1 1 300px;">
                        <label class="block text-xs font-medium text-gray-500 mb-1">수신 <span class="text-red-500">*</span></label>
                        <input type="text" id="ol-recipient" value="㈜경동" placeholder="수신처"
                               class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-blue-500">
                    </div>
                    <div style="flex: 1 1 250px;">
                        <label class="block text-xs font-medium text-gray-500 mb-1">참조</label>
                        <input type="text" id="ol-reference" placeholder="참조 (선택사항)"
                               class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-blue-500">
                    </div>
                </div>
                <div>
                    <label class="block text-xs font-medium text-gray-500 mb-1">제목 <span class="text-red-500">*</span></label>
                    <input type="text" id="ol-subject" value="기성실적증명서 도장 날인 협조요청" placeholder="공문서 제목"
                           class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-blue-500">
                </div>
            </div>
        </div>

        {{-- 3. 본문 --}}
        <div class="border border-gray-200 rounded-lg overflow-hidden">
            <div class="bg-gray-50 px-4 py-2 border-b border-gray-200">
                <h3 class="text-sm font-semibold text-gray-700">3. 본문</h3>
            </div>
            <div class="p-4">
                <textarea id="ol-body" rows="10"
                          class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-blue-500 leading-relaxed">1. 귀사의 무궁한 발전을 기원합니다.

2. 당사에서 진행하는 『000000설치공사』와 관련하여 실적증명 확인을 요청하오니, 아래의 기재 내역을 확인하시고 3월 15일까지 회신 부탁드립니다.

--  아      래 --

가. 공사명 : 000000 신규 설치
나. 발주처 : ㈜진성
다. 공사금액 :
라. 협조사항 : 건설공사 기성실적증명서 3부를 송부하오니 도장 날인하여 1부는 보관하시고 2부는 동봉해드리는 반송 우편 봉투에 담아 송부.</textarea>
            </div>
        </div>

        {{-- 4. 붙임 --}}
        <div class="border border-gray-200 rounded-lg overflow-hidden">
            <div class="bg-gray-50 px-4 py-2 border-b border-gray-200">
                <h3 class="text-sm font-semibold text-gray-700">4. 붙임 (첨부서류)</h3>
            </div>
            <div class="p-4">
                <textarea id="ol-attachments-desc" rows="3"
                          class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-blue-500">1. 기성실적증명서
   - 귀사 보관용 1부.
   - 당사 회신용 1부.
2. 실적증명서류
   - 세금계산서 사본 1부
   - 계약서 사본 1부. 끝</textarea>
            </div>
        </div>

        {{-- 5. 발신자 정보 (테넌트 자동) --}}
        <div class="border border-gray-200 rounded-lg overflow-hidden">
            <div class="bg-gray-50 px-4 py-2 border-b border-gray-200">
                <h3 class="text-sm font-semibold text-gray-700">5. 발신자</h3>
            </div>
            <div class="p-4 space-y-3">
                <div class="flex gap-4 flex-wrap">
                    <div style="flex: 1 1 200px;">
                        <label class="block text-xs font-medium text-gray-500 mb-1">상호</label>
                        <input type="text" readonly value="{{ $tenantInfo['company_name'] ?? '' }}"
                               class="w-full px-3 py-2 border border-gray-200 rounded-lg text-sm bg-gray-50 text-gray-700">
                    </div>
                    <div style="flex: 0 0 150px;">
                        <label class="block text-xs font-medium text-gray-500 mb-1">대표이사</label>
                        <input type="text" readonly value="{{ $tenantInfo['ceo_name'] ?? '' }}"
                               class="w-full px-3 py-2 border border-gray-200 rounded-lg text-sm bg-gray-50 text-gray-700">
                    </div>
                </div>
                <div>
                    <label class="block text-xs font-medium text-gray-500 mb-1">주소</label>
                    <input type="text" readonly value="{{ $tenantInfo['address'] ?? '' }}"
                           class="w-full px-3 py-2 border border-gray-200 rounded-lg text-sm bg-gray-50 text-gray-700">
                </div>
                <div class="flex gap-4 flex-wrap">
                    <div style="flex: 1 1 180px; max-width: 220px;">
                        <label class="block text-xs font-medium text-gray-500 mb-1">전화</label>
                        <input type="text" id="ol-phone-input" value="{{ $tenantInfo['phone'] ?? '' }}" placeholder="전화번호"
                               class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-blue-500">
                    </div>
                    <div style="flex: 1 1 180px; max-width: 220px;">
                        <label class="block text-xs font-medium text-gray-500 mb-1">팩스</label>
                        <input type="text" id="ol-fax-input" value="{{ $tenantInfo['fax'] ?? '' }}" placeholder="팩스번호"
                               class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-blue-500">
                    </div>
                    <div style="flex: 1 1 220px;">
                        <label class="block text-xs font-medium text-gray-500 mb-1">이메일</label>
                        <input type="text" id="ol-email-input" value="{{ $tenantInfo['email'] ?? '' }}" placeholder="이메일"
                               class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-blue-500">
                    </div>
                </div>
            </div>
        </div>

        {{-- 미리보기 버튼 --}}
        <div class="flex justify-end">
            <button type="button" onclick="openOfficialLetterPreview()"
                    class="px-3 py-2 bg-indigo-50 text-indigo-600 hover:bg-indigo-100 border border-indigo-200 rounded-lg text-sm font-medium transition inline-flex items-center gap-1">
                <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"/>
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"/>
                </svg>
                미리보기
            </button>
        </div>
    </div>
</div>

{{-- 공문서 미리보기 모달 --}}
<div id="official-letter-preview-modal" style="display: none;" class="fixed inset-0 z-50">
    <div class="absolute inset-0 bg-black/50" onclick="closeOfficialLetterPreview()"></div>
    <div class="relative flex items-center justify-center min-h-full p-4">
        <div class="bg-white rounded-xl shadow-2xl w-full overflow-hidden relative" style="max-width: 780px;">
            <div class="flex items-center justify-between px-5 py-3 border-b border-gray-200 bg-gray-50">
                <h3 class="text-base font-semibold text-gray-800">공문서 미리보기</h3>
                <div class="flex items-center gap-2">
                    <button type="button" onclick="printOfficialLetterPreview()"
                            class="px-3 py-1.5 bg-white border border-gray-300 hover:bg-gray-50 rounded-lg text-xs font-medium transition inline-flex items-center gap-1">
                        <svg class="w-3.5 h-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 17h2a2 2 0 002-2v-4a2 2 0 00-2-2H5a2 2 0 00-2 2v4a2 2 0 002 2h2m2 4h6a2 2 0 002-2v-4a2 2 0 00-2-2H9a2 2 0 00-2 2v4a2 2 0 002 2zm8-12V5a2 2 0 00-2-2H9a2 2 0 00-2 2v4h10z"/>
                        </svg>
                        인쇄
                    </button>
                    <button type="button" onclick="closeOfficialLetterPreview()"
                            class="p-1 text-gray-400 hover:text-gray-600 transition">
                        <svg class="w-5 h-5" 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>
            </div>
            <div class="overflow-y-auto" style="max-height: 80vh;">
                <div id="official-letter-preview-content" style="padding: 40px 48px; font-family: 'Pretendard', 'Malgun Gothic', sans-serif;"></div>
            </div>
        </div>
    </div>
</div>
