<?php
// /bending/_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/';
?>
<tr
    data-num="<?= htmlspecialchars($num, ENT_QUOTES, 'UTF-8') ?>"
    onclick="redirectToView(
        '<?= htmlspecialchars($num, ENT_QUOTES, 'UTF-8') ?>',
        '<?= htmlspecialchars($tablename, ENT_QUOTES, 'UTF-8') ?>'
    )"
>
    <td class="text-center"><?= htmlspecialchars($num, ENT_QUOTES, 'UTF-8') ?></td>
    <td class="text-center"><?= htmlspecialchars($item_sep, ENT_QUOTES, 'UTF-8') ?></td>
    <td class="text-center"><?= htmlspecialchars($model_UA, ENT_QUOTES, 'UTF-8') ?></td>
    <td class="text-center text-dark fw-bold">
        <?= htmlspecialchars($item_bending, ENT_QUOTES, 'UTF-8') ?>
    </td>
    <td class="text-start text-primary">
        <?= htmlspecialchars($itemName, ENT_QUOTES, 'UTF-8') ?>
    </td>
    <td class="text-center text-danger">
        <?= htmlspecialchars($item_spec, ENT_QUOTES, 'UTF-8') ?>
    </td>
    <td class="text-center">
        <?php if (!empty($imgdata)): ?>
            <img
                src="<?= htmlspecialchars($upload_dir . $imgdata, ENT_QUOTES, 'UTF-8') ?>"
                alt="형상 이미지"
                style="max-width:100px; max-height:50px;"
            >
        <?php else: ?>
            <span class="text-secondary">이미지 없음</span>
        <?php endif; ?>
    </td>
    <td class="text-center fw-bold">
        <?= htmlspecialchars($material, ENT_QUOTES, 'UTF-8') ?>
    </td>
    <td class="text-center">
        <?= htmlspecialchars(end($sumArr), ENT_QUOTES, 'UTF-8') ?>
    </td>
    <td class="text-center text-danger fw-bold">
        <?= $bendingCount ?>
    </td>
    <td class="text-center">
        <?= $shadeCount ? $shadeCount : '' ?>
    </td>
    <td class="text-center">
        <?= $aCount ? $aCount : '' ?>
    </td>
    <td class="text-center">
        <?= htmlspecialchars($widthsum, ENT_QUOTES, 'UTF-8') ?>
    </td>
    <td class="text-center text-secondary"
        title="<?= htmlspecialchars($author, ENT_QUOTES, 'UTF-8') ?>">
        <?= htmlspecialchars($author, ENT_QUOTES, 'UTF-8') ?>
    </td>
    <td class="text-center text-secondary">
        <?= htmlspecialchars($search_keyword, ENT_QUOTES, 'UTF-8') ?>
    </td>
    <td class="text-center text-secondary"
        title="<?= htmlspecialchars($memo, ENT_QUOTES, 'UTF-8') ?>">
        <?= htmlspecialchars($memo, ENT_QUOTES, 'UTF-8') ?>
    </td>
</tr>
