$(document).ready(function () {
});

$(document).ajaxError(function (event, jqxhr, settings, thrownError) {
    if (settings.url.startsWith(
        $("#UrlGetPAStoredProc").val(),
        $("#UrlGetPoslDog").val(),
        $("#UrlGetMailboxParams").val()),
        $("#UrlGetMnMailBoxDropdownValues").val()) {
        return;
    }
    location.reload();
});

$(window).load(function () {
    hideSelectSpan();
});

function deleteEparam(anQid) {
    var deleteText = $("#TxtDeleteQuestion").val();
    var deleteTitle = $("#TxtConfirmation").val();
    var confirmText = $("#TxtConfirm").val();
    var cancelText = $("#TxtCancel").val();

    $().customModal({
        destroyOnClose: true,
        title: deleteTitle,
        showCloseButton: true,
        help: function () {

        },
        id: "modalDeleteEparam",
        type: "question",
        text: deleteText,
        buttons: [{
            icon: 'fa fa-times',
            text: cancelText,
            action: function () {

            },
            closeOnAction: true,
            type: 'ghost'
        },
        {
            icon: 'fa fa-check',
            text: confirmText,
            closeOnAction: true,
            action: async function () {
                await new Promise((resolve, reject) => setTimeout(function () {
                    removeEparam(anQid);
                    resolve();
                }, 1500));
            },
            closeOnAction: true,
            type: 'primary'
        }]
    });
}

function removeEparam(anQid) {
    $.ajax({
        url: $("#UrlDeleteEparam").val() + '?anQId=' + anQid,
        type: 'GET',
        success: function (data) {
            if (data && data.Status) {
                $("#tr-ep-" + anQid).remove();
            }
            else {
                $(document).ready(function () {
                    $().customModal({
                        destroyOnClose: true,
                        title: '',
                        showCloseButton: true,
                        help: function () {

                        },
                        id: "modalDeleteEparamNotPossible",
                        type: "alert",
                        text: $("#TxtDelNotPossible").val(),
                        buttons: [
                            {
                                icon: 'fa fa-check',
                                text: $("#TxtConfirm").val(),
                                closeOnAction: true,
                                type: 'primary'
                            }]
                    });
                });
            }
        }
    });
}

function setEparamData(anQid) {
    let sufix = anQid != 0 ? "-" + anQid : "";

    let text = $("#input-ep-data" + sufix).val();

    $("#select2-input-ep-data" + sufix + "-container").text(text);

    if (anQid) {
        saveEparam(anQid);
    }
}

function saveEparam(anQid, reload = true) {
    let sufix = anQid != 0 ? "-" + anQid : "";

    let data = {
        AnQid: anQid ? anQid : 0,
        SubjectId: $("#SubjectId").val(),
        MailboxQid: $("#input-ep-mailbox" + sufix).val(),
        DocType: $("#input-ep-doctype" + sufix).val(),
        Type: $("#input-ep-type" + sufix).val(),
        Data: $("#input-ep-data" + sufix).val(),
    };

    postEparam(data, reload);
}

function postEparam(data, reload) {
    $.ajax({
        url: $("#UrlSaveEparam").val(),
        type: 'POST',
        data: data,
        success: function (responseData) {
            if (responseData.Status == false && responseData.Message == data.DocType) {
                showPrimaryKeyExistModal();
            }
            if (reload) {
                location.reload();
            }
        }
    });
}

function deleteWebshop(anQid) {
    var deleteText = $("#TxtDeleteQuestion").val();
    var deleteTitle = $("#TxtConfirmation").val();
    var confirmText = $("#TxtConfirm").val();
    var cancelText = $("#TxtCancel").val();

    $().customModal({
        destroyOnClose: true,
        title: deleteTitle,
        showCloseButton: true,
        help: function () {

        },
        id: "modalDeleteWebshop",
        type: "question",
        text: deleteText,
        buttons: [{
            icon: 'fa fa-times',
            text: cancelText,
            action: function () {

            },
            closeOnAction: true,
            type: 'ghost'
        },
        {
            icon: 'fa fa-check',
            text: confirmText,
            closeOnAction: true,
            action: async function () {
                await new Promise((resolve, reject) => setTimeout(function () {
                    removeWebshop(anQid);
                    resolve();
                }, 1500));
            },
            closeOnAction: true,
            type: 'primary'
        }]
    });
}

function removeWebshop(anQid) {
    $.ajax({
        url: $("#UrlDeleteWebShop").val() + '?anQId=' + anQid,
        type: 'GET',
        success: function (data) {
            if (data && data.Status) {
                $("#tr-ws-" + anQid).remove();
            }
            else {
                $(document).ready(function () {
                    $().customModal({
                        destroyOnClose: true,
                        title: '',
                        showCloseButton: true,
                        help: function () {

                        },
                        id: "modalDeleteEparamNotPossible",
                        type: "alert",
                        text: $("#TxtDelNotPossible").val(),
                        buttons: [
                            {
                                icon: 'fa fa-check',
                                text: $("#TxtConfirm").val(),
                                closeOnAction: true,
                                type: 'primary'
                            }]
                    });
                });
            }
        }
    });
}

function saveWebshop(anQid, reload = true) {
    let sufix = anQid != 0 ? "-" + anQid : "";

    let data = {
        AnQid: anQid ? anQid : 0,
        SubjectId: $("#SubjectId").val(),
        MailboxQid: $("#input-ws-mailbox" + sufix).val(),
        Name: $("#input-ws-name" + sufix).val(),
        Description: $("#input-ws-description" + sufix).val(),
    };

    postWebshop(data, reload);
}

function postWebshop(data, reload) {
    $.ajax({
        url: $("#UrlSaveWebShop").val(),
        type: 'POST',
        data: data,
        success: function (data) {
            if (reload) {
                location.reload();
            }
        }
    });
}

function changeType(id, anQid) {
    let type = $("#" + id).val();
    let sufix = anQid != 0 ? "-" + anQid : "";

    if (type === $("#EPMailboxTypeC").val()) {
        if (!$("#input-ep-data" + sufix).is("input")) {
            let selectElement = $("#input-ep-data" + sufix);
            selectElement.prop("hidden", true);
            selectElement.removeAttr('id');
            selectElement.parent().children('span').addClass('hidden');

            $("#input-ep-data-input" + sufix).val("");
            $("#input-ep-data-input" + sufix).attr('type', 'text');
            $("#input-ep-data-input" + sufix).attr('id', "input-ep-data" + sufix);
        }
    }
    else {
        if ($("#input-ep-data" + sufix).is("input")) {
            let input = $("#input-ep-data" + sufix);
            input.attr('type', 'hidden');
            input.attr('id', "input-ep-data-input" + sufix);

            let selectElement = input.parent().children("select");
            selectElement.val("").change();
            selectElement.prop("hidden", false);
            selectElement.attr('id', "input-ep-data" + sufix);
            selectElement.parent().children('span').removeClass('hidden');
        }
        else {
            if ($("#input-ep-data" + sufix).val()) {
                let selectElement = $("#input-ep-data" + sufix);
                selectElement.removeAttr('id');
                selectElement.val("").change();
                selectElement.attr('id', "input-ep-data" + sufix);
            }
        }
        let url = $("#UrlGetPAStoredProc").val() + "?epType=" + type;
        $("#input-ep-data" + sufix).attr('data-url', url);
    }
}

function hideSelectSpan() {
    $(".input-ep-data-hidden-select").parent().children('span').addClass('hidden');
}

function showPrimaryKeyExistModal() {
    $().customModal({
        destroyOnClose: true,
        showCloseButton: true,
        type: "alert",
        text: $("#PrimaryKeyExists").val(),
        buttons: [
            {
                icon: 'fa fa-check',
                text: $("#TxtConfirm").val(),
                closeOnAction: true,
                type: 'primary'
            }]
    });
}