var AjaxMethod;
(function (AjaxMethod) {
    AjaxMethod["POST"] = "POST";
    AjaxMethod["GET"] = "GET";
})(AjaxMethod || (AjaxMethod = {}));
var AjaxOptions = (function () {
    function AjaxOptions(fileds) {
        if (fileds) {
            Object.assign(this, fileds);
        }
    }
    return AjaxOptions;
}());
var AjaxService = (function () {
    function AjaxService() {
    }
    AjaxService.prototype.send = function (options) {
        var that = this;
        var call = $.ajax({
            url: options.url,
            type: options.method,
            data: options.data,
            success: function (d) {
                if (options.callback) {
                    options.callback(d);
                }
                if (options.successMsg) {
                    DevExpress.ui.notify(options.successMsg, "success", 5000);
                }
            },
            error: function (d) {
                if (options.errorCallback) {
                    options.errorCallback(d);
                }
                if (options.errorMsg) {
                    DevExpress.ui.notify(options.errorMsg, "error", 5000);
                }
            }
        });
        return call;
    };
    return AjaxService;
}());

var BaseView = (function () {
    function BaseView() {
        this.BindLayoutMenu();
        this.LogDbCallsErrors();
    }
    BaseView.prototype.BindLayoutMenu = function () {
        if (document.location.href.indexOf('Scheduler') <= -1 && (document.location.href.indexOf('Reports') > -1 || document.location.href.indexOf('Visualize') > -1)) {
            $('#reportsMenuDx').addClass('active');
            $('#usersMenuDx').removeClass('active');
            $('#logsMenuDx').removeClass('active');
            if (this.IsDefined($('#scheduleMenuDx'))) {
                $('#scheduleMenuDx').removeClass('active');
            }
        }
        else if (document.location.href.indexOf('Management') > -1 ||
            document.location.href.indexOf('ConnectionProfiles') > -1 ||
            document.location.href.indexOf('Settings') > -1) {
            $('#reportsMenuDx').removeClass('active');
            $('#usersMenuDx').addClass('active');
            $('#logsMenuDx').removeClass('active');
            if (this.IsDefined($('#scheduleMenuDx'))) {
                $('#scheduleMenuDx').removeClass('active');
            }
        }
        else if (document.location.href.indexOf('Logs') > -1) {
            $('#reportsMenuDx').removeClass('active');
            $('#usersMenuDx').removeClass('active');
            $('#logsMenuDx').addClass('active');
            if (this.IsDefined($('#scheduleMenuDx'))) {
                $('#scheduleMenuDx').removeClass('active');
            }
        }
        else if (document.location.href.indexOf('Scheduler') > -1) {
            $('#reportsMenuDx').removeClass('active');
            $('#usersMenuDx').removeClass('active');
            $('#logsMenuDx').removeClass('active');
            if (this.IsDefined($('#scheduleMenuDx'))) {
                $('#scheduleMenuDx').addClass('active');
            }
        }
        $('#reportsMenuDx').on("click", function (e) {
            document.location.href = "/Reports/Index";
        });
        $('#usersMenuDx').on("click", function (e) {
            document.location.href = "/Management/Index";
        });
        $('#logsMenuDx').on("click", function (e) {
            document.location.href = "/Logs/Index";
        });
        if (this.IsDefined($('#scheduleMenuDx'))) {
            $('#scheduleMenuDx').on("click", function (e) {
                document.location.href = "/Reports/Scheduler";
            });
        }
    };
    BaseView.prototype.IsNotEmpty = function (obj) {
        return this.IsDefined(obj) && obj !== "" && obj !== " ";
    };
    BaseView.prototype.IsDefined = function (obj) {
        return typeof obj !== 'undefined' && obj !== null;
    };
    BaseView.prototype.LogDbCallsErrors = function () {
        var that = this;
        DevExpress.data.setErrorHandler(function (error) {
            that.StatusCheck().done(function () {
                DevExpress.ui.notify("Error occured: " + error.message, "error", 5500);
            });
        });
    };
    BaseView.prototype.StatusCheck = function () {
        return $.ajax({
            url: '/Visualize/StatusCheck',
            type: "GET",
            statusCode: {
                401: function () {
                    window.location.reload();
                }
            }
        });
    };
    BaseView.prototype.LogError = function (msg, time, height) {
        DevExpress.ui.notify({ message: msg, height: height }, "error", time != null ? time : 5500);
    };
    BaseView.prototype.Info = function (msg, time, height) {
        DevExpress.ui.notify({ message: msg, height: height }, "info", this.IsDefined(time) ? time : 5500);
    };
    BaseView.prototype.Loading = function (text) {
        var loadingPanel = $('#wrLoadingPanel').dxLoadPanel("instance");
        return loadingPanel;
    };
    BaseView.prototype.IsMobile = function () {
        var currentWidth = $(window).width();
        return currentWidth < 992;
    };
    return BaseView;
}());
document.addEventListener("DOMContentLoaded", function documentReady() {
    this.removeEventListener("DOMContentLoaded", documentReady);
    var view = new BaseView();
});

var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        if (typeof b !== "function" && b !== null)
            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var TenantsRegistrationView = (function (_super) {
    __extends(TenantsRegistrationView, _super);
    function TenantsRegistrationView() {
        var _this = _super.call(this) || this;
        _this.callController = "/Settings";
        _this.tenantGridSelectedIndex = -1;
        _this.ajaxService = new AjaxService();
        return _this;
    }
    TenantsRegistrationView.prototype.UpdateTenantRegistration = function (e) {
        var that = this;
        var result = e.validationGroup.validate();
        if (result.isValid) {
            var tenant = this.GetCurrentViewModel();
            if (tenant) {
                var callUrl = this.callController + '/UpdateTenantRegistration';
                var data = {
                    __RequestVerificationToken: $('input[name="__RequestVerificationToken"]').val(),
                    viewModel: tenant
                };
                var callOptions = new AjaxOptions({
                    url: callUrl,
                    method: AjaxMethod.POST,
                    data: data,
                    callback: function (response) {
                        $("#tenantsGridDx").dxDataGrid("instance").refresh();
                        $('#tenantPopupDx').dxPopup('instance').hide();
                    },
                    errorCallback: function (response) {
                        console.log(response);
                        DevExpress.ui.notify(response.responseText, "error", 6000);
                    },
                    successMsg: "Tenant registered successfully"
                });
                this.ajaxService.send(callOptions);
            }
        }
    };
    TenantsRegistrationView.prototype.GetCurrentViewModel = function () {
        return {
            Id: this.IsNotEmpty($('#IdDx').val()) ? parseInt($('#IdDx').val().toString()) : null,
            CompanyId: $('#CompanyIdDx').dxTextBox('instance').option("value"),
            LicenseKey: $('#LicenseKeyDx').dxTextBox('instance').option("value"),
            RegistrationPW: $('#RegistrationPWDx').dxTextBox('instance').option("value"),
            EmailDomain: $('#EmailDomainDx').dxTextArea('instance').option("value"),
            UniqueID: this.IsNotEmpty($('#UniqueIDDx').val()) ? $('#UniqueIDDx').val() : null,
        };
    };
    TenantsRegistrationView.prototype.DeleteTenantRegistration = function (e) {
        var that = this;
        if (this.tenantGridSelectedIndex >= 0) {
            var result = DevExpress.ui.dialog.confirm("<strong>Are you sure? <br/>Note: All users associated with the Tenant's domain, and all associated user data, will be deleted!</strong>", "Delete");
            result.done(function (dialogResult) {
                if (dialogResult) {
                    var callUrl = that.callController + '/DeleteTenantRegistration';
                    var data = {
                        __RequestVerificationToken: $('input[name="__RequestVerificationToken"]').val(),
                        id: $("#tenantsGridDx").dxDataGrid("instance").getSelectedRowsData()[0].Id
                    };
                    var callOptions = new AjaxOptions({
                        url: callUrl,
                        method: AjaxMethod.POST,
                        data: data,
                        callback: function (response) {
                            that.tenantGridSelectedIndex = -1;
                            $("#tenantsGridDx").dxDataGrid("instance").deselectAll();
                            $("#tenantsGridDx").dxDataGrid("instance").refresh();
                        },
                        errorMsg: "Error occurs during operation"
                    });
                    that.ajaxService.send(callOptions);
                }
            });
        }
    };
    TenantsRegistrationView.prototype.EditTenant = function (e) {
        var that = this;
        var callUrl = that.callController + '/EditTenant';
        var data = {
            __RequestVerificationToken: $('input[name="__RequestVerificationToken"]').val(),
            id: $("#tenantsGridDx").dxDataGrid("instance").getSelectedRowsData()[0].Id
        };
        var callOptions = new AjaxOptions({
            url: callUrl,
            method: AjaxMethod.POST,
            data: data,
            callback: function (response) { that.PopupForm.call(that, response); },
            errorMsg: "Error occurs during operation"
        });
        that.ajaxService.send(callOptions);
    };
    TenantsRegistrationView.prototype.AddNewTenant = function () {
        var that = this;
        var callUrl = this.callController + '/AddNewTenant';
        var data = {
            __RequestVerificationToken: $('input[name="__RequestVerificationToken"]').val(),
        };
        var callOptions = new AjaxOptions({
            url: callUrl,
            method: AjaxMethod.POST,
            data: data,
            callback: function (response) { that.PopupForm.call(that, response); },
            errorMsg: "Error occurs during operation"
        });
        this.ajaxService.send(callOptions);
    };
    TenantsRegistrationView.prototype.TenantsGridOnSelectionChanged = function (e) {
        if (e.selectedRowKeys && e.selectedRowKeys[0]) {
            $("#updateTenantDx").dxButton("instance").option("disabled", false);
            $("#deleteTenantDx").dxButton("instance").option("disabled", false);
            var key = e.selectedRowKeys[0];
            var rowIndex = $("#tenantsGridDx").dxDataGrid("instance").getRowIndexByKey(key);
            this.tenantGridSelectedIndex = rowIndex;
        }
        else {
            $("#updateTenantDx").dxButton("instance").option("disabled", true);
            $("#deleteTenantDx").dxButton("instance").option("disabled", true);
            this.tenantGridSelectedIndex = -1;
        }
    };
    TenantsRegistrationView.prototype.PopupForm = function (content, show) {
        if (show === void 0) { show = true; }
        var popup = this.GetPopup();
        if (this.IsMobile()) {
            popup.option('width', '700px');
            popup.option('height', '370px');
        }
        popup.option('contentTemplate', null);
        popup.option('contentTemplate', $(content));
        popup.show();
    };
    TenantsRegistrationView.prototype.GetPopup = function () {
        var popup = $("#tenantPopupDx").dxPopup("instance");
        return popup;
    };
    return TenantsRegistrationView;
}(BaseView));
var View = window.View || {};
document.addEventListener("DOMContentLoaded", function documentReady() {
    this.removeEventListener("DOMContentLoaded", documentReady);
    View = window.View = new TenantsRegistrationView();
});
