(function () { 'use strict'; angular .module('app') .controller('UserMainController', userMain); function userMain($scope, $state, $http, $location, tableSearcher, userRepository, errorHandler, uiNotifications, userModels, $translate, localizationHelper) { /* jshint validthis:true */ var vm = this; vm.translationObject = {}; vm.loadTranslation = function(){ $translate(['Info', 'Success', 'Warning', 'Error', 'ClientLocked', 'ClientUnlocked']).then(function (tr) { vm.translationObject = tr; }); }; vm.unsubscribe = localizationHelper.eventEmitter.subscribe('onLanguageChangedEvent', function (lang) { vm.loadTranslation(); }); vm.loadTranslation(); vm.filters = new userModels.UsersFilters(); // Variables. vm.data = []; vm.searchValue = ""; vm.orderBy = "firstName"; vm.orderDirection = "desc"; vm.sorterIcon = ""; vm.status = +$state.params.status; vm.method = vm.status == 0 ? "getActive" : "getBlocked"; // Methods. vm.search = function (query, pageNumber, pageSize) { vm.filters.search = vm.searchValue ? encodeURIComponent(vm.searchValue) : vm.searchValue; vm.filters.orderBy = vm.orderBy; vm.filters.orderDirection = vm.orderDirection; userRepository[vm.method](pageNumber, pageSize, vm.filters).success(function (response, status, headers, config) { if (errorHandler.check(response, status, config) == errorHandler.STATUS.SUCCESS) { if (response && response.data) { tableSearcher.define(response ? response.data : []); $scope.bigTotalItems = JSON.parse(headers("X-Pagination")).TotalCount; vm.filteredData = tableSearcher.getData(); } else { tableSearcher.define([]); $scope.bigTotalItems = 0; vm.filteredData = tableSearcher.getData(); } } }); }; vm.searchByPhoneNumber = function(phoneNumber){ vm.searchValue = phoneNumber; vm.search(); } vm.getUsers = function () { vm.search(vm.searchValue, $scope.bigCurrentPage, $scope.itemsPerPage); }; vm.openUserProfile = function (id) { appLocalStorage.delete("currentTabUrl"); $state.go('users.profile', {"id": id}); }; vm.sortBy = function(key){ $scope.bigCurrentPage = 1; tableSearcher.sortBy(key, vm); }; vm.keyPress = function ($event) { tableSearcher.keypress($event, vm.getUsers); }; vm.phoneNumberConfirmationStatus = function(isPhoneNumberConfirmed){ return isPhoneNumberConfirmed ? "Confirmed" : "Pending"; } vm.updateUserStatus = function (id, lockedStatus) { userRepository[lockedStatus + "ById"](id).success(function (response, status, headers, config) { if (errorHandler.check(response, status, config) == errorHandler.STATUS.SUCCESS) { if (lockedStatus == "lock") { uiNotifications.inform(vm.translationObject['ClientLocked'], 'info'); } else { uiNotifications.inform(vm.translationObject['ClientUnlocked'], 'success'); } $state.reload(); } }); }; $scope.bigCurrentPage = tableSearcher.CONST.CURRENT_PAGE; $scope.itemsPerPage = tableSearcher.CONST.ITEMS_PER_PAGE; $scope.$watch('bigCurrentPage + itemsPerPage', function () { vm.getUsers(); }); $scope.$on("$destroy", function(){ vm.unsubscribe(); }); } // IoC container. userMain.$inject = [ "$scope", '$state', '$http', '$location', "appUtils.tableSearcher", "repository.user", "appUtils.errorHandler", "appUtils.uiNotifications", "appModels.userModels", "$translate", "helpers.localization" ]; })();