<?php
$MySQLi = new MySQLi($strDBHostName, $strDBUsername, $strDBPassword, $strDBName);
if (!isset($_GET['lpbj'])) {
    $_SESSION['tambah-lpbj'] = "TRUE";
    echo '<script type="text/javascript">';
    echo 'location.href = \'transaksi/persediaan/pemesanan/simpan.tambah.pesan.barang2.php\';';
    //echo 'location.href = \'transaksi/persediaan/pemesanan/tambah.pesan.barang2.php\';';
    echo '</script>';
}
if (isset($_GET['lpbj'])) {
    $query = "SELECT * 
                            FROM tpc_h_lpbj 
                            WHERE strKdLPBJ='" . $_GET['lpbj'] . "' AND 
                                  stsAktif=2
                            ";
    $result = $MySQLi->query($query);
    if ($result->num_rows == 0)
        echo "ERROR : DATA NOT FOUND";
    else $row = $result->fetch_array();
    $_SESSION['strEmailREQ'] = $row['employee_Email'];
    $_SESSION['strKdLPBJ'] = $row['strKdLPBJ'];
?>

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

        <div class="pagetitle">
            <h1>Tambah LPBJ</h1>

        </div>
        <section class="section">
            <div class="row">

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

                    <div class="card">
                        <div class="card-body">
                            <br>
                            <form name="f_tambah_barang" id="f_tambah_barang" class="row g-3" action="transaksi/persediaan/pemesanan/simpan.tambah.pesan.barang2.php" method="post" enctype="multipart/form-data" onsubmit="changeValue()">
                                <div class="col-md-12">
                                    <label for="inputNanme4" class="form-label">No LPBJ</label>
                                    <input id="inputLPBJ" type="text" name="strKdLPBJ" class="form-control" placeholder="No LPBJ" value="<?php echo $row['strKdLPBJ']; ?>" readonly>
                                </div>
                                <div class="col-md-4">
                                    <label for="inputNanme4" class="form-label">Tgl LPBJ</label>
                                    <input id="inputTanggal" type="text" name="dtCreate" class="form-control" placeholder="Tgl LPBJ" value="<?php echo date("d-m-Y"); ?>" readonly>
                                </div>
                                <div class="col-md-4">
                                    <label for="inputNanme4" class="form-label">Nama User</label>
                                    <input id="inputEmail" type="hidden" name="Email" class="form-control" placeholder="Email" value="<?php echo $row['usrCreate']; ?>" readonly>
                                    <input id="namauser" type="text" name="namauser" class="form-control" placeholder="Email" value="<?= $_SESSION["strNama"]; ?>" readonly>
                                </div>
                                <div class="col-md-4">
                                    <label for="inputNanme4" class="form-label">Divisi</label>
                                    <input id="inputDivisi" type="text" name="Divisi" class="form-control" placeholder="Divisi" value="<?php echo $row['strNmDivisiRequestor']; ?>" readonly>
                                </div>
                                <div class="col-md-4">
                                    <label for="inputNanme4" class="form-label">Nama Direktur</label>
                                    <input id="inputEmailDIR" type="hidden" name="strMailDIR" class="form-control" placeholder="Email Direktur" value="<?php echo $row['strMailDIR']; ?>" readonly>
                                    <input id="namadir" type="text" name="namadir" class="form-control" placeholder="Email" value="<?= $row['strFullNameDIR']; ?>" readonly>
                                </div>
                                <div class="col-md-4">
                                    <label for="inputNanme4" class="form-label">Nama Manager</label>
                                    <input id="inputEmailMGR" type="hidden" name="strMailMGR" class="form-control" placeholder="Email manager" value="<?php echo $row['strMailMGR']; ?>" readonly>
                                    <input id="namamgr" type="text" name="namamrg" class="form-control" placeholder="Email" value="<?= $row['strFullNameMGR']; ?>" readonly>
                                </div>
                                <div class="col-md-4">
                                    <label for="inputNanme4" class="form-label">Nama SPV</label>
                                    <input id="inputEmailSPV" type="hidden" name="strMailSPV" class="form-control" placeholder="Email SPV" value="<?php echo $row['strMailSPV']; ?>" readonly>
                                    <input id="namaspv" type="text" name="namaspv" class="form-control" placeholder="Email" value="<?= $row['strFullNameSPV']; ?>" readonly>
                                </div>

                                <input type="hidden" name="h_lpbj_ID" id="h_lpbj_ID" value="<?php echo $row['ID']; ?>">
                                <input type="hidden" name="iLevelREQ" id="iLevelREQ" value="<?php echo $row['iLevelREQ']; ?>">

                                <!-- form input detail lpbj -->
                                <div class="modal fade" id="Modaldetaillpbj" 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">Tambah Detail Barang LPBJ</h5>
                                                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                            </div>

                                            <div class="modal-body">

                                                <div class="row mb-6">
                                                    <label for="inputEmail3" class="col-sm-2 col-form-label">Deskripsi</label>
                                                    <div class="col-sm-10">
                                                        <input type="text" id="list_item" name="strDeskripsi" data-parsley-trigger="change" required="" placeholder="Masukan deskripsi barang" autocomplete="off" class="form-control form-control-sm" list="list_deskripsi">
                                                        <datalist id="list_deskripsi">
                                                            <?php
                                                            $query = "SELECT ID, strDeskripsi FROM item;";
                                                            $result = $MySQLi->query($query);
                                                            while ($row = $result->fetch_array())
                                                                echo "<option value='" . $row['strDeskripsi'] . "'  data-realvalue='" . $row['ID'] . "_" . $row['strDeskripsi'] . "'>";
                                                            ?>
                                                        </datalist>
                                                    </div>
                                                </div>

                                                <div class="row mb-6">
                                                    <label for="inputEmail3" class="col-sm-2 col-form-label">Jumlah</label>
                                                    <div class="col-sm-10">
                                                        <input type="number" name="iJumlah" id="iJumlah" data-parsley-trigger="change" required placeholder="masukan jumlah barang yang akan dipesan" autocomplete="off" class="form-control form-control-sm">
                                                    </div>
                                                </div>

                                                <div class="row mb-6">
                                                    <label for="inputEmail3" class="col-sm-2 col-form-label">Satuan</label>
                                                    <div class="col-sm-10">
                                                        <input id="inputUnit" type="text" name="strUnit" data-parsley-trigger="change" class="form-control form-control-sm" list="datalist_unit" autocomplete='off'>
                                                        <datalist id="datalist_unit"></datalist>
                                                    </div>
                                                </div>

                                                <div class="row mb-6">
                                                    <label for="inputEmail3" class="col-sm-2 col-form-label">Jumlah Stok</label>
                                                    <div class="col-sm-10">
                                                        <input type="text" name="iJumlahStok" id="iJumlahStok" data-parsley-trigger="change" required placeholder="masukan jumlah stok saat ini" autocomplete="off" class="form-control form-control-sm">
                                                    </div>
                                                </div>

                                                <div class="row mb-6">
                                                    <label for="inputEmail3" class="col-sm-2 col-form-label">Departemen</label>
                                                    <div class="col-sm-10">
                                                        <select class="form-control form-control-sm" id="inputDepartement" name="dept_ID">
                                                            <?php

                                                            $query = "SELECT ID, strNmDept 
                                                                FROM tpc_dept
                                                                WHERE stsAktif=1 AND 
                                                                strKdLokasi='" . $_SESSION['strKdLokasi'] . "'";

                                                            /*
                                                            $query = "SELECT A.ID, A.strNmDept,A.strKdLokasi 
                                                                FROM tpc_dept A
                                                                Left join tb_company B on B.label_company = A.strKdLokasi
                                                                Left join tb_akses_company C on C.id_company = B.id_company
                                                                WHERE A.stsAktif = 1 and C.id_user = " . $_SESSION['id_user'];
                                                                */
                                                            $result = $MySQLi->query($query);
                                                            while ($row = $result->fetch_array()) {
                                                                echo "<option value='" . $row['ID'] . "'>" . $row['strNmDept'] . "</option>";
                                                            }
                                                            ?>
                                                        </select>
                                                    </div>
                                                </div>

                                                <div class="row mb-6">
                                                    <label for="inputEmail3" class="col-sm-2 col-form-label">Tgl Dibutuhkan</label>
                                                    <div class="col-sm-10">
                                                        <input class="form-control form-control-sm" type="date" min="<?php echo date('Y-m-d'); ?>" name="dtDibutuhkan" id="dtDibutuhkan" value="<?php echo date('Y-m-d'); ?>" required>
                                                    </div>
                                                </div>

                                                <div class="row mb-6">
                                                    <label for="inputEmail3" class="col-sm-2 col-form-label">Gambar</label>
                                                    <div class="col-sm-10">
                                                        <input type="file" name="strNmFile" id="strNmFile" accept=".jpg,.jpeg,.png,.bmp" data-parsley-trigger="change" autocomplete="off" class="form-control form-control-sm">
                                                    </div>
                                                </div>

                                                <div class="row mb-6">
                                                    <label for="inputEmail3" class="col-sm-2 col-form-label">Keterangan</label>
                                                    <div class="col-sm-10">
                                                        <textarea name="strKeterangan" id="strKeterangan" class="form-control form-control-sm"></textarea>
                                                    </div>
                                                </div>
                                            </div>

                                            <div class="modal-footer">
                                                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
                                                <button type="submit" class="btn btn-primary" name="+" id="+"><i class="bi bi-save"></i>&nbsp;Simpan</button>
                                            </div>

                                        </div>
                                    </div>
                                </div>
                                <!-- end form input detail lpbj -->
                            </form>
                        </div>
                    </div>

                    <div class="card">
                        <div class="card-header">
                            <button type="button" class="btn btn-outline-primary" data-bs-toggle="modal" data-bs-target="#Modaldetaillpbj">
                                <i class="bi bi-plus-lg"></i>&nbsp;Tambah detail barang
                            </button>
                            <button type="button" class="btn btn-outline-success" onclick="tampildata()">
                                <i class="bi bi-arrow-repeat"></i>&nbsp;reload
                            </button>
                        </div>

                        <div class="card-body">
                            <div class="table-responsive">
                                <div id="tbdata"></div>
                            </div>
                            <!--
                            <div id="customModal" class="custom-modal">
                                <div class="custom-modal-content">
                                    <span class="close-btn">&times;</span>
                                    <img id="modalImage" src="" alt="Gambar Ukuran Penuh">
                                </div>
                            </div>
                                                        -->
                        </div>
                        <div class="card-footer">
                            <input class="form-check-input" type="checkbox" id="stsUrgent" name="stsUrgent" value="0">
                            <label for="chkurgent" style="color:red;">
                                Urgent (barang sudah dibeli)
                            </label>

                            <button type="button" class="btn btn-outline-primary" onclick="submitbrg()" style="float:right;">
                                <i class="bi bi-telegram"></i>&nbsp;Submit
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        </section>

    </main>

    <!-- form update lpbj -->
    <div class="modal fade" id="f_update" tabindex="-1">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <form name="f_edit_barang" id="f_edit_barang" class="row g-3" action="transaksi/persediaan/pemesanan/edit_barang2.php" method="post" enctype="multipart/form-data" onsubmit="changeValue()">
                    <div class="modal-header">
                        <h5 class="modal-title">Update LPBJ</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <div class="modal-body">
                        <!-- start -->
                        <div class="row mb-6">
                            <label for="inputEmail3" class="col-sm-2 col-form-label">Deskripsi</label>
                            <div class="col-sm-10">
                                <input type="hidden" id="rowid" name="ID">
                                <input type="text" id="edit_list_item" name="strDeskripsi" data-parsley-trigger="change" required="" placeholder="Masukan deskripsi barang" autocomplete="off" class="form-control form-control-sm" list="list_deskripsi">
                                <datalist id="list_deskripsi">
                                    <?php
                                    $query = "SELECT ID, strKdItem,strDeskripsi FROM item;";
                                    $result = $MySQLi->query($query);
                                    while ($row = $result->fetch_array())
                                        echo "<option value='" . $row['strDeskripsi'] . "'  data-realvalue='" . $row['ID'] . "_" . $row['strDeskripsi'] . "'>";
                                    ?>
                                </datalist>
                            </div>
                        </div>

                        <div class="row mb-6">
                            <label for="inputEmail3" class="col-sm-2 col-form-label">Jumlah</label>
                            <div class="col-sm-10">
                                <input type="number" name="iJumlah" id="ed_iJumlah" data-parsley-trigger="change" required placeholder="masukan jumlah barang yang akan dipesan" autocomplete="off" class="form-control form-control-sm">
                            </div>
                        </div>

                        <div class="row mb-6">
                            <label for="inputEmail3" class="col-sm-2 col-form-label">Satuan</label>
                            <div class="col-sm-10">
                                <input id="edUnit" type="text" name="strUnit" data-parsley-trigger="change" class="form-control form-control-sm" list="datalist_unit" autocomplete='off'>
                                <datalist id="datalist_unit"></datalist>
                            </div>
                        </div>

                        <div class="row mb-6">
                            <label for="inputEmail3" class="col-sm-2 col-form-label">Jumlah Stok</label>
                            <div class="col-sm-10">
                                <input type="text" name="iJumlahStok" id="ed_iJumlahStok" data-parsley-trigger="change" required placeholder="masukan jumlah stok saat ini" autocomplete="off" class="form-control form-control-sm">
                            </div>
                        </div>

                        <div class="row mb-6">
                            <label for="inputEmail3" class="col-sm-2 col-form-label">Departemen</label>
                            <div class="col-sm-10">
                                <select class="form-control form-control-sm" id="dept_ID" name="dept_ID">
                                    <?php
                                    $query = "SELECT ID, strNmDept 
                                                                FROM tpc_dept
                                                                WHERE stsAktif=1 AND 
                                                                strKdLokasi='" . $_SESSION['strKdLokasi'] . "'";
                                    $result = $MySQLi->query($query);
                                    while ($row = $result->fetch_array()) {
                                        echo "<option value='" . $row['ID'] . "'>" . $row['strNmDept'] . "</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>

                        <div class="row mb-6">
                            <label for="inputEmail3" class="col-sm-2 col-form-label">Tgl Dibutuhkan</label>
                            <div class="col-sm-10">
                                <input class="form-control form-control-sm" type="date" min="<?php echo date('Y-m-d'); ?>" name="dtDibutuhkan" id="eddtDibutuhkan" value="<?php echo date('Y-m-d'); ?>" required>
                            </div>
                        </div>

                        <div class="row mb-6">
                            <label for="inputEmail3" class="col-sm-2 col-form-label">Gambar</label>
                            <div class="col-sm-10">
                                <input type="file" name="strNmFile" id="strNmFile" accept=".jpg,.jpeg,.png,.bmp" data-parsley-trigger="change" autocomplete="off" class="form-control form-control-sm">
                            </div>
                        </div>

                        <div class="row mb-6">
                            <label for="inputEmail3" class="col-sm-2 col-form-label">Keterangan</label>
                            <div class="col-sm-10">
                                <textarea name="strKeterangan" id="edKeterangan" class="form-control form-control-sm"></textarea>
                            </div>
                        </div>
                        <!--end start -->
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                        <button type="submit" class="btn btn-primary">Save changes</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <!-- end form update lpbj -->
<?php
}
?>


<script src="assets/libs/js/jquery-3.6.0.min.js"></script>
<script>
    function changeValue() {
        var input = document.getElementById("list_item");
        var selectedValue = input.value;
        var realValue = document.querySelector('option[value="' + selectedValue + '"]').getAttribute("data-realvalue");
        input.value = realValue;
    }

    $(document).ready(function() {
        $('#list_item').on('input', function() {
            var strDeskripsi = $(this).val();
            $.ajax({
                url: 'transaksi/persediaan/pemesanan/get_unit.php',
                type: 'POST',
                data: {
                    strDeskripsi: strDeskripsi
                },
                dataType: 'json',
                success: function(response) {
                    $('#datalist_unit').empty(); // Kosongkan datalist sebelum mengisinya kembali
                    $.each(response, function(index, unit) {
                        $('#datalist_unit').append('<option value="' + unit + '">');
                    });
                },
                error: function(xhr, status, error) {
                    console.error(xhr.responseText);
                }
            });
        });
    });

    $(document).ready(function() {
        var modal = $('#customModal');
        var modalImage = $('#modalImage');

        $('.thumbnail').on('click', function() {
            var imageUrl = $(this).data('image');
            modalImage.attr('src', imageUrl);
            modal.show();
        });

        $('.close-btn').on('click', function() {
            modal.hide();
        });

        $(window).on('click', function(event) {
            if ($(event.target).is(modal)) {
                modal.hide();
            }
        });

        var lpbj = "<?= $_GET['lpbj']; ?>";
        $.get("transaksi/persediaan/pemesanan/tb_tambah_brg.php?lpbj=" + lpbj, function(data, status) {
            $("#tbdata").html(data);
        });
    });

    function apr(txt) {
        const Toast = Swal.mixin({
            toast: true,
            position: "top-end",
            showConfirmButton: false,
            timer: 3000,
            timerProgressBar: true,
            didOpen: (toast) => {
                toast.onmouseenter = Swal.stopTimer;
                toast.onmouseleave = Swal.resumeTimer;
            }
        });
        Toast.fire({
            icon: "error",
            title: txt
        });
    }

    function msgsimpan(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: "success",
            title: txt
        });
    }

    function msgerror(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 tampildata() {
        var lpbj = "<?= $_GET['lpbj']; ?>";
        $.get("transaksi/persediaan/pemesanan/tb_tambah_brg.php?lpbj=" + lpbj, function(data, status) {
            $("#tbdata").html(data);
            msgsimpan("Refresh");
        });
    }

    function tampildata2() {
        var lpbj = "<?= $_GET['lpbj']; ?>";
        $.get("transaksi/persediaan/pemesanan/tb_tambah_brg.php?lpbj=" + lpbj, function(data, status) {
            $("#tbdata").html(data);
        });
    }

    $("#f_tambah_barang").on("submit", function(e) {
        e.preventDefault();
        $.ajax({
            url: "transaksi/persediaan/pemesanan/simpan_barang2.php",
            type: "POST",
            data: new FormData(this),
            contentType: false,
            cache: false,
            processData: false,
            success: function(data) {
                if (data == 70) {
                    msgerror('Barang tidak ditemukan, Mohon Hub Bu Nancy');
                }
                if (data == 40) {
                    msgerror('Permintaan Barang gagal disimpan');
                }
                if (data == 35) {
                    $("#Modaldetaillpbj").modal("hide");
                    msgsimpan("Disimpan");
                    tampildata2();
                    clearall();
                }

            }
        });
    });

    function hapusbrg(rowid) {
        Swal.fire({
            title: "Hapus data ini ? ",
            //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) {
                $.ajax({
                    url: "transaksi/persediaan/pemesanan/hapus_barang2.php",
                    type: 'POST',
                    cache: false,
                    data: {
                        rowid: rowid
                    },
                    success: function() {
                        // begin                         
                        Swal.fire({
                            title: "Deleted!",
                            text: "Your file has been deleted.",
                            icon: "success"
                        });
                        tampildata2();
                        //end
                    }
                });
            }
        });
    }

    function clearall() {
        document.getElementById('list_item').value = '';
        document.getElementById('iJumlah').value = '';
        document.getElementById('inputUnit').value = '';
        document.getElementById('iJumlahStok').value = '';
        document.getElementById('strKeterangan').value = '';
    }

    function editbrg(rowid, desc, qty, uom, stok, dept, tgl, ket) {
        $("#rowid").val(rowid);
        $("#edit_list_item").val(desc);
        $("#ed_iJumlah").val(qty);
        $("#edUnit").val(uom);
        $("#ed_iJumlahStok").val(stok);
        $("#edDepartement").val(dept);
        $("#eddtDibutuhkan").val(tgl);
        $("#edKeterangan").val(ket);
    }

    $("#f_edit_barang").on("submit", function(e) {
        e.preventDefault();
        $.ajax({
            url: "transaksi/persediaan/pemesanan/edit_barang2.php",
            type: "POST",
            data: new FormData(this),
            contentType: false,
            cache: false,
            processData: false,
            success: function(data) {
                $("#f_update").modal("hide");
                msgsimpan("Disimpan");
                tampildata();
            }
        });
    });

    function submitbrg() {
        var lpbj = "<?= $_GET['lpbj']; ?>";
        if ($('#stsUrgent').is(':checked')) {
            var stsUrgent = 1;
        } else {
            var stsUrgent = 0;
        }
        $.get("transaksi/persediaan/pemesanan/cekbrg2.php?lpbj=" + lpbj, function(data, status) {
            if (data > 0) {
                $.ajax({
                    url: "transaksi/persediaan/pemesanan/submitbrg.php",
                    type: 'POST',
                    cache: false,
                    data: {
                        lpbj: lpbj,
                        stsUrgent: stsUrgent
                    },
                    success: function() {
                        // begin                         
                        Swal.fire({
                            title: "Tersimpan",
                            icon: "success"
                        }).then((result) => {
                            if (result.isConfirmed) {
                                window.open("index2.php", "_self");
                            }
                        });
                        tampildata2();
                        //end
                    }
                });
            } else {
                Swal.fire({
                    title: "Tidak ada data",
                    //text: "Your file has been deleted.",
                    icon: "error"
                });
            }
        });
    }
</script>