{{--
    위임장 읽기전용 렌더링
    Props:
        $content (array) - approvals.content JSON
--}}
<div class="space-y-4">
    {{-- 미리보기 버튼 --}}
    <div class="flex justify-end gap-2">
        <button type="button" onclick="openDelegationShowPreview()"
                class="px-3 py-1.5 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>

    {{-- 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">
            <div class="grid gap-3" style="grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));">
                <div>
                    <span class="text-xs text-gray-500">법인명</span>
                    <div class="text-sm font-medium mt-0.5">{{ $content['company_name'] ?? '-' }}</div>
                </div>
                <div>
                    <span class="text-xs text-gray-500">사업자등록번호</span>
                    <div class="text-sm font-medium mt-0.5">{{ $content['business_num'] ?? '-' }}</div>
                </div>
                <div style="grid-column: 1 / -1;">
                    <span class="text-xs text-gray-500">주소</span>
                    <div class="text-sm font-medium mt-0.5">{{ $content['company_address'] ?? '-' }}</div>
                </div>
                <div>
                    <span class="text-xs text-gray-500">대표자</span>
                    <div class="text-sm font-medium mt-0.5">{{ $content['ceo_name'] ?? '-' }}</div>
                </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="grid gap-3" style="grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));">
                <div>
                    <span class="text-xs text-gray-500">성명</span>
                    <div class="text-sm font-medium mt-0.5">{{ $content['agent_name'] ?? '-' }}</div>
                </div>
                <div>
                    <span class="text-xs text-gray-500">생년월일</span>
                    <div class="text-sm font-medium mt-0.5">{{ $content['agent_birth'] ?? '-' }}</div>
                </div>
                <div style="grid-column: 1 / -1;">
                    <span class="text-xs text-gray-500">주소</span>
                    <div class="text-sm font-medium mt-0.5">{{ $content['agent_address'] ?? '-' }}</div>
                </div>
                <div>
                    <span class="text-xs text-gray-500">연락처</span>
                    <div class="text-sm font-medium mt-0.5">{{ $content['agent_phone'] ?? '-' }}</div>
                </div>
                <div>
                    <span class="text-xs text-gray-500">소속</span>
                    <div class="text-sm font-medium mt-0.5">{{ $content['agent_department'] ?? '-' }}</div>
                </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">
            <h3 class="text-sm font-semibold text-gray-700">3. 위임사항</h3>
        </div>
        <div class="p-4">
            <div class="text-sm text-gray-700 whitespace-pre-wrap">{{ $content['delegation_detail'] ?? '-' }}</div>
        </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">
            <div class="text-sm font-medium">{{ $content['period_start'] ?? '-' }} ~ {{ $content['period_end'] ?? '-' }}</div>
        </div>
    </div>

    {{-- 5. 첨부서류 --}}
    @if(!empty($content['attachments_desc']))
    <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 class="text-sm text-gray-700 whitespace-pre-wrap">{{ $content['attachments_desc'] }}</div>
        </div>
    </div>
    @endif
</div>

{{-- 미리보기 모달 --}}
<div id="delegation-show-preview-modal" style="display: none;" class="fixed inset-0 z-50">
    <div class="absolute inset-0 bg-black/50" onclick="closeDelegationShowPreview()"></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="printDelegationShowPreview()"
                            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="closeDelegationShowPreview()"
                            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="delegation-show-preview-content" style="padding: 48px 56px; font-family: 'Pretendard', 'Malgun Gothic', sans-serif;">
                </div>
            </div>
        </div>
    </div>
</div>

@push('scripts')
<script>
function buildDelegationShowPreviewHtml() {
    const data = @json($content);
    return `
        <div style="text-align: center; margin-bottom: 36px;">
            <h1 style="font-size: 28px; font-weight: 800; letter-spacing: 8px;">위 임 장</h1>
        </div>
        <table style="width: 100%; border-collapse: collapse; font-size: 13px; margin-bottom: 24px;">
            <tr>
                <td style="border: 1px solid #333; background: #f5f5f5; padding: 8px 12px; font-weight: 600; width: 100px; text-align: center;" rowspan="4">위임인</td>
                <td style="border: 1px solid #333; background: #f5f5f5; padding: 8px 12px; font-weight: 600; width: 100px; text-align: center;">법인명</td>
                <td style="border: 1px solid #333; padding: 8px 12px;" colspan="3">${data.company_name || ''}</td>
            </tr>
            <tr>
                <td style="border: 1px solid #333; background: #f5f5f5; padding: 8px 12px; font-weight: 600; text-align: center;">사업자등록번호</td>
                <td style="border: 1px solid #333; padding: 8px 12px;" colspan="3">${data.business_num || ''}</td>
            </tr>
            <tr>
                <td style="border: 1px solid #333; background: #f5f5f5; padding: 8px 12px; font-weight: 600; text-align: center;">주소</td>
                <td style="border: 1px solid #333; padding: 8px 12px;" colspan="3">${data.company_address || ''}</td>
            </tr>
            <tr>
                <td style="border: 1px solid #333; background: #f5f5f5; padding: 8px 12px; font-weight: 600; text-align: center;">대표자</td>
                <td style="border: 1px solid #333; padding: 8px 12px;" colspan="3">${data.ceo_name || ''}</td>
            </tr>
            <tr>
                <td style="border: 1px solid #333; background: #f5f5f5; padding: 8px 12px; font-weight: 600; text-align: center;" rowspan="5">수임인</td>
                <td style="border: 1px solid #333; background: #f5f5f5; padding: 8px 12px; font-weight: 600; text-align: center;">성명</td>
                <td style="border: 1px solid #333; padding: 8px 12px;" colspan="3">${data.agent_name || ''}</td>
            </tr>
            <tr>
                <td style="border: 1px solid #333; background: #f5f5f5; padding: 8px 12px; font-weight: 600; text-align: center;">생년월일</td>
                <td style="border: 1px solid #333; padding: 8px 12px;" colspan="3">${data.agent_birth || ''}</td>
            </tr>
            <tr>
                <td style="border: 1px solid #333; background: #f5f5f5; padding: 8px 12px; font-weight: 600; text-align: center;">주소</td>
                <td style="border: 1px solid #333; padding: 8px 12px;" colspan="3">${data.agent_address || ''}</td>
            </tr>
            <tr>
                <td style="border: 1px solid #333; background: #f5f5f5; padding: 8px 12px; font-weight: 600; text-align: center;">연락처</td>
                <td style="border: 1px solid #333; padding: 8px 12px;" colspan="3">${data.agent_phone || ''}</td>
            </tr>
            <tr>
                <td style="border: 1px solid #333; background: #f5f5f5; padding: 8px 12px; font-weight: 600; text-align: center;">소속</td>
                <td style="border: 1px solid #333; padding: 8px 12px;" colspan="3">${data.agent_department || ''}</td>
            </tr>
        </table>
        <div style="margin-bottom: 20px;">
            <div style="font-weight: 600; font-size: 14px; margin-bottom: 8px; border-bottom: 2px solid #333; padding-bottom: 4px;">위임사항</div>
            <div style="font-size: 13px; line-height: 1.8; white-space: pre-wrap; padding: 8px 4px;">${data.delegation_detail || ''}</div>
        </div>
        <div style="margin-bottom: 20px;">
            <div style="font-weight: 600; font-size: 14px; margin-bottom: 8px; border-bottom: 2px solid #333; padding-bottom: 4px;">위임기간</div>
            <div style="font-size: 13px; padding: 8px 4px;">${data.period_start || ''} ~ ${data.period_end || ''}</div>
        </div>
        ${data.attachments_desc ? `
        <div style="margin-bottom: 20px;">
            <div style="font-weight: 600; font-size: 14px; margin-bottom: 8px; border-bottom: 2px solid #333; padding-bottom: 4px;">첨부서류</div>
            <div style="font-size: 13px; line-height: 1.8; white-space: pre-wrap; padding: 8px 4px;">${data.attachments_desc}</div>
        </div>` : ''}
        <div style="text-align: center; margin-top: 40px; font-size: 13px;">
            <p>위와 같이 권한을 위임합니다.</p>
            <p style="margin-top: 24px; font-size: 14px; font-weight: 600;">${data.company_name || ''}</p>
            <p style="margin-top: 4px;">대표이사 ${data.ceo_name || ''} (인)</p>
        </div>
    `;
}

function openDelegationShowPreview() {
    document.getElementById('delegation-show-preview-content').innerHTML = buildDelegationShowPreviewHtml();
    document.getElementById('delegation-show-preview-modal').style.display = '';
}
function closeDelegationShowPreview() {
    document.getElementById('delegation-show-preview-modal').style.display = 'none';
}
function printDelegationShowPreview() {
    const content = document.getElementById('delegation-show-preview-content').innerHTML;
    const win = window.open('', '_blank');
    win.document.write('<html><head><title>위임장</title><style>body{font-family:"Pretendard","Malgun Gothic",sans-serif;padding:48px 56px;}@media print{body{padding:20px 30px;}}</style></head><body>' + content + '</body></html>');
    win.document.close();
    win.print();
}
</script>
@endpush
