<?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']) ?  str_replace(' ', '', trim(strtoupper($_POST['item_name'])))  : '';
$tablename = 'instock';

print '원자재명 : ' . $item_name;

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

try {
    // 로트 번호를 품목명에 따라 조회 (대문자 및 공백 제거 후 검색)
    $sql = "SELECT num, lot_no, received_qty, supplier, inspection_date, item_name  
            FROM {$DB}.instock 
            WHERE REPLACE(UPPER(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', "%" . str_replace(' ', '', $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 '<th class="text-center">로트소진</th>';
    echo '</tr>';
    echo '</thead>';
    echo '<tbody id="lotModalBody">';

	foreach ($rows as $row) {
		$lot_no = htmlspecialchars($row['lot_no']);
		$item_name =  htmlspecialchars($row['item_name']);
		$received_qty = number_format((int)$row['received_qty']);
		$supplier = htmlspecialchars($row['supplier']);
		$inspection_date = htmlspecialchars($row['inspection_date']);
		$num = htmlspecialchars($row['num']); // 추가: num 값을 가져옵니다.

			echo '<tr>';
			echo '<td class="text-center" onclick="selectLotNumber_fabric(\'' . $lot_no . '\')"> ' . $lot_no . '</td>';
			echo '<td class="text-center" onclick="selectLotNumber_fabric(\'' . $lot_no . '\')"> ' . $item_name . '</td>';
			echo '<td class="text-center" onclick="selectLotNumber_fabric(\'' . $lot_no . '\')"> ' . $received_qty . '</td>';
			echo '<td class="text-center" onclick="selectLotNumber_fabric(\'' . $lot_no . '\')"> ' . $supplier . '</td>';
			echo '<td class="text-center" onclick="selectLotNumber_fabric(\'' . $lot_no . '\')"> ' . $inspection_date . '</td>';
			// '소진' 버튼 추가
			echo '<td class="text-center">';
			echo '<button type="button" class="btn btn-sm btn-danger lot-done-fabric-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>';
}
?>
