<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">Edit 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=edit-lpbj" class="breadcrumb-link">Edit LPBJ</a></li>
                            </ol>
                        </nav>
                    </div>
                </div>
            </div>
        </div>
        <!-- ============================================================== -->
        <!-- end pageheader -->
        <!-- ============================================================== -->

        <?php
        $MySQLi = new MySQLi($strDBHostName, $strDBUsername, $strDBPassword, $strDBName);
        if (isset($_GET['lpbj'])) {
            $query = "SELECT * 
                            FROM tpc_h_lpbj 
                            WHERE strKdLPBJ='" . $_GET['lpbj'] . "' AND 
                                  stsAktif=1
                            ";
            $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['h_lpbj_ID'] = $row['ID'];
            $_SESSION['strKdLPBJ'] = $row['strKdLPBJ'];
        ?>
            <div class="row">
                <form id="basicform" data-parsley-validate="" action="transaksi/persediaan/pemesanan/simpan.edit.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 -->
                    <!-- ============================================================== -->


                    <!-- ============================================================== -->
                    <!-- data table  -->
                    <!-- ============================================================== -->
                    <div class="col-xl-12 col-lg-12 col-md-12 col-sm-12 col-12">
                        <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" style="width:100%">
                                        <thead>
                                            <tr>
                                                <th>Kode</th>
                                                <th>Deskripsi</th>
                                                <th>Jumlah</th>
                                                <th>Satuan</th>
                                                <th>Jumlah Stok</th>
                                                <th>Keterangan</th>
                                                <th>Departement</th>
                                                <th>Tanggal dibutuhkan</th>
                                                <th>Gambar</th>
                                                <th>Upload Gambar Baru</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php
                                            $query = "SELECT  ITEM.strKdItem,
                                                                ITEM.strDeskripsi, 
                                                                DEPT.strNmDept, 
                                                                D_LPBJ.ID, 
                                                                D_LPBJ.item_strUnit AS strUnit, 
                                                                D_LPBJ.iJumlah, 
                                                                D_LPBJ.iJumlahStok, 
                                                                D_LPBJ.strNmFile, 
                                                                D_LPBJ.strKeteranganREQ, 
                                                                D_LPBJ.strKeteranganSPV, 
                                                                D_LPBJ.strKeteranganMGR, 
                                                                D_LPBJ.strKeteranganDIR, 
                                                                D_LPBJ.dtDibutuhkan, 
                                                                H_LPBJ.stsDone 
                                                        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'] . "' AND 
                                                              D_LPBJ.stsACCSPV > 0 AND 
                                                              D_LPBJ.stsACCMGR > 0 AND 
                                                              D_LPBJ.stsACCDIR > 0
                                                        ORDER BY D_LPBJ.ID ASC
                                                        ";
                                            $result = $MySQLi->query($query);
                                            $i = 0;
                                            $stsDone = "";
                                            while ($row = $result->fetch_array()) {
                                                $selectedUnit = $row['strUnit'];
                                                $i++;
                                                if ($i == 1)
                                                    $stsDone = $row['stsDone'];
                                                echo "<input type='hidden' name='d_lpbj_ID" . $i . "' value='" . $row['ID'] . "'>";
                                                echo "<tr>
                                                            <td>" . $row['strKdItem'] . "</td>
                                                            <td><input type='text' id='list_item' name='strDeskripsi" . $i . "' data-parsley-trigger='change' required='' placeholder='Masukan deskripsi barang' autocomplete='off' class='form-control' value='" . $row['strDeskripsi'] . "' list='list_deskripsi'>
                                                                <datalist id='list_deskripsi'>";
                                                $query = "SELECT ID, 
                                                                                strDeskripsi 
                                                                        FROM item 
                                                                        ";
                                                $result2 = $MySQLi->query($query);
                                                while ($row2 = $result2->fetch_array())
                                                    echo "<option value='" . $row2['strDeskripsi'] . "'  data-realvalue='" . $row2['ID'] . "_" . $row2['strDeskripsi'] . "'>";
                                                echo       "</datalist>
                                                            </td>
                                                            <td><input type='text' name='iJumlah" . $i . "' data-parsley-trigger='change' required placeholder='masukan jumlah barang yang akan dipesan' autocomplete='off' class='form-control' value='" . $row['iJumlah'] . "'></td>
                                                            <td>
                                                            <input id='inputUnit' type='text' name='strUnit" . $i . "' value='" . $row['strUnit'] . "' data-parsley-trigger='change' class='form-control' autocomplete='off' list='datalist_unit'>
                                                            <datalist id='datalist_unit'></datalist>
                                                            </td>
                                                            <td><input type='text' name='iJumlahStok" . $i . "' data-parsley-trigger='change' required placeholder='masukan jumlah stok saat ini' autocomplete='off' class='form-control' value='" . $row['iJumlahStok'] . "'></td>
                                                            <td>" . $row['strKeteranganREQ'] . "<br>" . $row['strKeteranganSPV'] . "<br>" . $row['strKeteranganMGR'] . "<br>" . $row['strKeteranganDIR'] . "<br><textarea class='form-control' name='strKeterangan" . $i . "' id='exampleFormControlTextarea1' rows='3'></textarea></td>
                                                            <td>
                                                                <select class='form-control' id='inputDepartement' name='dept_ID" . $i . "'>";
                                                $query = "SELECT ID, 
                                                                                strNmDept 
                                                                            FROM tpc_dept 
                                                                            WHERE stsAktif=1 AND 
                                                                                  strKdLokasi='" . $_SESSION['strKdLokasi'] . "'
                                                                            ";
                                                $result2 = $MySQLi->query($query);
                                                while ($row2 = $result2->fetch_array()) {
                                                    if ($row['strNmDept'] == $row2['strNmDept'])
                                                        $select = "selected";
                                                    else $select = "";
                                                    echo "<option value='" . $row2['ID'] . "' " . $select . ">" . $row2['strNmDept'] . "</option>";
                                                }
                                                echo        "</select>
                                                            </td>
                                                            <td><input type='date' name='dtDibutuhkan" . $i . "' value='" . $row['dtDibutuhkan'] . "' required></td>
                                                            <td>" . ($row['strNmFile'] == "" ? "&nbsp;" : "<img src='assets/images/lpbj_pic/thumb_{$row['strNmFile']}' width='50px' height='50px' class='thumbnail' data-image='assets/images/lpbj_pic/{$row['strNmFile']}'>") . "</td>
                                                            <td>
                                                            <input type='file' name='strNmFile" . $i . "' accept='.jpg,.jpeg,.png,.bmp' data-parsley-trigger='change' autocomplete='off' class='form-control'>
                                                            </td>
                                                        </tr>";
                                            }
                                            ?>
                                            <input type="hidden" name="max_iCounter" value="<?php echo $i; ?>">
                                        </tbody>
                                        <tfoot>
                                            <tr>
                                                <th>Kode</th>
                                                <th>Deskripsi</th>
                                                <th>Jumlah</th>
                                                <th>Satuan</th>
                                                <th>Jumlah Stok</th>
                                                <th>Keterangan</th>
                                                <th>Departement</th>
                                                <th>Tanggal dibutuhkan</th>
                                                <th>Gambar</th>
                                                <th>Upload Gambar Baru</th>
                                            </tr>
                                        </tfoot>
                                    </table>
                                </div>
                            </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>

                        <?php
                        if ($stsDone == 0 || $stsDone == 2) {
                        ?>
                            <div class="col-sm-6 pl-0">
                                <p class="text-left">
                                    <button type="submit" name="submit" class="btn btn-space btn-primary">Submit</button>
                                </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>