$(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 absensi
    function loadData() {
        var id = $('.id').val();
        $.ajax({
            url: 'mod/absensi/proses.php?action=absensi&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/absensi/proses.php?action=absensi&id=' + id,
            method: "POST",
            data: { month: month, year: year },
            dataType: "text",
            cache: false,
            async: false,
            beforeSend: function () {
                // loading();
            },
            success: function (data) {
                $('.loaddata').html(data);
            }
        });
    });

    // Inisialisasi SimpleLightbox
    (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 === '')
                ? "./absensi/print?action=pdfid=" + id
                : "./absensi/print?action=pdf&id=" + id + "&from=" + month + "&to=" + year;
        }

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

        if (type === 'print') {
            url = "./absensi/print?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 = "./absensi/print?action=allpdf&from=" + month + "&to=" + year;
        }

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

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

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

// Modal lokasi absensi
$(document).on('click', '.btn-modal', function () {
    $('#modal-location').modal();

    var latitude  = $(this).attr("data-latitude");
    var longitude = $(this).attr("data-longitude");
    var name      = $('.employees_name').html();

    $(".modal-title-name").html(name);
    $("#iframe-map").html(
        '<iframe src="mod/absensi/map.php?latitude=' + latitude + '&longitude=' + longitude + '&name=' + name + '" ' +
        'frameborder="0" width="100%" height="400px" marginwidth="0" marginheight="0" scrolling="no"></iframe>'
    );
});
