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

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

    // Fungsi untuk load data aktivitas
    function loadData() {
        var id = $('.id').val();
        $.ajax({
            url: 'mod/aktivitas/proses.php?action=aktivitas&id=' + id,
            type: 'POST',
            success: function (data) {
                $('.loaddata').html(data);
            }
        });
    }

    // Load pertama kali
    loadData();

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

    // Tombol sortir data berdasarkan bulan & tahun
    $('.btn-sortir').click(function () {
        var monthNames = [
            "January", "February", "March", "April", "May", "June",
            "July", "August", "September", "October", "November", "December"
        ];

        var id    = $('.id').val();
        var month = $('.month').val();
        var year  = $('.year').val();

        var d = new Date(month);
        var n = monthNames[d.getMonth()];
        $('.result-month').html(n);

        $.ajax({
            url: 'mod/aktivitas/proses.php?action=aktivitas&id=' + id,
            method: "POST",
            data: { month: month, year: year },
            dataType: "text",
            cache: false,
            async: false,
            success: function (data) {
                $('.loaddata').html(data);
            }
        });
    });

    // Inisialisasi SimpleLightbox untuk foto aktivitas
    (function () {
        new SimpleLightbox(".picture a", {});
    })();

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

        var url = "";

        if (type === 'pdf') {
            url = (month === '')
                ? "./aktivitas/print.php?action=pdfid=" + id
                : "./aktivitas/print.php?action=pdf&id=" + id + "&from=" + month + "&to=" + year;
        }

        if (type === 'excel') {
            url = (month === '')
                ? "./aktivitas/print.php?action=excelid=" + id
                : "./aktivitas/print.php?action=excel&id=" + id + "&from=" + month + "&to=" + year;
        }

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

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

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

        var url = "";

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

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

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

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

// Untuk aktivitas tidak ada lokasi (latitude/longitude), 
// jadi bagian modal map dihapus atau dinonaktifkan
