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

    <div class="pagetitle">
        <h1><i class="bi bi-truck"></i>&nbsp;Pengiriman 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-4">
                                    <input type="text" class="form-control" id="no_sj" placeholder="Nomor Surat Jalan">
                                </div>
                                <div class="col-md-4">
                                    <select class="form-control" onchange="fokus()" id="kendaraan">
                                        <option value="0">- Pilih Kendaraan -</option>
                                        <?php
                                        foreach ($kendaraan as $k) {
                                            echo "<option value='" . $k['id_kendaraan'] . "'>" . $k['no_pol'] . " - " . $k['merk'] . "</option>";
                                        }
                                        ?>
                                    </select>
                                </div>
                                <div class="col-md-4">
                                    <button onclick="tampildatakirim()" type="button" class="btn btn-primary">
                                        Lihat
                                    </button>
                                    <button onclick="f_scan()" type="button" class="btn btn-warning">
                                        Mulai Scan
                                    </button>
                                </div>
                            </div>
                            <br>
                            <div class="datatable-container">
                                <div id="tbbjdelivery"></div>
                            </div>
                        </div>
                        <!-- End Table with stripped rows -->

                    </div>
                </div>

            </div>
        </div>
    </section>
</main>

<div class="modal fade" id="f_scan_kirim" tabindex="-1" aria-hidden="true" style="display: none;">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Scan Kode Pengiriman</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="text" class="form-control" id="no_bj" onchange="cari_kode(this.value)" autofocus>
                    </div>
                    <div class="col-md-12">
                        <div id="notifikasi"></div>
                    </div>
                    <div class="col-md-12">
                        <div id="btnMulaiScan"></div>
                        <button type="button" style="float:right;" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                    </div>
                </div>
            </div>
            <div class="modal-footer">

            </div>
        </div>
    </div>
</div>

<script>
    function f_scan() {
        if ($("#no_sj").val() == "") {
            pesan_error("Nomor Surat jalan mohon diisi");
            document.getElementById('no_sj').focus();
        } else {
            if ($("#kendaraan").val() == 0) {
                pesan_error("Mohon Pilih kendaraan");
                document.getElementById('kendaraan').focus();
            } else {
                $('#f_scan_kirim').modal("show");
                $("#btnMulaiScan").html('<button type="button" class="btn btn-primary" onclick="fokus()">' +
                    'Scan Barcode <span class="badge bg-white text-primary">0</span></button>');
            }
        }
    }

    function tampildatakirim() {
        var no_sj = $("#no_sj").val();
        var kendaraan = $("#kendaraan").val();
        if ($("#no_sj").val() == "") {
            pesan_error("Nomor Surat jalan mohon diisi");
            document.getElementById('no_sj').focus();
        } else {
            if ($("#kendaraan").val() == 0) {
                pesan_error("Mohon Pilih kendaraan");
                document.getElementById('kendaraan').focus();
            } else {
                $.get("<?= base_url('Gudang/cek_pengiriman?no_sj='); ?>" + no_sj + "&kendaraan=" + kendaraan, function(data, status) {
                    $("#tbbjdelivery").html(data);
                });
            }
        }
    }

    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 no_sj = $("#no_sj").val();
        var kendaraan = $("#kendaraan").val();

        $.get("<?= base_url('Gudang/cari_kode_kirim1?kode='); ?>" + kode_fg, function(data, status) {
            if (data == 0) {
                document.getElementById('no_bj').value = "";
                document.getElementById('no_bj').focus();
                $("#notifikasi").html('<div class="alert alert-danger alert-dismissible fade show" role="alert">' +
                    'Kode ' + kode_fg + ' tidak ditemukan' +
                    '<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>' +
                    '</div>');
            }

            if (data == 1) {
                //alert("Ok");
                $.get("<?= base_url('Gudang/cari_kode_kirim2?kode='); ?>" + kode_fg, function(data, status) {
                    if (data == 0) {
                        document.getElementById('no_bj').value = "";
                        document.getElementById('no_bj').focus();
                        $("#notifikasi").html('<div class="alert alert-warning alert-dismissible fade show" role="alert">' +
                            'Kode ' + kode_fg + ' sudah dikirim' +
                            '<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>' +
                            '</div>');
                    }
                    if (data == 1) {
                        $.ajax({
                            url: "<?= base_url('Gudang/cari_kode_kirim_simpan'); ?>",
                            type: 'POST',
                            cache: false,
                            data: {
                                kode_fg: kode_fg,
                                no_sj: no_sj,
                                kendaraan: kendaraan
                            },
                            success: function() {
                                $("#notifikasi").html('<div class="alert alert-success alert-dismissible fade show" role="alert">' +
                                    'Kode ' + kode_fg + ' siap dikirim' +
                                    '<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>' +
                                    '</div>');
                                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
        });
    }
</script>