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

<div id="board-minutes-form-container" style="display: none;" class="mb-4"
     x-data="{
        agendas: [{ no: 1, title: '', result: '출석이사 전원 일치로 찬성하여 원안대로 승인 가결' }],
        signers: [
            { role: '의장 대표이사', name: '{{ $tenantInfo['ceo_name'] ?? '' }}' },
            { role: '사내이사', name: '' },
            { role: '감사', name: '' },
        ],
        addAgenda() {
            this.agendas.push({ no: this.agendas.length + 1, title: '', result: '출석이사 전원 일치로 찬성하여 원안대로 승인 가결' });
        },
        removeAgenda(idx) {
            if (this.agendas.length <= 1) return;
            this.agendas.splice(idx, 1);
            this.agendas.forEach((a, i) => a.no = i + 1);
        },
        addSigner() {
            this.signers.push({ role: '', name: '' });
        },
        removeSigner(idx) {
            if (this.signers.length <= 1) return;
            this.signers.splice(idx, 1);
        },
     }">
    <input type="hidden" id="bm-company-name" value="{{ $tenantInfo['company_name'] ?? '' }}">
    <input type="hidden" id="bm-business-num" value="{{ $tenantInfo['business_num'] ?? '' }}">
    <input type="hidden" id="bm-ceo-name" value="{{ $tenantInfo['ceo_name'] ?? '' }}">
    <input type="hidden" id="bm-company-address" value="{{ $tenantInfo['address'] ?? '' }}">

    <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 220px; max-width: 280px;">
                        <label class="block text-xs font-medium text-gray-500 mb-1">일시 <span class="text-red-500">*</span></label>
                        <input type="datetime-local" id="bm-meeting-datetime"
                               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 300px;">
                        <label class="block text-xs font-medium text-gray-500 mb-1">장소 <span class="text-red-500">*</span></label>
                        <input type="text" id="bm-meeting-place" 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>
        </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">
                <div class="flex gap-4 flex-wrap">
                    <div style="flex: 1 1 150px; max-width: 180px;">
                        <label class="block text-xs font-medium text-gray-500 mb-1">이사 총수 <span class="text-red-500">*</span></label>
                        <input type="number" id="bm-total-directors" min="1" value="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">
                    </div>
                    <div style="flex: 1 1 150px; max-width: 180px;">
                        <label class="block text-xs font-medium text-gray-500 mb-1">출석이사 <span class="text-red-500">*</span></label>
                        <input type="number" id="bm-present-directors" min="1" value="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">
                    </div>
                    <div style="flex: 1 1 150px; max-width: 180px;">
                        <label class="block text-xs font-medium text-gray-500 mb-1">감사 총수</label>
                        <input type="number" id="bm-total-auditors" min="0" value="1"
                               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 150px; max-width: 180px;">
                        <label class="block text-xs font-medium text-gray-500 mb-1">출석감사</label>
                        <input type="number" id="bm-present-auditors" min="0" value="1"
                               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>

        {{-- 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 flex items-center justify-between">
                <h3 class="text-sm font-semibold text-gray-700">3. 의안</h3>
                <button type="button" @click="addAgenda()"
                        class="px-2 py-1 bg-blue-50 text-blue-600 hover:bg-blue-100 border border-blue-200 rounded text-xs font-medium transition">+ 의안 추가</button>
            </div>
            <div class="p-4 space-y-3">
                <template x-for="(agenda, idx) in agendas" :key="idx">
                    <div class="flex gap-3 items-start border border-gray-100 rounded-lg p-3 bg-gray-50">
                        <div class="shrink-0 pt-6">
                            <span class="inline-flex items-center justify-center rounded-full bg-blue-100 text-blue-700 text-xs font-bold" style="width: 28px; height: 28px;" x-text="'#' + agenda.no"></span>
                        </div>
                        <div class="flex-1 space-y-2">
                            <div>
                                <label class="block text-xs font-medium text-gray-500 mb-1">의안 제목 <span class="text-red-500">*</span></label>
                                <input type="text" x-model="agenda.title" 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>
                                <label class="block text-xs font-medium text-gray-500 mb-1">결과</label>
                                <input type="text" x-model="agenda.result" 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>
                        <button type="button" @click="removeAgenda(idx)" x-show="agendas.length > 1"
                                class="shrink-0 mt-6 p-1 text-red-400 hover:text-red-600 transition">
                            <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="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"/>
                            </svg>
                        </button>
                    </div>
                </template>
            </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 space-y-3">
                <div style="max-width: 250px;">
                    <label class="block text-xs font-medium text-gray-500 mb-1">의장(대표이사) <span class="text-red-500">*</span></label>
                    <input type="text" id="bm-chairman-name" value="{{ $tenantInfo['ceo_name'] ?? '' }}" 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>
                    <label class="block text-xs font-medium text-gray-500 mb-1">의사 경과 내용</label>
                    <textarea id="bm-proceedings" rows="4"
                              placeholder="의장(대표이사 OOO)은 위와 같이 법정 수에 달하는 이사가 출석하였으므로 본 이사회가 적법하게 성립되었음을 선언하고, 다음의 의안을 상정하여 승인을 구하다."
                              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"></textarea>
                </div>
            </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">
                <div style="max-width: 180px;">
                    <label class="block text-xs font-medium text-gray-500 mb-1">폐회 시각</label>
                    <input type="time" id="bm-closing-time"
                           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>

        {{-- 6. 기명날인 (서명란) --}}
        <div class="border border-gray-200 rounded-lg overflow-hidden">
            <div class="bg-gray-50 px-4 py-2 border-b border-gray-200 flex items-center justify-between">
                <h3 class="text-sm font-semibold text-gray-700">6. 기명날인</h3>
                <button type="button" @click="addSigner()"
                        class="px-2 py-1 bg-blue-50 text-blue-600 hover:bg-blue-100 border border-blue-200 rounded text-xs font-medium transition">+ 서명자 추가</button>
            </div>
            <div class="p-4 space-y-2">
                <template x-for="(signer, idx) in signers" :key="idx">
                    <div class="flex gap-3 items-center">
                        <div style="flex: 0 0 160px;">
                            <input type="text" x-model="signer.role" 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 200px; max-width: 250px;">
                            <input type="text" x-model="signer.name" 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>
                        <span class="text-xs text-gray-400 shrink-0">(인)</span>
                        <button type="button" @click="removeSigner(idx)" x-show="signers.length > 1"
                                class="shrink-0 p-1 text-red-400 hover:text-red-600 transition">
                            <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="M6 18L18 6M6 6l12 12"/>
                            </svg>
                        </button>
                    </div>
                </template>
            </div>
        </div>

        {{-- 미리보기 버튼 --}}
        <div class="flex justify-end">
            <button type="button" onclick="openBoardMinutesPreview()"
                    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="board-minutes-preview-modal" style="display: none;" class="fixed inset-0 z-50">
    <div class="absolute inset-0 bg-black/50" onclick="closeBoardMinutesPreview()"></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: 700px;">
            <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="printBoardMinutesPreview()"
                            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="closeBoardMinutesPreview()"
                            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="board-minutes-preview-content" style="padding: 48px 56px; font-family: 'Pretendard', 'Malgun Gothic', sans-serif;">
                </div>
            </div>
        </div>
    </div>
</div>
