<?php
require_once($_SERVER['DOCUMENT_ROOT'] . "/session.php");

require_once($_SERVER['DOCUMENT_ROOT'] . "/lib/mydb.php");

$pdo = db_connect();

$item_name = isset($_POST['item_name']) ? trim($_POST['item_name']) : '';
$tablename = 'instock';

$lot_type = $_POST['lot_type'];
$usesurang = $_POST['usesurang'];


        // 품목명을 매핑하는 배열
        $prodNames = [
            'R' => '가이드레일(벽면형)',
            'S' => '가이드레일(측면형)',
            'G' => '연기차단재',
            'B' => '하단마감재(스크린)',
            'T' => '하단마감재(철재)',
            'L' => 'L - Bar',
            'C' => '케이스'
        ];
		
        // 종류명을 매핑하는 배열
        $specNames = [
            'I' => '화이바원단',
            'S' => 'SUS(마감)',
            'U' => 'SUS(마감)2',
            'E' => 'EGI(마감)',
            'A' => '스크린용',
            'D' => 'D형',
            'C' => 'C형',
            'M' => '본체',
            'T' => '본체(철재)',
            'B' => '후면코너부',
            'L' => '린텔부',
            'P' => '점검구',
            'F' => '전면부'
        ];

        // 모양&길이를 매핑하는 배열
        $slengthNames = [
            '53' => 'W50 × 3000',
            '54' => 'W50 × 4000',
            '83' => 'W80 × 3000',
            '84' => 'W80 × 4000',
            '12' => '1219',
            '24' => '2438',
            '30' => '3000',
            '35' => '3500',
            '40' => '4000',
            '41' => '4150',
            '42' => '4200',
            '43' => '4300'
        ];	

echo "찾는 형태: " . $lot_type . '<br>' ;  		

// echo $prod_value . ' ' . $item_name . ' ' . $slength_value ;	

// 원자재 로트번호 조회 코드    
if ($item_name=='S12')
	$rawitemname = "SUS 1.2T";   
if ($item_name=='S15')
	$rawitemname = "SUS 1.5T";
if ($item_name=='E155')
	$rawitemname = "EGI 1.55T";
if ($item_name=='E115')
	$rawitemname = "EGI 1.15T";
if ($item_name=='E08')
	$rawitemname = "EGI 0.8T";

echo "원자재 " . $rawitemname  . " 로트 ";	


if (empty($item_name)) {
    echo '<div class="alert alert-danger">품목명이 없습니다.</div>';
    exit;
}

try {
    // 로트 번호를 품목명에 따라 조회
$sql = "SELECT num, item_name, lot_no, received_qty, supplier, inspection_date 
        FROM {$DB}.instock 
        WHERE item_name LIKE :item_name AND is_deleted IS NULL AND lotDone IS NULL  
        ORDER BY inspection_date DESC, lot_no ASC 
        LIMIT 12";

    $stmh = $pdo->prepare($sql);
    $stmh->bindValue(':item_name', "%$rawitemname%", PDO::PARAM_STR);
    $stmh->execute();
    $rows = $stmh->fetchAll(PDO::FETCH_ASSOC);

 if (count($rows) > 0) {
    echo '<div style="max-height: 400px; overflow-y: auto;">'; // 스크롤 컨테이너 시작
    echo '<table class="table table-hover">';
    echo '<thead class="table-primary">';
    echo '<tr>';
    echo '<th class="text-center">로트 번호</th>';
    echo '<th class="text-center">품명 </th>';
    echo '<th class="text-center">길이(M)</th>';
    echo '<th class="text-center">납품업체</th>';
    echo '<th class="text-center">검사일</th>';
    echo '<th class="text-center">로트소진</th>';
    echo '</tr>';
    echo '</thead>';
    echo '<tbody id="lotModalBody">';

foreach ($rows as $row) {
    $lot_no = htmlspecialchars($row['lot_no']);
    $received_qty = number_format((int)$row['received_qty']);
    $supplier = htmlspecialchars($row['supplier']);
    $inspection_date = htmlspecialchars($row['inspection_date']);
    $num = htmlspecialchars($row['num']); 
    $item_name = htmlspecialchars($row['item_name']); 

    if ($item_name == '내화실') {
        echo '<tr>';
        echo '<td class="text-center" onclick="selectLotNumber_wire(\'' . $lot_no . '\')">' . $lot_no . '</td>';
        echo '<td class="text-center" onclick="selectLotNumber_wire(\'' . $lot_no . '\')">' . $item_name . '</td>';
        echo '<td class="text-center" onclick="selectLotNumber_wire(\'' . $lot_no . '\')">' . $received_qty . '</td>';
        echo '<td class="text-center" onclick="selectLotNumber_wire(\'' . $lot_no . '\')">' . $supplier . '</td>';
        echo '<td class="text-center" onclick="selectLotNumber_wire(\'' . $lot_no . '\')">' . $inspection_date . '</td>';
        // '소진' 버튼 추가
        echo '<td class="text-center">';
        echo '<button  type="button" class="btn btn-sm btn-danger lot-done-wire-btn " data-num="' . $num . '" data-tablename="' . $tablename . '">소진</button>';
        echo '</td>';
        echo '</tr>';
    } else  {
        echo '<tr>';
        echo '<td class="text-center" onclick="selectLotNumber(\'' . $lot_no . '\')"> ' . $lot_no . '</td>';
        echo '<td class="text-center" onclick="selectLotNumber(\'' . $lot_no . '\')"> ' . $item_name . '</td>';
        echo '<td class="text-center" onclick="selectLotNumber(\'' . $lot_no . '\')"> ' . $received_qty . '</td>';
        echo '<td class="text-center" onclick="selectLotNumber(\'' . $lot_no . '\')"> ' . $supplier . '</td>';
        echo '<td class="text-center" onclick="selectLotNumber(\'' . $lot_no . '\')"> ' . $inspection_date . '</td>';
        // '소진' 버튼 추가
        echo '<td class="text-center">';
        echo '<button type="button" class="btn btn-sm btn-danger lot-done-btn" data-num="' . $num . '" data-tablename="' . $tablename . '"   data-rawitemname="' . $rawitemname . '"  data-lottype="' . $lot_type . '"   >소진</button>';
        echo '</td>';
        echo '</tr>';
	} 
}

    echo '</tbody>';
    echo '</table>';
    echo '</div>'; // 스크롤 컨테이너 종료
} else {
    echo '<div class="alert alert-warning">해당 품목명에 대한 로트 번호가 없습니다. item_name : ' . $item_name . '</div>';
}

} catch (PDOException $e) {
    echo '<div class="alert alert-danger">오류: ' . htmlspecialchars($e->getMessage()) . '</div>';
}

?>
