<style>
    /* Modal styling */
    .custom-modal {
        display: none;
        position: fixed;
        z-index: 1000;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
        overflow: auto;
        background-color: rgb(0, 0, 0);
        background-color: rgba(0, 0, 0, 0.4);
    }

    .custom-modal-content {
        margin: 15% auto;
        padding: 20px;
        border: 1px solid #888;
        width: 80%;
        background-color: #fff;
        text-align: center;
    }

    .custom-modal-content img {
        max-width: 100%;
        height: auto;
    }

    .close-btn {
        color: #aaa;
        float: right;
        font-size: 28px;
        font-weight: bold;
    }

    .close-btn:hover,
    .close-btn:focus {
        color: black;
        text-decoration: none;
        cursor: pointer;
    }
</style>

<!-- ============================================================== -->
<!-- wrapper  -->
<!-- ============================================================== -->
<div class="dashboard-wrapper">
    <div class="container-fluid  dashboard-content">
        <!-- ============================================================== -->
        <!-- pageheader -->
        <!-- ============================================================== -->
        <div class="row">
            <div class="col-xl-12 col-lg-12 col-md-12 col-sm-12 col-12">
                <div class="page-header">
                    <h2 class="pageheader-title">Tambah LPBJ</h2>
                    <div class="page-breadcrumb">
                        <nav aria-label="breadcrumb">
                            <ol class="breadcrumb">
                                <li class="breadcrumb-item"><a href="index.php" class="breadcrumb-link"><?php echo namahari_ID(date('Y-m-d')) . ", " . date('d-m-Y'); ?>, Dashboard</a></li>
                                <li class="breadcrumb-item">Pembelian</a></li>
                                <li class="breadcrumb-item"><a href="?menu=daftar-lpbj" class="breadcrumb-link">LPBJ</a></li>
                                <li class="breadcrumb-item active" aria-current="page"><a href="?menu=tambah-lpbj" class="breadcrumb-link">Tambah LPBJ</a></li>
                            </ol>
                        </nav>
                    </div>
                </div>
            </div>
        </div>
        <!-- ============================================================== -->
        <!-- end pageheader -->
        <!-- ============================================================== -->

        <?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.barang.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'];
        ?>
            <div class="row">
                <form id="basicform" data-parsley-validate="" action="transaksi/persediaan/pemesanan/simpan.tambah.pesan.barang.php" method="post" enctype="multipart/form-data" onsubmit="changeValue()">
                    <!-- ============================================================== -->
                    <!-- basic form -->
                    <!-- ============================================================== -->
                    <div class="col-xl-6 col-lg-6 col-md-12 col-sm-12 col-12">
                        <div class="card">
                            <h5 class="card-header">Entry LPBJ</h5>
                            <div class="card-body">
                                <div class="form-group">
                                    <label for="inputLPBJ">No LPBJ : </label>
                                    <input id="inputLPBJ" type="text" name="strKdLPBJ" data-parsley-trigger="change" readonly class="form-control" value="<?php echo $row['strKdLPBJ']; ?>">
                                </div>
                                <div class="form-group">
                                    <label for="inputTanggal">Tanggal</label>
                                    <input id="inputTanggal" type="text" name="dtCreate" readonly data-parsley-trigger="change" class="form-control" value="<?php echo date("d-m-Y"); ?>">
                                </div>
                                <div class="form-group">
                                    <label for="inputEmail">Email</label>
                                    <input id="inputEmail" type="text" name="Email" readonly data-parsley-trigger="change" class="form-control" value="<?php echo $row['usrCreate']; ?>">
                                </div>
                                <div class="form-group">
                                    <label for="inputDivisi">Divisi</label>
                                    <input id="inputDivisi" type="text" name="Divisi" readonly data-parsley-trigger="change" class="form-control" value="<?php echo $row['strNmDivisiRequestor']; ?>">
                                </div>
                                <div class="form-group">
                                    <label for="inputEmailDIR">Email DIR</label>
                                    <input id="inputEmailDIR" type="text" name="strMailDIR" readonly data-parsley-trigger="change" class="form-control" value="<?php echo $row['strMailDIR']; ?>">
                                </div>
                                <div class="form-group">
                                    <label for="inputEmailMGR">Email MGR</label>
                                    <input id="inputEmailMGR" type="text" name="strMailMGR" readonly data-parsley-trigger="change" class="form-control" value="<?php echo $row['strMailMGR']; ?>">
                                </div>
                                <div class="form-group">
                                    <label for="inputEmailSPV">Email SPV</label>
                                    <input id="inputEmailSPV" type="text" name="strMailSPV" readonly data-parsley-trigger="change" class="form-control" value="<?php echo $row['strMailSPV']; ?>">
                                </div>
                            </div>
                        </div>
                    </div>
                    <!-- ============================================================== -->
                    <!-- end basic form -->
                    <!-- ============================================================== -->

                    <style>
                        .wide-table {
                            width: 2000px;
                        }
                    </style>
                    <!-- ============================================================== -->
                    <!-- data table  -->
                    <!-- ============================================================== -->
                    <div>
                        <div class="card">
                            <div class="card-header">
                                <h5 class="mb-0">Data List LPBJ</h5>
                            </div>
                            <input type="hidden" name="h_lpbj_ID" value="<?php echo $row['ID']; ?>">
                            <input type="hidden" name="iLevelREQ" value="<?php echo $row['iLevelREQ']; ?>">
                            <div class="card-body">
                                <div class="table-responsive">
                                    <table id="example" class="table table-striped table-bordered wide-table">
                                        <thead>
                                            <tr>
                                                <th width="100px">Kode</th>
                                                <th width="450px">Deskripsi</th>
                                                <th width="50px">Jumlah</th>
                                                <th width="100px">Satuan</th>
                                                <th width="50px">Jumlah Stok</th>
                                                <th width="650px">Keterangan</th>
                                                <th width="150px">Departement</th>
                                                <th width="100px">Tanggal dibutuhkan</th>
                                                <th width="250px">Gambar</th>
                                                <th width="100px">Option</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <tr>
                                                <td>&nbsp;</td>
                                                <td>
                                                    <input type="text" id="list_item" name="strDeskripsi" data-parsley-trigger="change" required="" placeholder="Masukan deskripsi barang" autocomplete="off" class="form-control" 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>
                                                </td>
                                                <td>
                                                    <input type="text" name="iJumlah" data-parsley-trigger="change" required placeholder="masukan jumlah barang yang akan dipesan" autocomplete="off" class="form-control">
                                                </td>
                                                <td>
                                                    <input id="inputUnit" type="text" name="strUnit" data-parsley-trigger="change" class="form-control" list="datalist_unit" autocomplete='off'>
                                                    <datalist id="datalist_unit"></datalist>
                                                </td>
                                                <td>
                                                    <input type="text" name="iJumlahStok" data-parsley-trigger="change" required placeholder="masukan jumlah stok saat ini" autocomplete="off" class="form-control">
                                                </td>
                                                <td>
                                                    <input type="text" name="strKeterangan" data-parsley-trigger="change" placeholder="Keterangan jika ada" autocomplete="off" class="form-control">
                                                </td>
                                                <td>
                                                    <select class="form-control" id="inputDepartement" 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>
                                                </td>
                                                <Td>
                                                    <input type="date" min="<?php echo date('Y-m-d'); ?>" name="dtDibutuhkan" value="<?php echo date('Y-m-d'); ?>" required>
                                                </td>
                                                <td>
                                                    <input type="file" name="strNmFile" accept=".jpg,.jpeg,.png,.bmp" data-parsley-trigger="change" autocomplete="off" class="form-control">
                                                </td>
                                                <td>
                                                    <p class="text-left">
                                                        <button type="submit" name="+" class="btn btn-space btn-primary">+</button>
                                                    </p>
                                                </td>
                                            </tr>
                                            <?php
                                            $i = 0;
                                            $query = "SELECT  ITEM.strKdItem,
                                                                ITEM.strDeskripsi, 
                                                                DEPT.strNmDept, 
                                                                D_LPBJ.ID, 
                                                                D_LPBJ.strNmFile, 
                                                                D_LPBJ.item_strUnit AS strUnit, 
                                                                D_LPBJ.iJumlah, 
                                                                D_LPBJ.iJumlahStok, 
                                                                D_LPBJ.strKeteranganREQ, 
                                                                D_LPBJ.dtDibutuhkan 
                                                        FROM tpc_d_lpbj AS D_LPBJ 
                                                        LEFT JOIN tpc_h_lpbj AS H_LPBJ 
                                                            ON D_LPBJ.h_lpbj_ID = H_LPBJ.ID 
                                                        LEFT JOIN item AS ITEM 
                                                            ON D_LPBJ.item_ID=ITEM.ID 
                                                        LEFT JOIN tpc_dept AS DEPT 
                                                            ON D_LPBJ.dept_ID=DEPT.ID 
                                                        WHERE (D_LPBJ.stsAktif BETWEEN 1 AND 2) AND 
                                                              (H_LPBJ.stsAktif BETWEEN 1 AND 2) AND 
                                                              H_LPBJ.strKdLPBJ='" . $_GET['lpbj'] . "' 
                                                        ORDER BY D_LPBJ.ID ASC
                                                        ";
                                            $result = $MySQLi->query($query);
                                            while ($row = $result->fetch_array()) {
                                                $i++;
                                                $img = "";
                                                if ($row['strNmFile'] <> "")
                                                    $img = "<img src='assets/images/lpbj_pic/thumb_{$row['strNmFile']}' width='50px' height='50px' class='thumbnail' data-image='assets/images/lpbj_pic/{$row['strNmFile']}'>";

                                                echo "<tr>
                                                            <td>" . $row['strKdItem'] . "</td>
                                                            <td>" . $row['strDeskripsi'] . "</td>
                                                            <td>" . $row['iJumlah'] . "</td>
                                                            <td>" . $row['strUnit'] . "</td>
                                                            <td>" . $row['iJumlahStok'] . "</td>
                                                            <td>" . $row['strKeteranganREQ'] . "</td>
                                                            <td>" . $row['strNmDept'] . "</td>
                                                            <td>" . $row['dtDibutuhkan'] . "</td>
                                                            <td>" . $img . "</td>
                                                            <td>
                                                                <p class='text-left'>
                                                                <a href='transaksi/persediaan/pemesanan/simpan.tambah.pesan.barang.php?data=" . $row['ID'] . "' class='btn btn-space btn-secondary'>-</a>
                                                                <a href='?menu=edit-lpbj-item&lpbj=" . $_GET['lpbj'] . "&id=" . $row['ID'] . "' class='btn btn-space btn-secondary'>Edit</a>
                                                                </p>
                                                            </td>
                                                        </tr>";
                                            }
                                            ?>
                                        </tbody>
                                        <tfoot>
                                            <tr>
                                                <th width="100px">Kode</th>
                                                <th width="450px">Deskripsi</th>
                                                <th width="50px">Jumlah</th>
                                                <th width="100px">Satuan</th>
                                                <th width="50px">Jumlah Stok</th>
                                                <th width="650px">Keterangan</th>
                                                <th width="150px">Departement</th>
                                                <th width="100px">Tanggal dibutuhkan</th>
                                                <th width="250px">Gambar</th>
                                                <th width="100px">Option</th>
                                            </tr>
                                        </tfoot>
                                    </table>
                                </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>
                        <?php
                        if ($i > 0) {
                        ?>
                            <div class="col-sm-6 pl-0">
                                <input type="checkbox" name="stsUrgent" value="0">
                                <label for="urgent" style="color: red;"> Urgent</label><br>
                                <p class="text-left">
                                    <a href="transaksi/persediaan/pemesanan/simpan.tambah.pesan.barang.php?lpbj=<?php echo $_SESSION['strKdLPBJ']; ?>" class="btn btn-space btn-primary">Submit</a>
                                </p>
                            </div>
                        <?php
                        }
                        ?>
                    </div>
                </form>
                <!-- ============================================================== -->
                <!-- end data table  -->
                <!-- ============================================================== -->
            </div>
        <?php
        }
        ?>
    </div>
    <!-- ============================================================== -->
    <!-- footer -->
    <!-- ============================================================== -->
    <div class="footer">
        <div class="container-fluid">
            <div class="row">
                <div class="col-xl-6 col-lg-6 col-md-6 col-sm-12 col-12">
                    Copyright © 2024 TPC-PACK, Developed By <a href="mailto: jockie@netportal.my.id">JC Comp</a>.
                </div>
                <!--<div class="col-xl-6 col-lg-6 col-md-6 col-sm-12 col-12">
                            <div class="text-md-right footer-links d-none d-sm-block">
                                <a href="javascript: void(0);">About</a>
                                <a href="javascript: void(0);">Support</a>
                                <a href="javascript: void(0);">Contact Us</a>
                            </div>
                        </div>-->
            </div>
        </div>
    </div>
    <!-- ============================================================== -->
    <!-- end footer -->
    <!-- ============================================================== -->
</div>
<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;
    }
</script>
<script src="assets/libs/js/jquery-3.6.0.min.js"></script>
<script>
    $(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();
            }
        });
    });
</script>