@extends('layouts.app')

@section('title', '신용평가 조회')

@section('content')
<div class="flex flex-col h-full">
    <!-- 페이지 헤더 -->
    <div class="flex flex-col sm:flex-row sm:justify-between sm:items-center gap-4 mb-6 flex-shrink-0">
        <div>
            <h1 class="text-2xl font-bold text-gray-800">신용평가 조회</h1>
            <p class="text-sm text-gray-500 mt-1">기업 신용평가 조회 이력을 관리합니다</p>
        </div>
        <div class="flex gap-2">
            <button type="button"
                    id="btnNewInquiry"
                    class="inline-flex items-center gap-2 px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition disabled:opacity-50"
                    {{ !$hasConfig ? 'disabled' : '' }}>
                <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="M12 4v16m8-8H4" />
                </svg>
                신규 조회
            </button>
            <a href="{{ route('credit.settings.index') }}"
               class="inline-flex items-center gap-2 px-4 py-2 border border-gray-300 text-gray-700 rounded-lg hover:bg-gray-50 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="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z" />
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z" />
                </svg>
                API 설정
            </a>
        </div>
    </div>

    @if(!$hasConfig)
    <!-- API 설정 없음 경고 -->
    <div class="bg-yellow-50 border border-yellow-200 rounded-lg p-4 mb-6 flex-shrink-0">
        <div class="flex items-center gap-3">
            <svg class="w-6 h-6 text-yellow-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z" />
            </svg>
            <div>
                <p class="font-medium text-yellow-800">쿠콘 API 설정이 필요합니다</p>
                <p class="text-sm text-yellow-700">신용평가 조회를 위해 먼저 <a href="{{ route('credit.settings.index') }}" class="underline font-medium">API 설정</a>을 등록해주세요.</p>
            </div>
        </div>
    </div>
    @endif

    <!-- 검색 필터 -->
    <div class="bg-white rounded-lg shadow-sm p-4 mb-6 flex-shrink-0">
        <form method="GET" action="{{ route('credit.inquiry.index') }}" class="flex flex-wrap gap-4 items-end">
            <div class="min-w-[180px]">
                <label class="block text-sm font-medium text-gray-700 mb-1">사업자번호</label>
                <input type="text"
                       name="company_key"
                       value="{{ $filters['company_key'] ?? '' }}"
                       placeholder="000-00-00000"
                       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 class="min-w-[140px]">
                <label class="block text-sm font-medium text-gray-700 mb-1">시작일</label>
                <input type="date"
                       name="start_date"
                       value="{{ $filters['start_date'] ?? '' }}"
                       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 class="min-w-[140px]">
                <label class="block text-sm font-medium text-gray-700 mb-1">종료일</label>
                <input type="date"
                       name="end_date"
                       value="{{ $filters['end_date'] ?? '' }}"
                       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 class="flex items-center gap-2">
                <label class="flex items-center gap-2 cursor-pointer">
                    <input type="checkbox"
                           name="has_issue"
                           value="1"
                           {{ ($filters['has_issue'] ?? false) ? 'checked' : '' }}
                           class="w-4 h-4 text-blue-600 border-gray-300 rounded focus:ring-blue-500">
                    <span class="text-sm text-gray-700">이슈 있는 것만</span>
                </label>
            </div>
            <div class="flex gap-2">
                <button type="submit" class="px-4 py-2 bg-gray-800 text-white rounded-lg text-sm hover:bg-gray-700 transition">
                    검색
                </button>
                <a href="{{ route('credit.inquiry.index') }}" class="px-4 py-2 border border-gray-300 text-gray-700 rounded-lg text-sm hover:bg-gray-50 transition">
                    초기화
                </a>
            </div>
        </form>
    </div>

    <!-- 조회 이력 테이블 -->
    <div class="bg-white rounded-lg shadow-sm flex-1 min-h-0 flex flex-col">
        <div class="overflow-auto flex-1">
            <table class="w-full text-sm">
                <thead class="bg-gray-50 sticky top-0">
                    <tr>
                        <th class="px-4 py-3 text-left font-medium text-gray-700">사업자번호</th>
                        <th class="px-4 py-3 text-left font-medium text-gray-700">업체정보</th>
                        <th class="px-4 py-3 text-center font-medium text-gray-700">국세청</th>
                        <th class="px-4 py-3 text-left font-medium text-gray-700">조회일시</th>
                        <th class="px-4 py-3 text-center font-medium text-gray-700">API상태</th>
                        <th class="px-4 py-3 text-center font-medium text-gray-700">이슈</th>
                        <th class="px-4 py-3 text-left font-medium text-gray-700">조회자</th>
                        <th class="px-4 py-3 text-right font-medium text-gray-700">액션</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-100">
                    @forelse($inquiries as $inquiry)
                    <tr class="hover:bg-gray-50" data-inquiry-key="{{ $inquiry->inquiry_key }}">
                        <td class="px-4 py-3 font-mono font-medium">{{ $inquiry->formatted_company_key }}</td>
                        <td class="px-4 py-3">
                            <div class="text-sm font-medium text-gray-900">{{ $inquiry->company_name ?? '-' }}</div>
                            @if($inquiry->ceo_name)
                            <div class="text-xs text-gray-500">대표: {{ $inquiry->ceo_name }}</div>
                            @endif
                        </td>
                        <td class="px-4 py-3 text-center">
                            @if($inquiry->nts_status_code === '01')
                                <span class="px-2 py-1 text-xs font-medium bg-green-100 text-green-700 rounded">{{ $inquiry->nts_status_label }}</span>
                            @elseif($inquiry->nts_status_code === '02')
                                <span class="px-2 py-1 text-xs font-medium bg-yellow-100 text-yellow-700 rounded">{{ $inquiry->nts_status_label }}</span>
                            @elseif($inquiry->nts_status_code === '03')
                                <span class="px-2 py-1 text-xs font-medium bg-red-100 text-red-700 rounded">{{ $inquiry->nts_status_label }}</span>
                            @else
                                <span class="px-2 py-1 text-xs font-medium bg-gray-100 text-gray-500 rounded">{{ $inquiry->nts_status_label }}</span>
                            @endif
                        </td>
                        <td class="px-4 py-3 text-gray-600">{{ $inquiry->inquired_at->format('Y-m-d H:i') }}</td>
                        <td class="px-4 py-3 text-center">
                            @if($inquiry->status === 'success')
                                <span class="px-2 py-1 text-xs font-medium bg-green-100 text-green-700 rounded">성공</span>
                            @elseif($inquiry->status === 'partial')
                                <span class="px-2 py-1 text-xs font-medium bg-yellow-100 text-yellow-700 rounded">부분</span>
                            @else
                                <span class="px-2 py-1 text-xs font-medium bg-red-100 text-red-700 rounded">실패</span>
                            @endif
                        </td>
                        <td class="px-4 py-3 text-center">
                            @if($inquiry->has_issue)
                                <span class="px-2 py-1 text-xs font-medium bg-red-100 text-red-700 rounded">{{ $inquiry->total_issue_count }}건</span>
                            @else
                                <span class="px-2 py-1 text-xs font-medium bg-green-100 text-green-700 rounded">없음</span>
                            @endif
                        </td>
                        <td class="px-4 py-3 text-gray-600">{{ $inquiry->user?->name ?? '-' }}</td>
                        <td class="px-4 py-3 text-right">
                            <div class="flex gap-1 justify-end">
                                <button type="button"
                                        class="btn-raw-data px-3 py-1.5 text-xs bg-gray-100 text-gray-700 rounded hover:bg-gray-200 transition"
                                        data-inquiry-key="{{ $inquiry->inquiry_key }}">
                                    원본
                                </button>
                                <button type="button"
                                        class="btn-report px-3 py-1.5 text-xs bg-blue-100 text-blue-700 rounded hover:bg-blue-200 transition"
                                        data-inquiry-key="{{ $inquiry->inquiry_key }}">
                                    리포트
                                </button>
                                <button type="button"
                                        class="btn-delete px-3 py-1.5 text-xs bg-red-100 text-red-700 rounded hover:bg-red-200 transition"
                                        data-inquiry-id="{{ $inquiry->id }}">
                                    삭제
                                </button>
                            </div>
                        </td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="8" class="px-4 py-12 text-center text-gray-500">
                            <svg class="w-12 h-12 text-gray-300 mx-auto mb-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z" />
                            </svg>
                            <p>조회 이력이 없습니다.</p>
                            <p class="text-sm mt-1">신규 조회 버튼을 눌러 신용평가를 조회하세요.</p>
                        </td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

        <!-- 페이지네이션 -->
        @if($inquiries->hasPages())
        <div class="px-4 py-3 border-t">
            {{ $inquiries->links() }}
        </div>
        @endif
    </div>
</div>

<!-- 신규 조회 모달 -->
<div id="newInquiryModal" class="fixed inset-0 bg-black bg-opacity-50 hidden z-50 flex items-center justify-center">
    <div class="bg-white rounded-lg shadow-xl w-full max-w-md mx-4">
        <div class="flex items-center justify-between p-4 border-b">
            <h3 class="text-lg font-semibold text-gray-800">신규 신용평가 조회</h3>
            <button type="button" class="modal-close text-gray-400 hover:text-gray-600">
                <svg class="w-6 h-6" 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 class="p-4">
            <form id="newInquiryForm">
                <div class="mb-4">
                    <label class="block text-sm font-medium text-gray-700 mb-1">사업자번호 / 법인번호 <span class="text-red-500">*</span></label>
                    <input type="text"
                           id="newCompanyKey"
                           placeholder="000-00-00000"
                           maxlength="12"
                           class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 font-mono">
                    <p class="text-xs text-gray-500 mt-1">숫자만 입력하면 자동으로 형식이 적용됩니다</p>
                </div>
                <div class="flex gap-2">
                    <button type="submit"
                            id="btnSubmitInquiry"
                            class="flex-1 bg-blue-600 text-white py-2 rounded-lg hover:bg-blue-700 transition flex items-center justify-center gap-2">
                        <span id="submitText">조회 실행</span>
                        <svg id="submitSpinner" class="w-5 h-5 animate-spin hidden" fill="none" viewBox="0 0 24 24">
                            <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
                            <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
                        </svg>
                    </button>
                    <button type="button" class="modal-close px-4 py-2 border border-gray-300 text-gray-700 rounded-lg hover:bg-gray-50 transition">
                        취소
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- 원본 데이터 모달 -->
<div id="rawDataModal" class="fixed inset-0 bg-black bg-opacity-50 hidden z-50 overflow-y-auto py-8">
    <div class="flex items-start justify-center min-h-full">
    <div class="bg-white rounded-lg shadow-xl w-full max-w-6xl mx-4 max-h-[80vh] flex flex-col">
        <div class="flex items-center justify-between p-4 border-b flex-shrink-0">
            <div>
                <h3 class="text-lg font-semibold text-gray-800">원본 데이터</h3>
                <p class="text-sm text-gray-500" id="rawDataSubtitle"></p>
            </div>
            <button type="button" class="modal-close text-gray-400 hover:text-gray-600">
                <svg class="w-6 h-6" 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 class="p-4 overflow-auto flex-1" id="rawDataContent">
            <!-- 원본 데이터 렌더링 -->
        </div>
    </div>
    </div>
</div>

<!-- 리포트 모달 -->
<div id="reportModal" class="fixed inset-0 bg-black bg-opacity-50 hidden z-50 overflow-y-auto py-8">
    <div class="flex items-start justify-center min-h-full">
    <div class="bg-stone-50 rounded-lg shadow-xl w-full max-w-6xl mx-4 max-h-[80vh] flex flex-col">
        <div class="flex items-center justify-between p-4 border-b bg-white flex-shrink-0 rounded-t-lg">
            <div class="flex items-center gap-3">
                <div class="w-10 h-10 bg-blue-600 rounded-lg flex items-center justify-center text-white font-bold text-xl shadow-sm">S</div>
                <div>
                    <h3 class="text-lg font-bold text-gray-900">SAM 기업 신용분석 리포트</h3>
                    <p class="text-sm text-gray-500" id="reportSubtitle"></p>
                </div>
            </div>
            <div class="flex items-center gap-2">
                <button type="button" id="btnPrintReport" class="px-3 py-1.5 text-sm bg-gray-100 text-gray-700 rounded-lg hover:bg-gray-200 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="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" class="modal-close text-gray-400 hover:text-gray-600 p-1">
                    <svg class="w-6 h-6" 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="p-6 overflow-auto flex-1" id="reportContent">
            <!-- 리포트 렌더링 -->
        </div>
    </div>
    </div>
</div>
@endsection

@push('styles')
<style>
    .traffic-light { width: 16px; height: 16px; border-radius: 50%; display: inline-block; }
    .light-red { background-color: #ef4444; box-shadow: 0 0 8px #ef4444; }
    .light-orange { background-color: #f97316; box-shadow: 0 0 8px #f97316; }
    .light-yellow { background-color: #eab308; box-shadow: 0 0 8px #eab308; }
    .light-green { background-color: #22c55e; box-shadow: 0 0 8px #22c55e; }
    .light-dim { opacity: 0.2; }
    .report-tab-active { border-bottom: 2px solid #3b82f6; color: #3b82f6; font-weight: 600; }
    .report-tab-inactive { color: #78716c; }
    .report-tab-inactive:hover { color: #44403c; }
    .chart-container { position: relative; height: 200px; }
</style>
@endpush

@push('scripts')
<script>
document.addEventListener('DOMContentLoaded', function() {
    const csrfToken = document.querySelector('meta[name="csrf-token"]').content;

    // 모달 요소들
    const newInquiryModal = document.getElementById('newInquiryModal');
    const rawDataModal = document.getElementById('rawDataModal');
    const reportModal = document.getElementById('reportModal');

    // 모달 닫기 버튼들
    document.querySelectorAll('.modal-close').forEach(btn => {
        btn.addEventListener('click', function() {
            this.closest('.fixed').classList.add('hidden');
        });
    });

    // ESC 키로 모달 닫기
    document.addEventListener('keydown', function(e) {
        if (e.key === 'Escape') {
            document.querySelectorAll('.fixed:not(.hidden)').forEach(modal => {
                modal.classList.add('hidden');
            });
        }
    });

    // 신규 조회 버튼
    document.getElementById('btnNewInquiry').addEventListener('click', function() {
        document.getElementById('newCompanyKey').value = '';
        newInquiryModal.classList.remove('hidden');
        document.getElementById('newCompanyKey').focus();
    });

    // 사업자번호 자동 포맷
    const companyKeyInput = document.getElementById('newCompanyKey');
    companyKeyInput.addEventListener('input', function() {
        let value = this.value.replace(/[^0-9]/g, '');
        if (value.length > 10) value = value.substring(0, 10);
        let formatted = '';
        if (value.length > 0) formatted = value.substring(0, 3);
        if (value.length > 3) formatted += '-' + value.substring(3, 5);
        if (value.length > 5) formatted += '-' + value.substring(5, 10);
        this.value = formatted;
    });

    // 신규 조회 제출
    document.getElementById('newInquiryForm').addEventListener('submit', async function(e) {
        e.preventDefault();

        const companyKey = document.getElementById('newCompanyKey').value.replace(/[^0-9]/g, '');
        if (!companyKey || companyKey.length !== 10) {
            showToast('사업자번호 10자리를 입력해주세요.', 'warning');
            return;
        }

        const submitBtn = document.getElementById('btnSubmitInquiry');
        const submitText = document.getElementById('submitText');
        const submitSpinner = document.getElementById('submitSpinner');

        submitBtn.disabled = true;
        submitText.textContent = '조회 중...';
        submitSpinner.classList.remove('hidden');

        try {
            const response = await fetch('{{ route('credit.inquiry.search') }}', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'X-CSRF-TOKEN': csrfToken,
                    'Accept': 'application/json',
                },
                body: JSON.stringify({ company_key: companyKey }),
            });

            const result = await response.json();

            if (result.success) {
                showToast('신용평가 조회가 완료되었습니다.', 'success');
                newInquiryModal.classList.add('hidden');

                // 페이지 새로고침하여 새 데이터 표시
                window.location.reload();
            } else {
                showToast(result.error || '조회에 실패했습니다.', 'error');
            }
        } catch (error) {
            console.error('조회 오류:', error);
            showToast('조회 중 오류가 발생했습니다.', 'error');
        } finally {
            submitBtn.disabled = false;
            submitText.textContent = '조회 실행';
            submitSpinner.classList.add('hidden');
        }
    });

    // 원본 데이터 버튼
    document.querySelectorAll('.btn-raw-data').forEach(btn => {
        btn.addEventListener('click', async function() {
            const inquiryKey = this.dataset.inquiryKey;
            await loadRawData(inquiryKey);
        });
    });

    // 리포트 버튼
    document.querySelectorAll('.btn-report').forEach(btn => {
        btn.addEventListener('click', async function() {
            const inquiryKey = this.dataset.inquiryKey;
            await loadReport(inquiryKey);
        });
    });

    // 삭제 버튼
    document.querySelectorAll('.btn-delete').forEach(btn => {
        btn.addEventListener('click', async function() {
            if (!confirm('이 조회 이력을 삭제하시겠습니까?')) return;

            const inquiryId = this.dataset.inquiryId;
            try {
                const response = await fetch(`{{ url('credit/inquiry') }}/${inquiryId}`, {
                    method: 'DELETE',
                    headers: {
                        'X-CSRF-TOKEN': csrfToken,
                        'Accept': 'application/json',
                    },
                });

                const result = await response.json();
                if (result.success) {
                    showToast('삭제되었습니다.', 'success');
                    this.closest('tr').remove();
                } else {
                    showToast(result.error || '삭제에 실패했습니다.', 'error');
                }
            } catch (error) {
                showToast('삭제 중 오류가 발생했습니다.', 'error');
            }
        });
    });

    // 원본 데이터 로드
    async function loadRawData(inquiryKey) {
        const content = document.getElementById('rawDataContent');
        const subtitle = document.getElementById('rawDataSubtitle');

        content.innerHTML = '<div class="flex items-center justify-center py-12"><svg class="w-8 h-8 animate-spin text-gray-400" fill="none" viewBox="0 0 24 24"><circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle><path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path></svg></div>';
        rawDataModal.classList.remove('hidden');

        try {
            const response = await fetch(`{{ url('credit/inquiry') }}/${inquiryKey}/raw`);
            const result = await response.json();

            if (result.success) {
                subtitle.textContent = `${result.inquiry.formatted_company_key} | ${result.inquiry.inquired_at}`;
                content.innerHTML = renderRawDataPanel(result.data, result.inquiry);
                bindRawDataEvents();
            } else {
                content.innerHTML = `<div class="text-center text-red-600 py-12">데이터 로드 실패: ${result.error}</div>`;
            }
        } catch (error) {
            content.innerHTML = '<div class="text-center text-red-600 py-12">데이터 로드 중 오류가 발생했습니다.</div>';
        }
    }

    // 리포트 로드
    async function loadReport(inquiryKey) {
        const content = document.getElementById('reportContent');
        const subtitle = document.getElementById('reportSubtitle');

        content.innerHTML = '<div class="flex items-center justify-center py-12"><svg class="w-8 h-8 animate-spin text-gray-400" fill="none" viewBox="0 0 24 24"><circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle><path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path></svg></div>';
        reportModal.classList.remove('hidden');

        try {
            const response = await fetch(`{{ url('credit/inquiry') }}/${inquiryKey}/report`);
            const result = await response.json();

            if (result.success) {
                subtitle.textContent = `${result.inquiry.formatted_company_key} | ${result.inquiry.inquired_at}`;
                // 탭 전환용 상세 데이터 저장
                currentReportDetails = result.data.details || {};
                content.innerHTML = renderReportPanel(result.data);
            } else {
                content.innerHTML = `<div class="text-center text-red-600 py-12">데이터 로드 실패: ${result.error}</div>`;
            }
        } catch (error) {
            content.innerHTML = '<div class="text-center text-red-600 py-12">데이터 로드 중 오류가 발생했습니다.</div>';
        }
    }

    // 인쇄 버튼
    document.getElementById('btnPrintReport').addEventListener('click', function() {
        const content = document.getElementById('reportContent');
        const printWindow = window.open('', '_blank');
        printWindow.document.write(`
            <html>
            <head>
                <title>신용평가 리포트</title>
                <style>
                    body { font-family: 'Malgun Gothic', sans-serif; padding: 20px; }
                    table { width: 100%; border-collapse: collapse; margin: 10px 0; }
                    th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
                    th { background: #f5f5f5; }
                    .header { text-align: center; margin-bottom: 20px; }
                    .section { margin: 20px 0; }
                    .section-title { font-weight: bold; font-size: 14px; margin-bottom: 10px; padding: 5px; background: #f0f0f0; }
                </style>
            </head>
            <body>${content.innerHTML}</body>
            </html>
        `);
        printWindow.document.close();
        printWindow.print();
    });

    // 원본 데이터 패널 렌더링 (기존 종합 패널과 동일)
    function renderRawDataPanel(data, inquiry) {
        // inquiry 객체에서 회사 정보 포함해서 렌더링
        return renderComprehensivePanel(data, inquiry);
    }

    // 종합 패널 렌더링 함수들 (기존 코드와 동일)
    const typeCodeMap = {
        'BB': '신용도판단정보(채무불이행)',
        'FD': '금융질서문란',
        'PB': '공공정보',
        'SB': '특수기록정보',
        'CB': '신용도판단정보(신용정보사)',
    };

    const rcnoKindMap = {
        '2': '사업자등록번호',
        '3': '법인등록번호',
    };

    function renderComprehensivePanel(data, inquiry) {
        let html = '';
        const companyKey = inquiry.company_key || inquiry;

        // 회사 정보 헤더
        html += `
            <div class="mb-6 pb-4 border-b">
                <div class="flex flex-col md:flex-row md:justify-between md:items-start gap-4">
                    <div class="flex-1">
                        <h2 class="text-lg font-bold text-gray-800 mb-2">
                            ${inquiry.company_name || '업체명 미확인'}
                            ${inquiry.nts_status_label ? renderNtsStatusBadge(inquiry.nts_status_code, inquiry.nts_status_label) : ''}
                        </h2>
                        <div class="grid grid-cols-1 md:grid-cols-2 gap-x-6 gap-y-1 text-sm">
                            <div class="flex gap-2">
                                <span class="text-gray-500 w-20">사업자번호</span>
                                <span class="font-mono font-medium">${formatBusinessNumber(companyKey)}</span>
                            </div>
                            ${inquiry.ceo_name ? `
                            <div class="flex gap-2">
                                <span class="text-gray-500 w-20">대표자</span>
                                <span>${inquiry.ceo_name}</span>
                            </div>` : ''}
                            ${inquiry.company_address ? `
                            <div class="flex gap-2 md:col-span-2">
                                <span class="text-gray-500 w-20">주소</span>
                                <span class="text-gray-700">${inquiry.company_address}</span>
                            </div>` : ''}
                            ${inquiry.business_type || inquiry.business_item ? `
                            <div class="flex gap-2">
                                <span class="text-gray-500 w-20">업종/업태</span>
                                <span class="text-gray-700">${[inquiry.business_type, inquiry.business_item].filter(Boolean).join(' / ')}</span>
                            </div>` : ''}
                            ${inquiry.nts_tax_type ? `
                            <div class="flex gap-2">
                                <span class="text-gray-500 w-20">과세유형</span>
                                <span class="text-gray-700">${inquiry.nts_tax_type}</span>
                            </div>` : ''}
                        </div>
                    </div>
                </div>
            </div>
        `;

        // 신용요약
        html += renderSummaryCard(data.summary);

        // 기업기본정보 섹션 (OA08)
        if (data.companyInfo) {
            html += renderSection('기업 기본정보', 'NICE', 'OA08', data.companyInfo, renderCompanyInfoDetail);
        }

        // 국세청 상태 섹션
        if (data.ntsStatus) {
            html += renderSection('사업자등록 상태', '국세청', 'NTS', { success: data.ntsStatus?.success, data: data.ntsStatus }, renderNtsStatusDetail);
        }

        // 각 섹션
        html += renderSection('단기연체정보', '한국신용정보원', 'OA13', data.shortTermOverdue, renderShortTermOverdue);
        html += renderSection('신용도판단정보', '한국신용정보원', 'OA14', data.negativeInfoKCI, renderNegativeInfoKCI);
        html += renderSection('신용도판단정보', '신용정보사', 'OA15', data.negativeInfoCB, renderNegativeInfoCB);
        html += renderSection('당좌거래정지정보', '금융결제원', 'OA16', data.suspensionInfo, renderSuspensionInfo);
        html += renderSection('법정관리/워크아웃정보', '', 'OA17', data.workoutInfo, renderWorkoutInfo);

        return html;
    }

    function renderNtsStatusBadge(statusCode, statusLabel) {
        const colors = {
            '01': 'bg-green-100 text-green-700',
            '02': 'bg-yellow-100 text-yellow-700',
            '03': 'bg-red-100 text-red-700',
        };
        const colorClass = colors[statusCode] || 'bg-gray-100 text-gray-600';
        return `<span class="ml-2 px-2 py-0.5 text-xs font-medium ${colorClass} rounded">${statusLabel}</span>`;
    }

    function renderCompanyInfoDetail(data) {
        const info = data?.data || data || {};
        if (!info || Object.keys(info).length === 0) {
            return renderNoData('기업 기본정보가 없습니다.');
        }

        let html = '<div class="grid grid-cols-2 gap-4 text-sm">';
        const fields = [
            ['korentrnm', '업체명'], ['korreprnm', '대표자명'], ['bizno', '사업자번호'],
            ['crpno', '법인번호'], ['addr', '주소'], ['bizcnd', '업종'], ['bizitm', '업태'],
            ['estbDt', '설립일'], ['empCnt', '종업원수'], ['salesAmt', '매출액']
        ];
        fields.forEach(([key, label]) => {
            if (info[key]) {
                html += `<div><span class="text-gray-500">${label}:</span> <span class="font-medium">${info[key]}</span></div>`;
            }
        });
        html += '</div>';
        return html;
    }

    function renderNtsStatusDetail(data) {
        const nts = data?.data || {};
        if (!nts || Object.keys(nts).length === 0) {
            return renderNoData('국세청 사업자등록 상태 정보가 없습니다.');
        }

        let html = '<div class="grid grid-cols-2 gap-4 text-sm">';
        html += `<div><span class="text-gray-500">사업자번호:</span> <span class="font-mono font-medium">${nts.b_no || '-'}</span></div>`;
        html += `<div><span class="text-gray-500">상태:</span> <span class="font-medium">${nts.b_stt || '-'}</span></div>`;
        html += `<div><span class="text-gray-500">과세유형:</span> <span class="font-medium">${nts.tax_type || '-'}</span></div>`;
        if (nts.end_dt) {
            html += `<div><span class="text-gray-500">폐업일:</span> <span class="font-medium text-red-600">${nts.end_dt}</span></div>`;
        }
        html += '</div>';
        return html;
    }

    function renderSummaryCard(result) {
        if (!result || !result.success) {
            return `<div class="bg-red-50 border border-red-200 rounded-lg p-4 mb-4"><p class="text-red-700">신용요약정보 조회 실패</p></div>`;
        }

        const data = result.data;
        const summaryList = data?.data?.creditSummaryList?.[0] || data?.creditSummaryList?.[0] || {};

        return `
            <div class="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-6 gap-3 mb-6">
                ${renderSummaryItem('단기연체', summaryList.shorttermOverdueCnt || 0)}
                ${renderSummaryItem('신용도판단(KCI)', summaryList.negativeInfoBbCnt || 0)}
                ${renderSummaryItem('공공정보', summaryList.negativeInfoPbCnt || 0)}
                ${renderSummaryItem('신용도판단(CB)', summaryList.negativeInfoCbCnt || 0)}
                ${renderSummaryItem('당좌정지', summaryList.suspensionInfoCnt || 0)}
                ${renderSummaryItem('법정관리', summaryList.workoutCnt || 0)}
            </div>
        `;
    }

    function renderSummaryItem(label, count) {
        const hasIssue = count > 0;
        const bgColor = hasIssue ? 'bg-red-50 border-red-200' : 'bg-green-50 border-green-200';
        const textColor = hasIssue ? 'text-red-700' : 'text-green-700';
        return `
            <div class="border rounded-lg p-2 text-center ${bgColor}">
                <p class="text-xs ${textColor}">${label}</p>
                <p class="text-xl font-bold ${textColor}">${count}</p>
            </div>
        `;
    }

    function renderSection(title, subtitle, apiId, result, contentRenderer) {
        const isSuccess = result?.success;
        const statusIcon = isSuccess ? '<span class="w-2 h-2 bg-green-500 rounded-full"></span>' : '<span class="w-2 h-2 bg-red-500 rounded-full"></span>';

        return `
            <div class="border rounded-lg mb-4 section-card">
                <div class="flex items-center justify-between p-3 border-b cursor-pointer section-toggle hover:bg-gray-50">
                    <div class="flex items-center gap-2">
                        ${statusIcon}
                        <span class="font-medium text-sm">${title}</span>
                        ${subtitle ? `<span class="text-xs text-gray-500">(${subtitle})</span>` : ''}
                        <span class="text-xs bg-gray-100 px-1.5 py-0.5 rounded">${apiId}</span>
                    </div>
                    <svg class="w-4 h-4 text-gray-400 toggle-icon transition-transform" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7" />
                    </svg>
                </div>
                <div class="section-content p-3">
                    ${isSuccess ? contentRenderer(result.data) : renderError(result)}
                </div>
            </div>
        `;
    }

    function renderError(result) {
        return `<div class="bg-red-50 border border-red-200 rounded p-3 text-sm text-red-700">${result?.error || '조회 실패'}</div>`;
    }

    function renderNoData(message) {
        return `<div class="bg-gray-50 border rounded p-4 text-center text-gray-600 text-sm">${message}</div>`;
    }

    function renderShortTermOverdue(data) {
        const list = data?.data?.creditShorttermOverdueList || data?.creditShorttermOverdueList || [];
        if (list.length === 0) return renderNoData('단기연체 정보가 없습니다.');

        let html = '';
        list.forEach(item => {
            html += `<div class="text-sm mb-2"><strong>건수:</strong> ${item.count || 0}</div>`;
            if (item.accountSubjectsList?.length > 0) {
                html += `<table class="w-full text-xs border-collapse"><thead class="bg-gray-50"><tr><th class="border p-2">계정과목</th><th class="border p-2">대과목</th><th class="border p-2">거래금액</th></tr></thead><tbody>`;
                item.accountSubjectsList.forEach(acc => {
                    html += `<tr><td class="border p-2">${acc.accname || '-'}</td><td class="border p-2">${acc.kind1 || '-'}</td><td class="border p-2 text-right">${formatNumber(acc.accamt)}</td></tr>`;
                });
                html += `</tbody></table>`;
            }
        });
        return html;
    }

    function renderNegativeInfoKCI(data) {
        const list = data?.data?.creditNegativeInfoList || data?.creditNegativeInfoList || [];
        if (list.length === 0) return renderNoData('신용도판단정보가 없습니다.');

        let html = '';
        list.forEach(item => {
            html += `<div class="grid grid-cols-5 gap-2 mb-3 text-xs">`;
            html += `<div class="border rounded p-2 text-center"><span class="block text-gray-500">발생총건수</span><span class="font-bold">${item.totaloccCnt || 0}</span></div>`;
            html += `<div class="border rounded p-2 text-center"><span class="block text-gray-500">채무불이행</span><span class="font-bold text-red-600">${item.bbCnt || 0}</span></div>`;
            html += `<div class="border rounded p-2 text-center"><span class="block text-gray-500">금융질서문란</span><span class="font-bold">${item.fdCnt || 0}</span></div>`;
            html += `<div class="border rounded p-2 text-center"><span class="block text-gray-500">공공기록</span><span class="font-bold">${item.pbCnt || 0}</span></div>`;
            html += `<div class="border rounded p-2 text-center"><span class="block text-gray-500">특수기록</span><span class="font-bold">${item.sbCnt || 0}</span></div>`;
            html += `</div>`;

            if (item.negativeInfoDetailList?.length > 0) {
                html += `<table class="w-full text-xs border-collapse"><thead class="bg-gray-50"><tr><th class="border p-2">유형</th><th class="border p-2">등록사유</th><th class="border p-2">등록금액</th><th class="border p-2">등록일</th></tr></thead><tbody>`;
                item.negativeInfoDetailList.forEach(detail => {
                    html += `<tr><td class="border p-2">${typeCodeMap[detail.typecode] || detail.typecode}</td><td class="border p-2">${detail.causename || '-'}</td><td class="border p-2 text-right">${formatNumber(detail.regamt)}</td><td class="border p-2">${formatDate(detail.regdate)}</td></tr>`;
                });
                html += `</tbody></table>`;
            }
        });
        return html;
    }

    function renderNegativeInfoCB(data) {
        const list = data?.data?.creditNegativeInfoCdList || data?.creditNegativeInfoCdList || [];
        if (list.length === 0) return renderNoData('신용도판단정보(신용정보사)가 없습니다.');

        let html = `<table class="w-full text-xs border-collapse"><thead class="bg-gray-50"><tr><th class="border p-2">등록사유</th><th class="border p-2">등록금액</th><th class="border p-2">등록일</th><th class="border p-2">발생기관</th></tr></thead><tbody>`;
        list.forEach(item => {
            html += `<tr><td class="border p-2">${item.causename || '-'}</td><td class="border p-2 text-right">${formatNumber(item.regamt)}</td><td class="border p-2">${formatDate(item.regdate)}</td><td class="border p-2">${item.orgname || '-'}</td></tr>`;
        });
        html += `</tbody></table>`;
        return html;
    }

    function renderSuspensionInfo(data) {
        const list = data?.data?.creditSuspensionInfoList || data?.creditSuspensionInfoList || [];
        if (list.length === 0) return renderNoData('당좌거래정지 정보가 없습니다.');

        let html = `<table class="w-full text-xs border-collapse"><thead class="bg-gray-50"><tr><th class="border p-2">업체명</th><th class="border p-2">대표자</th><th class="border p-2">발생일</th><th class="border p-2">해제일</th></tr></thead><tbody>`;
        list.forEach(item => {
            html += `<tr><td class="border p-2">${item.koreantrnm || '-'}</td><td class="border p-2">${item.korrenprnm || '-'}</td><td class="border p-2">${formatDate(item.occdate)}</td><td class="border p-2">${formatDate(item.relsdate)}</td></tr>`;
        });
        html += `</tbody></table>`;
        return html;
    }

    function renderWorkoutInfo(data) {
        const list = data?.data?.creditworkoutList || data?.creditworkoutList || [];
        if (list.length === 0) return renderNoData('법정관리/워크아웃 정보가 없습니다.');

        let html = `<table class="w-full text-xs border-collapse"><thead class="bg-gray-50"><tr><th class="border p-2">업체명</th><th class="border p-2">유형</th><th class="border p-2">사건번호</th><th class="border p-2">법원</th></tr></thead><tbody>`;
        list.forEach(item => {
            html += `<tr><td class="border p-2">${item.korentrnm || '-'}</td><td class="border p-2">${item.lglmgmtdivnm || '-'}</td><td class="border p-2">${item.hngno || '-'}</td><td class="border p-2">${item.lwcnm || '-'}</td></tr>`;
        });
        html += `</tbody></table>`;
        return html;
    }

    // 리포트 패널 렌더링 (SAM 기업 신용분석 리포트 형식)
    function renderReportPanel(data) {
        const company = data.company_info || {};
        const nts = data.nts_info || {};
        const summary = data.summary || {};
        const details = data.details || {};
        const hasIssue = summary.has_issue;
        const totalIssue = summary.total_issue_count || 0;

        // 신용 등급 계산 (Level 1~4)
        let creditLevel = 4; // 기본 양호
        let creditLabel = '양호';
        let creditColor = 'green';
        if (totalIssue >= 5 || nts.closure_date) { creditLevel = 1; creditLabel = '위험'; creditColor = 'red'; }
        else if (totalIssue >= 3) { creditLevel = 2; creditLabel = '경고'; creditColor = 'orange'; }
        else if (totalIssue >= 1) { creditLevel = 3; creditLabel = '주의'; creditColor = 'yellow'; }

        return `
            <div class="report-content max-w-5xl mx-auto">
                <!-- 헤더 섹션 -->
                <div class="text-center mb-8">
                    <div class="inline-flex items-center gap-2 bg-blue-50 text-blue-600 px-4 py-1.5 rounded-full text-sm font-semibold mb-4 border border-blue-100">
                        <span class="relative flex h-2 w-2">
                            <span class="animate-ping absolute inline-flex h-full w-full rounded-full bg-blue-400 opacity-75"></span>
                            <span class="relative inline-flex rounded-full h-2 w-2 bg-blue-500"></span>
                        </span>
                        실시간 신용정보 조회
                    </div>
                    <h2 class="text-2xl font-extrabold text-stone-900 mb-2">${company.company_name || '업체명 미확인'} 기업 신용 분석</h2>
                    <p class="text-stone-600">
                        사업자번호: <span class="font-mono font-medium">${company.company_key || '-'}</span>
                        ${company.ceo_name ? ` | 대표자: ${company.ceo_name}` : ''}
                        ${nts.tax_type && nts.tax_type !== '-' ? ` | ${nts.tax_type}` : ''}
                    </p>
                    <p class="text-sm text-stone-400 mt-1">조회일시: ${company.inquired_at || '-'}</p>
                </div>

                <!-- 중요 안내 -->
                <div class="bg-amber-50 border-2 border-amber-400 rounded-xl p-4 mb-6">
                    <div class="flex items-start gap-3">
                        <span class="text-2xl">⚠️</span>
                        <div>
                            <p class="font-bold text-amber-800 text-base mb-1">자료 유효기간 안내</p>
                            <p class="text-amber-700 text-sm">본 자료는 <strong>조회일(${company.inquired_at || '오늘'}) 기준</strong>으로 기록된 정보입니다. 신용정보는 수시로 변동될 수 있으므로 중요한 거래 결정 시 최신 정보를 재조회하시기 바랍니다.</p>
                        </div>
                    </div>
                </div>

                <!-- 종합 신용 신호등 -->
                <div class="grid grid-cols-1 lg:grid-cols-2 gap-6 mb-8">
                    <div class="bg-white rounded-2xl shadow-sm border border-stone-200 p-6">
                        <div class="flex justify-between items-start mb-4">
                            <div>
                                <h3 class="text-lg font-bold text-stone-900 mb-1">종합 신용 신호등</h3>
                                <p class="text-sm text-stone-500">거래처 건전성 모니터링</p>
                            </div>
                            <div class="flex gap-1.5 bg-stone-100 p-2 rounded-full border border-stone-200">
                                <span class="traffic-light ${creditLevel === 1 ? 'light-red' : 'light-red light-dim'}"></span>
                                <span class="traffic-light ${creditLevel === 2 ? 'light-orange' : 'light-orange light-dim'}"></span>
                                <span class="traffic-light ${creditLevel === 3 ? 'light-yellow' : 'light-yellow light-dim'}"></span>
                                <span class="traffic-light ${creditLevel === 4 ? 'light-green' : 'light-green light-dim'}"></span>
                            </div>
                        </div>
                        <div class="text-center py-4 bg-stone-50 rounded-xl mb-4 border ${creditColor === 'red' ? 'border-red-200' : creditColor === 'orange' ? 'border-orange-200' : creditColor === 'yellow' ? 'border-amber-200' : 'border-green-200'}">
                            <div class="text-3xl font-black ${creditColor === 'red' ? 'text-red-500' : creditColor === 'orange' ? 'text-orange-500' : creditColor === 'yellow' ? 'text-amber-500' : 'text-green-500'} mb-1">
                                ${creditLabel} (Level ${creditLevel})
                            </div>
                            <p class="text-stone-600 text-sm font-medium">
                                ${creditLevel === 1 ? '거래 중단 권고' : creditLevel === 2 ? '선입금 거래 권장' : creditLevel === 3 ? '거래 한도 축소 권장' : '정상 거래 가능'}
                            </p>
                        </div>
                        ${nts.closure_date ? `
                        <div class="bg-red-50 border border-red-200 rounded-lg p-3 text-center">
                            <p class="text-red-700 font-bold text-sm">⚠️ 폐업 사업자 (폐업일: ${nts.closure_date})</p>
                        </div>
                        ` : `
                        <div class="bg-${nts.is_active ? 'green' : 'yellow'}-50 border border-${nts.is_active ? 'green' : 'yellow'}-200 rounded-lg p-3 text-center">
                            <p class="text-${nts.is_active ? 'green' : 'yellow'}-700 font-medium text-sm">국세청 상태: ${nts.status_label || '미확인'}</p>
                        </div>
                        `}
                    </div>

                    <!-- 신용 리스크 프로필 (레이더 차트) -->
                    <div class="bg-white rounded-2xl shadow-sm border border-stone-200 p-6">
                        <h3 class="text-lg font-bold text-stone-900 mb-1">신용 리스크 프로필</h3>
                        <p class="text-sm text-stone-500 mb-4">5대 핵심 지표 다각도 분석</p>
                        ${renderRadarChart(summary, nts)}
                        <div class="mt-4 pt-4 border-t border-stone-200 space-y-2">
                            ${renderIssueRow('한국신용정보원 연체', summary.short_term_overdue_cnt, '건 발생')}
                            ${renderIssueRow('금융질서문란 정보', summary.negative_info_kci_cnt, '건 등록')}
                            ${renderIssueRow('공공기록 정보', summary.negative_info_pb_cnt, '건 등록')}
                        </div>
                    </div>
                </div>

                <!-- 상세 정보 탭 -->
                <div class="bg-white rounded-2xl shadow-sm border border-stone-200 p-6 mb-8">
                    <h3 class="text-lg font-bold text-stone-900 mb-4 text-center">신용 상세 정보</h3>
                    <div class="flex space-x-4 border-b border-stone-200 mb-6 overflow-x-auto justify-center">
                        <button onclick="switchReportTab('delinquency')" class="report-tab report-tab-active pb-3 px-2 transition whitespace-nowrap text-sm" id="report-tab-delinquency">단기연체정보</button>
                        <button onclick="switchReportTab('judgment')" class="report-tab report-tab-inactive pb-3 px-2 transition whitespace-nowrap text-sm" id="report-tab-judgment">신용도판단정보</button>
                        <button onclick="switchReportTab('suspension')" class="report-tab report-tab-inactive pb-3 px-2 transition whitespace-nowrap text-sm" id="report-tab-suspension">당좌거래정지</button>
                        <button onclick="switchReportTab('workout')" class="report-tab report-tab-inactive pb-3 px-2 transition whitespace-nowrap text-sm" id="report-tab-workout">법정관리/워크아웃</button>
                    </div>
                    <div id="report-tab-content">
                        ${renderDelinquencyTab(details)}
                    </div>
                </div>

                <!-- 거래 권고 -->
                <div class="bg-stone-900 rounded-2xl p-6 text-white">
                    <div class="flex items-center justify-between flex-wrap gap-4">
                        <div>
                            <h3 class="text-xl font-bold mb-1">거래 승인 판정</h3>
                            <p class="text-stone-400 text-sm">정량적 신용정보 기반 자동 판정</p>
                        </div>
                        <div class="flex items-center gap-4">
                            ${creditLevel >= 3 ? `
                            <div class="bg-green-500 text-white px-6 py-3 rounded-xl font-bold text-lg shadow-lg">
                                ✓ 거래 승인 권장
                            </div>
                            ` : creditLevel === 2 ? `
                            <div class="bg-amber-500 text-white px-6 py-3 rounded-xl font-bold text-lg shadow-lg">
                                ⚠ 조건부 승인
                            </div>
                            ` : `
                            <div class="bg-red-500 text-white px-6 py-3 rounded-xl font-bold text-lg shadow-lg">
                                ✗ 거래 보류 권고
                            </div>
                            `}
                        </div>
                    </div>
                    <div class="mt-4 pt-4 border-t border-stone-700 grid grid-cols-2 md:grid-cols-4 gap-4 text-center text-sm">
                        <div><span class="text-stone-400 block">총 이슈</span><span class="font-bold text-lg">${totalIssue}건</span></div>
                        <div><span class="text-stone-400 block">신용등급</span><span class="font-bold text-lg">Level ${creditLevel}</span></div>
                        <div><span class="text-stone-400 block">국세청</span><span class="font-bold text-lg">${nts.status_label || '미확인'}</span></div>
                        <div><span class="text-stone-400 block">추천 결제</span><span class="font-bold text-lg">${creditLevel >= 3 ? '외상가능' : creditLevel === 2 ? '선입금50%' : '현금거래'}</span></div>
                    </div>
                </div>

                <div class="text-center text-stone-400 py-4 text-xs mt-6">
                    <p>SAM Intelligence - 기업 신용분석 시스템</p>
                    <p class="mt-1">데이터 출처: 한국신용정보원, 금융결제원, 국세청 공공데이터</p>
                </div>
            </div>
        `;
    }

    // 현재 상세 데이터를 저장 (탭 전환용)
    let currentReportDetails = {};

    // 레이더 차트 렌더링
    function renderRadarChart(summary, nts) {
        // 5대 지표 점수 계산 (100점 만점, 높을수록 좋음 - 이슈가 없으면 100)
        // 성장성: 국세청 상태코드 기반 - 01(계속사업자)=100, 02(휴업)=50, 03(폐업)=20
        const growthScore = (() => {
            const statusCode = nts?.status_code || '';
            if (statusCode === '01') return 100; // 계속사업자 (정상)
            if (statusCode === '02') return 50;  // 휴업자
            if (statusCode === '03') return 20;  // 폐업자
            // 상태코드 없으면 is_active로 판단
            return nts?.is_active ? 100 : 60;
        })();

        const scores = {
            liquidity: Math.max(0, 100 - (summary.short_term_overdue_cnt || 0) * 30),      // 유동성: 단기연체 1건당 -30점
            publicRecord: Math.max(0, 100 - (summary.negative_info_pb_cnt || 0) * 25),    // 공공기록: 1건당 -25점
            stability: Math.max(0, 100 - ((summary.suspension_info_cnt || 0) + (summary.workout_cnt || 0)) * 40), // 안정성: 1건당 -40점
            growth: growthScore,                                                           // 성장성: 국세청 상태 기반
            delinquency: Math.max(0, 100 - ((summary.negative_info_kci_cnt || 0) + (summary.negative_info_cb_cnt || 0)) * 20) // 연체이력: 1건당 -20점
        };

        // SVG 레이더 차트 좌표 계산
        const cx = 150, cy = 130, r = 90;
        const angles = [-90, -18, 54, 126, 198].map(a => a * Math.PI / 180); // 5각형 각도
        const labels = ['유동성', '공공기록', '안정성', '성장성', '연체이력'];
        const values = [scores.liquidity, scores.publicRecord, scores.stability, scores.growth, scores.delinquency];

        // 배경 격자 그리기 (20, 40, 60, 80, 100)
        let gridLines = '';
        [20, 40, 60, 80, 100].forEach(level => {
            const ratio = level / 100;
            const points = angles.map(a => `${cx + r * ratio * Math.cos(a)},${cy + r * ratio * Math.sin(a)}`).join(' ');
            gridLines += `<polygon points="${points}" fill="none" stroke="#e5e7eb" stroke-width="1"/>`;
        });

        // 축 선 그리기
        let axisLines = '';
        angles.forEach(a => {
            axisLines += `<line x1="${cx}" y1="${cy}" x2="${cx + r * Math.cos(a)}" y2="${cy + r * Math.sin(a)}" stroke="#e5e7eb" stroke-width="1"/>`;
        });

        // 데이터 폴리곤
        const dataPoints = angles.map((a, i) => {
            const ratio = values[i] / 100;
            return `${cx + r * ratio * Math.cos(a)},${cy + r * ratio * Math.sin(a)}`;
        }).join(' ');

        // 라벨 위치
        const labelPositions = [
            { x: cx, y: cy - r - 15, anchor: 'middle' },           // 유동성 (상단)
            { x: cx + r + 10, y: cy - 30, anchor: 'start' },       // 공공기록 (우상단)
            { x: cx + r * 0.6, y: cy + r * 0.8 + 15, anchor: 'start' },  // 안정성 (우하단)
            { x: cx - r * 0.6, y: cy + r * 0.8 + 15, anchor: 'end' },    // 성장성 (좌하단)
            { x: cx - r - 10, y: cy - 30, anchor: 'end' }          // 연체이력 (좌상단)
        ];

        let labelElements = '';
        labels.forEach((label, i) => {
            labelElements += `<text x="${labelPositions[i].x}" y="${labelPositions[i].y}" text-anchor="${labelPositions[i].anchor}" class="text-xs fill-stone-600">${label}</text>`;
        });

        // 숫자 레이블 (100, 80, 60, 40, 20)
        let numberLabels = '';
        [100, 80, 60, 40, 20].forEach((num, i) => {
            const ratio = num / 100;
            numberLabels += `<text x="${cx + 5}" y="${cy - r * ratio + 4}" class="text-[10px] fill-stone-400">${num}</text>`;
        });

        return `
            <div class="flex justify-center">
                <svg width="300" height="280" class="overflow-visible">
                    <!-- 범례 -->
                    <rect x="100" y="5" width="12" height="12" fill="rgba(59, 130, 246, 0.3)" stroke="#3b82f6" stroke-width="1"/>
                    <text x="118" y="15" class="text-xs fill-stone-600">현재 기업 리스크</text>

                    <!-- 배경 격자 -->
                    ${gridLines}

                    <!-- 축 선 -->
                    ${axisLines}

                    <!-- 숫자 레이블 -->
                    ${numberLabels}

                    <!-- 데이터 영역 -->
                    <polygon points="${dataPoints}" fill="rgba(59, 130, 246, 0.3)" stroke="#3b82f6" stroke-width="2"/>

                    <!-- 데이터 포인트 -->
                    ${angles.map((a, i) => {
                        const ratio = values[i] / 100;
                        return `<circle cx="${cx + r * ratio * Math.cos(a)}" cy="${cy + r * ratio * Math.sin(a)}" r="4" fill="#3b82f6"/>`;
                    }).join('')}

                    <!-- 라벨 -->
                    ${labelElements}
                </svg>
            </div>
        `;
    }

    // 이슈 행 렌더링
    function renderIssueRow(label, count, suffix) {
        const hasIssue = count > 0;
        return `
            <div class="flex justify-between items-center text-sm">
                <span class="text-blue-600">${label}</span>
                <span class="font-bold ${hasIssue ? 'text-red-500' : 'text-stone-700'}">${hasIssue ? count + suffix : '해당 없음'}</span>
            </div>
        `;
    }

    function renderRiskItem(label, count, desc) {
        const hasIssue = count > 0;
        return `
            <div class="flex justify-between items-center p-3 rounded-lg ${hasIssue ? 'bg-red-50' : 'bg-stone-50'}">
                <div>
                    <span class="font-medium text-stone-800 text-sm">${label}</span>
                    <p class="text-xs text-stone-500">${desc}</p>
                </div>
                <span class="font-bold ${hasIssue ? 'text-red-600' : 'text-green-600'}">${count || 0}건</span>
            </div>
        `;
    }

    function renderDelinquencyTab(details) {
        const data = details?.shortTermOverdue;
        if (!data?.success) {
            return `<div class="text-center py-8 text-stone-500">단기연체정보 조회 실패</div>`;
        }
        const list = data.data?.data?.creditShorttermOverdueList || data.data?.creditShorttermOverdueList || [];
        if (list.length === 0) {
            return `<div class="text-center py-8 text-green-600 font-medium">✓ 단기연체 정보가 없습니다</div>`;
        }
        let html = '<div class="space-y-4">';
        list.forEach(item => {
            html += `<div class="border border-red-200 rounded-xl p-4 bg-red-50/50">
                <div class="grid grid-cols-2 md:grid-cols-3 gap-4 text-sm">
                    <div><span class="text-stone-500">건수:</span> <span class="font-bold text-red-600">${item.count || 0}건</span></div>
                    <div><span class="text-stone-500">기준일자:</span> <span class="font-medium">${formatDate(item.basedate)}</span></div>
                    <div><span class="text-stone-500">요청일자:</span> <span class="font-medium">${formatDate(item.reqdate)}</span></div>
                </div>
            </div>`;
        });
        html += '</div>';
        return html;
    }

    function renderJudgmentTab(details) {
        const kci = details?.negativeInfoKCI;
        const cb = details?.negativeInfoCB;
        let html = '<div class="space-y-4">';

        // KCI 정보
        html += '<h4 class="font-semibold text-stone-700 mb-2">한국신용정보원 (OA14)</h4>';
        if (kci?.success) {
            const list = kci.data?.data?.creditNegativeInfoList || kci.data?.creditNegativeInfoList || [];
            if (list.length === 0) {
                html += `<div class="text-center py-4 text-green-600 bg-green-50 rounded-lg">✓ 신용도판단정보(KCI) 없음</div>`;
            } else {
                list.forEach(item => {
                    html += `<div class="border rounded-xl p-4 bg-amber-50/50 border-amber-200">
                        <div class="grid grid-cols-2 md:grid-cols-5 gap-3 text-sm">
                            <div class="text-center"><span class="block text-stone-500">발생총건수</span><span class="font-bold">${item.totaloccCnt || 0}</span></div>
                            <div class="text-center"><span class="block text-stone-500">채무불이행</span><span class="font-bold text-red-600">${item.bbCnt || 0}</span></div>
                            <div class="text-center"><span class="block text-stone-500">금융질서문란</span><span class="font-bold">${item.fdCnt || 0}</span></div>
                            <div class="text-center"><span class="block text-stone-500">공공기록</span><span class="font-bold">${item.pbCnt || 0}</span></div>
                            <div class="text-center"><span class="block text-stone-500">특수기록</span><span class="font-bold">${item.sbCnt || 0}</span></div>
                        </div>
                    </div>`;
                });
            }
        } else {
            html += `<div class="text-center py-4 text-red-600 bg-red-50 rounded-lg">조회 실패</div>`;
        }

        // CB 정보
        html += '<h4 class="font-semibold text-stone-700 mb-2 mt-6">신용정보사 (OA15)</h4>';
        if (cb?.success) {
            const list = cb.data?.data?.creditNegativeInfoCdList || cb.data?.creditNegativeInfoCdList || [];
            if (list.length === 0) {
                html += `<div class="text-center py-4 text-green-600 bg-green-50 rounded-lg">✓ 신용도판단정보(CB) 없음</div>`;
            } else {
                list.forEach(item => {
                    html += `<div class="border rounded-xl p-4 bg-amber-50/50 border-amber-200 text-sm">
                        <div class="grid grid-cols-2 gap-3">
                            <div><span class="text-stone-500">유형:</span> <span class="font-medium">${item.typename || '-'}</span></div>
                            <div><span class="text-stone-500">등록사유:</span> <span class="font-medium">${item.causename || '-'}</span></div>
                            <div><span class="text-stone-500">등록금액:</span> <span class="font-bold text-red-600">${formatNumber(item.regamt)}</span></div>
                            <div><span class="text-stone-500">등록일자:</span> <span class="font-medium">${formatDate(item.regdate)}</span></div>
                        </div>
                    </div>`;
                });
            }
        } else {
            html += `<div class="text-center py-4 text-red-600 bg-red-50 rounded-lg">조회 실패</div>`;
        }

        html += '</div>';
        return html;
    }

    function renderSuspensionTab(details) {
        const data = details?.suspensionInfo;
        if (!data?.success) {
            return `<div class="text-center py-8 text-stone-500">당좌거래정지정보 조회 실패</div>`;
        }
        const list = data.data?.data?.creditSuspensionInfoList || data.data?.creditSuspensionInfoList || [];
        if (list.length === 0) {
            return `<div class="text-center py-8 text-green-600 font-medium">✓ 당좌거래정지 정보가 없습니다</div>`;
        }
        let html = '<div class="space-y-4">';
        list.forEach(item => {
            html += `<div class="border border-red-200 rounded-xl p-4 bg-red-50/50">
                <div class="grid grid-cols-2 gap-3 text-sm">
                    <div><span class="text-stone-500">업체명:</span> <span class="font-medium">${item.koreantrnm || '-'}</span></div>
                    <div><span class="text-stone-500">대표자:</span> <span class="font-medium">${item.korrenprnm || '-'}</span></div>
                    <div><span class="text-stone-500">교환소:</span> <span class="font-medium">${item.changeHouse || '-'}</span></div>
                    <div><span class="text-stone-500">발생일:</span> <span class="font-medium">${formatDate(item.occdate)}</span></div>
                    <div class="col-span-2"><span class="text-stone-500">주소:</span> <span class="font-medium">${item.address || '-'}</span></div>
                </div>
            </div>`;
        });
        html += '</div>';
        return html;
    }

    function renderWorkoutTab(details) {
        const data = details?.workoutInfo;
        if (!data?.success) {
            return `<div class="text-center py-8 text-stone-500">법정관리/워크아웃 정보 조회 실패</div>`;
        }
        const list = data.data?.data?.creditworkoutList || data.data?.creditworkoutList || [];
        if (list.length === 0) {
            return `<div class="text-center py-8 text-green-600 font-medium">✓ 법정관리/워크아웃 정보가 없습니다</div>`;
        }
        let html = '<div class="space-y-4">';
        list.forEach(item => {
            html += `<div class="border border-red-200 rounded-xl p-4 bg-red-50/50">
                <div class="grid grid-cols-2 gap-3 text-sm">
                    <div><span class="text-stone-500">업체명:</span> <span class="font-medium">${item.korentrnm || '-'}</span></div>
                    <div><span class="text-stone-500">유형:</span> <span class="font-bold text-red-600">${item.lglmgmtdivnm || '-'}</span></div>
                    <div><span class="text-stone-500">사건번호:</span> <span class="font-medium">${item.hngno || '-'}</span></div>
                    <div><span class="text-stone-500">법원:</span> <span class="font-medium">${item.lwcnm || '-'}</span></div>
                    <div><span class="text-stone-500">관례일자:</span> <span class="font-medium">${formatDate(item.lglmgmtRldDate)}</span></div>
                    <div><span class="text-stone-500">담당판사:</span> <span class="font-medium">${item.crgJudgNam || '-'}</span></div>
                </div>
            </div>`;
        });
        html += '</div>';
        return html;
    }

    function switchReportTab(tab) {
        document.querySelectorAll('.report-tab').forEach(btn => {
            btn.classList.remove('report-tab-active');
            btn.classList.add('report-tab-inactive');
        });
        document.getElementById(`report-tab-${tab}`).classList.remove('report-tab-inactive');
        document.getElementById(`report-tab-${tab}`).classList.add('report-tab-active');

        const content = document.getElementById('report-tab-content');
        switch(tab) {
            case 'delinquency':
                content.innerHTML = renderDelinquencyTab(currentReportDetails);
                break;
            case 'judgment':
                content.innerHTML = renderJudgmentTab(currentReportDetails);
                break;
            case 'suspension':
                content.innerHTML = renderSuspensionTab(currentReportDetails);
                break;
            case 'workout':
                content.innerHTML = renderWorkoutTab(currentReportDetails);
                break;
        }
    }
    // 전역 스코프에 노출 (onclick에서 호출 가능하도록)
    window.switchReportTab = switchReportTab;

    // 헬퍼 함수들
    function formatDate(dateStr) {
        if (!dateStr || dateStr.length !== 8) return '-';
        return `${dateStr.substring(0,4)}-${dateStr.substring(4,6)}-${dateStr.substring(6,8)}`;
    }

    function formatNumber(numStr) {
        if (!numStr) return '-';
        const num = parseInt(numStr, 10);
        if (isNaN(num)) return numStr;
        return num.toLocaleString('ko-KR') + '원';
    }

    function formatBusinessNumber(num) {
        if (!num || num.length !== 10) return num;
        return `${num.substring(0,3)}-${num.substring(3,5)}-${num.substring(5)}`;
    }

    // 섹션 토글 이벤트 바인딩
    function bindRawDataEvents() {
        document.querySelectorAll('#rawDataContent .section-toggle').forEach(btn => {
            btn.addEventListener('click', function() {
                const content = this.closest('.section-card').querySelector('.section-content');
                const icon = this.querySelector('.toggle-icon');
                content.classList.toggle('hidden');
                icon.classList.toggle('rotate-180');
            });
        });
    }
});
</script>
@endpush
