$(function () {
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });

    var table = $('.data-table').DataTable({
        processing: true,
        serverSide: true,
        responsive: true,
        ajax: url,
        columns: [
            { data: 'DT_RowIndex', name: 'DT_RowIndex' },
            { data: 'category', name: 'category' },
            { data: 'file', name: 'file' },
            { data: 'status', name: 'status' },
            { data: 'downloaded', name: 'downloaded' },
            { data: 'action', name: 'action', orderable: false, searchable: false },
        ],
        columnDefs: [
            { className: 'text-center', targets: [0, 3, 4, 5]},
            { className: 'text-left', targets: [1, 2]},
        ],
    });

    $("body").on("click", ".delete", function (e) {
        e.preventDefault();
        var id = $(this).data('id');

        swal({
            title: "Apakah Anda Yakin?",
            text: "Anda akan menghapus data ini!",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "Yes",
            cancelButtonText: "No",
            closeOnConfirm: false,
            closeOnCancel: false
        },
            function (isConfirm) {
                if (isConfirm) {
                    swal.close();
                    setTimeout(function () {
                        $.ajax({
                            dataType: 'json',
                            type: 'DELETE',
                            url: url + '/' + id,
                            headers: {
                                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                            },
                        }).done(function (data) {
                            table.draw();
                            swal({
                                title: "Data berhasil dihapus!",
                                type: "success",
                                timer: "3000"
                            });
                        });
                    }, 1000); // 1 second delay
                }
                else {
                    swal("Dibatalkan", "Data batal dihapus", "error");
                }
            }
        );
    });

});

$(function () {
    //iCheck for checkbox and radio inputs
    $('input[type="checkbox"].minimal, input[type="radio"].minimal').iCheck({
        checkboxClass: 'icheckbox_minimal-blue',
        radioClass: 'iradio_minimal-blue'
    });
});

function printErrorMsg(msg) {
    $(".print-error-msg").find("ul").html('');
    $(".print-error-msg").css('display', 'block');
    $.each(msg, function (key, value) {
        $(".print-error-msg").find("ul").append('<li>' + value + '</li>');
    });
}

function ValidateFile(oInput) {
    var _validFileExtensions = [".jpg", ".jpeg", ".png", ".doc", ".docx", ".pdf", ".xls", ".xlsx", ".ppt", ".pptx", ".mp3", ".mp4", ".mkv", "mpeg"];
    if (oInput.type == "file") {
        var sFileName = oInput.value;
        if (sFileName.length > 0) {
            var blnValid = false;
            for (var j = 0; j < _validFileExtensions.length; j++) {
                var sCurExtension = _validFileExtensions[j];
                if (sFileName.substr(sFileName.length - sCurExtension.length, sCurExtension.length).toLowerCase() == sCurExtension.toLowerCase()) {
                    blnValid = true;
                    pdf = true;
                    break;
                }
            }

            if (!blnValid) {
                swal("Oops...", "Ekstensi File tidak diperbolehkan", "error");
                oInput.value = "";
                pdf = false;
                return false;
            }
        }
    }
    return true;
}

function checkFileSizeFile(inputFile) {
    var max = 3145728; // 3MB

    if (inputFile.files && inputFile.files[0].size > max) {
        swal("Oops...", "File terlalu besar (lebih dari 3MB) ! Mohon kompres/perkecil ukuran file", "error");
        inputFile.value = null; // Clear the field.
    }
}

function copyToClipboard(element) {
    var $temp = $("<input>");
    $("body").append($temp);
    $temp.val($(element).html()).select();
    document.execCommand("copy");
    $temp.remove();

    swal("Copied!", "", "success");
}