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

$prod = isset($_POST['prod']) ? $_POST['prod'] : '';
$spec = isset($_POST['spec']) ? $_POST['spec'] : '';
$slength = isset($_POST['slength']) ? $_POST['slength'] : '';

if (!$prod || !$spec || !$slength) {
    echo '<div class="text-danger">잘못된 요청입니다.</div>';
    exit;
}

// 입고(생산) 이력 조회
$inSql = "SELECT reg_date, surang, remark FROM {$DB}.lot WHERE prod = ? AND spec = ? AND slength = ? AND is_deleted IS NULL ORDER BY reg_date DESC, num DESC";
$inStmt = $pdo->prepare($inSql);
$inStmt->execute([$prod, $spec, $slength]);
$inRows = $inStmt->fetchAll(PDO::FETCH_ASSOC);

// 사용 이력 조회 (num 컬럼 제거)
$outSql = "SELECT work_date, quantity, remark FROM {$DB}.bending_work_log WHERE prod_code = ? AND spec_code = ? AND slength_code = ? AND is_deleted IS NULL ORDER BY work_date DESC";
$outStmt = $pdo->prepare($outSql);
$outStmt->execute([$prod, $spec, $slength]);
$outRows = $outStmt->fetchAll(PDO::FETCH_ASSOC);
?>

<div class="container-fluid">
    <h6 class="mb-2 mt-2">입고(생산) 이력</h6>
    <div class="table-responsive mb-4">
        <table class="table table-bordered table-sm">
            <thead class="table-light">
                <tr>
                    <th>입고일</th>
                    <th>수량</th>
                    <th>비고</th>
                </tr>
            </thead>
            <tbody>
                <?php if (count($inRows) === 0): ?>
                    <tr><td colspan="3" class="text-center">입고 이력이 없습니다.</td></tr>
                <?php else: ?>
                    <?php foreach ($inRows as $row): ?>
                        <tr>
                            <td><?= htmlspecialchars($row['reg_date']) ?></td>
                            <td class="text-end"><?= number_format($row['surang']) ?></td>
                            <td><?= htmlspecialchars($row['remark']) ?></td>
                        </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>

    <h6 class="mb-2">사용 이력</h6>
    <div class="table-responsive">
        <table class="table table-bordered table-sm">
            <thead class="table-light">
                <tr>
                    <th>사용일</th>
                    <th>수량</th>
                    <th>비고</th>
                </tr>
            </thead>
            <tbody>
                <?php if (count($outRows) === 0): ?>
                    <tr><td colspan="3" class="text-center">사용 이력이 없습니다.</td></tr>
                <?php else: ?>
                    <?php foreach ($outRows as $row): ?>
                        <tr>
                            <td><?= htmlspecialchars($row['work_date']) ?></td>
                            <td class="text-end"><?= number_format($row['quantity']) ?></td>
                            <td><?= htmlspecialchars($row['remark']) ?></td>
                        </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div> 