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

    <div class="pagetitle">
        <h1>Final Check 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"><?= $datauser['FullName'] ?></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>
                            <br>
                            <div class="datatable-container">
                                <div id="tblpbj"></div>
                                <form id="f_cek" name="f_cek" method="" action="">
                                    <table class="table table-sm table-bordered table-stripped">
                                        <thead>
                                            <tr>
                                                <th>#</th>
                                                <th>Kode</th>
                                                <th>Nama barang</th>
                                                <th>Jml</th>
                                                <th>Unit</th>
                                                <th>Jml stok</th>
                                                <th>Keterangan</th>
                                                <th>Dept</th>
                                                <th>Tgl dibutuhkan</th>
                                                <th>Reject</th>
                                            </tr>
                                        </thead>
                                        <input type="hidden" value="<?= $nmrlpbj; ?>" name="kd" id="kd">
                                        <tbody>
                                            <?php
                                            $i = 1;
                                            foreach ($data as $d) {
                                                echo "<tr>";
                                                echo "<td>" . $i . "</td>";
                                                echo "<td>" . $d['kd_brg'] . "</td>";
                                                echo "<td>" . $d['nama_brg'] . "</td>";
                                                echo "<td style='text-align: center;'>" . $d['qty_req'] . "</td>";
                                                echo "<td style='text-align: center;'>" . $d['uom'] . "</td>";
                                                echo "<td style='text-align: center;'>" . $d['stok'] . "</td>";
                                                if ($akses['dml'] == 1) {
                                                    echo "<td>" . $d['keterangan'] . "<br><textarea class='form-control' name='keterangan[]' id='keterangan[]' placeholder='Keterangan'></textarea><input type='hidden' name='nmr[]' data-kd='" . $d['doc_id'] . "' value='2'><input type='hidden' name='kd_brg[]' id='kd_brg[]' value='" . $d['doc_id'] . "'></td>";
                                                } else {
                                                    echo "<td>" . $d['keterangan'] . "</td>";
                                                }
                                                echo "<td>" . $d['strNmDept'] . "</td>";
                                                echo "<td style='text-align: center;'>" . $d['tanggal'] . "</td>";
                                                if ($akses['dml'] == 1) {
                                                    echo "<td style='text-align: center;'><input onclick='ubah_nilai(this)' type='checkbox' name='pilihan[]' value='" . $d['doc_id'] . "'></td>";
                                                } else {
                                                    echo "<td></td>";
                                                }
                                                echo "</tr>";
                                                $i++;
                                            }
                                            ?>
                                        </tbody>
                                    </table>
                                    <?php if ($akses['dml'] == 1) { ?>
                                        <button type="submit" class="btn btn-primary btn-sm">Submit</button>
                                    <?php } ?>
                                </form>
                            </div>

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

                    </div>
                </div>

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

</main>
<script>
    function toggle(source) {
        var checkboxes = document.querySelectorAll('input[type="checkbox"]');
        for (var i = 0; i < checkboxes.length; i++) {
            if (checkboxes[i] != source)
                checkboxes[i].checked = source.checked;
        }
    }

    function ubah_nilai(checkbox) {
        var checkboxes = document.querySelectorAll('input[name="pilihan[]"]');
        checkboxes.forEach(function(checkbox) {
            var id = checkbox.value;
            var nmrInput = document.querySelector('input[name="nmr[]"][data-kd="' + id + '"]');
            if (checkbox.checked) {
                nmrInput.value = '0';
            } else {
                nmrInput.value = '2';
            }
        });
    }

    function toggleAll(checkbox) {
        var checkboxes = document.querySelectorAll('input[name="pilihan[]"]');
        checkboxes.forEach(function(checkbox) {
            checkbox.checked = checkbox.checked;
            var id = checkbox.value;
            var nmrInput = document.querySelector('input[name="nmr[]"][data-kd="' + id + '"]');
            nmrInput.value = checkbox.checked ? '1' : '0';
        });
    }
</script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
    $(document).ready(function() {
        $('form[name="f_cek"]').on('submit', function(event) {
            event.preventDefault();
            var formData = new FormData(this);
            Swal.fire({
                title: "Submit LPBJ ini?",
                //text: "You won't be able to revert this!",
                icon: "question",
                showCancelButton: true,
                confirmButtonColor: "#3085d6",
                cancelButtonColor: "#d33",
                confirmButtonText: "Ya, Submit!"
            }).then((result) => {
                if (result.isConfirmed) {

                    $.ajax({
                        url: '<?= base_url('Lpbj/final_apprv'); ?>',
                        type: 'POST',
                        data: formData,
                        processData: false,
                        contentType: false,
                        success: function(response) {
                            Swal.fire({
                                title: 'Terima kasih atas persetujuan anda',
                                icon: 'success',
                                didClose: () => {
                                    window.open("<?= base_url('Lpbj/final_check') ?>", "_self"); // Mengarahkan kembali ke halaman utama
                                }
                            });

                            // Menyimpan log
                            var kd_lpbj = "";
                            var strTable = 'tb_pr';
                            var keterangan = 'Telah melakukan aproval lpbj ' + kd_lpbj;

                            $.ajax({
                                url: "<?= base_url('Pengaturan/log'); ?>",
                                type: 'POST',
                                cache: false,
                                data: {
                                    strTable: strTable,
                                    keterangan: keterangan
                                },
                                success: function() {
                                    // begin
                                    //end
                                }
                            });
                        },
                        error: function(xhr, status, error) {
                            Swal.fire({
                                title: 'Terjadi kesalahan',
                                text: error,
                                icon: 'error'
                            });
                        }
                    });

                }
            });
        });
    });
</script>