<?php
require_once('includes/load.php');
page_require_level(2); // Memerlukan izin level 2 atau lebih tinggi

$bom_id = (int)$_GET['id'];

// Periksa apakah ID BOM valid
if (!$bom_id) {
    $session->msg("d", "Missing BOM ID.");
    redirect('bom_list.php');
}

// Jika pengguna mengkonfirmasi penghapusan
if (isset($_POST['confirm'])) {
    // Hapus BOM dari database berdasarkan ID
    $sql = "DELETE FROM bom_level WHERE id = '{$bom_id}'";
    $result = $db->query($sql);

    if ($result && $db->affected_rows() === 1) {
        $session->msg("s", "BOM deleted successfully!");
    } else {
        $session->msg("d", "Failed to delete BOM.");
    }
    redirect('bom_list.php');
}

// Ambil data BOM untuk konfirmasi pesan
$bom = find_by_id('bom_level', $bom_id);

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

<div class="row">
    <div class="col-md-12">
        <?php echo display_msg($msg); ?>
    </div>
    <div class="col-md-12">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title">Delete BOM</h3>
            </div>
            <div class="panel-body">
                <form method="post" action="delete_bom.php?id=<?php echo (int)$bom['id']; ?>" class="clearfix">
                    <p>Are you sure you want to delete BOM <strong><?php echo htmlspecialchars($bom['parent_bom']); ?></strong>?</p>
                    <button type="submit" name="confirm" class="btn btn-danger">Yes, Delete</button>
                    <a href="bom_list.php" class="btn btn-default">Cancel</a>
                </form>
            </div>
        </div>
    </div>
</div>

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