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

header("Content-Type: application/json");

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

$lotnum = isset($_POST['lotnum']) ? trim($_POST['lotnum']) : '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (empty($lotnum)) {
        echo json_encode(['success' => false, 'message' => '로트번호가 비어 있습니다.']);
        exit;
    }

    try {
        if (ctype_alpha($lotnum[0])) {
            // lot 테이블에서 데이터 가져오기 (영문자로 시작하는 경우)
            $stmt = $pdo->prepare("
                SELECT 
                    num, lot_number, prod, spec, remark
                FROM 
                    {$DB}.lot
                WHERE 
                    lot_number = :lotnum 
                    AND (is_deleted IS NULL OR is_deleted = 0 OR is_deleted = '')
            ");
            $stmt->bindParam(':lotnum', $lotnum);
            $stmt->execute();
            $row = $stmt->fetch(PDO::FETCH_ASSOC);

            if ($row) {
                echo json_encode([
                    'success' => true,
                    'item' => 'offering',
                    'num' => htmlspecialchars($row['num']),
                    'lot_number' => htmlspecialchars($row['lot_number']),
                    'prod' => htmlspecialchars($row['prod']),
                    'spec' => htmlspecialchars($row['spec']),
                    'remark' => htmlspecialchars($row['remark']),
                ]);
            } else {
                echo json_encode([
                    'success' => false,
                    'message' => '해당 제공품 정보를 찾을 수 없습니다.'
                ]);
            }
        } else {
            // instock 테이블에서 데이터 가져오기
            $stmt = $pdo->prepare("
                SELECT 
                    num, 
                    item_name AS itemname, 
                    specification AS spec, 
                    remarks 
                FROM 
                    {$DB}.instock 
                WHERE 
                    lot_no = :lot_no 
                    AND (is_deleted IS NULL OR is_deleted = 0 OR is_deleted = '')
            ");
            $stmt->bindParam(':lot_no', $lotnum);
            $stmt->execute();
            $certificate = $stmt->fetch(PDO::FETCH_ASSOC);

            if ($certificate) {
                echo json_encode([
                    'success' => true,
                    'item' => 'regular',
                    'num' => htmlspecialchars($certificate['num']),
                    'itemname' => htmlspecialchars($certificate['itemname']),
                    'specification' => htmlspecialchars($certificate['spec']),
                    'remarks' => htmlspecialchars($certificate['remarks']),
                ]);
            } else {
                echo json_encode([
                    'success' => false,
                    'message' => '해당 로트번호의 성적서를 찾을 수 없습니다.'
                ]);
            }
        }
    } catch (PDOException $e) {
        echo json_encode([
            'success' => false,
            'message' => '데이터베이스 오류: ' . $e->getMessage()
        ]);
    }
} else {
    echo json_encode([
        'success' => false,
        'message' => '잘못된 요청입니다.'
    ]);
}
