<?php
require_once('includes/load.php'); // Sesuaikan dengan struktur file Anda

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['csv_file'])) {
    $csv_file = $_FILES['csv_file']['tmp_name'];
    
    // Membaca data dari file CSV
    $csv_data = array_map('str_getcsv', file($csv_file));

    // Hapus baris header
    $header = array_shift($csv_data);

    // Iterasi melalui data BOM
    foreach ($csv_data as $row) {
        $child_bom = $db->escape($row[0]);
        $child_name = $db->escape($row[1]);
        $level = (int)$row[2];
        $quantity = (int)$row[3];

        // Lakukan sesuatu dengan data BOM, misalnya masukkan ke database
        // Ganti kode di bawah ini dengan operasi yang sesuai dengan aplikasi Anda
        // ...
    }

    // Redirect ke halaman BOM View setelah mengimpor
    header('Location: view_bom.php');
    exit();
}

$page_title = 'Import BOM CSV';
include_once('layouts/header.php');
?>

<div class="row">
    <div class="col-md-6 col-md-offset-3">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title">Import BOM CSV</h3>
            </div>
            <div class="panel-body">
                <form action="import_bom_csv.php" method="post" enctype="multipart/form-data">
                    <div class="form-group">
                        <label for="csv_file">Choose CSV File:</label>
                        <input type="file" name="csv_file" id="csv_file" accept=".csv" required>
                    </div>
                    <button type="submit" class="btn btn-primary">Import</button>
                </form>
            </div>
        </div>
    </div>
</div>

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