<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">Mutasi SJ</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">
                    <br>
                    <div class="row g-3">
                        <div class="col-md-6">
                            <label class="form-label"><i class="bi bi-box-arrow-right"></i>&nbsp;SJ Awal</label>
                            <select class="form-control" id="sj_awal" onchange="get_sj(this.value)">
                                <option value='0'>-Pilih SJ Awal-</option>
                                <?php
                                foreach ($sj as $s) {
                                    echo "<option value='" . $s['doc_sj'] . "'>" . $s['no_sj'] . "</option>";
                                }
                                ?>
                            </select>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label"><i class="bi bi-box-arrow-in-right"></i>&nbsp;SJ Tujuan</label>
                            <select class="form-control" id="sj_tujuan" onchange="tampildata()"></select>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div id="tb_fc"></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="tampildata()" 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 fokus() {
        $('#no_bj').focus();
        document.getElementById('no_bj').focus();
    }

    function get_sj(val) {
        $.get("<?= base_url('scan/get_sj'); ?>", {
            sj: val
        }, function(data, status) {
            $("#sj_tujuan").html(data);
        }).fail(function(jqXHR, textStatus, errorThrown) {
            pesan_error("Request failed: " + textStatus + ", " + errorThrown);
        });
    }

    function tampildatakirim() {
        $.get("<?= base_url('Scan/tb_scan_kirim'); ?>", function(data, status) {
            $("#tb_loading").html(data);
        }).fail(function(jqXHR, textStatus, errorThrown) {
            pesan_error("Request failed: " + textStatus + ", " + errorThrown);
        });
    }

    function tampildata() {
        var sj = $("#sj_tujuan").val();
        $.get("<?= base_url('Scan/tb_scan_pindah_sj'); ?>", {
            sj: sj
        }, function(data, status) {
            $("#tb_fc").html(data);
        }).fail(function(jqXHR, textStatus, errorThrown) {
            pesan_error("Request failed: " + textStatus + ", " + errorThrown);
        });
    }

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

    function beep(txt, tipe) {
        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: tipe,
            title: txt
        });
        var snd = new Audio("data:audio/wav;base64,//uQRAAAAWMSLwUIYAAsYkXgoQwAEaYLWfkWgAI0wWs/ItAAAGDgYtAgAyN+QWaAAihwMWm4G8QQRDiMcCBcH3Cc+CDv/7xA4Tvh9Rz/y8QADBwMWgQAZG/ILNAARQ4GLTcDeIIIhxGOBAuD7hOfBB3/94gcJ3w+o5/5eIAIAAAVwWgQAVQ2ORaIQwEMAJiDg95G4nQL7mQVWI6GwRcfsZAcsKkJvxgxEjzFUgfHoSQ9Qq7KNwqHwuB13MA4a1q/DmBrHgPcmjiGoh//EwC5nGPEmS4RcfkVKOhJf+WOgoxJclFz3kgn//dBA+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rOF/FvDoADYAGBMGb7FtErm5MXMlmPAJQVgWta7Zx2go+8xJ0UiCb8LHHdftWyLJE0QIAIsI+UbXu67dZMjmgDGCGl1H+vpF4NSDckSIkk7Vd+sxEhBQMRU8j/12UIRhzSaUdQ+rQU5kGeFxm+hb1oh6pWWmv3uvmReDl0UnvtapVaIzo1jZbf/pD6ElLqSX+rUmOQNpJFa/r+sa4e/pBlAABoAAAAA3CUgShLdGIxsY7AUABPRrgCABdDuQ5GC7DqPQCgbbJUAoRSUj+NIEig0YfyWUho1VBBBA//uQZB4ABZx5zfMakeAAAAmwAAAAF5F3P0w9GtAAACfAAAAAwLhMDmAYWMgVEG1U0FIGCBgXBXAtfMH10000EEEEEECUBYln03TTTdNBDZopopYvrTTdNa325mImNg3TTPV9q3pmY0xoO6bv3r00y+IDGid/9aaaZTGMuj9mpu9Mpio1dXrr5HERTZSmqU36A3CumzN/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, tipe) {
        const Toast = Swal.mixin({
            toast: true,
            position: "top-end",
            showConfirmButton: false,
            timer: 2000,
            timerProgressBar: true,
            didOpen: (toast) => {
                toast.onmouseenter = Swal.stopTimer;
                toast.onmouseleave = Swal.resumeTimer;
            }
        });
        Toast.fire({
            icon: tipe,
            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 cari_kode(kode) {
        var kendaraan = $("#no_pol").val();
        var no_sj = $("#no_sj").val();
        var sj = $("#sj_awal").val();
        $("#notifikasi_kirim").html('<h5 class="modal-title">' + kode + '</h5>');
        $.get("<?= base_url('Scan/cari_kode_sj'); ?>", {
            kode: kode,
            sj: sj
        }, function(data, status) {
            if (data == 1) {
                $.ajax({
                    url: "<?= base_url('Gudang/cari_kode_kirim_simpan2'); ?>",
                    type: 'POST',
                    cache: false,
                    data: {
                        kode_fg: kode,
                        kendaraan: kendaraan,
                        no_sj: no_sj
                    },
                    success: function() {
                        beep("Kode " + kode + " telah dipindahkan");
                        //log
                        var strTable = 'tb_fg_gudang';
                        var keterangan = 'Telah melakukan scan mutasi dengan nomor LOT : ' + kode + ' dari kode SJ : ' + sj + ' ke kode SJ ' + no_sj;
                        $.ajax({
                            url: "<?= base_url('Pengaturan/log'); ?>",
                            type: 'POST',
                            cache: false,
                            data: {
                                strTable: strTable,
                                keterangan: keterangan
                            },
                            success: function() {}
                        });
                    }
                });
            }
        }).fail(function(jqXHR, textStatus, errorThrown) {
            pesan_error("Request failed: " + textStatus + ", " + errorThrown);
        });
    }

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