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

// Proses form jika dikirim
if (isset($_POST['add_stock'])) {
    $part_number = $_POST['part_number'];
    $product_name = $_POST['product_name'];
    $stocktaking_date = $_POST['stocktaking_date'];
    $quantity = $_POST['quantity'];

    // Validasi input jika diperlukan
    // ...

    // Simpan data ke database
    $query = "INSERT INTO stocktaking (part_number, product_name, stocktaking_date, quantity) VALUES ('$part_number', '$product_name', '$stocktaking_date', '$quantity')";
    if ($db->query($query)) {
        $session->msg('s', "Stock taking berhasil ditambahkan!");
        redirect('stocktaking.php');
    } else {
        $session->msg('d', "Gagal menambahkan stock taking.");
        redirect('stocktaking.php');
    }
}

// Ambil data produk untuk ditampilkan dalam dropdown
$products = find_all('products');
?>

<?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-6">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title">Tambah Stock Taking</h3>
            </div>
            <div class="panel-body">
                <form method="post" action="stocktaking.php">
                    <div class="form-group">
                        <label for="part_number">Part Number</label>
                        <input type="text" class="form-control" name="part_number" required>
                    </div>
                    <div class="form-group">
                        <label for="product_name">Nama Produk</label>
                        <select class="form-control" name="product_name" required>
                            <?php foreach ($products as $product) : ?>
                                <option value="<?php echo $product['name']; ?>"><?php echo $product['name']; ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="stocktaking_date">Tanggal Stock Taking</label>
                        <input type="date" class="form-control" name="stocktaking_date" required>
                    </div>
                    <div class="form-group">
                        <label for="quantity">Jumlah</label>
                        <input type="number" class="form-control" name="quantity" required>
                    </div>
                    <button type="submit" name="add_stock" class="btn btn-primary">Tambah</button>
                </form>
            </div>
        </div>
    </div>
</div>

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