<main id="main" class="main">

    <div class="pagetitle">
        <h1>Master Barang</h1>
        <nav>
            <ol class="breadcrumb">

            </ol>
        </nav>
    </div><!-- End Page Title -->

    <section class="section">
        <div class="row">
            <div class="col-lg-12">

                <div class="card">
                    <div class="card-body">
                        <br>
                        <!-- Table with stripped rows -->
                        <div class="datatable-wrapper datatable-loading no-footer sortable searchable fixed-columns">
                            <div class="row g-3">
                                <div class="col-md-9">
                                    <input type="text" id="list_item" name="strDeskripsi" data-parsley-trigger="change" required="" placeholder="Cari deskripsi barang" autocomplete="off" class="form-control" list="list_deskripsi">
                                    <datalist id="list_deskripsi">
                                        <?php
                                        foreach ($brg as $row) {
                                            echo "<option value='" . $row['strDeskripsi'] . "'>   (" . $row['strKdItem'] . ")</option>";
                                        }
                                        ?>
                                    </datalist>
                                </div>
                                <!--
                                <div class="col-md-3">
                                    <select id="mySelect" class="form-control form-control-sm" multiple="multiple">
                                        <?php
                                        foreach ($unit as $un) {
                                            echo "<option value='" . $un['unit'] . "'>" . $un['unit'] . "</option>";
                                        }
                                        ?>
                                    </select>
                                </div>
                                -->
                                <div class="col-md-3">
                                    <?php if ($akses['unduh'] == 1) {
                                        echo '<button style="float:right;margin: 2px;" type="button" id="btnunduh" class="btn btn-success" onclick="unduhdata()"><i class="bi bi-download"></i>&nbsp;Unduh</button>';
                                    }
                                    ?>
                                    <button style="display:none;float:right;margin: 2px;" class="btn btn-primary" type="button" disabled="" id="btnloading">
                                        <span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>
                                        Cari
                                    </button>
                                    <button style="float:right;margin: 2px;" type="button" id="btnview" class="btn btn-primary" onclick="tampildata()"><i class="bi bi-search"></i>&nbsp;Cari</button>
                                </div>

                                <div class="col-md-1">
                                    <label for="inputUnit">Display</label>
                                    <select class="form-control form-control-sm" id="viewitem" onchange="ubahitem(this.value)">
                                        <option value="10">10</option>
                                        <option value="20">20</option>
                                        <option value="50">50</option>
                                        <option value="100">100</option>
                                        <option value="500">500</option>
                                        <option value="100000">>1000</option>
                                    </select>
                                </div>
                                <div class="col-md-2">
                                    <label for="inputUnit">Halaman</label>
                                    <div class="btn-group" role="group" aria-label="Basic outlined example">
                                        <button type="button" onclick="btnprev()" class="btn btn-outline-primary btn-sm"><i class="bi bi-arrow-left-circle"></i></button>
                                        <input type="number" class="form-control form-control-sm" id="halaman" value="1">
                                        <button type="button" onclick="btnnext()" class="btn btn-outline-primary btn-sm"><i class="bi bi-arrow-right-circle"></i></button>
                                    </div>
                                </div>

                                <div class="col-md-8">

                                </div>

                            </div>
                            <br>
                            <div class="datatable-container">

                                <div id="tbbrg"></div>

                            </div>
                        </div>
                        <!-- End Table with stripped rows -->

                    </div>
                </div>

            </div>
        </div>
    </section>

</main>


<div class="modal fade" id="Modalupdate" tabindex="-1" style="display: none;" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Update Master Item</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <div class="row g-3">
                    <div class="col-md-6">
                        <label for="inputName5" class="form-label">Kode barang</label>
                        <input type="text" class="form-control" id="edkodebrg" readonly>
                    </div>
                    <div class="col-md-6">
                        <label for="inputName5" class="form-label">Nama barang</label>
                        <input type="text" class="form-control" id="eddeskripsibrg">
                    </div>
                    <div class="col-md-2">
                        <label for="inputName5" class="form-label">Unit 1</label>
                        <input type="text" class="form-control" id="edunit1brg">
                    </div>
                    <div class="col-md-2">
                        <label for="inputName5" class="form-label">Unit 2</label>
                        <input type="text" class="form-control" id="edunit2brg">
                    </div>
                    <div class="col-md-2">
                        <label for="inputName5" class="form-label">Ratio 2</label>
                        <input type="text" class="form-control" id="edratio2brg">
                    </div>
                    <div class="col-md-2">
                        <label for="inputName5" class="form-label">Unit 3</label>
                        <input type="text" class="form-control" id="edunit3brg">
                    </div>
                    <div class="col-md-2">
                        <label for="inputName5" class="form-label">Ratio 3</label>
                        <input type="text" class="form-control" id="edratio3brg">
                    </div>
                    <div class="col-md-6">
                        <!--
                        <label for="inputName5" class="form-label">Lokasi</label>
                        
                        <input type="text" class="form-control" id="edlokasibrg">
                                    
                        <select class="form-control" id="edlokasibrg">
                            <?php
                            //foreach ($unit as $u) {
                            //    echo "<option value='" . $u['unit'] . "'>" . $u['unit'] . "</option>";
                            //}
                            ?>
                        </select>
                        -->
                    </div>
                    <div class="col-md-6">
                        <input type="hidden" class="form-control" id="edidbrg">
                    </div>

                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary" onclick="updatebrg()">Save changes</button>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="f_add_unit" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <form id="frm_add_unit" name="frm_add_unit" method="" action="">
                <div class="modal-header">
                    <h5 class="modal-title">Tambahkan unit</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">

                    <input type="hidden" id="kd_brg" name="kd_brg">
                    <div id="tblunit"></div>

                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
                    <button type="submit" class="btn btn-primary">Simpan</button>
                </div>
            </form>
        </div>
    </div>
</div>

<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet" />

<script>
    $(document).ready(function() {
        $('#mySelect').select2();
    });
</script>

<script>
    function msgsimpan(txt) {
        const Toast = Swal.mixin({
            toast: true,
            position: "top-end",
            showConfirmButton: false,
            timer: 2000,
            timerProgressBar: false,
            didOpen: (toast) => {
                toast.onmouseenter = Swal.stopTimer;
                toast.onmouseleave = Swal.resumeTimer;
            }
        });
        Toast.fire({
            icon: "success",
            title: txt
        });
    }

    function msgerror(txt) {
        const Toast = Swal.mixin({
            toast: true,
            position: "top-end",
            showConfirmButton: false,
            timer: 2000,
            timerProgressBar: false,
            didOpen: (toast) => {
                toast.onmouseenter = Swal.stopTimer;
                toast.onmouseleave = Swal.resumeTimer;
            }
        });
        Toast.fire({
            icon: "error",
            title: txt
        });
    }

    function tampildata() {
        var cari = $("#list_item").val();
        var items = $("#viewitem").val();
        var page = $("#halaman").val();
        document.getElementById("btnloading").style.display = "block";
        document.getElementById("btnview").style.display = "none";
        $.get("<?= base_url() ?>Masterdata/tb_brg?cari=" + cari + "&items=" + items + "&page=" + page, function(data, status) {
            $("#tbbrg").html(data);
            document.getElementById("btnloading").style.display = "none";
            document.getElementById("btnview").style.display = "block";
        });
    }

    function tambahbrg() {
        var kodebrg = $("#kodebrg").val();
        var deskripsibrg = $("#deskripsibrg").val();
        var unit1brg = $("#unit1brg").val();
        var unit2brg = $("#unit2brg").val();
        var ratio2brg = $("#ratio2brg").val();
        var unit3brg = $("#unit3brg").val();
        var ratio3brg = $("#ratio3brg").val();
        var lokasibrg = $("#lokasibrg").val();

        if (kodebrg == '') {
            msgerror("Kode barang harus diisi");
            $("#kodebrg").focus();
        } else {
            if (deskripsibrg == '') {
                msgerror("Deskripsi barang harus diisi");
                $("#deskripsibrg").focus();
            } else {
                $.ajax({
                    url: "<?= base_url('Masterdata/simpan_brg'); ?>",
                    type: 'POST',
                    cache: false,
                    data: {
                        kodebrg: kodebrg,
                        deskripsibrg: deskripsibrg,
                        unit1brg: unit1brg,
                        unit2brg: unit2brg,
                        ratio2brg: ratio2brg,
                        unit3brg: unit3brg,
                        ratio3brg: ratio3brg,
                        lokasibrg: lokasibrg
                    },
                    success: function(data) {
                        msgsimpan("Data telah disimpan");
                        tampildata();
                        //log
                        var strTable = 'item';
                        var keterangan = 'Telah menambahkan master data dengan kode barang : ' + kodebrg;
                        $.ajax({
                            url: "<?= base_url('Pengaturan/log'); ?>",
                            type: 'POST',
                            cache: false,
                            data: {
                                strTable: strTable,
                                keterangan: keterangan
                            },
                            success: function() {}
                        });
                        /*
                        if (data == 1) {
                            
                        }
                        if (data == 0) {
                            msgerror("Data gagal disimpan");
                        }
                            */
                    }
                });
            }
        }
    }

    function editbrg(edkodebrg, eddeskripsibrg, edunit1brg, edunit2brg, edratio2brg, edunit3brg, edratio3brg, edlokasibrg) {
        $("#edkodebrg").val(edkodebrg);
        $("#eddeskripsibrg").val(eddeskripsibrg);
        $("#edunit1brg").val(edunit1brg);
        $("#edunit2brg").val(edunit2brg);
        $("#edratio2brg").val(edratio2brg);
        $("#edunit3brg").val(edunit3brg);
        $("#edratio3brg").val(edratio3brg);
        $("#edlokasibrg").val(edlokasibrg);
    }

    function updatebrg() {
        var edkodebrg = $("#edkodebrg").val();
        var eddeskripsibrg = $("#eddeskripsibrg").val();
        var edunit1brg = $("#edunit1brg").val();
        var edunit2brg = $("#edunit2brg").val();
        var edratio2brg = $("#edratio2brg").val();
        var edunit3brg = $("#edunit3brg").val();
        var edratio3brg = $("#edratio3brg").val();
        var edlokasibrg = $("#edlokasibrg").val();

        if (eddeskripsibrg == '') {
            msgerror("nama Barang harus diisi");
            document.getElementById('eddeskripsibrg').focus();
        } else {
            if (edunit1brg == '') {
                msgerror("Unit 1 harus diisi");
                document.getElementById('edunit1brg').focus();
            } else {
                $.ajax({
                    url: "<?= base_url('Masterdata/update_brg'); ?>",
                    type: 'POST',
                    cache: false,
                    data: {
                        edkodebrg: edkodebrg,
                        eddeskripsibrg: eddeskripsibrg,
                        edunit1brg: edunit1brg,
                        edunit2brg: edunit2brg,
                        edratio2brg: edratio2brg,
                        edunit3brg: edunit3brg,
                        edratio3brg: edratio3brg,
                        edlokasibrg: edlokasibrg
                    },
                    success: function(data) {
                        msgsimpan("Update sukses");
                        tampildata();
                        $("#Modalupdate").modal('hide');
                        //log

                        var strTable = 'item';
                        var keterangan = 'Telah mengubah master data dengan kode barang : ' + edkodebrg;
                        $.ajax({
                            url: "<?= base_url('Pengaturan/log'); ?>",
                            type: 'POST',
                            cache: false,
                            data: {
                                strTable: strTable,
                                keterangan: keterangan
                            },
                            success: function() {}
                        });

                    }
                });
            }
        }
    }

    function hapusbrg(kode) {
        Swal.fire({
            title: "Hapus data ini ? ",
            //text: "You won't be able to revert this!",
            icon: "warning",
            showCancelButton: true,
            confirmButtonColor: "#3085d6",
            cancelButtonColor: "#d33",
            confirmButtonText: "Yes, delete it!"
        }).then((result) => {
            if (result.isConfirmed) {
                $.ajax({
                    url: "<?= base_url('Masterdata/hapus_brg'); ?>",
                    type: 'POST',
                    cache: false,
                    data: {
                        kode: kode
                    },
                    success: function(data) {
                        msgsimpan("data Dihapus");
                        tampildata();
                        //log
                        var strTable = 'item';
                        var keterangan = 'Telah menghapus master data dengan kode barang : ' + kode;
                        $.ajax({
                            url: "<?= base_url('Pengaturan/log'); ?>",
                            type: 'POST',
                            cache: false,
                            data: {
                                strTable: strTable,
                                keterangan: keterangan
                            },
                            success: function() {}
                        });
                    }
                });
            }
        });
    }

    function unduhdata() {
        var cari = $("#list_item").val();
        window.open("<?= base_url('masterdata/unduhbrg?cari=') ?>" + cari, "_self");
    }

    function tbhalaman(id, halaman) {
        alert(id + halaman);
        var cari = $("#list_item").val();
        var item = $("#viewitem").val();
        document.getElementById("halaman").value = halaman;
        document.getElementById("btnloading").style.display = "block";
        document.getElementById("btnview").style.display = "none";
        $.get("master/barang/tb_brg2.php?cari=" + cari + "&item=" + item + "&page=20", function(data, status) {
            $("#tbbrg").html(data);
            document.getElementById("btnloading").style.display = "none";
            document.getElementById("btnview").style.display = "block";
        });
    }

    $(document).ready(function() {
        load_data();

        function load_data(page) {
            $.ajax({
                url: "data.php",
                method: "POST",
                data: {
                    page: page
                },
                success: function(data) {
                    $('#tbbrg').html(data);
                }
            })
        }
        $(document).on('click', '.halaman', function() {
            var page = $(this).attr("id");
            load_data(page);
        });
    });

    function btnprev() {
        var halaman = $("#halaman").val();
        if (halaman >= 1) {
            total = parseInt(halaman) - parseInt(1);
            $("#halaman").val(total);
            tampildata();
        }
        if (halaman == 1) {
            $("#halaman").val(1);
            tampildata();
        }
        if (halaman == '') {
            $("#halaman").val(1);
            tampildata();
        }
    }

    function btnnext() {
        var halaman = $("#halaman").val();
        total = parseInt(halaman) + parseInt(1);
        $("#halaman").val(total);
        tampildata();
        if (halaman == '') {
            $("#halaman").val(1);
            tampildata();
        }
    }

    function ubahitem(item) {
        //$("#halaman").val(1);
        tampildata();
    }

    function tambah_unit(id) {
        $("#kd_brg").val(id);
        $.get("<?= base_url() ?>Masterdata/tb_unit?kd=" + id, function(data, status) {
            $("#tblunit").html(data);
        });
        $("#f_add_unit").modal("show");
    }

    function hapus_unit(kd, unit) {
        Swal.fire({
            title: "Hapus data unit " + unit + " dari kode barang " + kd,
            text: "kode barang tidak bisa diakses oleh unit yang telah terhapus.",
            icon: "question",
            showCancelButton: true,
            confirmButtonColor: "#3085d6",
            cancelButtonColor: "#d33",
            confirmButtonText: "Ya, hapus!"
        }).then((result) => {
            if (result.isConfirmed) {
                //begin
                $.ajax({
                    url: "<?= base_url('Masterdata/hapus_unit'); ?>",
                    type: 'POST',
                    cache: false,
                    data: {
                        kd: kd,
                        unit: unit
                    },
                    success: function() {
                        // begin
                        tampildata();
                        //end
                    }
                });
                //end
            }
        });
    }
</script>

<script>
    document.addEventListener('DOMContentLoaded', function() {
        var searchInput = document.getElementById('list_item');

        searchInput.addEventListener('keyup', function(event) {
            if (event.keyCode === 13) { // 13 adalah kode key untuk Enter
                $("#halaman").val(1);
                tampildata();
            }
        });
    });
</script>

<script>
    document.addEventListener('DOMContentLoaded', function() {
        var searchInput = document.getElementById('halaman');

        searchInput.addEventListener('keyup', function(event) {
            if (event.keyCode === 13) { // 13 adalah kode key untuk Enter                
                tampildata();
                if (halaman == '') {
                    $("#halaman").val(1);
                    tampildata();
                }
            }
        });
    });
</script>

<script>
    document.addEventListener('DOMContentLoaded', function() {
        tampildata();
    });
</script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
    $(document).ready(function() {
        $('form[name="frm_add_unit"]').on('submit', function(event) {
            event.preventDefault();
            var kd = $("#kd_brg").val();
            var formData = new FormData(this);
            $.ajax({
                url: '<?= base_url('Masterdata/add_unit'); ?>',
                type: 'POST',
                data: formData,
                processData: false,
                contentType: false,
                success: function(response) {
                    $("#f_add_unit").modal("hide");
                    tampildata();
                    msgsimpan("Unit berhasil ditambahkan");

                    // Menyimpan log 
                    var strTable = 'tb_brg_unit';
                    var keterangan = 'Telah menambahkan unit pada kode barang ' + kd;
                    $.ajax({
                        url: "<?= base_url('Pengaturan/log'); ?>",
                        type: 'POST',
                        cache: false,
                        data: {
                            strTable: strTable,
                            keterangan: keterangan
                        },
                        success: function() {
                            // begin
                            //end
                        }
                    });

                },
                error: function(xhr, status, error) {
                    Swal.fire({
                        title: 'Terjadi kesalahan',
                        text: error,
                        icon: 'error'
                    });
                }
            });
        });
    });
</script>