<!-- 품목 메타 데이터 (JS에서 FG 감지용) -->
<div id="item-meta-data"
     data-item-id="{{ $item->id }}"
     data-item-code="{{ $item->code }}"
     data-item-type="{{ $item->item_type }}"
     style="display:none;"></div>

{{-- 기본정보 --}}
<div class="space-y-4">
    <div>
        <h3 class="text-sm font-semibold text-gray-800 mb-2 pb-1 border-b border-gray-200">기본정보</h3>
        <table class="w-full text-sm">
            <tbody>
                <tr class="border-b border-gray-50">
                    <td class="py-1.5 text-gray-500 w-24">코드</td>
                    <td class="py-1.5 font-mono text-gray-900">{{ $item->code }}</td>
                </tr>
                <tr class="border-b border-gray-50">
                    <td class="py-1.5 text-gray-500">품목명</td>
                    <td class="py-1.5 text-gray-900 font-medium">{{ $item->name }}</td>
                </tr>
                <tr class="border-b border-gray-50">
                    <td class="py-1.5 text-gray-500">유형</td>
                    <td class="py-1.5">
                        @php
                            $badgeColors = [
                                'FG' => 'bg-blue-100 text-blue-800',
                                'PT' => 'bg-green-100 text-green-800',
                                'SM' => 'bg-yellow-100 text-yellow-800',
                                'RM' => 'bg-orange-100 text-orange-800',
                                'CS' => 'bg-gray-100 text-gray-800',
                            ];
                            $typeLabels = [
                                'FG' => '완제품',
                                'PT' => '부품',
                                'SM' => '부자재',
                                'RM' => '원자재',
                                'CS' => '소모품',
                            ];
                            $color = $badgeColors[$item->item_type] ?? 'bg-gray-100 text-gray-800';
                        @endphp
                        <span class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium {{ $color }}">
                            {{ $item->item_type }} ({{ $typeLabels[$item->item_type] ?? $item->item_type }})
                        </span>
                    </td>
                </tr>
                @if($item->item_category)
                <tr class="border-b border-gray-50">
                    <td class="py-1.5 text-gray-500">카테고리</td>
                    <td class="py-1.5 text-gray-900">{{ $item->item_category }}</td>
                </tr>
                @endif
                <tr class="border-b border-gray-50">
                    <td class="py-1.5 text-gray-500">단위</td>
                    <td class="py-1.5 text-gray-900">{{ $item->unit ?? '-' }}</td>
                </tr>
                @if($item->category)
                <tr class="border-b border-gray-50">
                    <td class="py-1.5 text-gray-500">분류</td>
                    <td class="py-1.5 text-gray-900">{{ $item->category->name }}</td>
                </tr>
                @endif
                <tr class="border-b border-gray-50">
                    <td class="py-1.5 text-gray-500">상태</td>
                    <td class="py-1.5">
                        @if($item->is_active)
                            <span class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium bg-green-100 text-green-800">활성</span>
                        @else
                            <span class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium bg-red-100 text-red-800">비활성</span>
                        @endif
                    </td>
                </tr>
                @if($item->description)
                <tr class="border-b border-gray-50">
                    <td class="py-1.5 text-gray-500">설명</td>
                    <td class="py-1.5 text-gray-900 text-xs">{{ $item->description }}</td>
                </tr>
                @endif
            </tbody>
        </table>
    </div>

    {{-- Options 정보 --}}
    @if($item->options)
    <div>
        <h3 class="text-sm font-semibold text-gray-800 mb-2 pb-1 border-b border-gray-200">관리 옵션</h3>
        <table class="w-full text-sm">
            <tbody>
                @if(isset($item->options['lot_managed']))
                <tr class="border-b border-gray-50">
                    <td class="py-1.5 text-gray-500 w-24">LOT 관리</td>
                    <td class="py-1.5">{{ $item->options['lot_managed'] ? '예' : '아니오' }}</td>
                </tr>
                @endif
                @if(isset($item->options['consumption_method']))
                <tr class="border-b border-gray-50">
                    <td class="py-1.5 text-gray-500">소진방식</td>
                    <td class="py-1.5">{{ $item->options['consumption_method'] }}</td>
                </tr>
                @endif
                @if(isset($item->options['production_source']))
                <tr class="border-b border-gray-50">
                    <td class="py-1.5 text-gray-500">조달구분</td>
                    <td class="py-1.5">{{ $item->options['production_source'] }}</td>
                </tr>
                @endif
            </tbody>
        </table>
    </div>
    @endif

    {{-- BOM 구성 (1depth) --}}
    <div>
        <h3 class="text-sm font-semibold text-gray-800 mb-2 pb-1 border-b border-gray-200">
            BOM 구성
            @if(count($bomChildren) > 0)
                <span class="text-gray-400 font-normal">({{ count($bomChildren) }}건)</span>
            @endif
        </h3>
        @if(count($bomChildren) > 0)
            <div class="space-y-1">
                @foreach($bomChildren as $child)
                    <div class="flex items-center gap-2 py-1.5 px-2 rounded hover:bg-gray-50 cursor-pointer transition-colors"
                         onclick="selectTreeNode({{ $child['id'] }})">
                        @php
                            $childColor = $badgeColors[$child['item_type']] ?? 'bg-gray-100 text-gray-800';
                        @endphp
                        <span class="inline-flex items-center px-1.5 py-0.5 rounded text-xs font-medium {{ $childColor }}">
                            {{ $child['item_type'] }}
                        </span>
                        <span class="font-mono text-xs text-gray-500">{{ $child['code'] }}</span>
                        <span class="text-sm text-gray-700 truncate flex-1">{{ $child['name'] }}</span>
                        <span class="text-xs text-blue-600 font-medium">{{ $child['quantity'] }} {{ $child['unit'] ?? '' }}</span>
                    </div>
                @endforeach
            </div>
        @else
            <p class="text-gray-400 text-sm py-2">BOM 구성이 없습니다.</p>
        @endif
    </div>

    {{-- 상세정보 (ItemDetail) --}}
    @if($item->details)
        {{-- 절곡 정보 --}}
        @if($item->details->bending_details)
        <div>
            <h3 class="text-sm font-semibold text-gray-800 mb-2 pb-1 border-b border-gray-200">절곡 정보</h3>
            <div class="bg-gray-50 rounded p-2 text-xs">
                <pre class="whitespace-pre-wrap text-gray-700">{{ json_encode($item->details->bending_details, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) }}</pre>
            </div>
        </div>
        @endif

        {{-- 제품 정보 --}}
        @if($item->isProduct())
        <div>
            <h3 class="text-sm font-semibold text-gray-800 mb-2 pb-1 border-b border-gray-200">제품 상세</h3>
            <table class="w-full text-sm">
                <tbody>
                    <tr class="border-b border-gray-50">
                        <td class="py-1.5 text-gray-500 w-24">판매가능</td>
                        <td class="py-1.5">{{ $item->details->is_sellable ? '예' : '아니오' }}</td>
                    </tr>
                    <tr class="border-b border-gray-50">
                        <td class="py-1.5 text-gray-500">구매가능</td>
                        <td class="py-1.5">{{ $item->details->is_purchasable ? '예' : '아니오' }}</td>
                    </tr>
                    <tr class="border-b border-gray-50">
                        <td class="py-1.5 text-gray-500">생산가능</td>
                        <td class="py-1.5">{{ $item->details->is_producible ? '예' : '아니오' }}</td>
                    </tr>
                    @if($item->details->safety_stock)
                    <tr class="border-b border-gray-50">
                        <td class="py-1.5 text-gray-500">안전재고</td>
                        <td class="py-1.5">{{ $item->details->safety_stock }}</td>
                    </tr>
                    @endif
                    @if($item->details->lead_time)
                    <tr class="border-b border-gray-50">
                        <td class="py-1.5 text-gray-500">리드타임</td>
                        <td class="py-1.5">{{ $item->details->lead_time }}일</td>
                    </tr>
                    @endif
                    @if($item->details->product_category)
                    <tr class="border-b border-gray-50">
                        <td class="py-1.5 text-gray-500">제품분류</td>
                        <td class="py-1.5">{{ $item->details->product_category }}</td>
                    </tr>
                    @endif
                    @if($item->details->part_type)
                    <tr class="border-b border-gray-50">
                        <td class="py-1.5 text-gray-500">부품유형</td>
                        <td class="py-1.5">{{ $item->details->part_type }}</td>
                    </tr>
                    @endif
                </tbody>
            </table>
        </div>
        @endif

        {{-- 자재 정보 --}}
        @if($item->isMaterial() && ($item->details->specification || $item->details->remarks))
        <div>
            <h3 class="text-sm font-semibold text-gray-800 mb-2 pb-1 border-b border-gray-200">자재 상세</h3>
            <table class="w-full text-sm">
                <tbody>
                    @if($item->details->specification)
                    <tr class="border-b border-gray-50">
                        <td class="py-1.5 text-gray-500 w-24">규격</td>
                        <td class="py-1.5">{{ $item->details->specification }}</td>
                    </tr>
                    @endif
                    @if($item->details->is_inspection)
                    <tr class="border-b border-gray-50">
                        <td class="py-1.5 text-gray-500">검사여부</td>
                        <td class="py-1.5">{{ $item->details->is_inspection === 'Y' ? '예' : '아니오' }}</td>
                    </tr>
                    @endif
                    @if($item->details->remarks)
                    <tr class="border-b border-gray-50">
                        <td class="py-1.5 text-gray-500">비고</td>
                        <td class="py-1.5 text-xs">{{ $item->details->remarks }}</td>
                    </tr>
                    @endif
                </tbody>
            </table>
        </div>
        @endif

        {{-- 인증 정보 --}}
        @if($item->details->certification_number)
        <div>
            <h3 class="text-sm font-semibold text-gray-800 mb-2 pb-1 border-b border-gray-200">인증 정보</h3>
            <table class="w-full text-sm">
                <tbody>
                    <tr class="border-b border-gray-50">
                        <td class="py-1.5 text-gray-500 w-24">인증번호</td>
                        <td class="py-1.5">{{ $item->details->certification_number }}</td>
                    </tr>
                    @if($item->details->certification_start_date)
                    <tr class="border-b border-gray-50">
                        <td class="py-1.5 text-gray-500">유효기간</td>
                        <td class="py-1.5">
                            {{ $item->details->certification_start_date->format('Y-m-d') }}
                            ~ {{ $item->details->certification_end_date?->format('Y-m-d') ?? '미정' }}
                        </td>
                    </tr>
                    @endif
                </tbody>
            </table>
        </div>
        @endif
    @endif

    {{-- 파일/이미지 --}}
    @if($item->files->isNotEmpty())
    <div>
        <h3 class="text-sm font-semibold text-gray-800 mb-2 pb-1 border-b border-gray-200">
            첨부파일
            <span class="text-gray-400 font-normal">({{ $item->files->count() }}건)</span>
        </h3>
        <div class="space-y-1">
            @foreach($item->files as $file)
                <div class="flex items-center gap-2 py-1.5 px-2 rounded bg-gray-50">
                    <svg class="w-4 h-4 text-gray-400 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15.172 7l-6.586 6.586a2 2 0 102.828 2.828l6.414-6.586a4 4 0 00-5.656-5.656l-6.415 6.585a6 6 0 108.486 8.486L20.5 13"></path>
                    </svg>
                    <span class="text-sm text-gray-700 truncate">{{ $file->original_name }}</span>
                    @if($file->size)
                        <span class="text-xs text-gray-400 flex-shrink-0">{{ number_format($file->size / 1024, 1) }}KB</span>
                    @endif
                </div>
            @endforeach
        </div>
    </div>
    @endif
</div>
