<?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';

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

try {
    // 로트 번호를 품목명에 따라 조회
$sql = "SELECT num, lot_no, received_qty, supplier,prodcode, 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', "%$item_name%", 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 '</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['prodcode']);
    $num = htmlspecialchars($row['num']); // 추가: num 값을 가져옵니다.

    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 . '\')">' . $inspection_date . '</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">';
        echo '<button class="btn btn-sm btn-danger lot-done-wire-btn " data-num="' . $num . '" data-tablename="' . $tablename . '">소진</button>';
        echo '</td>';
        echo '</tr>';
    } else if ($item_name == '실리카원단' or $item_name == '와이어원단' or $item_name == '화이바원단')  {
        echo '<tr>';
        echo '<td class="text-center" onclick="selectLotNumber(\'' . $lot_no . '\')"> ' . $lot_no . '</td>';
        echo '<td class="text-center" onclick="selectLotNumber(\'' . $lot_no . '\')"> ' . $inspection_date . '</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">';
        echo '<button class="btn btn-sm btn-danger lot-done-btn" data-num="' . $num . '" data-tablename="' . $tablename . '">소진</button>';
        echo '</td>';
        echo '</tr>';
	}   else if ($item_name == '슬랫코일' ) {
        echo '<tr>';
        echo '<td class="text-center" onclick="selectLotNumber(\'' . $lot_no . '\')"> ' . $lot_no . '</td>';
        echo '<td class="text-center" onclick="selectLotNumber(\'' . $lot_no . '\')"> ' . $inspection_date . '</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">';
        echo '<button class="btn btn-sm btn-danger lot-done-btn" data-num="' . $num . '" data-tablename="' . $tablename . '">소진</button>';
        echo '</td>';
        echo '</tr>';
	} 
}


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

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