<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 pengiriman klero</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="d-grid gap-2 mt-3">
                        <button onclick="tampildatakirim()" type="button" id="btnview" class="btn btn-primary">Reload</button>
                        <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>
        <div class="row">
            <div id="tb_loading"></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>
                <button type="button" onclick="tampildatakirim()" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
            </div>
            <div class="modal-body">
                <input type="hidden" id="no_pol">
                <input type="hidden" id="no_sj">
                <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>



<script>
    function tampildatakirim() {
        document.getElementById("btnloading").style.display = "block";
        document.getElementById("btnview").style.display = "none";
        $.get("<?= base_url('Sub_klero/tb_scan_kirim'); ?>", function(data, status) {
            $("#tb_loading").html(data);
            document.getElementById("btnloading").style.display = "none";
            document.getElementById("btnview").style.display = "block";
        });
    }

    function f_scan(no_pol, sj) {
        $("#notifikasi_kirim").html('<h5 class="modal-title">Scan Pengiriman</h5>');
        $("#no_pol").val(no_pol);
        $("#no_sj").val(sj);
        tutup();
        $("#scan_kirim_fg").modal("show");
    }

    function beep() {
        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//uQZAUAB1WI0PZugAAAAAoQwAAAEk3nRd2qAAAAACiDgAAAAAAABCqEEQRLCgwpBGMlJkIz8jKhGvj4k6jzRnqasNKIeoh5gI7BJaC1A1AoNBjJgbyApVS4IDlZgDU5WUAxEKDNmmALHzZp0Fkz1FMTmGFl1FMEyodIavcCAUHDWrKAIA4aa2oCgILEBupZgHvAhEBcZ6joQBxS76AgccrFlczBvKLC0QI2cBoCFvfTDAo7eoOQInqDPBtvrDEZBNYN5xwNwxQRfw8ZQ5wQVLvO8OYU+mHvFLlDh05Mdg7BT6YrRPpCBznMB2r//xKJjyyOh+cImr2/4doscwD6neZjuZR4AgAABYAAAABy1xcdQtxYBYYZdifkUDgzzXaXn98Z0oi9ILU5mBjFANmRwlVJ3/6jYDAmxaiDG3/6xjQQCCK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KI/gviKL6qvvFs1+bWtaz58uUNnryq6kt5RzOCkPWlVqVX2a/EEBUdU1KrXLf40GoiiFXK///qpoiDXrOgqDR38JB0bw7SoL+ZB9o1RCkQjQ2CBYZKd/+VJxZRRZlqSkKiws0WFxUyCwsKiMy7hUVFhIaCrNQsKkTIsLivwKKigsj8XYlwt/WKi2N4d//uQRCSAAjURNIHpMZBGYiaQPSYyAAABLAAAAAAAACWAAAAApUF/Mg+0aohSIRobBAsMlO//Kk4soosy1JSFRYWaLC4qZBYWFRGZdwqKiwkNBVmoWFSJkWFxX4FFRQWR+LsS4W/rFRb/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////VEFHAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAU291bmRib3kuZGUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMjAwNGh0dHA6Ly93d3cuc291bmRib3kuZGUAAAAAAAAAACU=");
        snd.play();
    }
</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) {
        var kendaraan = $("#no_pol").val();
        var no_sj = $("#no_sj").val();
        //$("#notifikasi_kirim").html('<h5 class="modal-title">' + kode_fg + '</h5>');
        $.get("<?= base_url('Sub_klero/cari_kode_kirim1?kode='); ?>" + kode_fg, function(data, status) {
            if (data == 0) {
                document.getElementById('no_bj').value = "";
                document.getElementById('no_bj').focus();

                $("#notifikasi_kirim").html('<span class="badge bg-danger"><i class="bi bi-check-circle me-1"></i>&nbsp;' + kode_fg + '</span>');

            }

            if (data > 0) {
                $.get("<?= base_url('Sub_klero/cari_kode_kirim2?kode='); ?>" + kode_fg, function(data, status) {
                    if (data == 1) {
                        document.getElementById('no_bj').value = "";
                        document.getElementById('no_bj').focus();

                        $("#notifikasi_kirim").html('<span class="badge bg-warning"><i class="bi bi-check-circle me-1"></i>&nbsp;' + kode_fg + '</span>');
                    }
                    if (data == 0) {
                        $.ajax({
                            url: "<?= base_url('Sub_klero/simpan_kirim'); ?>",
                            type: 'POST',
                            cache: false,
                            data: {
                                kode_fg: kode_fg,
                                no_sj: no_sj
                            },
                            success: function() {
                                beep();
                                $("#notifikasi_kirim").html('<span class="badge bg-success"><i class="bi bi-check-circle me-1"></i>&nbsp;' + kode_fg + '</span>');
                                document.getElementById('no_bj').value = "";
                                document.getElementById('no_bj').focus();
                                //log
                                var strTable = 'tb_fg_gudang';
                                var keterangan = 'Telah melakukan scan barcode untuk pengiriman dengan nomor LOT : ' + kode_fg + ' dan no Surat jalan : ' + no_sj;
                                $.ajax({
                                    url: "<?= base_url('Pengaturan/log'); ?>",
                                    type: 'POST',
                                    cache: false,
                                    data: {
                                        strTable: strTable,
                                        keterangan: keterangan
                                    },
                                    success: function() {}
                                });
                                /*
                                                                $.get("<?= base_url('Gudang/hitung_kirim?kendaraan='); ?>" + kendaraan + "&no_sj=" + no_sj, function(data, status) {
                                                                    $("#btnMulaiScan").html('<button type="button" class="btn btn-primary" onclick="fokus()">' +
                                                                        'Scan Barcode <span class="badge bg-white text-primary">' + data + '</span></button>');
                                                                });
                                                                */
                            }
                        });
                    }
                });
            }
        });
    }

    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);
        tutup();
        $("#scan_kirim_fg").modal("show");
    }

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