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

    <div class="pagetitle">
        <h1>Nomor Lot FG</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 mb-3">
                                <label class="col-sm-1 col-form-label">Mulai</label>
                                <div class="col-sm-2">
                                    <input type="date" class="form-control form-control" id="mulai" value="<?= date('Y-m-d'); ?>">
                                </div>
                                <label class="col-sm-1 col-form-label">Hingga</label>
                                <div class="col-sm-2">
                                    <input type="date" class="form-control" id="hingga" value="<?= date('Y-m-d'); ?>">
                                </div>
                            </div>
                            <div class="row g-3">
                                <div class="col-md-1">
                                    <label class="form-label">Gudang</label>
                                </div>

                                <div class="col-md-11">
                                    <select id="id_gudang" name="id_gudang[]" multiple="multiple" class="form-control">
                                        <?php
                                        foreach ($gudang as $g) {
                                            echo "<option value='" . $g['id_gudang'] . "'>" . $g['gudang'] . "</option>";
                                        }
                                        ?>
                                    </select>
                                </div>
                                <div class="col-md-1">
                                    <label class="form-label"></label>
                                </div>
                                <div class="col-md-4">
                                    <button style="float:left;margin: 2px;" class="btn btn-primary" id="btnview" onclick="tampildata()"><i class='bi bi-search'></i>&nbsp;Lihat FG</button>
                                    <button style="display:none;float:left;margin: 2px;" id="btnloading" class="btn btn-primary" type="button" disabled="">
                                        <span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>
                                        Loading...
                                    </button>
                                    <?php
                                    if ($akses['unduh'] == 1) {
                                        echo '<button style="float:left;margin: 2px;" class="btn btn-success" id="btnview" onclick="unduhdatalot()"><i class="bi bi-download"></i>&nbsp;Unduh</button>';
                                    }
                                    ?>
                                </div>
                            </div>
                            <br>
                            <div class="datatable-container">
                                <div id="tbllot"></div>
                            </div>
                        </div>
                        <!-- End Table with stripped rows -->
                    </div>
                </div>
            </div>
        </div>
    </section>
</main>

<div class="modal fade" id="f_update_lot" tabindex="-1" style="display: none;" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Update LOT</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">
                    <input type="hidden" class="form-control" id="edidlot">
                    <div class="col-12">
                        <label for="inputNanme4" class="form-label">Nama Barang</label>
                        <select id="ed_fg" class="form-control">
                            <?php
                            foreach ($fg as $f) {
                                echo "<option value='" . $f['sku'] . "'>" . $f['barang'] . "</option>";
                            }
                            ?>
                        </select>
                    </div>
                    <div class="col-3">
                        <label for="inputNanme4" class="form-label">Sift</label>
                        <select id="ed_sift" class="form-control">
                            <option value="1">Shift 1</option>
                            <option value="2">Shift 2</option>
                            <option value="3">Shift 3</option>
                        </select>
                    </div>
                    <div class="col-9">
                        <label for="inputNanme4" class="form-label">Mesin</label>
                        <select id="ed_mesin" class="form-control">
                            <?php
                            foreach ($mesin as $m) {
                                echo "<option value='" . $m['kd_mesin'] . "'>" . $m['mesin'] . "</option>";
                            }
                            ?>
                        </select>
                    </div>

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

<div class="modal fade" id="f_tf" tabindex="-1" style="display: none;" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Transfer Stok ke gudang</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">
                    <input type="hidden" class="form-control" id="edidlot">
                    <input type="hidden" class="form-control" id="idlot">
                    <input type="hidden" class="form-control" id="skutf">
                    <input type="hidden" class="form-control" id="qtytf">
                    <div class="col-12">
                        <label for="inputNanme4" class="form-label">Gudang</label>
                        <select class="form-control" id="tf_gudang">
                            <option value="0">-Pilih Gudang-</option>
                            <?php
                            foreach ($gudang as $g) {
                                echo "<option value='" . $g['id_gudang'] . "'>" . $g['gudang'] . "</option>";
                            }
                            ?>
                        </select>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                <button type="button" onclick="tf_stok()" class="btn btn-primary">Transfer</button>
            </div>
        </div>
    </div>
</div>

<!--
<div class="modal fade" id="f_tf" tabindex="-1" style="display: none;" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Transfer Stok ke gudang</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">
                    <input type="hidden" class="form-control" id="edidlot">
                    <div class="col-12">
                        <label for="inputNanme4" class="form-label">Nomor Dokumen</label>
                        <input type="hidden" class="form-control" id="idlot">
                        <input type="hidden" class="form-control" id="skutf">
                        <input type="hidden" class="form-control" id="qtytf">
                        <input type="text" class="form-control" id="tfid">
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                <button type="button" onclick="tf_stok()" class="btn btn-primary">Transfer</button>
            </div>
        </div>
    </div>
</div>
-->

<div class="modal fade" id="f_void" tabindex="-1" style="display: none;" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Void Number</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form id="frm_void" name="frm_void" method="POST" Action="<?= base_url('Produksi/void_lot'); ?>">
                <div class="modal-body">
                    <input type="hidden" id="sku" name="sku">
                    <div id="tb_void"></div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                    <button type="submit" class="btn btn-primary">Ajukan Void</button>
                </div>
            </form>
        </div>
    </div>
</div>

<div class="modal fade" id="f_void_gudang" tabindex="-1" style="display: none;" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Void Number</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form id="frm_void_gudang" name="frm_void_gudang" method="POST" Action="<?= base_url('Produksi/void_lot'); ?>">
                <div class="modal-body">
                    <input type="hidden" id="sku_gudang" name="sku_gudang">
                    <div id="tb_void_gudang"></div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                    <button type="submit" class="btn btn-primary">Ajukan Void</button>
                </div>
            </form>
        </div>
    </div>
</div>

<script type="module">

</script>

<script>
    function tampildata() {
        var mulai = $("#mulai").val();
        var hingga = $("#hingga").val();
        var gudang = $("#id_gudang").val();
        if (gudang == '') {
            pesan_error('Gudang Mohon diisi');
        } else {
            document.getElementById("btnloading").style.display = "block";
            document.getElementById("btnview").style.display = "none";
            $.get("<?= base_url('Produksi/tb_lotfg?mulai='); ?>" + mulai + "&hingga=" + hingga + "&gudang=" + gudang, function(data, status) {
                $("#tbllot").html(data);
                document.getElementById("btnloading").style.display = "none";
                document.getElementById("btnview").style.display = "block";
            });
        }
    }
    /*
        $(document).ready(function() {
            tampildata();
        });
    */

/* this code is old but its works
    function cetaklot(id, sku, qty) {
        window.open("<?= base_url('Produksi/cetaklotfg?id='); ?>" + id, "_blank");
        //log
        var strTable = 'tb_lot';
        var keterangan = 'Telah mencetak barcode dengan Kode SKU : ' + sku + ' dan Qty sebanyak :' + qty;
        $.ajax({
            url: "<?= base_url('Pengaturan/log'); ?>",
            type: 'POST',
            cache: false,
            data: {
                strTable: strTable,
                keterangan: keterangan
            },
            success: function() {}
        });
        //end log
    }
*/

function cetaklot(id, sku, qty) {
        $.ajax({
            url: "<?= base_url('Produksi/sts_cetak'); ?>",
            type: 'POST',
            cache: false,
            data: {
                id: id
            },
            success: function() {
                tampildata();
                window.open("<?= base_url('Produksi/cetaklotfg?id='); ?>" + id, "_blank");
            }
        });
        //log
        var strTable = 'tb_lot';
        var keterangan = 'Telah mencetak barcode dengan Kode SKU : ' + sku + ' dan Qty sebanyak :' + qty;
        $.ajax({
            url: "<?= base_url('Pengaturan/log'); ?>",
            type: 'POST',
            cache: false,
            data: {
                strTable: strTable,
                keterangan: keterangan
            },
            success: function() {}
        });
        //end log
    }

    function tflot(id, sku, qty) {
        $("#idlot").val(id);
        $("#skutf").val(sku);
        $("#qtytf").val(qty);
        $("#f_tf").modal("show");
    }

/*
    function tf_stok() {
        var id = $("#idlot").val();
        var sku = $("#skutf").val();
        var qty = $("#qtytf").val();
        var tfid = $("#tfid").val();
        if (tfid == '') {
            pesan_error('Document number harus diisi');
        } else {
            $.ajax({
                url: "<?= base_url('Produksi/tfgudang'); ?>",
                type: 'POST',
                cache: false,
                data: {
                    id: id,
                    tfid: tfid
                },
                success: function() {
                    tampildata();
                    document.getElementById('tfid').value = '';
                    $("#f_tf").modal("hide");
                    //log
                    var strTable = 'tb_fg_gudang';
                    var keterangan = 'Telah melakukan Transfer barang FG dari Produksi ke Gudang dengan Kode SKU : ' + sku + ' dan Qty sebanyak :' + qty;
                    $.ajax({
                        url: "<?= base_url('Pengaturan/log'); ?>",
                        type: 'POST',
                        cache: false,
                        data: {
                            strTable: strTable,
                            keterangan: keterangan
                        },
                        success: function() {}
                    });
                    //end log
                }
            });
        }
    }
*/

    function tf_stok() {
        var id = $("#idlot").val();
        var sku = $("#skutf").val();
        var qty = $("#qtytf").val();
        var tf_gudang = $("#tf_gudang").val();
        if (tf_gudang == '0') {
            pesan_error('Pilih Gudang');
        } else {
            $.ajax({
                url: "<?= base_url('Produksi/tfgudang'); ?>",
                type: 'POST',
                cache: false,
                data: {
                    id: id,
                    tf_gudang: tf_gudang
                },
                success: function() {
                    tampildata();
                    document.getElementById('tf_gudang').value = '0';
                    $("#f_tf").modal("hide");
                    //log
                    var strTable = 'tb_fg_gudang';
                    var keterangan = 'Telah melakukan Transfer barang FG dari Produksi ke Gudang dengan Kode SKU : ' + sku + ' dan Qty sebanyak :' + qty;
                    $.ajax({
                        url: "<?= base_url('Pengaturan/log'); ?>",
                        type: 'POST',
                        cache: false,
                        data: {
                            strTable: strTable,
                            keterangan: keterangan
                        },
                        success: function() {}
                    });
                    //end log
                }
            });
        }
    }    

    function simpanlot() {
        var tgllot = $("#tgllot").val();
        var shiftlot = $("#shiftlot").val();
        var mesinlot = $("#mesinlot").val();
        var qtylot = $("#qtylot").val();
        var brglot = $("#brglot").val();
        if (tgllot == '') {
            document.getElementById('tgllot').focus();
            Swal.fire({
                title: "Tanggal harus diisi",
                icon: "warning"
            });
        } else {
            if (qtylot == '') {
                document.getElementById('qtylot').focus();
                Swal.fire({
                    title: "Jumlah BJ harus diisi",
                    icon: "warning"
                });
            } else {
		$("#tbllot").html('');
                $.ajax({
                    url: "<?= base_url('Produksi/simpanlotfg'); ?>",
                    type: 'POST',
                    cache: false,
                    data: {
                        brglot: brglot,
                        tgllot: tgllot,
                        shiftlot: shiftlot,
                        mesinlot: mesinlot,
                        qtylot: qtylot
                    },
                    success: function() {
                        tampildata();
                        //log
                        var strTable = 'tb_lot';
                        var keterangan = 'Telah membuat barcode dengan Kode SKU : ' + brglot + ' dan Qty sebanyak :' + qtylot;
                        $.ajax({
                            url: "<?= base_url('Pengaturan/log'); ?>",
                            type: 'POST',
                            cache: false,
                            data: {
                                strTable: strTable,
                                keterangan: keterangan
                            },
                            success: function() {}
                        });
                        //end log
                    }
                });
            }
        }
    }

    function unduhdatalot() {
        var mulai = $("#mulai").val();
        var hingga = $("#hingga").val();
	var gudang = $("#id_gudang").val();
        window.open("<?= base_url(); ?>Produksi/tb_lotfg_xls?mulai=" + mulai + "&hingga=" + hingga+"&gudang="+gudang, "_self");
    }

    function edit_lot(id, sku, sift, mesin) {
        $("#edidlot").val(id);
        $("#ed_fg").val(sku);
        $("#ed_sift").val(sift);
        $("#ed_mesin").val(mesin);
    }

    function update_lot() {
        var id = $("#edidlot").val();
        var sku = $("#ed_fg").val();
        var sift = $("#ed_sift").val();
        var mesin = $("#ed_mesin").val();
        $.ajax({
            url: "<?= base_url('Produksi/update_lot'); ?>",
            type: 'POST',
            cache: false,
            data: {
                id: id,
                sku: sku,
                sift: sift,
                mesin: mesin
            },
            success: function() {
                tampildata();
                $("#f_update_lot").modal("hide");
                pesan("Data update");
            }
        });
    }

    function pesan(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 pesan_error(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 lot_number(id, sku) {
        $("#f_void").modal("show");
        $("#sku").val(sku);
        $.get("<?= base_url('Produksi/tb_void?id='); ?>" + id, function(data, status) {
            $("#tb_void").html(data);
        });
    }

    /*
        function void_lot(lot, id) {
            
            Swal.fire({
                title: "Void Nomer Lot Berikut : ",
                text: lot,
                icon: "question",
                showCancelButton: true,
                confirmButtonColor: "#3085d6",
                cancelButtonColor: "#d33",
                confirmButtonText: "Yes, Void!"
            }).then((result) => {
                if (result.isConfirmed) {
                    //begin
                    $.ajax({
                        url: "<?= base_url('Produksi/void_lot'); ?>",
                        type: 'POST',
                        cache: false,
                        data: {
                            lot: lot
                        },
                        success: function() {
                            lot_number(id);
                            //log
                            var strTable = 'tb_lot';
                            var keterangan = 'Telah melakukan void nomor lot : ' + lot;
                            $.ajax({
                                url: "<?= base_url('Pengaturan/log'); ?>",
                                type: 'POST',
                                cache: false,
                                data: {
                                    strTable: strTable,
                                    keterangan: keterangan
                                },
                                success: function() {}
                            });
                            //end log
                        }
                    });
                    //end
                }
            });            
        }
            */

    function open_lot(lot, id) {
        Swal.fire({
            title: "Batalkan Void : ",
            text: lot,
            icon: "question",
            showCancelButton: true,
            confirmButtonColor: "#3085d6",
            cancelButtonColor: "#d33",
            confirmButtonText: "Yes, Open Void!"
        }).then((result) => {
            if (result.isConfirmed) {
                //begin
                $.ajax({
                    url: "<?= base_url('Produksi/open_lot'); ?>",
                    type: 'POST',
                    cache: false,
                    data: {
                        lot: lot
                    },
                    success: function() {
                        lot_number(id);
                        //log
                        var strTable = 'tb_lot';
                        var keterangan = 'Telah membatalkan void nomor lot : ' + lot;
                        $.ajax({
                            url: "<?= base_url('Pengaturan/log'); ?>",
                            type: 'POST',
                            cache: false,
                            data: {
                                strTable: strTable,
                                keterangan: keterangan
                            },
                            success: function() {}
                        });
                        //end log
                    }
                });
                //end
            }
        });
    }
</script>

<script>
    function toggle(source) {
        var checkboxes = document.querySelectorAll('input[type="checkbox"]');
        for (var i = 0; i < checkboxes.length; i++) {
            if (checkboxes[i] != source)
                checkboxes[i].checked = source.checked;
        }
    }
</script>

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
    $(document).ready(function() {
        $('form[name="frm_void"]').on('submit', function(event) {
            event.preventDefault();
            var formData = new FormData(this);
            $.ajax({
                url: '<?= base_url('Produksi/void_lot'); ?>',
                type: 'POST',
                data: formData,
                processData: false,
                contentType: false,
                success: function(response) {
                    Swal.fire("Void nomor lot telah diajukan");
                    $("#f_void").modal("hide");
                    /*
                    Swal.fire({
                        title: 'Void nomor lot telah diajukan',
                        icon: 'success',
                        didClose: () => {
                            $("#f_void").modal("hide");
                        }
                    });
                    */
                    /*
                    // Menyimpan log
                    var kd_lpbj = "<?= $nmrlpbj['kd_lpbj']; ?>";
                    var strTable = 'tb_apprv';
                    var keterangan = 'Telah melakukan aproval lpbj ' + kd_lpbj;

                    $.ajax({
                        url: "<?= base_url('Pengaturan/log'); ?>",
                        type: 'POST',
                        cache: false,
                        data: {
                            strTable: strTable,
                            keterangan: keterangan
                        },
                        success: function() {
                            // Anda bisa menambahkan kode lain di sini jika diperlukan
                        }
                    });
                    */
                },
                error: function(xhr, status, error) {
                    Swal.fire({
                        title: 'Terjadi kesalahan',
                        text: error,
                        icon: 'error'
                    });
                }
            });
        });

        $('form[name="frm_void_gudang"]').on('submit', function(event) {
            event.preventDefault();
            var formData = new FormData(this);
            $.ajax({
                url: '<?= base_url('Produksi/void_lot_gudang'); ?>',
                type: 'POST',
                data: formData,
                processData: false,
                contentType: false,
                success: function(response) {
                    Swal.fire("Void nomor lot telah diajukan");
                    $("#f_void_gudang").modal("hide");
                    /*
                    Swal.fire({
                        title: 'Void nomor lot telah diajukan',
                        icon: 'success',
                        didClose: () => {
                            $("#f_void").modal("hide");
                        }
                    });
                    */
                    /*
                    // Menyimpan log
                    var kd_lpbj = "<?= $nmrlpbj['kd_lpbj']; ?>";
                    var strTable = 'tb_apprv';
                    var keterangan = 'Telah melakukan aproval lpbj ' + kd_lpbj;

                    $.ajax({
                        url: "<?= base_url('Pengaturan/log'); ?>",
                        type: 'POST',
                        cache: false,
                        data: {
                            strTable: strTable,
                            keterangan: keterangan
                        },
                        success: function() {
                            // Anda bisa menambahkan kode lain di sini jika diperlukan
                        }
                    });
                    */
                },
                error: function(xhr, status, error) {
                    Swal.fire({
                        title: 'Terjadi kesalahan',
                        text: error,
                        icon: 'error'
                    });
                }
            });
        });
    });

$(document).ready(function() {
        $('#id_gudang').select2();
    });

function lot_number_gudang(id, sku) {
        $("#f_void_gudang").modal("show");
        $("#sku_gudang").val(sku);
        $.get("<?= base_url('Produksi/tb_void_gudang?id='); ?>" + id, function(data, status) {
            $("#tb_void_gudang").html(data);
        });
    }
</script>