<?= $this->extend('Admin/Layout/app_layout'); ?>
<?= $this->section('css'); ?>
<style>
    /* .modal-full-page {
        min-height: 100%;
        min-width: 90%;
        margin: 0;
    }

    .modal-content {
        min-height: 100vh;
    }

    .modal-header {
        border-bottom: 1px solid #dee2e6;
    }

    .modal-dialog {
        margin: 0;
        width: 100%;
    }

    .modal-body {
        overflow-y: auto;
    } */
</style>
<?= $this->endSection(); ?>
<?= $this->section('content'); ?>

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

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

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

            <div class="widget ">

                <div class="widget-heading">
                    <h5 class="">Menu Penilaian</h5>
                    <div class="task-action">
                        <!-- <button type="button" onclick="add()" class="btn btn-primary"><i data-feather="plus-circle"></i> <span class="btn-text-inner">Tambah</span></button> -->
                    </div>
                </div>

                <div class="widget-content">
                    <div class="table-responsive">
                        <table class="table" id="tujuanTable" class="display">
                            <thead>
                                <tr>
                                    <th class="text-center">
                                        #
                                    </th>
                                    <th>Mata Pelajaran</th>
                                    <th>Rombel</th>
                                    <th>Guru</th>
                                    <th>Progress</th>
                                    <th>Penilaian Akhir</th>
                                    <th>Capaian Kompetensi</th>

                                </tr>
                            </thead>
                            <tbody>

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

</div>


<!-- Full Page Modal -->
<div class="modal fade" id="modalpenilaian" tabindex="-1" role="dialog" aria-labelledby="fullPageModalLabel" aria-hidden="true" data-bs-backdrop="static">
    <div class="modal-dialog modal-xl" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="fullPageModalLabel">Penilaian Pembelajaran</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close">
                    <i data-feather="x"></i>
                </button>
            </div>
            <div class="modal-body">
                <div id="alert"></div>
                <form class="form-penilaian">

                    <input type="hidden" class="form-control" id="idpembelajaran" aria-describedby="helpId" placeholder="">

                    <div class="table-responsive">
                        <table id="tabel-siswa" class="table table-bordered">
                            <thead>
                                <tr>
                                    <th scope="col">No</th>
                                    <th scope="col">Nama</th>
                                    <th scope="col">Nilai</th>
                                    <th class="text-center" scope="col">Tujuan Pembelajaran</th>

                                </tr>
                            </thead>
                            <tbody>
                            </tbody>
                        </table>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn btn-light-dark" data-bs-dismiss="modal"><i class="flaticon-cancel-12"></i> Discard</button>
                <button type="button" id="btnSaveTujuan" class="btn btn-primary">Save</button>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="modalcapaian" tabindex="-1" role="dialog" aria-labelledby="fullPageModalLabel" aria-hidden="true" data-bs-backdrop="static">
    <div class="modal-dialog modal-xl" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="fullPageModalLabel">Capaian Pembelajaran</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close">
                    <i data-feather="x"></i>
                </button>
            </div>
            <div class="modal-body">
                <div id="alertcapaian"></div>
                <form class="form-penilaian">

                    <input type="hidden" class="form-control" id="idpem" aria-describedby="helpId" placeholder="">
                    <input type="hidden" id="idrombel">
                    <div class="table-responsive">
                        <table id="tabel-capaian" class="table table-bordered">
                            <thead>
                                <tr>
                                    <th scope="col">No</th>
                                    <th scope="col">Nama</th>
                                    <th scope="col">Nilai</th>
                                    <th scope="col">Capaian Tercapai</th>
                                    <th scope="col">Capaian Belum</th>
                                    <th scope="col">#</th>

                                </tr>
                            </thead>
                            <tbody>
                            </tbody>
                        </table>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn btn-light-dark" data-bs-dismiss="modal">Cancel</button>
                <button type="button" id="btnResetCapaian" class="btn btn btn-danger">Reset Capaian</button>
                <button type="button" id="btnSaveCapaian" class="btn btn-primary">Save</button>
            </div>
        </div>
    </div>
</div>
<?= $this->endSection(); ?>
<?= $this->section('pagejs'); ?>
<script type="text/javascript">
    $(document).ready(function() {
        $('.modal').on('hidden.bs.modal', function() {
            $('#tujuanTable').DataTable().ajax.reload();
        });
        // Ketika input-nilai berubah
        $(document).on('input', '.input-nilai', function() {
            // Hapus semua karakter selain angka dari nilai input
            $(this).val($(this).val().replace(/\D/g, ''));
        });
        $('#tabel-siswa').on('keydown', 'input, textarea', function(e) {
            var code = e.keyCode || e.which;
            var $this = $(this);
            var $td = $this.closest('td');
            var $tr = $td.closest('tr');
            var index = $td.index();

            if (code == 40) { // Panah bawah
                e.preventDefault();
                var $nextTr = $tr.next();
                var $nextTd = $nextTr.children().eq(index).find('input, textarea');
                if ($nextTd.length > 0) {
                    $nextTd.focus().select();
                } else {
                    var $nextInput = $tr.next().find('input, textarea').eq(0);
                    if ($nextInput.length > 0) {
                        $nextInput.focus().select();
                    }
                }
            } else if (code == 38) { // Panah atas
                e.preventDefault();
                var $prevTr = $tr.prev();
                var $prevTd = $prevTr.children().eq(index).find('input, textarea');
                if ($prevTd.length > 0) {
                    $prevTd.focus().select();
                } else {
                    var $prevInput = $tr.prev().find('input, textarea').eq(0);
                    if ($prevInput.length > 0) {
                        $prevInput.focus().select();
                    }
                }
            } else if (code == 37) { // Panah kiri
                e.preventDefault();
                var $prevTd = $td.prev('td').find('input, textarea');
                if ($prevTd.length > 0) {
                    $prevTd.focus().select();
                }
            } else if (code == 39) { // Panah kanan
                e.preventDefault();
                var $nextTd = $td.next('td').find('input, textarea');
                if ($nextTd.length > 0) {
                    $nextTd.focus().select();
                }
            } else if (code == 13) { // Tombol enter
                e.preventDefault();
                var $nextTr = $tr.next();
                var $nextTd = $nextTr.children().eq(index).find('input, textarea');
                if ($nextTd.length > 0) {
                    $nextTd.focus().select();
                } else {
                    var $nextInput = $tr.next().find('input, textarea').eq(0);
                    if ($nextInput.length > 0) {
                        $nextInput.focus().select();
                    }
                }
            }
        });
        $('#tabel-siswa').on('paste', '.input-nilai', function(event) {
            var clipboardData = (event.originalEvent || event).clipboardData;
            var pastedData = clipboardData.getData('text');
            var rows = pastedData.split('\n');

            $('#tabel-siswa .dataRow').each(function(index, row) {
                var values = rows[index] ? rows[index].split('\t') : [];
                $(row).find('.input-nilai').each(function(index, input) {
                    $(input).val(values[index] || '');
                });
            });

            event.preventDefault(); // Prevent the default paste behavior
        });
        $('#btnSaveTujuan').click(function(e) {
            e.preventDefault(); // Menghentikan aksi default tombol "btn-save"
            var allNilaiFilled = true;
            $('input[name="nilai[]"]').each(function() {
                if ($(this).val() === '') {
                    allNilaiFilled = false;
                    $(this).addClass('is-invalid'); // Tandai input yang tidak diisi
                } else {
                    $(this).removeClass('is-invalid');
                }
            });

            // Jika ada input yang kosong, tampilkan pesan dan hentikan proses
            if (!allNilaiFilled) {
                Snackbar.show({
                    text: "lengkapi semua nilai terlebih dahulu",
                    pos: 'top-center'
                });
                return;
            }
            var nilaiArray = {};
            $('input[name="nilai[]"]').each(function() {
                var idSiswa = $(this).data('id');
                var nilaiValue = $(this).val();
                nilaiArray[idSiswa] = nilaiValue;
            });

            var switchArray = {};
            $('.switch-input').each(function() {
                var idSiswa = $(this).data('idsiswa');
                var idTujuan = $(this).data('id');
                var isChecked = $(this).prop('checked');

                if (!switchArray[idSiswa]) {
                    switchArray[idSiswa] = {
                        'tercapai': [],
                        'tidak_tercapai': []
                    };
                }

                if (isChecked) {
                    switchArray[idSiswa]['tercapai'].push(idTujuan);
                } else {
                    switchArray[idSiswa]['tidak_tercapai'].push(idTujuan);
                }
            });

            $.ajax({
                url: '<?= base_url('penilaian/akhir/save') ?>',
                type: 'POST',
                dataType: 'json',
                data: {
                    nilai: nilaiArray,
                    tujuan: switchArray,
                    'idpembelajaran': $('#idpembelajaran').val(),
                },
                success: function(response) {
                    Snackbar.show({
                        text: response.message,
                        pos: 'top-center'
                    });

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


        });

        $('#btnSaveCapaian').click(function(e) {

            e.preventDefault(); // Menghentikan aksi default tombol "btn-save"

            var dataToUpdate = [];
            var idpem = $('#idpem').val();
            var idrombel = $('#idrombel').val();
            // Loop melalui setiap baris data pada tabel
            $('#tabel-capaian tbody tr').each(function(index, row) {
                var id_siswa = $(row).find('input[type="text"]').data('id');
                var deskripsi_tercapai = $(row).find('.sudah_tercapai').val();
                var deskripsi_belum = $(row).find('.belum_tercapai').val();

                // Menambahkan data ke array dataToUpdate
                dataToUpdate.push({
                    id_siswa: id_siswa,
                    deskripsi_tercapai: deskripsi_tercapai,
                    deskripsi_belum: deskripsi_belum
                });
            });

            // Simpan data ke database menggunakan Ajax
            $.ajax({
                url: '<?= base_url('penilaian/akhir/savecapaian') ?>', // Ganti dengan URL Anda
                method: 'POST',
                data: {
                    'idpembelajaran': idpem,
                    'dataToUpdate': dataToUpdate // Mengirim dataToUpdate dalam bentuk JSON
                },
                success: function(response) {
                    Snackbar.show({
                        text: response.message,
                        pos: 'top-center'
                    });
                    getCapaian(idpem);
                },
                error: function(xhr, status, error) {
                    // Tindakan yang diambil jika pengiriman gagal
                    console.error('Terjadi kesalahan saat mengirim data ke server:', error);
                }
            });


        });

        $('#btnResetCapaian').click(function(e) {

            e.preventDefault(); // Menghentikan aksi default tombol "btn-save"
            var idpem = $('#idpem').val();
            var idrombel = $('#idrombel').val();


            // Simpan data ke database menggunakan Ajax
            $.ajax({
                url: '<?= base_url('penilaian/akhir/resetcapaian') ?>', // Ganti dengan URL Anda
                method: 'POST',
                data: {
                    'idpem': idpem,
                },
                success: function(response) {
                    Snackbar.show({
                        text: response.message,
                        pos: 'top-center'
                    });
                    getCapaian(idpem);
                },
                error: function(xhr, status, error) {
                    // Tindakan yang diambil jika pengiriman gagal
                    console.error('Terjadi kesalahan saat mengirim data ke server:', error);
                }
            });


        });

        $('#tujuanTable').dataTable({
            ajax: {
                url: '<?= base_url('penilaian/akhir/getpembelajaran') ?>', // Sesuaikan dengan URL Controller Anda
                type: 'GET'
            },
            columns: [{
                    data: null,
                    render: function(data, type, row, meta) {
                        return meta.row + 1; // Nomor urut dimulai dari 1, bukan 0
                    },
                    orderable: false // Kolom ini tidak dapat diurutkan
                },

                {
                    data: 'nama_matapelajaran'
                },
                {
                    data: 'nama_rombel'
                },
                {
                    data: 'nama_guru'
                },
                {
                    data: null,
                    render: function(data, type, row) {
                        var progressValue = row.jumlah_siswa != 0 ? Math.round((row.jumlah_dinilai / row.jumlah_siswa) * 100) : 0; // Hitung persentase progres
                        //var progressBarStyle = 'background-color: #007bff; width: ' + progressValue + '%;'; // Gaya untuk progres bar
                        return '<span>' + row.jumlah_dinilai + ' dari ' + row.jumlah_siswa + ' Siswa</span><div class="progress br-30" style="height:15px"><div class="progress-bar bg-primary" role="progressbar" style="width: ' + progressValue + '%" aria-valuemin="0" aria-valuemax="100">' + progressValue + '%</div></div>';
                    }
                },
                {
                    data: null,
                    render: function(data, type, row) {
                        if (row.jumlah_siswa != 0) {
                            return '<button class="penilaian-btn btn  btn-secondary" data-rombel="' + row.id_rombel + '" data-mapel="' + row.id_matapelajaran + '" data-id="' + row.id + '"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-plus-circle"><circle cx="12" cy="12" r="10"></circle><line x1="12" y1="8" x2="12" y2="16"></line><line x1="8" y1="12" x2="16" y2="12"></line></svg> Nilai</button>';
                        } else {

                            return '<span class="badge badge-danger">Siswa Kosong</span>';
                        }
                    }
                },
                {
                    data: null,
                    render: function(data, type, row) {
                        return '<button class="capaian-btn btn  btn-info" data-rombel="' + row.id_rombel + '" data-mapel="' + row.id_matapelajaran + '" data-id="' + row.id + '"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-plus-circle"><circle cx="12" cy="12" r="10"></circle><line x1="12" y1="8" x2="12" y2="16"></line><line x1="8" y1="12" x2="16" y2="12"></line></svg> Capaian</button>';
                    }
                },

            ],
            "dom": "<'dt--top-section'<'row'<'col-12 col-sm-6 d-flex justify-content-sm-start justify-content-center'l><'col-12 col-sm-6 d-flex justify-content-sm-end justify-content-center mt-sm-0 mt-3'f>>>" +
                "<'table-responsive'tr>" +
                "<'dt--bottom-section d-sm-flex justify-content-sm-between text-center'<'dt--pages-count  mb-sm-0 mb-3'i><'dt--pagination'p>>",
            "oLanguage": {
                "oPaginate": {
                    "sPrevious": '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-arrow-left"><line x1="19" y1="12" x2="5" y2="12"></line><polyline points="12 19 5 12 12 5"></polyline></svg>',
                    "sNext": '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-arrow-right"><line x1="5" y1="12" x2="19" y2="12"></line><polyline points="12 5 19 12 12 19"></polyline></svg>'
                },
                "sInfo": "Showing page _PAGE_ of _PAGES_",
                "sSearch": '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-search"><circle cx="11" cy="11" r="8"></circle><line x1="21" y1="21" x2="16.65" y2="16.65"></line></svg>',
                "sSearchPlaceholder": "Search...",
                "sLengthMenu": "Results :  _MENU_",
            },
            "stripeClasses": [],
            "lengthMenu": [7, 10, 20, 50],
            "pageLength": 10
        });

        $('#tujuanTable').on('click', '.penilaian-btn', function() {
            var id = $(this).data('id');
            var idmapel = $(this).data('mapel');
            var idrombel = $(this).data('rombel');
            $('#modalpenilaian').modal('show');
            $('#idpembelajaran').val(id);


            getPenilaian(idrombel, id); //

        });
        $('#tujuanTable').on('click', '.capaian-btn', function() {
            var id = $(this).data('id');
            var idmapel = $(this).data('mapel');
            var idrombel = $(this).data('rombel');
            $('#modalcapaian').modal('show');
            $('#idpem').val(id);
            $('#idrombel').val(idrombel);
            getCapaian(id);


        });
        $('#inputrow-container').on('click', '.delete-tujuan', function() {
            var id = $(this).data('id');
            deleteTujuan(id);
        });
    });
    var save_method; //for save method string
    var table;

    function getCapaian(id) {
        $.ajax({
            url: '<?= base_url('penilaian/akhir/getcapaian/') ?>' + id, // Ubah ini sesuai dengan URL endpoint Anda
            type: 'GET',
            dataType: "json",
            success: function(data) {
                // Data yang diterima adalah dalam format JSON
                // Anda perlu menguraikan data dan mengisinya ke dalam tabel
                var tbody = $('#tabel-capaian tbody');
                $("#alertcapaian").empty();
                tbody.empty();
                if (data.siswa.length == 0) {

                    var alertElement = $('<div>').addClass('alert alert-info').attr('role', 'alert').text('Harap isikan Nilai terlebih dahulu');
                    return $('#alertcapaian').append(alertElement);
                }

                $.each(data.siswa, function(index, item) {
                    var no = index + 1;
                    var nilaiInputValue = item.nilai_akhir !== null ? item.nilai_akhir : "";
                    var tujuan_tercapai = item.tujuan_tercapai.split(',');
                    var tujuan_belum = item.tujuan_belum.split(',');
                    var tujuan_tercapai_text = "";
                    var tujuan_belum_text = "";
                    $.each(tujuan_tercapai, function(index, id) {
                        var tujuan = data.tujuan.find(function(t) {
                            return t.id === id;
                        });
                        if (tujuan) {
                            tujuan_tercapai_text += tujuan_tercapai_text ? " dan " + tujuan.tujuan : "Menunjukkan penguasaan yang baik dalam " + tujuan.tujuan;
                        }
                    });

                    $.each(tujuan_belum, function(index, id) {
                        var tujuan = data.tujuan.find(function(t) {
                            return t.id === id;
                        });
                        if (tujuan) {
                            tujuan_belum_text += tujuan_belum_text ? " dan " + tujuan.tujuan : "Perlu ditingkatkan dalam " + tujuan.tujuan;
                        }
                    });

                    // Pengecekan deskripsi tercapai
                    var deskripsi_tercapai = item.deskripsi_tercapai !== "" ? item.deskripsi_tercapai : tujuan_tercapai_text;
                    // Pengecekan deskripsi belum tercapai
                    var deskripsi_belum = item.deskripsi_belum !== "" ? item.deskripsi_belum : tujuan_belum_text;
                    var iconsudah = '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-check-circle"><path d="M22 11.08V12a10 10 0 1 1-5.93-9.14"></path><polyline points="22 4 12 14.01 9 11.01"></polyline></svg>';
                    var iconwarning = '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-alert-circle"><circle cx="12" cy="12" r="10"></circle><line x1="12" y1="8" x2="12" y2="12"></line><line x1="12" y1="16" x2="12.01" y2="16"></line></svg>';
                    var icon;
                    if (item.deskripsi_belum == "" && item.deskripsi_tercapai == "") {
                        icon = iconwarning;
                    } else {
                        icon = iconsudah;
                    }
                    var row = '<tr>' +
                        '<td>' + no + '</td>' +
                        '<td>' + item.nama + '</td>' +
                        '<td><input type="text" data-id="' + item.siswa_id + '" style="width:80px" class="form-control form-control-sm" value="' + nilaiInputValue + '" disabled></td>' +
                        '<td><textarea class="form-control form-control-sm sudah_tercapai">' + deskripsi_tercapai + '</textarea></td>' +
                        '<td><textarea class="form-control form-control-sm belum_tercapai">' + deskripsi_belum + '</textarea></td>' +
                        '<td>' + icon + '</td>' +
                        '</tr>';

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

    function getPenilaian(idrombel, id) {
        $.ajax({
            url: '<?= base_url('penilaian/akhir/getsiswa/') ?>' + idrombel + '/' + id, // Ubah ini sesuai dengan URL endpoint Anda
            type: 'GET',
            dataType: "json",
            success: function(data) {
                // Data yang diterima adalah dalam format JSON
                // Anda perlu menguraikan data dan mengisinya ke dalam tabel
                var tabelSiswa = $('#tabel-siswa tbody');
                $("#alert").empty();
                tabelSiswa.empty();

                if (data.tujuan.length == 0) {

                    var alertElement = $('<div>').addClass('alert alert-info').attr('role', 'alert').text('Harap isikan Tujuan pembelajaran terlebih dahulu');
                    return $('#alert').append(alertElement);
                }
                $.each(data.siswa, function(index, item) {
                    var no = index + 1; // Menghitung nomor otomatis
                    var nilaiInputValue = item.nilai_akhir !== null ? item.nilai_akhir : "";
                    var row = '<tr class="dataRow">' +
                        '<td>' + no + '</td>' +
                        '<td>' + item.nama + '</td>' +
                        '<td><input type="text" data-id="' + item.siswa_id + '" style="width:80px" class="form-control form-control-sm input-nilai" name="nilai[]" value="' + nilaiInputValue + '" required></td>' +
                        '<td><div class="tujuan-container" data-siswa="' + item.siswa_id + '"></div></td>' + // Menambahkan data-siswa ke tujuan-container
                        '</tr>';
                    tabelSiswa.append(row); // Tambahkan baris ke tabel

                    var tujuanContainer = $('.tujuan-container[data-siswa="' + item.siswa_id + '"]');
                    // Iterasi melalui data tujuan
                    $.each(data.tujuan, function(index, tujuan) {
                        var switchElement = $('<div>').addClass('switch form-switch-custom switch-inline mb-2 form-switch-success');
                        var input = $('<input>').addClass('switch-input').attr({
                            'type': 'checkbox',
                            'role': 'switch',
                            'value': tujuan.id,
                            'data-id': tujuan.id,
                            'data-idsiswa': item.siswa_id,
                        });

                        // Periksa apakah tujuan ini sudah tercapai atau belum
                        var isTercapai = item.tujuan_tercapai ? item.tujuan_tercapai.split(',').includes(tujuan.id.toString()) : false;
                        var isBelum = item.tujuan_belum ? item.tujuan_belum.split(',').includes(tujuan.id.toString()) : false;
                        // Tentukan apakah switch harus dicentang atau tidak
                        input.prop('checked', isTercapai);
                        var label = $('<label>').addClass('switch-label').attr('for', 'tujuan-switch-' + tujuan.id).text(tujuan.tujuan);

                        // Tambahkan input dan label ke dalam switch
                        switchElement.append(input, label);
                        // Tambahkan gaya display: block ke elemen switch
                        switchElement.css('display', 'block');
                        // Tambahkan switch ke dalam kontainer tujuan
                        tujuanContainer.append(switchElement);
                    });
                });
            },
            error: function(xhr, status, error) {
                console.error(status, error);
                // Tindakan yang sesuai jika ada kesalahan dalam pengambilan data
            }
        });
    }



    function deleteTujuan(Id) {
        var idpembelajaran = $('#idpembelajaran').val();
        Swal.fire({
            title: 'Are you sure?',
            text: "Hati hati saat menghapus data ini akan menyebabkan semua yang terkait hilang!",
            icon: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: 'Yes, delete it!'
        }).then((result) => {
            if (result.isConfirmed) {
                $.ajax({
                    url: '<?= base_url('referensi/tujuan/delete/') ?>' + Id, // Sesuaikan dengan URL Controller Anda
                    type: 'DELETE',
                    success: function(response) {
                        Snackbar.show({
                            text: 'berhasil dihapus',
                            pos: 'top-center'
                        });
                        gettujuan(idpembelajaran);
                    },
                    error: function(error) {
                        console.error('Error deleting user:', error);
                    }
                });
            }
        })
    }
</script>
<?= $this->endSection(); ?>