<?php
require_once($_SERVER['DOCUMENT_ROOT'] . "/session.php");
require_once($_SERVER['DOCUMENT_ROOT'] . "/lib/mydb.php");
$pdo = db_connect();

$model_id = isset($_GET['model_id']) ? intval($_GET['model_id']) : 0;
if ($model_id <= 0) {
    echo "Invalid model ID";
    exit;
}

global $DB;

// 2단계 부품 조회
$sql_parts = "SELECT * FROM " . $DB . ".parts WHERE model_id = ? AND is_deleted = 0 ORDER BY part_id ASC";
$stmt_parts = $pdo->prepare($sql_parts);
$stmt_parts->execute([$model_id]);
$parts = $stmt_parts->fetchAll(PDO::FETCH_ASSOC);

if (!$parts) {
    echo "해당 모델의 부품 정보가 없습니다.";
    exit;
}

echo '<div class="child-details">';

foreach ($parts as $part) {
    echo '<div class="part-section" style="margin-bottom:20px;">';
    
    // 2단계 부품 정보    
    echo '<table class="table table-bordered table-sm w-60">';
    echo '<thead class="table-secondary"><tr>';
    echo '<th>중분류 부품명</th>';
    echo '<th>규격</th>';
    echo '<th>단위</th>';
    echo '<th>수량</th>';
    echo '<th>단가</th>';
    echo '<th>비고</th>';
    echo '</tr></thead>';
    echo '<tbody>';
    echo '<tr>';
    echo '<td>' . htmlspecialchars($part['part_name'], ENT_QUOTES, 'UTF-8') . '</td>';
    echo '<td>' . htmlspecialchars($part['spec'], ENT_QUOTES, 'UTF-8') . '</td>';
    echo '<td>' . htmlspecialchars($part['unit'], ENT_QUOTES, 'UTF-8') . '</td>';
    echo '<td>' . htmlspecialchars($part['quantity'], ENT_QUOTES, 'UTF-8') . '</td>';
    echo '<td>' . htmlspecialchars($part['unitprice'], ENT_QUOTES, 'UTF-8') . '</td>';
    echo '<td>' . htmlspecialchars($part['memo'], ENT_QUOTES, 'UTF-8') . '</td>';
    echo '</tr>';
    echo '</tbody>';
    echo '</table>';
    
    // 3단계 하위 부품 조회
    $sql_sub = "SELECT * FROM " . $DB . ".parts_sub WHERE part_id = ? AND is_deleted = 0 ORDER BY subpart_id ASC";
    $stmt_sub = $pdo->prepare($sql_sub);
    $stmt_sub->execute([$part['part_id']]);
    $subparts = $stmt_sub->fetchAll(PDO::FETCH_ASSOC);
		
	if ($subparts) {
		echo '<div class="subparts-section" style="margin-left:80px;">';
		echo '<table class="table table-sm table-bordered subpartsTable w-60">';
		echo '<thead class="table-success">';
		echo '<tr>';
		echo '<th class="text-center w180px">부품명</th>';
		echo '<th class="text-center w100px">재질</th>';
		echo '<th class="text-center w100px">연신율 합</th>';
		echo '<th class="text-center w100px">전개도 합</th>';
		echo '<th class="text-center w110px">연신율 + 전개도</th>';
		echo '<th class="text-center w100px">면적당 단가</th>';
		echo '<th class="text-center w110px">단가</th>';
		echo '<th class="text-center w60px">수량</th>';
		echo '<th class="text-center w110px">단가합계</th>';
		echo '</tr>';
		echo '</thead>';
		echo '<tbody class="subpartsTable">';

		foreach ($subparts as $sub) {
			echo '<tr data-subpart_id="' . htmlspecialchars($sub['subpart_id'], ENT_QUOTES, 'UTF-8') . '">';
			echo '<td class="text-center"><input type="text" name="subpart_name[]" class="form-control" readonly value="' . htmlspecialchars($sub['subpart_name'], ENT_QUOTES, 'UTF-8') . '"></td>';
			echo '<td class="text-center"><input type="text" name="material[]" class="form-control" readonly value="' . htmlspecialchars($sub['material'], ENT_QUOTES, 'UTF-8') . '"></td>';
			echo '<td class="text-center"><input type="text" name="bendSum[]" class="form-control text-end" readonly value="' . htmlspecialchars($sub['bendSum'], ENT_QUOTES, 'UTF-8') . '"></td>';
			echo '<td class="text-center"><input type="text" name="plateSum[]" class="form-control text-end" readonly value="' . htmlspecialchars($sub['plateSum'], ENT_QUOTES, 'UTF-8') . '"></td>';
			echo '<td class="text-center"><input type="text" name="finalSum[]" class="form-control text-end" readonly value="' . htmlspecialchars($sub['finalSum'], ENT_QUOTES, 'UTF-8') . '"></td>';
			echo '<td class="text-center"><input type="text" name="unitPrice[]" class="form-control text-end" readonly value="' . htmlspecialchars($sub['unitPrice'], ENT_QUOTES, 'UTF-8') . '"></td>';
			echo '<td class="text-center"><input type="text" name="computedPrice[]" class="form-control text-end" readonly value="' . htmlspecialchars($sub['computedPrice'], ENT_QUOTES, 'UTF-8') . '"></td>';
			echo '<td class="text-center"><input type="text" name="quantity[]" class="form-control text-end" readonly value="' . htmlspecialchars($sub['quantity'], ENT_QUOTES, 'UTF-8') . '"></td>';
			echo '<td class="text-center">';
			echo '<input type="text" name="lineTotal[]" class="form-control text-end" readonly value="' . htmlspecialchars($sub['lineTotal'], ENT_QUOTES, 'UTF-8') . '">';
			echo '<input type="hidden" name="subpart_id[]" value="' . htmlspecialchars($sub['subpart_id'], ENT_QUOTES, 'UTF-8') . '">';
			echo '<input type="hidden" name="parent_part_id[]" value="' . htmlspecialchars($sub['parent_part_id'], ENT_QUOTES, 'UTF-8') . '">';
			echo '<input type="hidden" name="image_url[]" value="' . htmlspecialchars($sub['image_url'], ENT_QUOTES, 'UTF-8') . '">';
			echo '</td>';
			echo '</tr>';
		}

		echo '</tbody>';
		echo '</table>';
		echo '</div>'; // subparts-section
	} else {
		echo '<div class="subparts-section" style="margin-left:20px;"><em>하위 부품 없음</em></div>';
	}      
    
    echo '</div>'; // part-section
}
echo '</div>'; // child-details
?>
