$(document).ready(function () {
    // Inisialisasi DataTable
    $('#swdatatable').dataTable({
        iDisplayLength: 31,
        aLengthMenu: [[31, 50, 100, -1], [31, 50, 100, "Semua"]],
    });

    // Fungsi animasi loading
    function loading() {
        $(".loading").show();
        $(".loading").delay(1000).fadeOut(500);
    }

    // ====== Fungsi untuk memuat data ibadah per nama ======
    function loadData() {
        var nama = $('.nama').val(); // ambil nama jamaah aktif
        $.ajax({
            url: 'mod/ibadah/proses.php?action=ibadah&nama=' + encodeURIComponent(nama),
            type: 'POST',
            beforeSend: function () {
                loading();
            },
            success: function (data) {
                $('.loaddata').html(data);
            },
            error: function () {
                Swal.fire({
                    icon: 'error',
                    title: 'Gagal Memuat Data',
                    text: 'Tidak dapat mengambil data ibadah dari server.'
                });
            }
        });
    }

    // Load pertama kali saat halaman dibuka
    loadData();

    // ====== Tombol clear filter (reset bulan/tahun) ======
    $('.btn-clear').click(function () {
        $('.month').val('');
        $('.year').val('');
        loadData();
    });

    // ====== Tombol sortir data berdasarkan bulan & tahun ======
    $('.btn-sortir').click(function () {
        var nama  = $('.nama').val();
        var month = $('.month').val();
        var year  = $('.year').val();

        $.ajax({
            url: 'mod/ibadah/proses.php?action=ibadah&nama=' + encodeURIComponent(nama),
            method: "POST",
            data: { month: month, year: year },
            dataType: "text",
            cache: false,
            beforeSend: function () {
                loading();
            },
            success: function (data) {
                $('.loaddata').html(data);
            },
            error: function () {
                Swal.fire({
                    icon: 'error',
                    title: 'Kesalahan Server',
                    text: 'Tidak dapat memproses filter data ibadah.'
                });
            }
        });
    });

    // ====== Tombol print / export per individu ======
    $('.btn-print').click(function () {
        var nama  = $('.nama').val();
        var month = $('.month').val();
        var year  = $('.year').val();
        var type  = $(this).attr("data-id");
        var url   = "";

        if (type === 'pdf') {
            url = (month === '')
                ? "./ibadah/print?action=pdf&nama=" + encodeURIComponent(nama)
                : "./ibadah/print?action=pdf&nama=" + encodeURIComponent(nama) + "&from=" + month + "&to=" + year;
        }

        if (type === 'excel') {
            url = (month === '')
                ? "./ibadah/print?action=excel&nama=" + encodeURIComponent(nama)
                : "./ibadah/print?action=excel&nama=" + encodeURIComponent(nama) + "&from=" + month + "&to=" + year;
        }

        if (type === 'print') {
            url = "./ibadah/print?action=excel&nama=" + encodeURIComponent(nama) + "&from=" + month + "&to=" + year + "&print=print";
        }

        window.open(url, '_blank');
    });

    // ====== Tombol print / export semua data ibadah ======
    $('.btn-print-all').click(function () {
        var month = $('.month').val();
        var year  = $('.year').val();
        var type  = $('.type').val();
        var url   = "";

        if (type === 'pdf') {
            url = "./ibadah/print?action=allpdf&from=" + month + "&to=" + year;
        }

        if (type === 'excel') {
            url = "./ibadah/print?action=allexcel&from=" + month + "&to=" + year;
        }

        if (type === 'print') {
            url = "./ibadah/print?action=allexcel&from=" + month + "&to=" + year + "&print=print";
        }

        window.open(url, '_blank');
    });

    // ====== Tooltip bootstrap (opsional) ======
    $(function () {
        $('[data-toggle="tooltip"]').tooltip();
    });
});
