<?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-50">';
    echo '<thead class="table-secondary"><tr>';
    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['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-bordered table-sm w-50">';
        echo '<thead class="table-success"><tr>';
        echo '<th>하위 부품명</th>';
        echo '<th>규격</th>';
        echo '<th>재질</th>';
        echo '<th>가로폭</th>';
        echo '<th>세로폭</th>';
        echo '<th>이미지</th>';
        echo '</tr></thead>';
        echo '<tbody>';
        foreach ($subparts as $sub) {
            echo '<tr>';
            echo '<td>' . htmlspecialchars($sub['subpart_name'], ENT_QUOTES, 'UTF-8') . '</td>';
            echo '<td>' . htmlspecialchars($sub['spec'], ENT_QUOTES, 'UTF-8') . '</td>';
            echo '<td>' . htmlspecialchars($sub['material'], ENT_QUOTES, 'UTF-8') . '</td>';
            echo '<td>' . htmlspecialchars($sub['horizontal_width'], ENT_QUOTES, 'UTF-8') . '</td>';			
            echo '<td>' . htmlspecialchars($sub['vertical_width'], ENT_QUOTES, 'UTF-8') . '</td>';
		  
			  $img = htmlspecialchars($sub['image_url'], ENT_QUOTES, 'UTF-8');
			  if ($img) {
				  $img_path = "/bending/img/" . $img;
				  echo '<td> <img src="' . $img_path . '" alt="이미지" style="max-width:100px;max-height:50px;"> </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
?>
