<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)">
                                </div>
                                <div class="col-md-4">
                                    <div id="notifikasi_kode_bj"></div>
                                </div>
                                <div class="col-md-4">
                                    <div id="notifikasi"></div>
                                </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() {
        document.getElementById('no_bj').focus();
        $.get("bj_list.php", function(data, status) {
            $("#tbbjlist").html(data);
        });
    });

    function scan(id) {
        $.ajax({
            url: "scan_bj.php",
            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: "cari_bj_gudang1.php",
                        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: "cari_bj_gudang2.php",
                                    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: "bj_diterima_gudang.php",
                                                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>');
                                                    $.get("bj_list.php", function(data, status) {
                                                        $("#tbbjlist").html(data);
                                                    });
                                                }
                                            });
                                        }
                                    }
                                });
                            }

                        }
                    });

                }

            }
        });
    }
</script>