<main id="main" class="main">
    <div class="pagetitle">
        <h1>Selamat Datang <?= $this->session->strNama; ?></h1>
        <nav>
            <ol class="breadcrumb">
                <li class="breadcrumb-item"><a href="index.html">Home</a></li>
                <li class="breadcrumb-item active">Scan kirim retur</li>
            </ol>
        </nav>
    </div>
    <script src="<?= base_url(); ?>assets/js/html5-qrcode.min.js"></script>
    <section class="section dashboard">
        <div class="col-lg-12">
            <div class="card">
                <div class="card-body">
                    <div class="row g-3">
                        <div class="col-md-12">
                            <div class="d-grid gap-2 mt-3">
                                <button onclick="f_scan()" type="button" id="btnview" class="btn btn-primary" type="button"><i class="bi bi-upc-scan"></i>&nbsp;Scan Final Retur</button>
                            </div>
                        </div>
                    </div>
                    <button type="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>

                </div>
            </div>
        </div>
        <div class="row">
            <div id="tb_retur"></div>
        </div>
    </section>
</main>
<script src="<?= base_url(); ?>assets/js/scanner_kirim.js"></script>


<div class="modal fade" id="scan_kirim_fg" tabindex="-1" aria-hidden="true" style="display: none;">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <div id="notifikasi_kirim"></div>
                <div id="notifikasi_sts"></div>
                <button type="button" onclick="tampildatakirim()" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
            </div>
            <div class="modal-body">
                <input type="hidden" id="id_retur">
                <input type="hidden" id="sts">
                <div class="row g-3">
                    <div class="col-md-12">
                        <div id="reader"></div>
                        <input type="hidden" class="form-control" id="no_bj" onchange="cari_kode(this.value)" autofocus>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="f_scrap" tabindex="-1" aria-hidden="true" style="display: none;">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Keterangan Scrap</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-12">
                        <input type="hidden" id="id_scrap">
                        <input type="hidden" id="lot_scrap">
                        <textarea class="form-control" id="keterangan_scrap"></textarea>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                <button type="button" onclick="simpan_scrap()" class="btn btn-danger">Simpan Scrap</button>
            </div>
        </div>
    </div>
</div>


<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
    $(document).ready(function() {
        $.get("<?= base_url('Qc/tb_kirim_retur'); ?>", function(data, status) {
            $("#tb_retur").html(data);
        });
    });
</script>

<script>
    function scrap(id, lot) {
        $("#id_scrap").val(id);
        $("#lot_scrap").val(lot);
    }

    function simpan_scrap() {
        var id_scrap = $("#id_scrap").val();
        var keterangan = $("#keterangan_scrap").val();
        var lot = $("#lot_scrap").val();
        if (keterangan == '') {
            pesan('keterangan mohon diisi');
        } else {
            $.ajax({
                url: "<?= base_url('Qc/simpan_scrap'); ?>",
                type: 'POST',
                cache: false,
                data: {
                    id_scrap: id_scrap,
                    keterangan: keterangan
                },
                success: function(data, status) {
                    tampildatakirim();
                    document.getElementById('keterangan_scrap').value = '';
                    $("#f_scrap").modal("hide");
                    //log
                    var strTable = 'tb_retur';
                    var keterangan = 'Telah melakukan scan barcode untuk scrap dengan nomor LOT : ' + lot;
                    $.ajax({
                        url: "<?= base_url('Pengaturan/log'); ?>",
                        type: 'POST',
                        cache: false,
                        data: {
                            strTable: strTable,
                            keterangan: keterangan
                        },
                        success: function() {}
                    });
                }
            });
        }
    }

    function tampildatakirim() {
        document.getElementById("btnloading").style.display = "block";
        document.getElementById("btnview").style.display = "none";
        $.get("<?= base_url('Qc/tb_kirim_retur'); ?>", function(data, status) {
            $("#tb_retur").html(data);
            document.getElementById("btnloading").style.display = "none";
            document.getElementById("btnview").style.display = "block";
        });
        tutup();
    }

    function f_scan() {
        //$("#id_retur").val(id);
        //$("#sts").val(sts);
        $("#notifikasi_kirim").html('<h5 class="modal-title">Scan Retur</h5>');
        $("#scan_kirim_fg").modal("show");
    }

    function beep(txt) {
        const Toast = Swal.mixin({
            toast: true,
            position: "top-end",
            showConfirmButton: false,
            timer: 3000,
            timerProgressBar: false,
            didOpen: (toast) => {
                toast.onmouseenter = Swal.stopTimer;
                toast.onmouseleave = Swal.resumeTimer;
            }
        });
        Toast.fire({
            icon: "success",
            title: txt + " Disimpan"
        });
        var snd = new Audio("data:audio/wav;base64,//uQRAAAAWMSLwUIYAAsYkXgoQwAEaYLWfkWgAI0wWs/ItAAAGDgYtAgAyN+QWaAAihwMWm4G8QQRDiMcCBcH3Cc+CDv/7xA4Tvh9Rz/y8QADBwMWgQAZG/ILNAARQ4GLTcDeIIIhxGOBAuD7hOfBB3/94gcJ3w+o5/5eIAIAAAVwWgQAVQ2ORaIQwEMAJiDg95G4nQL7mQVWI6GwRcfsZAcsKkJvxgxEjzFUgfHoSQ9Qq7KNwqHwuB13MA4a1q/DmBrHgPcmjiGoh//EwC5nGPEmS4RcfkVKOhJf+WOgoxJclFz3kgn//dBA+ya1GhurNn8zb//9NNutNuhz31f////9vt///z+IdAEAAAK4LQIAKobHItEIYCGAExBwe8jcToF9zIKrEdDYIuP2MgOWFSE34wYiR5iqQPj0JIeoVdlG4VD4XA67mAcNa1fhzA1jwHuTRxDUQ//iYBczjHiTJcIuPyKlHQkv/LHQUYkuSi57yQT//uggfZNajQ3Vmz+Zt//+mm3Wm3Q576v////+32///5/EOgAAADVghQAAAAA//uQZAUAB1WI0PZugAAAAAoQwAAAEk3nRd2qAAAAACiDgAAAAAAABCqEEQRLCgwpBGMlJkIz8jKhGvj4k6jzRnqasNKIeoh5gI7BJaC1A1AoNBjJgbyApVS4IDlZgDU5WUAxEKDNmmALHzZp0Fkz1FMTmGFl1FMEyodIavcCAUHDWrKAIA4aa2oCgILEBupZgHvAhEBcZ6joQBxS76AgccrFlczBvKLC0QI2cBoCFvfTDAo7eoOQIn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ERTZSmqU36A3CumzN/9Robv/Xx4v9ijkSRSNLQhAWumap82WRSBUqXStV/YcS+XVLnSS+WLDroqArFkMEsAS+eWmrUzrO0oEmE40RlMZ5+ODIkAyKAGUwZ3mVKmcamcJnMW26MRPgUw6j+LkhyHGVGYjSUUKNpuJUQoOIAyDvEyG8S5yfK6dhZc0Tx1KI/gviKL6qvvFs1+bWtaz58uUNnryq6kt5RzOCkPWlVqVX2a/EEBUdU1KrXLf40GoiiFXK///qpoiDXrOgqDR38JB0bw7SoL+ZB9o1RCkQjQ2CBYZKd/+VJxZRRZlqSkKiws0WFxUyCwsKiMy7hUVFhIaCrNQsKkTIsLivwKKigsj8XYlwt/WKi2N4d//uQRCSAAjURNIHpMZBGYiaQPSYyAAABLAAAAAAAACWAAAAApUF/Mg+0aohSIRobBAsMlO//Kk4soosy1JSFRYWaLC4qZBYWFRGZdwqKiwkNBVmoWFSJkWFxX4FFRQWR+LsS4W/rFRb/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////VEFHAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAU291bmRib3kuZGUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMjAwNGh0dHA6Ly93d3cuc291bmRib3kuZGUAAAAAAAAAACU=");
        snd.play();
    }

    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: "error",
            title: txt
        });
    }
</script>

<script>
    function fokus() {
        $('#no_bj').focus();
        document.getElementById('no_bj').focus();
    }

    $('#f_scan_kirim').on('shown.bs.modal', function() {
        $('#no_bj').focus();
    });

    function cari_kode(kode_fg) {
        $("#notifikasi_kirim").html('<h5 class="modal-title">' + kode_fg + '</h5>');
        $.get("<?= base_url('Qc/cari_retur_kirim?kd='); ?>" + kode_fg, function(data, status) {
            if (data == 1) {
                $.ajax({
                    url: "<?= base_url('Qc/kirim_kode_retur'); ?>",
                    type: 'POST',
                    cache: false,
                    data: {
                        kode_fg: kode_fg
                    },
                    success: function() {
                        beep(kode_fg);
                        $("#notifikasi").html('');
                        document.getElementById('no_bj').value = "";
                        document.getElementById('no_bj').focus();
                        //log
                        var strTable = 'tb_retur';
                        var keterangan = 'Telah melakukan scan barcode untuk kirim retur dengan nomor LOT : ' + kode_fg;
                        $.ajax({
                            url: "<?= base_url('Pengaturan/log'); ?>",
                            type: 'POST',
                            cache: false,
                            data: {
                                strTable: strTable,
                                keterangan: keterangan
                            },
                            success: function() {}
                        });
                    }
                });
            }
        });
    }

    var input = document.getElementById("no_bj");
    var kode_fg = $("#no_bj").val();
    input.addEventListener("keypress", function(event) {
        if (event.key === "Enter") {
            event.preventDefault();
            cari_kode(kode_fg);
        }
    });

    function pesankirim() {
        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: "Barang telah discan"
        });
    }

    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 f_scan_exp(sj) {
            $("#notifikasi_kirim").html('<h5 class="modal-title">Scan Pengiriman</h5>');
            document.getElementById('no_pol').value = '';
            $("#no_sj").val(sj);
            $("#scan_kirim_fg").modal("show");
        }
        */

    function tutup() {
        $('#html5-qrcode-button-camera-stop').click();
    }

    function kirim_gudang(id, lot) {
        Swal.fire({
            title: "Kirim barang ini ke gudang ?",
            //text: "You won't be able to revert this!",
            icon: "question",
            showCancelButton: true,
            confirmButtonColor: "#3085d6",
            cancelButtonColor: "#d33",
            confirmButtonText: "Ya, kirim!"
        }).then((result) => {
            if (result.isConfirmed) {
                $.ajax({
                    url: "<?= base_url('Qc/kirim_gudang'); ?>",
                    type: 'POST',
                    cache: false,
                    data: {
                        id: id
                    },
                    success: function(data) {
                        //log
                        var strTable = 'tb_retur';
                        var keterangan = 'Telah melakukan scan barcode untuk kirim retur dengan nomor LOT : ' + lot;
                        $.ajax({
                            url: "<?= base_url('Pengaturan/log'); ?>",
                            type: 'POST',
                            cache: false,
                            data: {
                                strTable: strTable,
                                keterangan: keterangan
                            },
                            success: function() {}
                        });
                    }
                });
            }
        });
    }
</script>