<?php
// /bendingfee/_rowinput.php
// insert.php 또는 process.php 에서 extract($row) 이후에 include하여
// 방금 작업한 한 행(<tr>…</tr>)을 렌더링합니다.

// JSON으로 저장된 리스트 필드를 배열로 디코딩
$sumArr = json_decode($sumList, true);
if (!is_array($sumArr)) {
    $sumArr = [];
}

$colorArr = json_decode($colorList, true);
if (!is_array($colorArr)) {
    $colorArr = [];
}

$aArr = json_decode($AList, true);
if (!is_array($aArr)) {
    $aArr = [];
}

// 절곡 회수 계산: sumArr 중 false 아닌 요소 개수 – 1
$countSum = 0;
foreach ($sumArr as $v) {
    if ($v !== false) {
        $countSum++;
    }
}
$bendingCount = ($countSum > 0) ? $countSum - 1 : '';

// 역방향(음영) 개수 계산
$shadeCount = 0;
foreach ($colorArr as $v) {
    if ($v) {
        $shadeCount++;
    }
}

// A각 표시 개수 계산
$aCount = 0;
foreach ($aArr as $v) {
    if ($v) {
        $aCount++;
    }
}

$upload_dir = '../bending/img/';

// bendingfee/list.php의 테이블 구조에 맞게 변수 설정
$num = $row['num'] ?? '';
$major_category = $row['major_category'] ?? '';
$seconditem = $row['seconditem'] ?? '';
$model_name = $row['model_name'] ?? '';
$check_type = $row['check_type'] ?? '';
$finishing_type = $row['finishing_type'] ?? '';
$spec = $row['spec'] ?? '';
$box_width = $row['box_width'] ?? '';
$box_height = $row['box_height'] ?? '';
$front_bottom_width = $row['front_bottom_width'] ?? '';
$rail_width = $row['rail_width'] ?? '';
$unitprice = $row['unitprice'] ?? '';
$description = $row['description'] ?? '';
$priceDate = $row['priceDate'] ?? '';
$created_at = $row['created_at'] ?? '';
$updated_at = $row['updated_at'] ?? '';

// 이미지 URL 설정 (list.php와 동일한 로직)
$imgUrl = '';
if ($seconditem === '가이드레일') {
    $jsonFile = $_SERVER['DOCUMENT_ROOT'].'/guiderail/guiderail.json';
    if (file_exists($jsonFile)) {
        $jsonData = file_get_contents($jsonFile);
        $data = json_decode($jsonData, true);
        if (is_array($data)) {
            foreach ($data as $item) {
                if (
                    $item['model_name'] === $model_name &&
                    $item['check_type'] === $check_type &&
                    $item['finishing_type'] === $finishing_type
                ) {
                    $imgUrl = "<img src='{$item['image']}' alt='가이드레일 이미지' style='width:80px;height:auto;'>";
                    break;
                }
            }
        }
    }
} elseif ($seconditem === '케이스') {
    switch ($check_type) {
        case '양면 점검구':
            $imgUrl = "<img src='../img/box/box_both.png?v=1' alt='양면 점검구' style='width:80px;height:auto;'>";
            break;
        case '밑면 점검구':
            $imgUrl = "<img src='../img/box/box_bottom.png?v=1' alt='밑면 점검구' style='width:80px;height:auto;'>";
            break;
        case '후면 점검구':
            $imgUrl = "<img src='../img/box/box_back.png?v=1' alt='후면 점검구' style='width:80px;height:auto;'>";
            break;
        default:
            $imgUrl = "<img src='../img/box/box_both.png?v=1' alt='기본 케이스 이미지' style='width:80px;height:auto;'>";
            break;
    }
} elseif ($seconditem === '하단마감재') {
    $jsonFile = $_SERVER['DOCUMENT_ROOT'].'/bottombar/bottombar.json';
    if (file_exists($jsonFile)) {
        $jsonData = file_get_contents($jsonFile);
        $data = json_decode($jsonData, true);
        if (is_array($data)) {
            foreach ($data as $item) {
                if (
                    $item['model_name'] === $model_name &&
                    $item['finishing_type'] === $finishing_type
                ) {
                    $imgUrl = "<img src='{$item['image']}' alt='하단마감재 이미지' style='width:80px;height:auto;'>";
                    break;
                }
            }
        }
    }
} elseif ($seconditem === 'L-BAR') {
    $imgUrl = "<img src='../img/Lbar/Lbar2.jpg' alt='L-BAR 이미지' style='width:80px;height:auto;'>";
} elseif ($seconditem === '보강평철') {
    $imgUrl = "<img src='../bending/img/2024_09_28_11_48_24_평철.jpg' alt='보강평철 이미지' style='width:80px;height:auto;'>";
} elseif ($seconditem === '마구리') {
    $imgUrl = "<img src='../img/box/box_both_Maguri.jpg' alt='마구리 이미지' style='width:40px;height:auto;'>";
} elseif ($seconditem === '가이드레일용 연기차단재' || $seconditem === '케이스용 연기차단재') {
    $imgUrl = "<img src='../img/part/smokeban.jpg' alt='연기차단재 이미지' style='width:80px;height:auto;'>";
} else {
    $imgUrl = "<img src='../img/no_image.png' alt='No Image' style='width:80px;height:auto;'>";
}
?>

<tr data-num="<?= htmlspecialchars($num, ENT_QUOTES, 'UTF-8') ?>" onclick="redirectToView('<?= htmlspecialchars($num, ENT_QUOTES, 'UTF-8') ?>')">
    <td class="text-center"><?= htmlspecialchars($num, ENT_QUOTES, 'UTF-8') ?></td>
    <td class="text-center text-primary fw-bold"><?= htmlspecialchars($major_category, ENT_QUOTES, 'UTF-8') ?></td>
    <td class="text-center fw-bold"><?= htmlspecialchars($seconditem, ENT_QUOTES, 'UTF-8') ?></td>
    <td class="text-center"><?= htmlspecialchars($model_name, ENT_QUOTES, 'UTF-8') ?></td>
    <td class="text-center text-danger"><?= htmlspecialchars($check_type, ENT_QUOTES, 'UTF-8') ?></td>
    <td class="text-center"><?= htmlspecialchars($finishing_type, ENT_QUOTES, 'UTF-8') ?></td>
    <td class="text-center"><?= htmlspecialchars($spec, ENT_QUOTES, 'UTF-8') ?></td>
    <td class="text-center"><?= htmlspecialchars($box_width, ENT_QUOTES, 'UTF-8') ?></td>
    <td class="text-center"><?= htmlspecialchars($box_height, ENT_QUOTES, 'UTF-8') ?></td>
    <td class="text-center"><?= htmlspecialchars($front_bottom_width, ENT_QUOTES, 'UTF-8') ?></td>
    <td class="text-center"><?= htmlspecialchars($rail_width, ENT_QUOTES, 'UTF-8') ?></td>
    <td class="text-center"><?= $imgUrl ?></td>
    <td class="text-end"><?= htmlspecialchars($unitprice, ENT_QUOTES, 'UTF-8') ?></td>
    <td class="text-center"><?= htmlspecialchars($description, ENT_QUOTES, 'UTF-8') ?></td>
    <td class="text-center"><?= htmlspecialchars(substr($priceDate, 2), ENT_QUOTES, 'UTF-8') ?></td>
    <td class="text-center"><?= htmlspecialchars(substr($created_at, 5), ENT_QUOTES, 'UTF-8') ?></td>
    <td class="text-center"><?= htmlspecialchars(substr($updated_at, 5), ENT_QUOTES, 'UTF-8') ?></td>
</tr>
