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

    <div class="pagetitle">
        <h1>Penerimaan 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-12">
                                    <label class="form-label">Scan Nomor LOT FG</label>
                                    <input type="text" class="form-control" id="no_bj" onchange="scan(this.value)" autofocus>
                                </div>
                                <div class="col-md-4">
                                    <div id="notifikasi_kode_bj"></div>
                                </div>
                                <div class="col-md-4">
                                    <div id="notifikasi"></div>
                                </div>
                                <div class="col-md-4">
                                    <button type="button" style="float:right;" class="btn btn-primary" onclick="tampildata()">List Scan Hari ini</button>
                                </div>
                            </div>
                            <br>
                            <div class="datatable-container">
                                <div id="tbbjlist"></div>
                            </div>
                        </div>
                        <!-- End Table with stripped rows -->

                    </div>
                </div>

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

</main>

<script>
    $(document).ready(function() {
        $.get("<?= base_url('Gudang/fg_list'); ?>", function(data, status) {
            $("#tbbjlist").html(data);
        });
    });

    function tampildata() {
        $.get("<?= base_url('Gudang/fg_list'); ?>", function(data, status) {
            $("#tbbjlist").html(data);
        });
        document.getElementById('no_bj').focus();
    }

    function scan(id) {
        $.ajax({
            url: "<?= base_url('Gudang/scan_fg'); ?>",
            type: 'POST',
            cache: false,
            data: {
                id: id
            },
            success: function(data) {
                document.getElementById('no_bj').value = "";
                document.getElementById('no_bj').focus();

                if (data == 0) {
                    $("#notifikasi_kode_bj").html('<div class="alert alert-primary alert-dismissible fade show" role="alert">' +
                        'Kode FG : ' + id +
                        '<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>' +
                        '</div>');

                    $("#notifikasi").html('<div class="alert alert-danger alert-dismissible fade show" role="alert">' +
                        'Barang tidak ditemukan' +
                        '<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>' +
                        '</div>');
                }

                if (data > 0) {

                    $.ajax({
                        url: "<?= base_url('Gudang/cari_fg_gudang'); ?>",
                        type: 'POST',
                        cache: false,
                        data: {
                            id: id
                        },
                        success: function(data) {

                            if (data == 0) {
                                $("#notifikasi_kode_bj").html('<div class="alert alert-primary alert-dismissible fade show" role="alert">' +
                                    'Kode FG : ' + id +
                                    '<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>' +
                                    '</div>');

                                $("#notifikasi").html('<div class="alert alert-warning alert-dismissible fade show" role="alert">' +
                                    'Barang belum 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_fg_gudang2'); ?>",
                                    type: 'POST',
                                    cache: false,
                                    data: {
                                        id: id
                                    },
                                    success: function(data) {
                                        if (data == 1) {
                                            $("#notifikasi_kode_bj").html('<div class="alert alert-primary alert-dismissible fade show" role="alert">' +
                                                'Kode FG : ' + id +
                                                '<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>' +
                                                '</div>');

                                            $("#notifikasi").html('<div class="alert alert-warning alert-dismissible fade show" role="alert">' +
                                                'Barang sudah digudang' +
                                                '<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>' +
                                                '</div>');
                                        }
                                        if (data == 0) {
                                            $.ajax({
                                                url: "<?= base_url('Gudang/fg_diterima_gudang'); ?>",
                                                type: 'POST',
                                                cache: false,
                                                data: {
                                                    id: id
                                                },
                                                success: function(data) {
                                                    $("#notifikasi_kode_bj").html('<div class="alert alert-primary alert-dismissible fade show" role="alert">' +
                                                        'Kode FG : ' + id +
                                                        '<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>' +
                                                        '</div>');

                                                    $("#notifikasi").html('<div class="alert alert-success alert-dismissible fade show" role="alert">' +
                                                        'Barang diterima' +
                                                        '<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>' +
                                                        '</div>');
                                                    tampildata();
                                                    //log
                                                    var strTable = 'tb_fg_gudang';
                                                    var keterangan = 'Telah menerima barang FG dengan Nomor LOT : ' + id;
                                                    $.ajax({
                                                        url: "<?= base_url('Pengaturan/log'); ?>",
                                                        type: 'POST',
                                                        cache: false,
                                                        data: {
                                                            strTable: strTable,
                                                            keterangan: keterangan
                                                        },
                                                        success: function() {}
                                                    });
                                                }
                                            });
                                        }
                                    }
                                });
                            }

                        }
                    });

                }

            }
        });
    }
</script>