<?php
$page_title = 'Stock Opname Production Inventory';
require_once('includes/load.php');
page_require_level(3);

// Proses tambah data baru jika form disubmit
if (isset($_POST['submit'])) {
    $product_id = (int)$db->escape($_POST['product_id']);
    $part_number = $db->escape($_POST['part_number']);
    $new_code = $db->escape($_POST['new_code']);
    $name = $db->escape($_POST['name']);
    $quantity = (int)$_POST['quantity'];
    $uom = (int)$_POST['uom'];
    $categorie_id = (int)$_POST['categorie_id'];
    $opname_date = $db->escape($_POST['opname_date']);

    $sql = "INSERT INTO stock_opname_prod_loc (product_id, part_number, new_code, name, quantity, uom, categorie_id, opname_date)
            VALUES ('$product_id', '$part_number', '$new_code', '$name', '$quantity', '$uom', '$categorie_id', '$opname_date')";
    $result = $db->query($sql);

    if ($result) {
        $session->msg('s', 'Stock opname added successfully!');
        redirect('stock_opname_prod_loc.php', false);
    } else {
        $session->msg('d', 'Failed to add stock opname.');
        redirect('stock_opname_prod_loc.php', false);
    }
}

// Mengambil semua data dari tabel stock_opname_prod_loc
$sql = "SELECT so.*, c.name AS category_name
        FROM stock_opname_prod_loc so
        LEFT JOIN categories c ON so.categorie_id = c.id";
$stock_opnames = $db->query($sql);
?>

<?php include_once('layouts/header.php'); ?>

<div class="row">
    <div class="col-md-12">
        <?php echo display_msg($msg); ?>
    </div>
</div>

<div class="row">
    <div class="col-md-12">
        <div class="panel panel-default">
            <div class="panel-heading clearfix">
                <div class="pull-right">
                    <a href="add_stock_opname.php" class="btn btn-success">
                        <i class="glyphicon glyphicon-plus"></i> Add Stock Opname
                    </a>
                </div>
                <div class="panel-title">
                    <h4>Stock Opname Production Inventory</h4>
                </div>
            </div>
            <div class="panel-body">
                <table class="table table-bordered table-striped">
                    <thead>
                        <tr>
                            <th class="text-center">#</th>
                            <th>Part Number</th>
                            <th>New Code</th>
                            <th>Name</th>
                            <th>Quantity</th>
                            <th>UOM</th>
                            <th>Category</th>
                            <th>Opname Date</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($stock_opnames as $stock_opname) : ?>
                            <tr>
                                <td class="text-center"><?php echo $stock_opname['id']; ?></td>
                                <td><?php echo htmlspecialchars($stock_opname['part_number']); ?></td>
                                <td><?php echo htmlspecialchars($stock_opname['new_code']); ?></td>
                                <td><?php echo htmlspecialchars($stock_opname['name']); ?></td>
                                <td><?php echo $stock_opname['quantity']; ?></td>
                                <td><?php echo $stock_opname['uom']; ?></td>
                                <td><?php echo htmlspecialchars($stock_opname['category_name']); ?></td>
                                <td><?php echo $stock_opname['opname_date']; ?></td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<?php include_once('layouts/footer.php'); ?>
