{{-- 제조 견적서 클래식 템플릿 — 한국 표준 제조업 견적서 양식 --}}
<style>
    .mfg-classic .doc-table { width: 100%; border-collapse: collapse; }
    .mfg-classic .doc-table th, .mfg-classic .doc-table td { border: 1px solid #333; padding: 6px 10px; font-size: 13px; }
    .mfg-classic .doc-table th { background-color: #f3f4f6; font-weight: 600; }
    .mfg-classic .seal-box {
        display: inline-block; width: 60px; height: 60px; border: 2px solid #dc2626;
        border-radius: 50%; text-align: center; line-height: 56px; color: #dc2626;
        font-weight: 700; font-size: 16px;
    }
    @media print {
        .mfg-classic { padding: 10mm 15mm !important; box-shadow: none !important; }
    }
</style>

@php
    $materialItems = $quotation->items->where('item_category', 'material');
    $laborItems = $quotation->items->where('item_category', 'labor');
    $installItems = $quotation->items->where('item_category', 'install');
    $otherItems = $quotation->items->whereNotIn('item_category', ['material', 'labor', 'install']);
    $allItems = $materialItems->merge($laborItems)->merge($installItems)->merge($otherItems);

    $materialTotal = $materialItems->sum('total_price');
    $laborTotal = $laborItems->sum('total_price');
    $installTotal = $installItems->sum('total_price');
@endphp

<div class="mfg-classic document-page max-w-[210mm] mx-auto my-8 bg-white shadow-lg" style="padding: 15mm 20mm;">

    {{-- 제목 --}}
    <h1 class="text-center text-3xl font-bold tracking-[0.5em] mb-8 pb-4 border-b-2 border-gray-800">
        견 적 서
    </h1>

    {{-- 견적 정보 --}}
    <div class="flex justify-between mb-6 text-sm">
        <div>
            <p><span class="font-semibold">견적번호:</span> {{ $quotationNo }}</p>
            <p><span class="font-semibold">유효기간:</span> {{ $validUntil }}</p>
        </div>
        <div class="text-right">
            <p><span class="font-semibold">견적일자:</span> {{ $quotation->created_at->format('Y년 m월 d일') }}</p>
            <p><span class="font-semibold">제품구분:</span> {{ $quotation->product_category === 'STEEL' ? '철재' : '방화스크린' }}</p>
        </div>
    </div>

    {{-- 수신 / 공급자 --}}
    <table class="doc-table mb-6">
        <colgroup>
            <col style="width: 8%;"><col style="width: 42%;"><col style="width: 8%;"><col style="width: 42%;">
        </colgroup>
        <thead>
            <tr>
                <th colspan="2" class="text-center" style="background-color: #eff6ff;">수 신</th>
                <th colspan="2" class="text-center" style="background-color: #f0fdf4;">공 급 자</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <th>상 호</th><td>{{ $client['company'] ?? $quotation->title }}</td>
                <th>상 호</th><td>(주)코드브릿지엑스</td>
            </tr>
            <tr>
                <th>담당자</th><td>{{ $client['contact'] ?? '-' }}</td>
                <th>대 표</th><td>이의찬</td>
            </tr>
            <tr>
                <th>연락처</th><td>{{ $client['phone'] ?? '-' }}</td>
                <th>주 소</th><td>인천 남동구 남동대로 215번길 30</td>
            </tr>
            <tr>
                <th>이메일</th><td>{{ $client['email'] ?? '-' }}</td>
                <th>연락처</th><td>032-123-4567</td>
            </tr>
            @if(!empty($project['name']))
            <tr>
                <th>현장명</th><td colspan="3">{{ $project['name'] }}{{ !empty($project['location']) ? ' ('.$project['location'].')' : '' }}</td>
            </tr>
            @endif
        </tbody>
    </table>

    {{-- 합계 --}}
    <div class="mb-6 p-4 border-2 border-gray-800 text-center">
        <p class="text-sm mb-2">아래와 같이 견적합니다.</p>
        <p class="text-xl font-bold">
            합계금액: 금 {{ $subtotalKorean }}원정
            <span class="text-base font-normal">(&#8361;{{ number_format($subtotal) }})</span>
        </p>
        <p class="text-xs text-gray-600 mt-1">※ 부가가치세 별도</p>
    </div>

    {{-- 품목 테이블 --}}
    <table class="doc-table mb-6">
        <colgroup>
            <col style="width: 5%;"><col style="width: 10%;"><col style="width: 20%;">
            <col style="width: 15%;"><col style="width: 7%;"><col style="width: 7%;">
            <col style="width: 18%;"><col style="width: 18%;">
        </colgroup>
        <thead>
            <tr>
                <th class="text-center">No</th>
                <th class="text-center">위치</th>
                <th class="text-center">품 목</th>
                <th class="text-center">규 격</th>
                <th class="text-center">단위</th>
                <th class="text-center">수량</th>
                <th class="text-center">단가 (원)</th>
                <th class="text-center">금액 (원)</th>
            </tr>
        </thead>
        <tbody>
            @php $no = 1; @endphp

            {{-- 재료비 --}}
            @if($materialItems->isNotEmpty())
            <tr style="background-color: #eff6ff;">
                <td colspan="8" class="text-sm font-semibold" style="padding: 4px 10px;">[ 재료비 ]</td>
            </tr>
            @foreach($materialItems as $item)
            <tr>
                <td class="text-center">{{ $no++ }}</td>
                <td class="text-center text-xs">{{ $item->floor_code }}</td>
                <td>{{ $item->module_name }}</td>
                <td class="text-center text-xs">{{ $item->specification }}</td>
                <td class="text-center">{{ $item->unit }}</td>
                <td class="text-center">{{ number_format((float)$item->quantity, 0) }}</td>
                <td class="text-right">{{ number_format((float)$item->unit_price) }}</td>
                <td class="text-right">{{ number_format((float)$item->total_price) }}</td>
            </tr>
            @endforeach
            @endif

            {{-- 노무비 --}}
            @if($laborItems->isNotEmpty())
            <tr style="background-color: #f0fdf4;">
                <td colspan="8" class="text-sm font-semibold" style="padding: 4px 10px;">[ 노무비 ]</td>
            </tr>
            @foreach($laborItems as $item)
            <tr>
                <td class="text-center">{{ $no++ }}</td>
                <td class="text-center text-xs">{{ $item->floor_code }}</td>
                <td>{{ $item->module_name }}</td>
                <td class="text-center text-xs">{{ $item->specification }}</td>
                <td class="text-center">{{ $item->unit }}</td>
                <td class="text-center">{{ number_format((float)$item->quantity, 0) }}</td>
                <td class="text-right">{{ number_format((float)$item->unit_price) }}</td>
                <td class="text-right">{{ number_format((float)$item->total_price) }}</td>
            </tr>
            @endforeach
            @endif

            {{-- 설치비 --}}
            @if($installItems->isNotEmpty())
            <tr style="background-color: #fff7ed;">
                <td colspan="8" class="text-sm font-semibold" style="padding: 4px 10px;">[ 설치비 ]</td>
            </tr>
            @foreach($installItems as $item)
            <tr>
                <td class="text-center">{{ $no++ }}</td>
                <td class="text-center text-xs">{{ $item->floor_code }}</td>
                <td>{{ $item->module_name }}</td>
                <td class="text-center text-xs">{{ $item->specification }}</td>
                <td class="text-center">{{ $item->unit }}</td>
                <td class="text-center">{{ number_format((float)$item->quantity, 0) }}</td>
                <td class="text-right">{{ number_format((float)$item->unit_price) }}</td>
                <td class="text-right">{{ number_format((float)$item->total_price) }}</td>
            </tr>
            @endforeach
            @endif

            {{-- 기타 --}}
            @foreach($otherItems as $item)
            <tr>
                <td class="text-center">{{ $no++ }}</td>
                <td class="text-center text-xs">{{ $item->floor_code }}</td>
                <td>{{ $item->module_name }}</td>
                <td class="text-center text-xs">{{ $item->specification }}</td>
                <td class="text-center">{{ $item->unit }}</td>
                <td class="text-center">{{ number_format((float)$item->quantity, 0) }}</td>
                <td class="text-right">{{ number_format((float)$item->unit_price) }}</td>
                <td class="text-right">{{ number_format((float)$item->total_price) }}</td>
            </tr>
            @endforeach
        </tbody>
        <tfoot>
            {{-- 카테고리별 소계 --}}
            @if($materialItems->isNotEmpty())
            <tr>
                <th colspan="7" class="text-right text-xs">재료비 소계</th>
                <td class="text-right font-semibold">{{ number_format((int)$materialTotal) }}</td>
            </tr>
            @endif
            @if($laborItems->isNotEmpty())
            <tr>
                <th colspan="7" class="text-right text-xs">노무비 소계</th>
                <td class="text-right font-semibold">{{ number_format((int)$laborTotal) }}</td>
            </tr>
            @endif
            @if($installItems->isNotEmpty())
            <tr>
                <th colspan="7" class="text-right text-xs">설치비 소계</th>
                <td class="text-right font-semibold">{{ number_format((int)$installTotal) }}</td>
            </tr>
            @endif
            <tr>
                <th colspan="7" class="text-right">소 계</th>
                <td class="text-right font-bold">{{ number_format($subtotal) }}</td>
            </tr>
            @if($discountAmount > 0)
            <tr>
                <th colspan="7" class="text-right text-red-600">할인 ({{ $discountRate }}%)</th>
                <td class="text-right text-red-600">-{{ number_format($discountAmount) }}</td>
            </tr>
            @endif
            <tr>
                <th colspan="7" class="text-right">부가세 (10%)</th>
                <td class="text-right">{{ number_format($vatAmount) }}</td>
            </tr>
            <tr style="background-color: #f3f4f6;">
                <th colspan="7" class="text-right text-base">합 계</th>
                <td class="text-right font-bold text-base">{{ number_format($finalAmount) }}</td>
            </tr>
        </tfoot>
    </table>

    {{-- 비고 / 조건 --}}
    <div class="mb-8">
        <table class="doc-table">
            <thead><tr><th class="text-left">비 고</th></tr></thead>
            <tbody>
                <tr>
                    <td class="text-sm leading-relaxed" style="padding: 12px 16px;">
                        <ol class="list-decimal list-inside space-y-1">
                            <li>상기 금액은 부가가치세 별도입니다.</li>
                            <li>결제 조건: {{ $terms['payment'] ?? '계약 시 50%, 설치 완료 후 50%' }}</li>
                            <li>납기 조건: {{ $terms['delivery'] ?? '계약 후 4주 이내' }}</li>
                            <li>본 견적서의 유효기간: {{ $validUntil }}까지</li>
                            <li>설치 현장 여건에 따라 규격 및 금액이 변경될 수 있습니다.</li>
                            <li>세부 사항은 별도 협의를 통해 조정될 수 있습니다.</li>
                        </ol>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>

    {{-- 서명 --}}
    <div class="flex justify-end items-center gap-6 mt-12">
        <div class="text-right">
            <p class="text-lg font-bold mb-1">(주)코드브릿지엑스</p>
            <p class="text-sm text-gray-600">대표이사 이 의 찬</p>
        </div>
        <div class="seal-box">(인)</div>
    </div>

</div>
