<?= $this->extend('Admin/Layout/app_layout'); ?>
<?= $this->section('content'); ?>

<div class="layout-px-spacing">

    <div class="middle-content container-xxl p-0">

        <div class="layout-top-spacing">

            <div class="widget widget-three">

                <div class="widget-heading">
                    <h5 class="">Koordinator Projek P5</h5>
                    <div class="task-action">
                        <button type="button" id="simpan" class="btn btn-primary"><i data-feather="save"></i> <span class="btn-text-inner"> Simpan</span></button>
                    </div>
                </div>

                <div class="widget-content">
                    <div class="table-responsive">
                        <table class="table" id="rombelTable" class="display">
                            <thead>
                                <tr>
                                    <th class="text-center">
                                        #
                                    </th>
                                    <th>Tingkat</th>
                                    <th>Nama Rombel</th>
                                    <th>Koordinator</th>
                                </tr>
                            </thead>
                            <tbody>

                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>

</div>

<?= $this->endSection(); ?>
<?= $this->section('pagejs'); ?>
<script type="text/javascript">
    $(document).ready(function() {

        $('#simpan').click(function(e) {
            e.preventDefault(); // Menghentikan aksi default tombol "btn-save"
            var koor = [];
            $('select[name="koordinator[]"]').each(function() {
                var id_rombel = $(this).data('id');
                var koordinator = $(this).val(); // Ambil nilai dari input
                koor.push({
                    id_rombel: id_rombel,
                    koordinator: koordinator
                });
            });


            $.ajax({
                url: '<?= base_url('projek/koordinator/save') ?>',
                type: 'POST',
                dataType: 'json',
                data: {
                    koor: koor,
                },
                success: function(response) {
                    Snackbar.show({
                        text: response.message,
                        pos: 'top-center'
                    });

                },
                error: function(xhr, status, error) {
                    console.error(status, error); // Handle error jika terjadi
                }
            });


        });
        getrombel();

        function getrombel() {
            $.ajax({
                url: '<?= base_url('projek/koordinator/getrombel') ?>', // Ubah ini sesuai dengan URL endpoint Anda
                type: 'GET',
                dataType: "json",
                success: function(data) {
                    var tbody = $('#rombelTable tbody');
                    tbody.empty();
                    $.each(data.rombel, function(index, item) {
                        var no = index + 1;
                        var row = '<tr>' +
                            '<td>' + no + '</td>' +
                            '<td>' + item.tingkat + '</td>' +
                            '<td>' + item.nama_rombel + '</td>' +
                            '<td>' +
                            '<select data-id="' + item.id + '" class="form-control form-control-sm" name="koordinator[]" required>' +
                            '<option value="">Pilih Koordinator</option>';

                        // Mulai dari sini, kita memperbaiki sintaksis
                        $.each(data.guru, function(index, guru) {
                            var selected = '';
                            if (item.koordinator == guru.id) {
                                selected = 'selected';
                            }
                            row += '<option value="' + guru.id + '" ' + selected + '>' + guru.nama + '</option>';
                        });

                        // Menutup tag select dan tag row
                        row += '</select>' +
                            '</td>' +
                            '</tr>';

                        // Menambahkan baris ke dalam tbody
                        tbody.append(row);
                    });
                },
                error: function(xhr, status, error) {
                    console.error(status, error);
                    // Tindakan yang sesuai jika ada kesalahan dalam pengambilan data
                }
            });
        }

    });
    var save_method; //for save method string
    var table;
</script>
<?= $this->endSection(); ?>