<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 g-3">
                                <div class="col-md-1">
                                    <label class="form-label">Mulai</label>
                                </div>
                                <div class="col-md-3">
                                    <input type="date" class="form-control" id="mulai" value="<?= date('Y-m-d'); ?>">
                                </div>
                                <div class="col-md-1">
                                    <label class="form-label">Hingga</label>
                                </div>
                                <div class="col-md-3">
                                    <input type="date" class="form-control" id="hingga" value="<?= date('Y-m-d'); ?>">
                                </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">
                    <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>

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

    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 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 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 {
                $.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();
        window.open("<?= base_url(); ?>Produksi/tb_lotfg_xls?mulai=" + mulai + "&hingga=" + hingga, "_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
        });
    }
</script>