$(document).ready(function () {
    var totalFiles = 0;
    var uploadedFiles = 0;
    var spinner = $('#upload-spinner');
    var errorMsg = $('#error-msg');

    $('#fileupload').fileupload({
        previewMaxHeight: 80,
        previewMaxWidth: 120,
        url: 'https://api.imgur.com/3/image',
        type: 'POST',
        headers: {
            Authorization: 'Client-ID 9e57cb1c4791cea'
        },
        dataType: 'json',
        singleFileUploads: true,
        autoUpload: true,
        disableImageResize: /Android(?!.*Chrome)|Opera/.test(window.navigator.userAgent),
        maxFileSize: 999000,
        acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
        add: function (e, data) {
            spinner.show();
            data.formData = {
                image: data.files[0]
            };
            data.submit();
        },
        done: function (e, data) {
            var fileUrl = data.result.data.link;
            $.ajax({
                url: './includes/post_smarters_pro_banners.php',
                type: 'POST',
                data: {
                    post_images: 1,
                    fileUrl: fileUrl
                },
                success: function (response) {
                    uploadedFiles++;
                    if (uploadedFiles === totalFiles) {
                        location.reload();
                    }
                }
            });
            spinner.hide();
        },
        fail: function (e, data) {
            errorMsg.html('Upload failed: ' + data.errorThrown).removeClass('d-none');
            spinner.hide();
        }
    });

    $('#fileupload').on('fileuploadadd', function (e, data) {
        totalFiles++;
    });

    $('#fileupload').on('fileuploadchange', function (e, data) {
        $('#fileupload .empty-row').hide();
    });

    $('#fileupload').on('fileuploadfail', function (e, data) {
        if (data.errorThrown === 'abort') {
            if ($('#fileupload .files tr').not('.empty-row').length == 1) {
                $('#fileupload .empty-row').show();
            }
        }
    });

    if ($.support.cors) {
        $.ajax({
            url: 'https://api.imgur.com/3/image/0r65LVT',
            type: 'GET',
            headers: {
                'Authorization': 'Client-ID 9e57cb1c4791cea'
            },
            success: function (response) {
                console.log('Imgur API is up:', response);
            },
            error: function () {
                var alert = '<div class="alert alert-danger m-b-0 m-t-15">Imgur API server currently unavailable - ' + new Date() + '</div>';
                errorMsg.removeClass('d-none').html(alert);
            }
        });
    }
});

function viewImage(url) {
    document.getElementById('modalImage').src = url;
    $('#imageModal').modal('show');
}

function deleteImage(id) {
    var result = confirm('Are you sure you want to delete this image?');
    if (result) {
        $.ajax({
            url: './includes/post_smarters_pro_banners.php',
            type: 'POST',
            data: {
                delete_image: 1,
                id: id
            },
            success: function (response) {
                location.reload();
            }
        });
    }
}

$(function () {
    $("#post_announcement").on("submit", function (e) {
        e.preventDefault();
        var formData = new FormData(this);
        formData.append('post_announcement', '1');
        $.ajax({
            type: "post",
            url: "./includes/post_smarters_pro_banners.php",
            data: formData,
            processData: false,
            contentType: false,
            success: function () {
                location.reload();
            },
        });
    });
    $("#post_message").on("submit", function (e) {
        e.preventDefault();
        var formData = new FormData(this);
        formData.append('post_message', '1');
        $.ajax({
            type: "post",
            url: "./includes/post_smarters_pro_banners.php",
            data: formData,
            processData: false,
            contentType: false,
            success: function () {
                location.reload();
            },
        });
    });
});

function deleteAnnouncement(id) {
    var result = confirm('Are you sure you want to delete this announcement?');
    if (result) {
        $.ajax({
            url: './includes/post_smarters_pro_banners.php',
            type: 'POST',
            data: {
                delete_announcement: 1,
                id: id
            },
            success: function (response) {
                location.reload();
            }
        });
    }
}

function deleteMessage(id) {
    var result = confirm('Are you sure you want to delete this advertisement?');
    if (result) {
        $.ajax({
            url: './includes/post_smarters_pro_banners.php',
            type: 'POST',
            data: {
                delete_message: 1,
                id: id
            },
            success: function (response) {
                location.reload();
            }
        });
    }
}
