<?php

$judul = "Import Checklist";
$menu  = "checklist";

include 'layout/header.php';

if($_SESSION['role'] != 'admin'){
    die("Akses ditolak");
}

if(isset($_POST['import'])){

    $file = $_FILES['file_csv']['tmp_name'];

    if(($handle = fopen($file, "r")) !== FALSE){

        // Lewati Header CSV
        fgetcsv($handle, 1000, ",");

        while(($data = fgetcsv($handle, 1000, ",")) !== FALSE){

            $nama_checklist = mysqli_real_escape_string($conn,$data[0]);
            $kategori       = mysqli_real_escape_string($conn,$data[1]);
            $sub_kategori   = mysqli_real_escape_string($conn,$data[2]);
            $waktu          = mysqli_real_escape_string($conn,$data[3]);
            $periode        = mysqli_real_escape_string($conn,$data[4]);
            $pic            = mysqli_real_escape_string($conn,$data[5]);
            $wajib_foto     = mysqli_real_escape_string($conn,$data[6]);
            $status         = mysqli_real_escape_string($conn,$data[7]);

            mysqli_query($conn,"
                INSERT INTO checklist_master
                (
                    nama_checklist,
                    kategori,
                    sub_kategori,
                    waktu,
                    periode,
                    pic,
                    wajib_foto,
                    status
                )
                VALUES
                (
                    '$nama_checklist',
                    '$kategori',
                    '$sub_kategori',
                    '$waktu',
                    '$periode',
                    '$pic',
                    '$wajib_foto',
                    '$status'
                )
            ");
        }

        fclose($handle);

        echo "
        <script>
            alert('Import berhasil');
            window.location='checklist.php';
        </script>";
        exit;
    }
}
?>

<?php include 'layout/sidebar.php'; ?>

<div class="card shadow">

    <div class="card-header bg-success text-white">
        Import Checklist CSV
    </div>

    <div class="card-body">

        <div class="alert alert-info">

            Format CSV:

            <br><br>

            nama_checklist,kategori,sub_kategori,waktu,periode,pic,wajib_foto,status

        </div>

        <form method="POST" enctype="multipart/form-data">

            <div class="mb-3">

                <label>Pilih File CSV</label>

                <input
                    type="file"
                    name="file_csv"
                    class="form-control"
                    accept=".csv"
                    required>

            </div>

            <button
                type="submit"
                name="import"
                class="btn btn-success">

                Import

            </button>

            <a
                href="checklist.php"
                class="btn btn-secondary">

                Kembali

            </a>

        </form>

    </div>

</div>

<?php include 'layout/footer.php'; ?>