<?php
header('Content-Type: application/json'); // JSON 반환 헤더 추가
require_once($_SERVER['DOCUMENT_ROOT'] . "/session.php");  
require_once($_SERVER['DOCUMENT_ROOT'] . "/lib/mydb.php");
$pdo = db_connect();

$mode = $_POST['mode'] ?? '';
$item_name = $_POST['item_name'] ?? '';

if ($mode === 'fetch_specifications' && $item_name) {
    $sql = "SELECT specification FROM {$DB}.instock WHERE item_name = ? AND is_deleted IS NULL";
    try {
        $stmh = $pdo->prepare($sql);
        $stmh->execute([$item_name]);
        $result = $stmh->fetchAll(PDO::FETCH_COLUMN); // 컬럼 값만 배열로 가져옴

        // '전동개폐기'인 경우 '2000KG380V(유선)' 등 추가
        if ($item_name === '전동개폐기') {
            $result[] = '2000KG380V(유선)';
            $result[] = '2000KG380V(무선)';
        }

        // 'GI0.5T'인 경우 '1219*1219' 추가
        if ($item_name === 'GI0.5T') {
            $result[] = '1219*1219';
        }

        if ($result) {
            echo json_encode(['success' => true, 'data' => $result]);
        } else {
            echo json_encode(['success' => false, 'error' => 'No matching data found']);
        }
    } catch (PDOException $e) {
        echo json_encode(['success' => false, 'error' => $e->getMessage()]);
    }
} else {
    echo json_encode(['success' => false, 'error' => 'Invalid request parameters']);
}
?>
