{{--
    절곡 작업일지 전용 렌더링
    React BendingWorkLogContent.tsx + bending/ sub-components와 동일 구조

    필요 변수:
    - $document: Document model
    - $workOrder: work_orders row (stdClass)
    - $salesOrder: orders row (stdClass, nullable)
    - $workOrderItems: work_order_items collection
    - $bendingInfo: array (work_order.options.bending_info)
    - $itemLotMap: collection (work_order_item_id → lot_no)
--}}

@php
    // ============================================================
    // 상수 (React utils.ts와 동일)
    // ============================================================
    $SUS_DENSITY = 7.93;
    $EGI_DENSITY = 7.85;
    $WALL_PART_WIDTH = 412;
    $SIDE_PART_WIDTH = 462;
    $WALL_BASE_HEIGHT = 80;
    $SIDE_BASE_HEIGHT = 130;
    $BASE_WIDTH = 135;
    $BOTTOM_BAR_WIDTH = 184;
    $EXTRA_FINISH_WIDTH = 238;
    $SMOKE_BARRIER_WIDTH = 26;
    $BOX_FINISH_MATERIAL = 'EGI 1.55T';
    $BOX_COVER_LENGTH = 1219;

    $apiBaseUrl = rtrim(config('app.api_url', 'http://api.sam.kr'), '/');

    // ============================================================
    // 유틸 함수 (React utils.ts 포팅)
    // ============================================================

    $calcWeight = function(string $material, float $width, float $height) use ($SUS_DENSITY, $EGI_DENSITY): array {
        preg_match('/(\d+(\.\d+)?)/', $material, $m);
        $thickness = $m ? (float)$m[1] : 0;
        $isSUS = stripos($material, 'SUS') !== false;
        $density = $isSUS ? $SUS_DENSITY : $EGI_DENSITY;
        $volume_cm3 = ($thickness * $width * $height) / 1000;
        $weight_kg = ($volume_cm3 * $density) / 1000;
        return ['weight' => round($weight_kg, 2), 'type' => $isSUS ? 'SUS' : 'EGI'];
    };

    $parseBaseDimension = function(?string $baseDimension, float $fallbackHeight) use ($BASE_WIDTH): array {
        if ($baseDimension) {
            $parts = array_map('intval', explode('*', $baseDimension));
            if (count($parts) === 2 && $parts[0] > 0 && $parts[1] > 0) {
                return ['width' => $parts[0], 'height' => $parts[1]];
            }
        }
        return ['width' => $BASE_WIDTH, 'height' => $fallbackHeight];
    };

    // getMaterialMapping (React utils.ts 포팅)
    $getMaterialMapping = function(string $productCode, string $finishMaterial): array {
        if (in_array($productCode, ['KQTS01', 'KSS01', 'KSS02'])) {
            return [
                'guideRailFinish' => 'SUS 1.2T', 'bodyMaterial' => 'EGI 1.55T',
                'guideRailExtraFinish' => '', 'bottomBarFinish' => 'SUS 1.5T', 'bottomBarExtraFinish' => '없음',
            ];
        }
        if ($productCode === 'KTE01') {
            $isSUS = $finishMaterial === 'SUS마감';
            return [
                'guideRailFinish' => 'EGI 1.55T', 'bodyMaterial' => 'EGI 1.55T',
                'guideRailExtraFinish' => $isSUS ? 'SUS 1.2T' : '',
                'bottomBarFinish' => 'EGI 1.55T', 'bottomBarExtraFinish' => $isSUS ? 'SUS 1.2T' : '없음',
            ];
        }
        $isSUS = str_contains($finishMaterial ?? '', 'SUS');
        return [
            'guideRailFinish' => 'EGI 1.55T', 'bodyMaterial' => 'EGI 1.55T',
            'guideRailExtraFinish' => $isSUS ? 'SUS 1.2T' : '',
            'bottomBarFinish' => 'EGI 1.55T', 'bottomBarExtraFinish' => $isSUS ? 'SUS 1.2T' : '없음',
        ];
    };

    // 가이드레일 행 생성
    $buildGuideRailRows = function(array $lengthData, string $baseDimension, array $mapping, string $productCode, string $type) use ($calcWeight, $parseBaseDimension, $WALL_PART_WIDTH, $SIDE_PART_WIDTH, $WALL_BASE_HEIGHT, $SIDE_BASE_HEIGHT): array {
        $rows = [];
        $isWall = $type === 'wall';
        $partWidth = $isWall ? $WALL_PART_WIDTH : $SIDE_PART_WIDTH;
        $codePrefix = preg_replace('/\d+$/', '', $productCode) ?: 'KSS';
        $isSteel = $codePrefix === 'KTE';
        $isSUS = in_array($codePrefix, ['KSS', 'KQTS', 'KTE']);
        $finishPrefix = $isWall ? ($isSUS ? 'RS' : 'RE') : ($isSUS ? 'SS' : 'SE');
        $bodyPrefix = $isWall ? ($isSteel ? 'RT' : 'RM') : ($isSteel ? 'ST' : 'SM');

        foreach ($lengthData as $ld) {
            if (($ld['quantity'] ?? 0) <= 0) continue;
            $len = $ld['length']; $qty = $ld['quantity'];

            $fw = $calcWeight($mapping['guideRailFinish'], $partWidth, $len);
            $rows[] = ['partName' => '①②마감재', 'material' => $mapping['guideRailFinish'], 'length' => $len, 'quantity' => $qty, 'weight' => round($fw['weight'] * $qty, 2), 'lotPrefix' => $finishPrefix];

            $bw = $calcWeight($mapping['bodyMaterial'], $partWidth, $len);
            $rows[] = ['partName' => '③본체', 'material' => $mapping['bodyMaterial'], 'length' => $len, 'quantity' => $qty, 'weight' => round($bw['weight'] * $qty, 2), 'lotPrefix' => $bodyPrefix];
            $rows[] = ['partName' => '④C형', 'material' => $mapping['bodyMaterial'], 'length' => $len, 'quantity' => $qty, 'weight' => round($bw['weight'] * $qty, 2), 'lotPrefix' => $isWall ? 'RC' : 'SC'];
            $rows[] = ['partName' => '⑤D형', 'material' => $mapping['bodyMaterial'], 'length' => $len, 'quantity' => $qty, 'weight' => round($bw['weight'] * $qty, 2), 'lotPrefix' => $isWall ? 'RD' : 'SD'];

            if ($isWall && $mapping['guideRailExtraFinish']) {
                $ew = $calcWeight($mapping['guideRailExtraFinish'], $partWidth, $len);
                $rows[] = ['partName' => '⑥별도마감', 'material' => $mapping['guideRailExtraFinish'], 'length' => $len, 'quantity' => $qty, 'weight' => round($ew['weight'] * $qty, 2), 'lotPrefix' => 'YY'];
            }
        }

        $totalQty = array_sum(array_column($lengthData, 'quantity'));
        if ($totalQty > 0) {
            $baseDim = $parseBaseDimension($baseDimension, $isWall ? $WALL_BASE_HEIGHT : $SIDE_BASE_HEIGHT);
            $baseW = $calcWeight('EGI 1.55T', $baseDim['width'], $baseDim['height']);
            $rows[] = ['partName' => '하부BASE', 'material' => 'EGI 1.55T', 'length' => 0, 'quantity' => $totalQty, 'weight' => round($baseW['weight'] * $totalQty, 2), 'lotPrefix' => 'XX', 'isBase' => true];
        }
        return $rows;
    };

    // 하단마감재 행 생성
    $buildBottomBarRows = function(array $bottomBar, array $mapping, string $productCode) use ($calcWeight, $BOTTOM_BAR_WIDTH, $EXTRA_FINISH_WIDTH): array {
        $rows = [];
        $codePrefix = preg_replace('/\d+$/', '', $productCode) ?: 'KSS';
        $isSteel = $codePrefix === 'KTE';
        $lotPrefix = $isSteel ? 'TS' : (str_contains($mapping['bottomBarFinish'], 'SUS') ? 'BS' : 'BE');

        foreach ([3000, 4000] as $len) {
            $qtyKey = "length{$len}Qty";
            $qty = $bottomBar[$qtyKey] ?? 0;
            if ($qty <= 0) continue;
            $w = $calcWeight($mapping['bottomBarFinish'], $BOTTOM_BAR_WIDTH, $len);
            $rows[] = ['partName' => '①하단마감재', 'material' => $mapping['bottomBarFinish'], 'length' => $len, 'quantity' => $qty, 'weight' => round($w['weight'] * $qty, 2), 'lotPrefix' => $lotPrefix];
        }

        if ($mapping['bottomBarExtraFinish'] !== '없음' && $mapping['bottomBarExtraFinish']) {
            foreach ([3000, 4000] as $len) {
                $qtyKey = "length{$len}Qty";
                $qty = $bottomBar[$qtyKey] ?? 0;
                if ($qty <= 0) continue;
                $w = $calcWeight($mapping['bottomBarExtraFinish'], $EXTRA_FINISH_WIDTH, $len);
                $rows[] = ['partName' => '④별도마감재', 'material' => $mapping['bottomBarExtraFinish'], 'length' => $len, 'quantity' => $qty, 'weight' => round($w['weight'] * $qty, 2), 'lotPrefix' => 'YY'];
            }
        }
        return $rows;
    };

    // 셔터박스 행 생성
    $buildShutterBoxRows = function(array $box) use ($calcWeight, $BOX_FINISH_MATERIAL, $BOX_COVER_LENGTH): array {
        $rows = [];
        $sizeParts = array_map('intval', explode('*', $box['size'] ?? '500*380'));
        $boxWidth = $sizeParts[0] ?: 500;
        $boxHeight = $sizeParts[1] ?: 380;
        $isStandard = $box['size'] === '500*380';
        $direction = $box['direction'] ?? '양면';

        if ($isStandard) {
            $parts = [
                ['name' => '①전면부', 'prefix' => 'CF', 'dim' => $boxHeight + 122],
                ['name' => '②린텔부', 'prefix' => 'CL', 'dim' => $boxWidth - 330],
                ['name' => '③⑤점검구', 'prefix' => 'CP', 'dim' => $boxWidth - 200],
                ['name' => '④후면코너부', 'prefix' => 'CB', 'dim' => 170],
            ];
        } elseif ($direction === '양면') {
            $parts = [
                ['name' => '①전면부', 'prefix' => 'XX', 'dim' => $boxHeight + 122],
                ['name' => '②린텔부', 'prefix' => 'CL', 'dim' => $boxWidth - 330],
                ['name' => '③점검구', 'prefix' => 'XX', 'dim' => $boxWidth - 200],
                ['name' => '④후면코너부', 'prefix' => 'CB', 'dim' => 170],
                ['name' => '⑤점검구', 'prefix' => 'XX', 'dim' => $boxHeight - 100],
            ];
        } elseif ($direction === '밑면') {
            $parts = [
                ['name' => '①전면부', 'prefix' => 'XX', 'dim' => $boxHeight + 122],
                ['name' => '②린텔부', 'prefix' => 'CL', 'dim' => $boxWidth - 330],
                ['name' => '③점검구', 'prefix' => 'XX', 'dim' => $boxWidth - 200],
                ['name' => '④후면부', 'prefix' => 'CB', 'dim' => $boxHeight + 85 * 2],
            ];
        } elseif ($direction === '후면') {
            $parts = [
                ['name' => '①전면부', 'prefix' => 'XX', 'dim' => $boxHeight + 122],
                ['name' => '②린텔부', 'prefix' => 'CL', 'dim' => $boxWidth + 85 * 2],
                ['name' => '③점검구', 'prefix' => 'XX', 'dim' => $boxHeight - 200],
                ['name' => '④후면코너부', 'prefix' => 'CB', 'dim' => $boxHeight + 85 * 2],
            ];
        } else {
            $parts = [];
        }

        foreach ($parts as $p) {
            foreach ($box['lengthData'] ?? [] as $ld) {
                if (($ld['quantity'] ?? 0) <= 0) continue;
                $w = $calcWeight($BOX_FINISH_MATERIAL, $p['dim'], $ld['length']);
                $rows[] = ['partName' => $p['name'], 'material' => $BOX_FINISH_MATERIAL, 'dimension' => (string)$ld['length'], 'quantity' => $ld['quantity'], 'weight' => round($w['weight'] * $ld['quantity'], 2), 'lotPrefix' => $p['prefix']];
            }
        }

        $coverQty = $box['coverQty'] ?? 0;
        if ($coverQty > 0) {
            $coverWidth = $boxWidth - 111;
            $w = $calcWeight($BOX_FINISH_MATERIAL, $coverWidth, $BOX_COVER_LENGTH);
            $coverName = $isStandard ? '⑤상부덮개' : ($direction === '양면' ? '⑥상부덮개' : '⑤상부덮개');
            $rows[] = ['partName' => $coverName, 'material' => $BOX_FINISH_MATERIAL, 'dimension' => "1219 * {$coverWidth}", 'quantity' => $coverQty, 'weight' => round($w['weight'] * $coverQty, 2), 'lotPrefix' => 'XX'];
        }

        $finCoverQty = $box['finCoverQty'] ?? 0;
        if ($finCoverQty > 0) {
            $w = $calcWeight($BOX_FINISH_MATERIAL, $boxWidth, $boxHeight);
            $finName = $isStandard ? '⑥측면부(마구리)' : ($direction === '양면' ? '⑦측면부(마구리)' : '⑥측면부(마구리)');
            $rows[] = ['partName' => $finName, 'material' => $BOX_FINISH_MATERIAL, 'dimension' => ($boxWidth+5).' * '.($boxHeight+5), 'quantity' => $finCoverQty, 'weight' => round($w['weight'] * $finCoverQty, 2), 'lotPrefix' => 'XX'];
        }
        return $rows;
    };

    // lengthToCode (React utils.ts getSLengthCode 포팅)
    $lengthToCode = function(int $length, string $category = ''): ?string {
        if ($category === '연기차단재50') return match($length) { 3000 => '53', 4000 => '54', default => null };
        if ($category === '연기차단재80') return match($length) { 3000 => '83', 4000 => '84', default => null };
        $map = [1219 => '12', 2438 => '24', 3000 => '30', 3500 => '35', 4000 => '40', 4150 => '41', 4200 => '42', 4300 => '43'];
        return $map[$length] ?? null;
    };

    // 연기차단재 행 생성
    $buildSmokeBarrierRows = function(array $smokeBarrier) use ($calcWeight, $SMOKE_BARRIER_WIDTH, $lengthToCode): array {
        $rows = [];
        foreach ($smokeBarrier['w50'] ?? [] as $ld) {
            if (($ld['quantity'] ?? 0) <= 0) continue;
            $w = $calcWeight('EGI 0.8T', $SMOKE_BARRIER_WIDTH, $ld['length']);
            $code = $lengthToCode($ld['length'], '연기차단재50');
            $rows[] = ['partName' => '레일용 [W50]', 'material' => 'EGI 0.8T', 'length' => $ld['length'], 'quantity' => $ld['quantity'], 'weight' => round($w['weight'] * $ld['quantity'], 2), 'lotCode' => $code ? "GI-{$code}" : 'GI'];
        }
        $w80Qty = $smokeBarrier['w80Qty'] ?? 0;
        if ($w80Qty > 0) {
            $w = $calcWeight('EGI 0.8T', $SMOKE_BARRIER_WIDTH, 3000);
            $code = $lengthToCode(3000, '연기차단재80');
            $rows[] = ['partName' => '케이스용 [W80]', 'material' => 'EGI 0.8T', 'length' => 3000, 'quantity' => $w80Qty, 'weight' => round($w['weight'] * $w80Qty, 2), 'lotCode' => $code ? "GI-{$code}" : 'GI'];
        }
        return $rows;
    };

    // 생산량 합계 계산
    $calculateProductionSummary = function(array $bi, array $mapping) use ($calcWeight, $parseBaseDimension, $buildShutterBoxRows, $buildSmokeBarrierRows, $WALL_PART_WIDTH, $SIDE_PART_WIDTH, $BOTTOM_BAR_WIDTH, $EXTRA_FINISH_WIDTH, $WALL_BASE_HEIGHT, $SIDE_BASE_HEIGHT): array {
        $susTotal = 0; $egiTotal = 0;
        $addWeight = function(string $mat, float $w, float $h, int $qty) use (&$susTotal, &$egiTotal, $calcWeight) {
            if ($qty <= 0) return;
            $r = $calcWeight($mat, $w, $h);
            $total = $r['weight'] * $qty;
            if ($r['type'] === 'SUS') $susTotal += $total; else $egiTotal += $total;
        };
        // 가이드레일 벽면형
        if (!empty($bi['guideRail']['wall'])) {
            foreach ($bi['guideRail']['wall']['lengthData'] ?? [] as $ld) {
                if (($ld['quantity'] ?? 0) <= 0) continue;
                $addWeight($mapping['guideRailFinish'], $WALL_PART_WIDTH, $ld['length'], $ld['quantity']);
                $addWeight($mapping['bodyMaterial'], $WALL_PART_WIDTH, $ld['length'], $ld['quantity'] * 3);
                if ($mapping['guideRailExtraFinish']) $addWeight($mapping['guideRailExtraFinish'], $WALL_PART_WIDTH, $ld['length'], $ld['quantity']);
            }
            $totalWallQty = array_sum(array_column($bi['guideRail']['wall']['lengthData'] ?? [], 'quantity'));
            $wallBase = $parseBaseDimension($bi['guideRail']['wall']['baseDimension'] ?? null, $WALL_BASE_HEIGHT);
            $addWeight('EGI 1.55T', $wallBase['width'], $wallBase['height'], $totalWallQty);
        }
        // 가이드레일 측면형
        if (!empty($bi['guideRail']['side'])) {
            foreach ($bi['guideRail']['side']['lengthData'] ?? [] as $ld) {
                if (($ld['quantity'] ?? 0) <= 0) continue;
                $addWeight($mapping['guideRailFinish'], $SIDE_PART_WIDTH, $ld['length'], $ld['quantity']);
                $addWeight($mapping['bodyMaterial'], $SIDE_PART_WIDTH, $ld['length'], $ld['quantity'] * 3);
            }
            $totalSideQty = array_sum(array_column($bi['guideRail']['side']['lengthData'] ?? [], 'quantity'));
            $sideBase = $parseBaseDimension($bi['guideRail']['side']['baseDimension'] ?? null, $SIDE_BASE_HEIGHT);
            $addWeight('EGI 1.55T', $sideBase['width'], $sideBase['height'], $totalSideQty);
        }
        // 하단마감재
        foreach ([3000, 4000] as $len) {
            $qty = $bi['bottomBar']["length{$len}Qty"] ?? 0;
            if ($qty > 0) {
                $addWeight($mapping['bottomBarFinish'], $BOTTOM_BAR_WIDTH, $len, $qty);
                if ($mapping['bottomBarExtraFinish'] !== '없음' && $mapping['bottomBarExtraFinish']) {
                    $addWeight($mapping['bottomBarExtraFinish'], $EXTRA_FINISH_WIDTH, $len, $qty);
                }
            }
        }
        // 셔터박스
        foreach ($bi['shutterBox'] ?? [] as $box) {
            foreach ($buildShutterBoxRows($box) as $row) {
                if ($row['weight'] > 0) $egiTotal += $row['weight'];
            }
        }
        // 연기차단재
        foreach ($buildSmokeBarrierRows($bi['smokeBarrier'] ?? []) as $row) {
            if ($row['weight'] > 0) $egiTotal += $row['weight'];
        }
        return ['susTotal' => round($susTotal, 2), 'egiTotal' => round($egiTotal, 2), 'grandTotal' => round($susTotal + $egiTotal, 2)];
    };

    // 포맷 헬퍼
    $fmt = fn($v) => ($v !== null && $v > 0) ? number_format($v) : '-';
    $fmtWeight = fn($v) => $v > 0 ? number_format($v, 2) : '-';

    // lookupLotNo (React utils.ts 포팅 - lotNoMap에서 LOT NO 조회)
    $lookupLotNo = function(array $lotNoMap, string $prefix, ?int $length = null) use ($lengthToCode): string {
        if (empty($lotNoMap)) return '-';
        // 1. 정확한 매칭 (prefix + lengthCode)
        if ($length) {
            $code = $lengthToCode($length);
            if ($code && isset($lotNoMap["BD-{$prefix}-{$code}"])) return $lotNoMap["BD-{$prefix}-{$code}"];
        }
        // 2. Fallback: prefix만으로 매칭
        $prefixKey = "BD-{$prefix}-";
        foreach ($lotNoMap as $k => $v) {
            if (str_starts_with($k, $prefixKey)) return $v;
        }
        return '-';
    };

    // ============================================================
    // 데이터 준비
    // ============================================================
    $bi = $bendingInfo ?? [];
    $hasBendingData = !empty($bi['productCode']);
    $mapping = $hasBendingData ? $getMaterialMapping($bi['productCode'], $bi['finishMaterial'] ?? '') : null;
    $summary = ($hasBendingData && $mapping) ? $calculateProductionSummary($bi, $mapping) : ['susTotal' => 0, 'egiTotal' => 0, 'grandTotal' => 0];

    // 날짜 포맷
    $today = now()->format('Y-m-d');
    $fullDate = now()->format('Y년 n월 j일');

    // 기본 정보
    $documentNo = $workOrder->work_order_no ?? '-';
    $primaryAssignee = '-';
    // work_order_assignees에서 primary 찾기
    if ($workOrder) {
        $assignee = \Illuminate\Support\Facades\DB::table('work_order_assignees')
            ->join('users', 'users.id', '=', 'work_order_assignees.user_id')
            ->where('work_order_assignees.work_order_id', $workOrder->id)
            ->where('work_order_assignees.is_primary', true)
            ->select('users.name')
            ->first();
        $primaryAssignee = $assignee->name ?? '-';
    }
    $dueDate = $salesOrder->delivery_date ?? $workOrder->scheduled_date ?? null;
    $formattedDueDate = $dueDate ? \Carbon\Carbon::parse($dueDate)->format('Y-m-d') : '-';

    // 담당자(수주 작성자) 조회
    $salesWriterName = '-';
    if ($salesOrder && $salesOrder->writer_id) {
        $salesWriter = \Illuminate\Support\Facades\DB::table('users')->where('id', $salesOrder->writer_id)->first();
        $salesWriterName = $salesWriter->name ?? '-';
    }

    // LOT NO (work_order에 lot_no 컬럼 없음 - work_order_no 활용)
    $lotNo = $workOrder->work_order_no ?? '-';

    // lotNoMap 빌드: item.code (BD-{prefix}-{lengthCode}) → lot_no 매핑
    $lotNoMap = [];
    if ($workOrder) {
        $lotRows = \Illuminate\Support\Facades\DB::table('work_order_material_inputs as mi')
            ->join('stock_lots as sl', 'sl.id', '=', 'mi.stock_lot_id')
            ->join('items as i', 'i.id', '=', 'mi.item_id')
            ->where('mi.work_order_id', $workOrder->id)
            ->select('i.code', 'sl.lot_no')
            ->distinct()
            ->get();
        foreach ($lotRows as $lr) {
            if ($lr->code && !isset($lotNoMap[$lr->code])) {
                $lotNoMap[$lr->code] = $lr->lot_no;
            }
        }
    }
@endphp

<div class="bg-white rounded-lg shadow-sm border border-gray-200 p-6">
    {{-- ===== 헤더 ===== --}}
    <div class="flex justify-between items-start mb-6">
        <div>
            <h1 class="text-2xl font-bold">작업일지 (절곡)</h1>
            <p class="text-xs text-gray-500 mt-1">
                문서번호: {{ $documentNo }} | 작성일자: {{ $fullDate }}
            </p>
        </div>
        {{-- 결재란 --}}
        <table class="border-collapse text-xs flex-shrink-0">
            <thead>
                <tr>
                    <th class="border border-gray-400 bg-gray-100 px-3 py-1 text-center">작성</th>
                    <th class="border border-gray-400 bg-gray-100 px-3 py-1 text-center">검토</th>
                    <th class="border border-gray-400 bg-gray-100 px-3 py-1 text-center">승인</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td class="border border-gray-400 px-3 py-3 text-center min-w-[60px]">{{ $primaryAssignee }}</td>
                    <td class="border border-gray-400 px-3 py-3 text-center min-w-[60px]"></td>
                    <td class="border border-gray-400 px-3 py-3 text-center min-w-[60px]"></td>
                </tr>
            </tbody>
        </table>
    </div>

    {{-- ===== 신청업체 / 신청내용 ===== --}}
    <table class="w-full border-collapse text-xs mb-6">
        <thead>
            <tr>
                <th class="border border-gray-400 bg-gray-100 px-3 py-2 text-center" colspan="2">신청업체</th>
                <th class="border border-gray-400 bg-gray-100 px-3 py-2 text-center" colspan="2">신청내용</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td class="border border-gray-400 bg-gray-50 px-3 py-2 font-medium w-24">수주일</td>
                <td class="border border-gray-400 px-3 py-2">{{ $salesOrder?->received_at ? \Carbon\Carbon::parse($salesOrder->received_at)->format('Y-m-d') : '-' }}</td>
                <td class="border border-gray-400 bg-gray-50 px-3 py-2 font-medium w-24">현장명</td>
                <td class="border border-gray-400 px-3 py-2">{{ $salesOrder->site_name ?? $workOrder->project_name ?? '-' }}</td>
            </tr>
            <tr>
                <td class="border border-gray-400 bg-gray-50 px-3 py-2 font-medium">수주처</td>
                <td class="border border-gray-400 px-3 py-2">{{ $salesOrder->client_name ?? '-' }}</td>
                <td class="border border-gray-400 bg-gray-50 px-3 py-2 font-medium">작업일자</td>
                <td class="border border-gray-400 px-3 py-2">{{ $today }}</td>
            </tr>
            <tr>
                <td class="border border-gray-400 bg-gray-50 px-3 py-2 font-medium">담당자</td>
                <td class="border border-gray-400 px-3 py-2">{{ $salesWriterName }}</td>
                <td class="border border-gray-400 bg-gray-50 px-3 py-2 font-medium">제품 LOT NO</td>
                <td class="border border-gray-400 px-3 py-2">{{ $lotNo }}</td>
            </tr>
            <tr>
                <td class="border border-gray-400 bg-gray-50 px-3 py-2 font-medium">연락처</td>
                <td class="border border-gray-400 px-3 py-2">{{ $salesOrder->client_contact ?? '-' }}</td>
                <td class="border border-gray-400 bg-gray-50 px-3 py-2 font-medium">생산담당자</td>
                <td class="border border-gray-400 px-3 py-2">{{ $primaryAssignee }}</td>
            </tr>
            <tr>
                <td class="border border-gray-400 bg-gray-50 px-3 py-2 font-medium" colspan="2"></td>
                <td class="border border-gray-400 bg-gray-50 px-3 py-2 font-medium">출고예정일</td>
                <td class="border border-gray-400 px-3 py-2">{{ $formattedDueDate }}</td>
            </tr>
        </tbody>
    </table>

    {{-- ===== 제품 정보 ===== --}}
    <table class="w-full border-collapse text-xs mb-6">
        <thead>
            <tr class="bg-gray-100">
                <th class="border border-gray-400 p-2">제품명</th>
                <th class="border border-gray-400 p-2">재질</th>
                <th class="border border-gray-400 p-2">마감</th>
                <th class="border border-gray-400 p-2">유형</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td class="border border-gray-400 p-2">{{ $hasBendingData ? $bi['productCode'] : '-' }}</td>
                <td class="border border-gray-400 p-2">{{ $mapping['bodyMaterial'] ?? '-' }}</td>
                <td class="border border-gray-400 p-2">{{ $hasBendingData ? $bi['finishMaterial'] : '-' }}</td>
                <td class="border border-gray-400 p-2">{{ $hasBendingData ? ($bi['common']['type'] ?? '-') : '-' }}</td>
            </tr>
        </tbody>
    </table>

    @if($hasBendingData && $mapping)
        {{-- ===== 1. 가이드레일 ===== --}}
        @php
            $wallRows = !empty($bi['guideRail']['wall']) ? $buildGuideRailRows($bi['guideRail']['wall']['lengthData'] ?? [], $bi['guideRail']['wall']['baseDimension'] ?? '135*80', $mapping, $bi['productCode'], 'wall') : [];
            $sideRows = !empty($bi['guideRail']['side']) ? $buildGuideRailRows($bi['guideRail']['side']['lengthData'] ?? [], $bi['guideRail']['side']['baseDimension'] ?? '135*130', $mapping, $bi['productCode'], 'side') : [];
        @endphp
        @if(count($wallRows) > 0 || count($sideRows) > 0)
        <div class="mb-6">
            <div class="bg-gray-800 text-white text-xs font-bold px-3 py-1.5 mb-2">1. 가이드레일</div>

            @foreach(['wall' => $wallRows, 'side' => $sideRows] as $grType => $grRows)
                @if(count($grRows) > 0)
                @php
                    $grTitle = $grType === 'wall' ? '1.1 벽면형' : '1.2 측면형';
                    $grData = $bi['guideRail'][$grType] ?? [];
                    $baseSize = $grData['baseDimension'] ?? $grData['baseSize'] ?? '-';
                @endphp
                <div class="mb-4">
                    <div class="text-xs font-bold mb-1">{{ $grTitle }}</div>
                    <div class="flex-1">
                        <table class="w-full border-collapse text-xs">
                            <thead>
                                <tr class="bg-gray-100">
                                    <th class="border border-gray-400 px-1 py-0.5">세부품명</th>
                                    <th class="border border-gray-400 px-1 py-0.5">재질</th>
                                    <th class="border border-gray-400 px-1 py-0.5">길이</th>
                                    <th class="border border-gray-400 px-1 py-0.5">수량</th>
                                    <th class="border border-gray-400 px-1 py-0.5">LOT NO</th>
                                    <th class="border border-gray-400 px-1 py-0.5">무게(kg)</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($grRows as $row)
                                <tr>
                                    <td class="border border-gray-400 px-1 py-0.5">{{ $row['partName'] }}</td>
                                    <td class="border border-gray-400 px-1 py-0.5 text-center">{{ $row['material'] }}</td>
                                    <td class="border border-gray-400 px-1 py-0.5 text-center">{{ ($row['isBase'] ?? false) ? $baseSize : $fmt($row['length']) }}</td>
                                    <td class="border border-gray-400 px-1 py-0.5 text-center">{{ $fmt($row['quantity']) }}</td>
                                    <td class="border border-gray-400 px-1 py-0.5 text-center">{{ $lookupLotNo($lotNoMap, $row['lotPrefix'], $row['length'] ?? null) }}</td>
                                    <td class="border border-gray-400 px-1 py-0.5 text-center">{{ $fmtWeight($row['weight']) }}</td>
                                </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>
                @endif
            @endforeach
        </div>
        @endif

        {{-- ===== 2. 하단마감재 ===== --}}
        @php
            $bottomRows = $buildBottomBarRows($bi['bottomBar'] ?? [], $mapping, $bi['productCode']);
        @endphp
        @if(count($bottomRows) > 0)
        <div class="mb-6">
            <div class="bg-gray-800 text-white text-xs font-bold px-3 py-1.5 mb-2">2. 하단마감재</div>
            <div class="flex-1">
                <table class="w-full border-collapse text-xs">
                    <thead>
                        <tr class="bg-gray-100">
                            <th class="border border-gray-400 px-1 py-0.5">세부품명</th>
                            <th class="border border-gray-400 px-1 py-0.5">재질</th>
                            <th class="border border-gray-400 px-1 py-0.5">길이</th>
                            <th class="border border-gray-400 px-1 py-0.5">수량</th>
                            <th class="border border-gray-400 px-1 py-0.5">LOT NO</th>
                            <th class="border border-gray-400 px-1 py-0.5">무게(kg)</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($bottomRows as $row)
                        <tr>
                            <td class="border border-gray-400 px-1 py-0.5">{{ $row['partName'] }}</td>
                            <td class="border border-gray-400 px-1 py-0.5 text-center">{{ $row['material'] }}</td>
                            <td class="border border-gray-400 px-1 py-0.5 text-center">{{ $fmt($row['length']) }}</td>
                            <td class="border border-gray-400 px-1 py-0.5 text-center">{{ $fmt($row['quantity']) }}</td>
                            <td class="border border-gray-400 px-1 py-0.5 text-center">{{ $lookupLotNo($lotNoMap, $row['lotPrefix'], $row['length'] ?? null) }}</td>
                            <td class="border border-gray-400 px-1 py-0.5 text-center">{{ $fmtWeight($row['weight']) }}</td>
                        </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        </div>
        @endif

        {{-- ===== 3. 셔터박스 ===== --}}
        @if(!empty($bi['shutterBox']))
        <div class="mb-6">
            <div class="bg-gray-800 text-white text-xs font-bold px-3 py-1.5 mb-2">3. 셔터박스</div>
            @foreach($bi['shutterBox'] as $boxIdx => $box)
                @php $boxRows = $buildShutterBoxRows($box); @endphp
                @if(count($boxRows) > 0)
                <div class="mb-4">
                    <div class="text-xs font-bold mb-1">3.{{ $boxIdx + 1 }} 셔터박스 [{{ $box['size'] ?? '-' }}] {{ $box['direction'] ?? '' }}</div>
                    <div class="flex-1">
                        <table class="w-full border-collapse text-xs">
                            <thead>
                                <tr class="bg-gray-100">
                                    <th class="border border-gray-400 px-1 py-0.5">구성요소</th>
                                    <th class="border border-gray-400 px-1 py-0.5">재질</th>
                                    <th class="border border-gray-400 px-1 py-0.5">길이/치수</th>
                                    <th class="border border-gray-400 px-1 py-0.5">수량</th>
                                    <th class="border border-gray-400 px-1 py-0.5">LOT NO</th>
                                    <th class="border border-gray-400 px-1 py-0.5">무게(kg)</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($boxRows as $row)
                                @php
                                    // 셔터박스 LOT: dimension에서 숫자 추출하여 길이코드로 변환
                                    $boxDimNum = is_numeric($row['dimension']) ? (int)$row['dimension'] : null;
                                    $boxLot = $boxDimNum ? $lookupLotNo($lotNoMap, $row['lotPrefix'], $boxDimNum) : $lookupLotNo($lotNoMap, $row['lotPrefix']);
                                @endphp
                                <tr>
                                    <td class="border border-gray-400 px-1 py-0.5">{{ $row['partName'] }}</td>
                                    <td class="border border-gray-400 px-1 py-0.5 text-center">{{ $row['material'] }}</td>
                                    <td class="border border-gray-400 px-1 py-0.5 text-center">{{ $row['dimension'] }}</td>
                                    <td class="border border-gray-400 px-1 py-0.5 text-center">{{ $fmt($row['quantity']) }}</td>
                                    <td class="border border-gray-400 px-1 py-0.5 text-center">{{ $boxLot }}</td>
                                    <td class="border border-gray-400 px-1 py-0.5 text-center">{{ $fmtWeight($row['weight']) }}</td>
                                </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>
                @endif
            @endforeach
        </div>
        @endif

        {{-- ===== 4. 연기차단재 ===== --}}
        @php
            $smokeRows = $buildSmokeBarrierRows($bi['smokeBarrier'] ?? []);
        @endphp
        @if(count($smokeRows) > 0)
        <div class="mb-6">
            <div class="bg-gray-800 text-white text-xs font-bold px-3 py-1.5 mb-2">4. 연기차단재</div>
            <div class="flex-1">
                <table class="w-full border-collapse text-xs">
                    <thead>
                        <tr class="bg-gray-100">
                            <th class="border border-gray-400 px-1 py-0.5">파트</th>
                            <th class="border border-gray-400 px-1 py-0.5">재질</th>
                            <th class="border border-gray-400 px-1 py-0.5">길이</th>
                            <th class="border border-gray-400 px-1 py-0.5">수량</th>
                            <th class="border border-gray-400 px-1 py-0.5">LOT NO</th>
                            <th class="border border-gray-400 px-1 py-0.5">무게(kg)</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($smokeRows as $row)
                        @php
                            // 연기차단재 LOT: lotCode (GI-53, GI-83 등) 사용
                            $smokeLotCode = $row['lotCode'] ?? 'GI';
                            $smokeLot = isset($lotNoMap["BD-{$smokeLotCode}"]) ? $lotNoMap["BD-{$smokeLotCode}"] : $lookupLotNo($lotNoMap, explode('-', $smokeLotCode)[0], $row['length']);
                        @endphp
                        <tr>
                            <td class="border border-gray-400 px-1 py-0.5">{{ $row['partName'] }}</td>
                            <td class="border border-gray-400 px-1 py-0.5 text-center">{{ $row['material'] }}</td>
                            <td class="border border-gray-400 px-1 py-0.5 text-center">{{ $fmt($row['length']) }}</td>
                            <td class="border border-gray-400 px-1 py-0.5 text-center">{{ $fmt($row['quantity']) }}</td>
                            <td class="border border-gray-400 px-1 py-0.5 text-center">{{ $smokeLot }}</td>
                            <td class="border border-gray-400 px-1 py-0.5 text-center">{{ $fmtWeight($row['weight']) }}</td>
                        </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        </div>
        @endif
    @else
        <div class="text-center text-gray-400 text-sm py-8 border border-gray-300 mb-6">
            절곡 데이터가 없습니다. (bending_info 미등록)
        </div>
    @endif

    {{-- ===== 생산량 합계 ===== --}}
    <div class="mb-6">
        <table class="w-full border-collapse text-xs">
            <thead>
                <tr class="bg-gray-100">
                    <th class="border border-gray-400 px-3 py-2">생산량 합계 [kg]</th>
                    <th class="border border-gray-400 px-3 py-2">SUS</th>
                    <th class="border border-gray-400 px-3 py-2">EGI</th>
                    <th class="border border-gray-400 px-3 py-2">합계</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td class="border border-gray-400 px-3 py-2 text-center font-medium">무게</td>
                    <td class="border border-gray-400 px-3 py-2 text-center">{{ $summary['susTotal'] > 0 ? number_format($summary['susTotal'], 2) . ' kg' : '-' }}</td>
                    <td class="border border-gray-400 px-3 py-2 text-center">{{ $summary['egiTotal'] > 0 ? number_format($summary['egiTotal'], 2) . ' kg' : '-' }}</td>
                    <td class="border border-gray-400 px-3 py-2 text-center font-bold">{{ $summary['grandTotal'] > 0 ? number_format($summary['grandTotal'], 2) . ' kg' : '-' }}</td>
                </tr>
            </tbody>
        </table>
    </div>

    {{-- ===== 비고 ===== --}}
    <table class="w-full border-collapse text-xs">
        <tbody>
            <tr>
                <td class="border border-gray-400 bg-gray-100 px-3 py-2 font-medium w-40 align-top">비고</td>
                <td class="border border-gray-400 px-3 py-3 min-h-[60px]">
                    {{ $workOrder->memo ?? '' }}
                </td>
            </tr>
        </tbody>
    </table>
</div>