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

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

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

    // Load pertama kali
    loadData();

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

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

        $.ajax({
            url: 'mod/permintaan/proses.php?action=permintaan&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.'
                });
            }
        });
    });

    // Tombol print individual laporan permintaan
    $('.btn-print').click(function () {
        var nama  = $('.penanggung_jawab').val();
        var month = $('.month').val();
        var year  = $('.year').val();
        var type  = $(this).attr("data-id");
        var url   = "";

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

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

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

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

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

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

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

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

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

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