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

    <div class="pagetitle">
        <h1>Buat LPBJ</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">
                                    <table class="table table-sm table-bordered">
                                        <thead>
                                            <tr>
                                                <th width="120px">No LPBJ</th>
                                                <td width="120px"><?= $nmrlpbj; ?></td>
                                                <th width="120px">Dibuat oleh</th>
                                                <td width="120px"><?= $this->session->strNama; ?></td>
                                                <th width="120px">Manager</th>
                                                <td width="120px"><?php
                                                                    if ($datauser['level_manager'] == 1) {
                                                                        echo $datauser['nama_spv'];
                                                                    }
                                                                    if ($datauser['level_manager'] == 2) {
                                                                        echo $datauser['nama_manager'];
                                                                    }
                                                                    ?>
                                                </td>
                                            </tr>
                                            <tr>
                                                <th width="120px">Tgl LPBJ</th>
                                                <td width="120px"><?= date("Y-m-d"); ?></td>
                                                <th width="120px">SPV</th>
                                                <td width="120px"><?= $datauser['nama_spv']; ?></td>
                                                <th width="120px">Direktur</th>
                                                <td width="120px"><?= $datauser['nama_direktur']; ?></td>
                                            </tr>
                                        </thead>
                                    </table>
                                </div>

                                <div class="col-md-12">

                                </div>
                                <div class="col-md-12">
                                    <button type="button" class="btn btn-primary btn-sm" onclick="tambahdetail()"><i class="bi bi-plus-circle"></i>&nbsp;Tambah Detail barang</button>
                                    <button type="button" onclick="get_detail()" class="btn btn-warning btn-sm"><i class="bi bi-arrow-counterclockwise"></i>&nbsp;Reload</button>
                                </div>
                            </div>
                            <br>
                            <div class="datatable-container">
                                <div id="tblpbj"></div>
                                <div class="form-check form-switch">
                                    <input class="form-check-input" type="checkbox" id="sts_urgent">
                                    <label class="form-check-label" for="sts_urgent">Urgent (Barang sudah dibeli)</label>
                                </div>

                                <button type="button" onclick="submit_lpbj()" class="btn btn-primary btn-sm">Submit</button>

                            </div>

                        </div>
                        <!-- End Table with stripped rows -->

                    </div>
                </div>

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

</main>

<div class="modal fade" id="f_add_brg" tabindex="-1" style="display: none;" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Tambah detail LPBJ</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form name="f_tambah_barang" action="" method="POST" enctype="multipart/form-data">
                <div class="modal-body">
                    <input type="hidden" id="kd_lpbj" name="kd_lpbj" value="<?= $nmrlpbj; ?>">
                    <div class="row mb-6">
                        <label for="inputEmail3" class="col-sm-3 col-form-label">Unit Usaha</label>
                        <div class="col-sm-9">
                            <select class="form-control form-control-sm" id="unitusaha" name="unitusaha">
                                <?php
                                foreach ($unit as $u) {
                                    echo "<option value='" . $u['unit'] . "'>" . $u['unit'] . "</option>";
                                }
                                ?>
                            </select>
                        </div>
                    </div>
                    <div class="row mb-6">
                        <label for="inputEmail3" class="col-sm-3 col-form-label">Kode Brg</label>
                        <div class="col-sm-9">
                            <input type="text" id="kd_brg" name="kd_brg" data-parsley-trigger="change" required="" placeholder="Cari deskripsi barang" autocomplete="off" class="form-control form-control-sm" list="list_deskripsi" onchange="get_satuan(this.value)" required>
                            <datalist id="list_deskripsi">
                                <?php
                                foreach ($brg as $row) {
                                    echo "<option value='" . $row['kd_brg'] . "'>   (" . $row['brg'] . ")</option>";
                                }
                                ?>
                            </datalist>
                        </div>
                    </div>
                    <div class="row mb-6">
                        <label for="inputEmail3" class="col-sm-3 col-form-label">Nama Brg</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control form-control-sm" id="nm_brg" readonly>
                        </div>
                    </div>
                    <div class="row mb-6">
                        <label for="inputEmail3" class="col-sm-3 col-form-label">Jumlah</label>
                        <div class="col-sm-4">
                            <input type="number" class="form-control form-control-sm" step="0.0001" id="jml_brg" name="jml_brg" required>
                        </div>
                    </div>
                    <div class="row mb-6">
                        <label for="inputEmail3" class="col-sm-3 col-form-label">Satuan</label>
                        <div class="col-sm-4">
                            <input type="text" id="datalist_unit" name="datalist_unit" data-parsley-trigger="change" required="" placeholder="Cari deskripsi barang" autocomplete="off" class="form-control form-control-sm" list="list_uom" required>
                            <datalist id="list_uom"></datalist>
                            <!--
                            <select class="form-control form-control-sm" id="datalist_unit" name="datalist_unit"></select>
                            -->
                        </div>
                    </div>
                    <div class="row mb-6">
                        <label for="inputEmail3" class="col-sm-3 col-form-label">Jumlah Stok</label>
                        <div class="col-sm-4">
                            <input type="number" class="form-control form-control-sm" step="0.0001" id="jml_stok" name="jml_stok" required>
                        </div>
                    </div>
                    <div class="row mb-6">
                        <label for="inputEmail3" class="col-sm-3 col-form-label">Dept</label>
                        <div class="col-sm-9">
                            <select class="form-control form-control-sm" id="dept_id" name="dept_id">
                                <?php
                                foreach ($dept as $d) {
                                    echo "<option value='" . $d['ID'] . "'>" . $d['strNmDept'] . "</option>";
                                }
                                ?>
                            </select>
                        </div>
                    </div>
                    <div class="row mb-6">
                        <label for="inputEmail3" class="col-sm-3 col-form-label">Tgl dibutuhkan</label>
                        <div class="col-sm-4">
                            <input type="date" min="<?php echo date('Y-m-d'); ?>" class="form-control form-control-sm" id="tgl_lpbj" name="tgl_lpbj" required>
                        </div>
                    </div>
                    <div class="row mb-6">
                        <label for="inputEmail3" class="col-sm-3 col-form-label">Gambar</label>
                        <div class="col-sm-9">
                            <input type="file" class="form-control form-control-sm" name="gambar_lpbj" id="gambar_lpbj" accept=".jpg,.jpeg,.png">
                        </div>
                    </div>
                    <div class="row mb-6">
                        <label for="inputEmail3" class="col-sm-3 col-form-label">Keterangan</label>
                        <div class="col-sm-9">
                            <textarea class="form-control form-control-sm" id="keterangan_lpbj" name="keterangan_lpbj"></textarea>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary btn-sm" data-bs-dismiss="modal">Close</button>
                    <button type="submit" class="btn btn-primary btn-sm">Simpan</button>
                </div>
            </form>
        </div>
    </div>
</div>


<div class="modal fade" id="f_view_brg" tabindex="-1" style="display: none;" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Gambar LPBJ</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form name="f_tambah_barang" action="" method="POST" enctype="multipart/form-data">
                <div class="modal-body">
                    <div class="row g-3">
                        <div class="col-md-12">
                            <div id="preview_gambar"></div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary btn-sm" data-bs-dismiss="modal">Close</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

<div class="modal fade" id="f_edit_brg" tabindex="-1" style="display: none;" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Update detail LPBJ</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form name="f_update_barang" action="" method="POST" enctype="multipart/form-data">
                <div class="modal-body">
                    <input type="hidden" id="ed_kd_lpbj" name="ed_kd_lpbj" value="<?= $nmrlpbj; ?>">
                    <div class="row mb-6">
                        <label for="inputEmail3" class="col-sm-3 col-form-label">Kode Brg</label>
                        <div class="col-sm-9">
                            <input type="text" id="ed_kd_brg" name="ed_kd_brg" data-parsley-trigger="change" required="" placeholder="Cari deskripsi barang" autocomplete="off" class="form-control form-control-sm" list="ed_list_deskripsi" onchange="get_satuan_ed(this.value)">
                            <datalist id="ed_list_deskripsi">
                                <?php
                                foreach ($brg as $row) {
                                    echo "<option value='" . $row['kd_brg'] . "'>   (" . $row['brg'] . ")</option>";
                                }
                                ?>
                            </datalist>
                        </div>
                    </div>
                    <div class="row mb-6">
                        <label for="inputEmail3" class="col-sm-3 col-form-label">Nama Brg</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control form-control-sm" id="ed_nm_brg" readonly>
                        </div>
                    </div>
                    <div class="row mb-6">
                        <label for="inputEmail3" class="col-sm-3 col-form-label">Jumlah</label>
                        <div class="col-sm-4">
                            <input type="number" step="0.0001" class="form-control form-control-sm" id="ed_jml_brg" name="ed_jml_brg">
                        </div>
                    </div>
                    <div class="row mb-6">
                        <label for="inputEmail3" class="col-sm-3 col-form-label">Satuan</label>
                        <div class="col-sm-4">
                            <input type="text" id="ed_datalist_unit" name="ed_datalist_unit" data-parsley-trigger="change" required="" placeholder="Cari deskripsi barang" autocomplete="off" class="form-control form-control-sm" list="ed_list_uom" required>
                            <datalist id="ed_list_uom"></datalist>
                            <!--
                            <select class="form-control form-control-sm" id="ed_datalist_unit" name="ed_datalist_unit"></select>
                            -->
                        </div>
                    </div>
                    <div class="row mb-6">
                        <label for="inputEmail3" class="col-sm-3 col-form-label">Jumlah Stok</label>
                        <div class="col-sm-4">
                            <input type="number" step="0.0001" class="form-control form-control-sm" id="ed_jml_stok" name="ed_jml_stok">
                        </div>
                    </div>
                    <div class="row mb-6">
                        <label for="inputEmail3" class="col-sm-3 col-form-label">Dept</label>
                        <div class="col-sm-9">
                            <select class="form-control form-control-sm" id="ed_dept_id" name="ed_dept_id">
                                <?php
                                foreach ($dept as $d) {
                                    echo "<option value='" . $d['ID'] . "'>" . $d['strNmDept'] . "</option>";
                                }
                                ?>
                            </select>
                        </div>
                    </div>
                    <div class="row mb-6">
                        <label for="inputEmail3" class="col-sm-3 col-form-label">Tgl dibutuhkan</label>
                        <div class="col-sm-4">
                            <input type="date" min="<?php echo date('Y-m-d'); ?>" class="form-control form-control-sm" id="ed_tgl_lpbj" name="ed_tgl_lpbj">
                        </div>
                    </div>
                    <div class="row mb-6">
                        <label for="inputEmail3" class="col-sm-3 col-form-label">Gambar</label>
                        <div class="col-sm-9">
                            <input type="file" class="form-control form-control-sm" name="ed_gambar_lpbj" id="ed_gambar_lpbj" accept=".jpg,.jpeg,.png">
                        </div>
                    </div>
                    <div class="row mb-6">
                        <label for="inputEmail3" class="col-sm-3 col-form-label">Keterangan</label>
                        <div class="col-sm-9">
                            <textarea class="form-control form-control-sm" id="ed_keterangan_lpbj" name="ed_keterangan_lpbj"></textarea>
                        </div>
                    </div>
                    <input type="hidden" id="nama_gambar" name="nama_gambar">
                    <input type="hidden" id="nama_brg" name="nama_brg">
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary btn-sm" data-bs-dismiss="modal">Close</button>
                    <button type="submit" class="btn btn-success btn-sm">Update</button>
                </div>
            </form>
        </div>
    </div>
</div>


<script>
    function tambahdetail() {
        var kd = "<?= $nmrlpbj; ?>";

        $.get("<?= base_url('Lpbj/cari_lpbj?id='); ?>" + kd, function(data, status) {
            if (data == 1) {
                Swal.fire("LPBJ " + kd + " dalam proses persetujuan");
            }

            if (data == 0) {
                $("#f_add_brg").modal("show");
            }
        });
    }

    function get_satuan(id) {
        $.get("<?= base_url('Lpbj/satuan?brg='); ?>" + id, function(data, status) {
            $("#list_uom").html(data);
        });

        $.get("<?= base_url('Lpbj/get_brg?kd_brg='); ?>" + id, function(data, status) {
            $("#nm_brg").val(data);
        });
    }

    function get_satuan_ed(id) {
        $.get("<?= base_url('Lpbj/satuan?brg='); ?>" + id, function(data, status) {
            $("#ed_list_uom").html(data);
        });

        $.get("<?= base_url('Lpbj/get_brg?kd_brg='); ?>" + id, function(data, status) {
            $("#ed_nm_brg").val(data);
        });
    }

    function get_detail() {
        var id = "<?= $nmrlpbj; ?>";
        $.get("<?= base_url('Lpbj/lpbj_list?kd='); ?>" + id, function(data, status) {
            $("#tblpbj").html(data);
        });
    }

    $(document).ready(function() {

        $('#add_brg').select2();
        $('#akun_bantu').select2();

    });

    function lihat(txt) {
        $("#preview_gambar").html("<img class='form-control' width='500' height='600' src='<?= base_url('assets/img/') ?>" + txt + "'>");
    }

    function edit_detail(brg, nama, jml, uom, stok, dept, tgl, keterangan, gambar) {
        $("#ed_kd_brg").val(brg);
        $("#ed_nm_brg").val(nama);
        $("#ed_jml_brg").val(jml);
        $("#ed_jml_stok").val(stok);
        $("#ed_dept_id").val(dept);
        $("#ed_tgl_lpbj").val(tgl);
        $("#ed_keterangan_lpbj").val(keterangan);
        $("#nama_gambar").val(gambar);
        $("#nama_brg").val(brg);

        var $select = $("#ed_datalist_unit");
        var options = [{
            value: uom,
            text: uom
        }];

        $select.empty();

        options.forEach(function(option) {
            $select.append($('<option>', {
                value: option.value,
                text: option.text
            }));
        });

        $select.val(uom);
    }

    $(document).ready(function() {
        addOptionsAndSelect('unit2');
    });
</script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
    $(document).ready(function() {
        var id = "<?= $nmrlpbj; ?>";
        $.get("<?= base_url('Lpbj/lpbj_list?kd='); ?>" + id, function(data, status) {
            $("#tblpbj").html(data);
        });

        $('form[name="f_tambah_barang"]').on('submit', function(event) {
            event.preventDefault();
            var formData = new FormData(this);
            $.ajax({
                url: '<?= base_url('Lpbj/simpan_detail_barang'); ?>',
                type: 'POST',
                data: formData,
                processData: false,
                contentType: false,
                success: function(response) {
                    //alert('Data berhasil disimpan!');
                    $('#f_add_brg').modal('hide');
                    get_detail();
                    clear_up();
                    //log
                    var kd_brg = $("#kd_brg").val();
                    var strTable = 'lpbj_d';
                    var keterangan = 'Telah menambahkan detail lpbj <?= $nmrlpbj; ?> dengan kode barang ' + kd_brg;
                    $.ajax({
                        url: "<?= base_url('Pengaturan/log'); ?>",
                        type: 'POST',
                        cache: false,
                        data: {
                            strTable: strTable,
                            keterangan: keterangan
                        },
                        success: function() {}
                    });
                },
                error: function(xhr, status, error) {
                    alert('Terjadi kesalahan: ' + error);
                }
            });

        });
    });

    function hapus_item(kd, brg, gambar) {
        Swal.fire({
            title: "Hapus Data ini? " + gambar,
            //text: "You won't be able to revert this!",
            icon: "warning",
            showCancelButton: true,
            confirmButtonColor: "#3085d6",
            cancelButtonColor: "#d33",
            confirmButtonText: "Yes, delete it!"
        }).then((result) => {
            if (result.isConfirmed) {
                //Begin
                $.ajax({
                    url: "<?= base_url('Lpbj/hapus_item'); ?>",
                    type: 'POST',
                    cache: false,
                    data: {
                        kd: kd,
                        brg: brg,
                        gambar: gambar
                    },
                    success: function() {
                        get_detail();
                        //log                        
                        var strTable = 'lpbj_d';
                        var keterangan = 'Telah menghapus detail lpbj <?= $nmrlpbj; ?> dengan kode barang ' + kd;
                        $.ajax({
                            url: "<?= base_url('Pengaturan/log'); ?>",
                            type: 'POST',
                            cache: false,
                            data: {
                                strTable: strTable,
                                keterangan: keterangan
                            },
                            success: function() {}
                        });
                    }
                });
                //end
            }
        });
    }

    function submit_lpbj() {
        var kd = "<?= $nmrlpbj; ?>";
        if ($('#sts_urgent').is(":checked")) {
            var sts = 1;
        } else {
            var sts = 0;
        }

        $.get("<?= base_url('Lpbj/hitung_lpbj?id='); ?>" + kd, function(data, status) {
            if (data == 0) {
                Swal.fire("Tidak ada Item pada LPBJ ini");
            }

            if (data > 0) {
                $.get("<?= base_url('Lpbj/cari_lpbj?id='); ?>" + kd, function(data, status) {
                    if (data == 1) {
                        Swal.fire("LPBJ " + kd + " dalam proses persetujuan");
                    }

                    if (data == 0) {

                        Swal.fire({
                            title: "Ajukan Persetujuan untuk LPBJ " + kd,
                            text: "Mohon pastikan barang dan jumlah sudah sesuai",
                            icon: "question",
                            showCancelButton: true,
                            confirmButtonColor: "#3085d6",
                            cancelButtonColor: "#d33",
                            confirmButtonText: "Ya, ajukan"
                        }).then((result) => {
                            if (result.isConfirmed) {
                                let timerInterval;
                                Swal.fire({
                                    title: "LPBJ Sedang diproses",
                                    //html: "",
                                    timer: 0,
                                    timerProgressBar: false,
                                    didOpen: () => {
                                        Swal.showLoading();
                                        const timer = Swal.getPopup().querySelector("b");
                                        timerInterval = setInterval(() => {
                                            timer.textContent = `${Swal.getTimerLeft()}`;
                                        }, 100);
                                    },
                                    willClose: () => {
                                        clearInterval(timerInterval);
                                    }
                                }).then((result) => {
                                    //Begin
                                    //End
                                });
                                $.ajax({
                                    url: "<?= base_url('Lpbj/ajukan_approval'); ?>",
                                    type: 'POST',
                                    cache: false,
                                    data: {
                                        kd: kd,
                                        sts: sts
                                    },
                                    success: function() {
                                        //Swal.fire("LPBJ telah diajukan");
                                        //get_detail();
                                        window.open("<?= base_url('Lpbj'); ?>", "_self");
                                    }
                                });

                            }
                        });

                    }
                });
            }
        });
    }

    $(document).ready(function() {
        $('form[name="f_update_barang"]').on('submit', function(event) {
            event.preventDefault();
            var formData = new FormData(this);
            $.ajax({
                url: '<?= base_url('Lpbj/update_detail_barang'); ?>',
                type: 'POST',
                data: formData,
                processData: false,
                contentType: false,
                success: function(response) {
                    //alert('Data berhasil disimpan!');
                    $('#f_edit_brg').modal('hide');
                    get_detail();
                    //log
                    var kd_brg = $("#ed_kd_brg").val();
                    var strTable = 'lpbj_d';
                    var keterangan = 'Telah melakukan update detail lpbj <?= $nmrlpbj; ?> dengan kode barang ' + kd_brg;
                    $.ajax({
                        url: "<?= base_url('Pengaturan/log'); ?>",
                        type: 'POST',
                        cache: false,
                        data: {
                            strTable: strTable,
                            keterangan: keterangan
                        },
                        success: function() {}
                    });
                },
                error: function(xhr, status, error) {
                    alert('Terjadi kesalahan: ' + error);
                }
            });
        });
    });

    function clear_up() {
        document.getElementById('kd_brg').value = '';
        document.getElementById('nm_brg').value = '';
        document.getElementById('jml_brg').value = '';
        document.getElementById('datalist_unit').value = '';
        document.getElementById('jml_stok').value = '';
        document.getElementById('tgl_lpbj').value = '';
        document.getElementById('gambar_lpbj').value = '';
        document.getElementById('keterangan_lpbj').value = '';
    }
</script>