$(function(){
    let openEditorForPage = parseInt($('#inpOpenEditorForPage').val());
    if (!isNaN(openEditorForPage) && openEditorForPage > 0){
        $('#inpOpenEditorForPage').val(0);
        openEditor(openEditorForPage);
    }
    $('[data-toggle="tooltip"]').tooltip();
});

function openModalAddPage(){
    $('#inpModalAddEditPageIdPage').val(0);
    $('#inpModalAddEditPageName').val('');
    $('#divModalAddEditPage').modal('show');
}


function openModalEditPage(el){
    let idBulletinBoardPage = parseInt($(el).parent().parent().attr('data-id'));
    if (isNaN(idBulletinBoardPage)){
        Swal.fire('Napaka ni id strani!', '', 'error');
        return;
    }
    $.blockUI();
    $.ajax({
        type: "GET",
        url: "/Pages/Index/?handler=Page",
        data: {
            id: idBulletinBoardPage
        },
        success: function (data) {
            if (data.successful){
                $('#inpModalAddEditPageIdPage').val(data.page.idBulletinBoardPage);
                $('#inpModalAddEditPageName').val(data.page.name);
                $('#selModalAddEditPageRatio').val(data.page.ratio).trigger('change');
                $('#selModalAddEditPageType').val(data.page.type).trigger('change');
                $('#divModalAddEditPage').modal('show');
            } else {
                Swal.fire('Napaka', data.error, 'error');
                console.log(data);
            }
            $.unblockUI();
        },
        error: function (xhr, ajaxOptions, thrownError) {
            console.log(xhr);
            alert(xhr.responseText);
            $.unblockUI();
        }
    });
}

function saveModalAddEditPage(){
    let name = $('#inpModalAddEditPageName').val();
    let ratio = parseInt($('#selModalAddEditPageRatio').val());
    let type = parseInt($('#selModalAddEditPageType').val());
    let idBulletinBoardPage = parseInt($('#inpModalAddEditPageIdPage').val());
    if (name === ''){
        Swal.fire('Ime ni vneseno', '', 'warning');
        return;
    }

    if (isNaN(ratio)){
        Swal.fire('Razmerje ni vneseno', '', 'warning');
        return;
    }

    if (isNaN(type)){
        Swal.fire('Tip ni vnesen', '', 'warning');
        return;
    }

    $.blockUI();
    $.ajax({
        type: "POST",
        beforeSend: function(xhr) {
            xhr.setRequestHeader("XSRF-TOKEN",
                $('input:hidden[name="__RequestVerificationToken"]').val());
        },
        url: "/Pages/Index/?handler=BoardPage",
        data: { page : {
            idBulletinBoardPage,
            name,
            ratio,
            type
        }},
        success: function(data) {
            if (data.successful){
                $('#divModalAddEditPage').modal('hide');
                if (idBulletinBoardPage > 0){
                    //Ce je edit ne odprem editorja
                    $('#inpOpenEditorForPage').val(0);
                } else {
                    $('#inpOpenEditorForPage').val(data.page.idBulletinBoardPage);
                }
                $('#btnRefresh').trigger('click');
            } else {
                Swal.fire('Napaka', data.error, 'error');
                console.log(data);
            }
            $.unblockUI();
        },
        error: function (xhr, ajaxOptions, thrownError) {
            alert(xhr.responseText);
            $.unblockUI();
        }
    });
}

function rowOpenEditor(el){
    openEditor(parseInt($(el).parent().parent().attr('data-id')));
}

function openEditor(id){
    $.blockUI();
    $.ajax({
        type: "GET",
        url: "/Pages/Index/?handler=Page",
        data: {
            id
        },
        success: function (data) {
            if (data.successful){
                switch (data.page.type){
                    case 1: //Editor notes
                        window.location.href = "/Pages/EditMain?idPage=" + data.page.idBulletinBoardPage;
                        break;
                    case 2: //Picture
                        $('#inpModalPageEditorPictureIdPage').val(data.page.idBulletinBoardPage);
                        $('#imgModalPageEditorPicture').attr("src","/bulletin-board-images/pages/" + data.page.image);
                        $('#divModalPageEditorPicture').modal('show');
                        break;
                    case 3: //Link
                        $('#inpModalPageEditorLinkIdPage').val(data.page.idBulletinBoardPage);
                        $('#inpModalPageEditorLinkLink').val(data.page.externalLink);
                        $('#divModalPageEditorLink').modal('show');
                        break;
                }
            } else {
                Swal.fire('Napaka', data.error, 'error');
                console.log(data);
            }
            $.unblockUI();
        },
        error: function (xhr, ajaxOptions, thrownError) {
            console.log(xhr);
            alert(xhr.responseText);
            $.unblockUI();
        }
    });
}

function saveModalPageEditorLink(){
    let link = $('#inpModalPageEditorLinkLink').val();
    let idPage = parseInt($('#inpModalPageEditorLinkIdPage').val());

    $.blockUI();
    $.ajax({
        type: "POST",
        beforeSend: function(xhr) {
            xhr.setRequestHeader("XSRF-TOKEN",
                $('input:hidden[name="__RequestVerificationToken"]').val());
        },
        url: "/Pages/Index/?handler=BoardPageLink",
        data: { idPage, link},
        success: function(data) {
            if (data.successful){
                $('#divModalAddEditPage').modal('hide');
                $('#btnRefresh').trigger('click');
            } else {
                Swal.fire('Napaka', data.error, 'error');
                console.log(data);
            }
            $.unblockUI();
        },
        error: function (xhr, ajaxOptions, thrownError) {
            alert(xhr.responseText);
            $.unblockUI();
        }
    });
}

function saveModalPageEditorPicture(){
    let idPage = parseInt($('#inpModalPageEditorPictureIdPage').val());

    let files = $('#inpModalPageEditorPicturePicture').prop("files"); 
	let formData = new FormData();
	formData.append("file", files[0]);
	formData.append("idPage", idPage);

    $.blockUI();
    $.ajax({
        type: "POST",
        beforeSend: function(xhr) {
            xhr.setRequestHeader("XSRF-TOKEN",
                $('input:hidden[name="__RequestVerificationToken"]').val());
        },
        url: "/Pages/Index/?handler=BoardPagePicture",
        data: formData,
        cache: false,
		contentType: false,
		processData: false,
        success: function(data) {
            if (data.successful){
                $('#divModalPageEditorPicture').modal('hide');
                $('#btnRefresh').trigger('click');
            } else {
                Swal.fire('Napaka', data.error, 'error');
                console.log(data);
            }
            $.unblockUI();
        },
        error: function (xhr, ajaxOptions, thrownError) {
            alert(xhr.responseText);
            $.unblockUI();
        }
    });
}