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

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $part_number = $db->escape($_POST['part_number']);
    $quantity = $db->escape($_POST['quantity']);
    $transfer_date = $db->escape($_POST['transfer_date']);
    $description = $db->escape($_POST['description']);

    // Validasi data jika diperlukan

    $query = "INSERT INTO wip_oven (part_number, quantity, transfer_date, description) VALUES ('{$part_number}', {$quantity}, '{$transfer_date}', '{$description}')";

    if ($db->query($query)) {
        $session->msg('s', 'Transfer to WIP Oven added successfully.');
        redirect('wip_oven.php', false);
    } else {
        $session->msg('d', 'Error adding transfer to WIP Oven.');
        redirect('wip_oven.php', false);
    }
}

$wip_oven_transfers = find_all('wip_oven');
?>

<?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">
                <h3 class="panel-title">Transfer to WIP Oven</h3>
            </div>
            <div class="panel-body">
                <form method="post" action="wip_oven.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="quantity">Quantity:</label>
                        <input type="number" class="form-control" name="quantity" required>
                    </div>
                    <div class="form-group">
                        <label for="transfer_date">Transfer Date:</label>
                        <input type="date" class="form-control" name="transfer_date" required>
                    </div>
                    <div class="form-group">
                        <label for="description">Description:</label>
                        <textarea class="form-control" name="description" rows="4"></textarea>
                    </div>
                    <button type="submit" class="btn btn-primary">Add Transfer</button>
                </form>
            </div>
        </div>
    </div>
</div>

<div class="row">
    <div class="col-md-12">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title">WIP Oven Transfers</h3>
            </div>
            <div class="panel-body">
                <table class="table table-bordered">
                    <thead>
                        <tr>
                            <th>Part Number</th>
                            <th>Quantity</th>
                            <th>Transfer Date</th>
                            <th>Description</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($wip_oven_transfers as $transfer) : ?>
                            <tr>
                                <td><?php echo remove_junk($transfer['part_number']); ?></td>
                                <td><?php echo (int) $transfer['quantity']; ?></td>
                                <td><?php echo date('Y-m-d', strtotime($transfer['transfer_date'])); ?></td>
                                <td><?php echo remove_junk($transfer['description']); ?></td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

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