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

    <div class="pagetitle">
        <h1>Purchase detail</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>
                            <br>
                            <div class="datatable-container">
                                <form id="f_purc" name="f_purc" method="" action="">
                                    <input type="hidden" value="<?= $kd; ?>" name="kd" id="kd">
                                    <table class="table table-sm table-bordered table-striped">
                                        <thead>
                                            <th>#</th>
                                            <th>No LPBJ</th>
                                            <th>Kode barang</th>
                                            <th>Barang</th>
                                            <th>Qty</th>
                                            <th>Unit</th>
                                            <th>Dept</th>
                                            <th>Keterangan</th>
                                            <th>Reject</th>
                                        </thead>
                                        <tbody>
                                            <?php
                                            $i = 1;
                                            foreach ($data as $d) {
                                                echo "<tr>";
                                                echo "<td>" . $i . "</td>";
                                                echo "<td>" . $d['kd_lpbj'] . "</td>";
                                                echo "<td>" . $d['kd_brg'] . "</td>";
                                                echo "<td>" . $d['brg'] . "</td>";
                                                //echo "<td>" . $d['qty_req_pr'] . "</td>";
                                                echo "<td><input type='number' id='qty[]' name='qty[]' step='0.0001' class='form-control' value='" . $d['qty_req_pr'] . "'></td>";
                                                echo "<td>" . $d['uom'] . "</td>";
                                                echo "<td>" . $d['dept'] . "</td>";
                                                echo "<td>" . $d['keterangan'] . "<textarea class='form-control' id='keterangan[]' name='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>";
                                                echo "<td style='text-align: center;'><input onclick='ubah_nilai(this)' type='checkbox' name='pilihan[]' value='" . $d['doc_id'] . "'></td>";
                                                echo "</tr>";
                                                $i++;
                                            }
                                            ?>
                                        </tbody>
                                        <tfoot>
                                            <td colspan='9'><button type='submit' class="btn btn-primary btn-sm">Submit</button></td>
                                        </tfoot>
                                    </table>
                                </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_purc"]').on('submit', function(event) {
            event.preventDefault();
            var formData = new FormData(this);
            Swal.fire({
                title: "Proses LPBJ ini menjadi PR ?",
                //text: "You won't be able to revert this!",
                icon: "warning",
                showCancelButton: true,
                confirmButtonColor: "#3085d6",
                cancelButtonColor: "#d33",
                confirmButtonText: "Ya, Submit!"
            }).then((result) => {
                if (result.isConfirmed) {
                    $.ajax({
                        url: '<?= base_url('Purchase/purch_approve'); ?>',
                        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('Purchase') ?>", "_self");
                                }
                            });

                            // Menyimpan log                            
                            var strTable = 'tb_pr';
                            var keterangan = 'Telah melakukan aproval purchasing ' + <?= $kd; ?>;

                            $.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>