mainAppModule.controller('administrareUtilizatoriCtrl',function($scope,ajaxService){ $scope.info = { table : { isEmpty : true, items : null, totalItemsCount : null, filteredItemsCount : null, totalPageNum : null, crtPageNum : null, crtPageGroup : null, groupSelectOptions : null, pageSelectOptions : null, additInfo : null }, params : { type : null, info : { queryEntity : null, pageNum : null, pageGroup : null, addit : null } }, search : { cnp : "", mail : "", successMemento : null }, selectedItem : { info : null }, jsonRequestUrl : "", lastAction : "", status : { show : false, message : "" }, operationInfo : { ceItemTmpl : { isChangeEmail : true, newEmail : "", confNewEmail : "", btnDisabled : false, saveStatus : { code : null, message : null } }, daItemTmpl : { isDeactivateAcc : true, reason : "", btnDisabled : false, saveStatus : { code : null, message : null } }, crtItemIndex : null, lastAction : "" } }; $scope.template = { table : angular.copy($scope.info.table), params : angular.copy($scope.info.params), search : angular.copy($scope.info.search), selectedItem : angular.copy($scope.info.selectedItem), status : angular.copy($scope.info.status), ceItem : angular.copy($scope.info.operationInfo.ceItemTmpl), daItem : angular.copy($scope.info.operationInfo.daItemTmpl) }; $scope.formRetrTableInfoParams = function(type){ $scope.info.lastAction = type; $params = angular.copy($scope.template.params); $params.type = type; $params.info.queryEntity = $scope.info.search.successMemento; $params.info.pageNum = $scope.info.table.crtPageNum == null ? 1 : $scope.info.table.crtPageNum; $params.info.pageGroup = $scope.info.table.crtPageGroup == null ? 5 : $scope.info.table.crtPageGroup; $params.info = angular.toJson($params.info); return $params; }; $scope.retrieveItems = function(params){ ajaxService.sendXTR($scope.info.jsonRequestUrl,$.param(params)) .then(function(result){ $scope.info.table = result; $scope.info.operationInfo.crtItemIndex = null; }, function(status){ console.log("Eroare la request XHR trimis in functia 'retrieveItems' :"); console.log(status); }); }; $scope.onInitPage = function(){ $scope.retrieveItems($scope.formRetrTableInfoParams("onInitPage")); }; $scope.onChangePage = function(){ $scope.retrieveItems($scope.formRetrTableInfoParams("onChangePage")); }; $scope.onChangeGroup = function(){ $scope.retrieveItems($scope.formRetrTableInfoParams("onChangeGroup")); }; $scope.onSearch = function(event){ if(event.which == 13){ $search = $scope.info.search; if($search.cnp == "" && $search.mail == "") $scope.info.search.successMemento = null; else $scope.info.search.successMemento = { column : "utilizator.username", value : $search.mail, opInLink : null, opType : 1, lads : [{ column : "solicitant.identificatorUnic", value : $search.cnp, opInLink : "and", opType : 1, lads : [] }] }; $scope.info.selectedItem = angular.copy($scope.template.selectedItem); $scope.retrieveItems($scope.formRetrTableInfoParams("onSearch")); } }; $scope.onCancelFilter = function(){ $scope.info.search = angular.copy($scope.template.search); $scope.info.selectedItem = angular.copy($scope.template.selectedItem); $scope.onInitPage(); }; $scope.onSelectTr = function(item,itemIndex){ if($scope.info.operationInfo.lastAction == "onSelectTr"){ if($scope.info.operationInfo.crtItemIndex != null && $scope.info.operationInfo.crtItemIndex -1 != itemIndex){ $scope.info.table.items.splice($scope.info.operationInfo.crtItemIndex,1); $scope.info.operationInfo.crtItemIndex = null; } } $scope.info.selectedItem.info = angular.copy(item); $scope.info.operationInfo.lastAction = "onSelectTr"; }; $scope.onOperation = function(opType,itemIndex){ $item = null; if($scope.info.operationInfo.crtItemIndex == null || $scope.info.operationInfo.crtItemIndex - 1 != itemIndex){ var newItem = $item = opType == "isChangeEmail" ? angular.copy($scope.template.ceItem) : angular.copy($scope.template.daItem); if($scope.info.operationInfo.crtItemIndex != null){ $scope.info.table.items.splice($scope.info.operationInfo.crtItemIndex,1); if($scope.info.operationInfo.crtItemIndex < itemIndex) itemIndex--; } $scope.info.table.items.splice(itemIndex + 1,0,newItem); $scope.info.operationInfo.crtItemIndex = itemIndex + 1; } else if($scope.info.operationInfo.crtItemIndex - 1 == itemIndex && ((opType == "isChangeEmail" && $scope.info.table.items[itemIndex + 1].isDeactivateAcc != undefined) || (opType == "isDeactivateAcc" && $scope.info.table.items[itemIndex + 1].isChangeEmail != undefined))){ $scope.info.table.items.splice($scope.info.operationInfo.crtItemIndex,1); var newItem = $item = opType == "isChangeEmail" ? angular.copy($scope.template.ceItem) : angular.copy($scope.template.daItem); $scope.info.table.items.splice($scope.info.operationInfo.crtItemIndex,0,newItem); } $scope.info.operationInfo.lastAction = "onOperation"; }; $scope.onChangeInput = function(opType,item){ if(opType == 'isChangeEmail' && (item.newEmail.length == 0 || item.confNewEmail.length == 0)) item.saveStatus = { code : null, message : null }; else if(opType == 'isDeactivateAcc' && item.reason.length == 0){ item.saveStatus = { code : null, message : null }; } }; $scope.onSaveChangeEmail = function(){ var item = $scope.info.table.items[$scope.info.operationInfo.crtItemIndex]; if(item.newEmail != item.confNewEmail){ item.saveStatus.code = "NO_MATCH"; item.saveStatus.message = "Cele doua adrese de email introduse nu sunt egale."; } else{ item.btnDisabled = true; $params = { type : "isChangeEmail", username : $scope.info.table.items[$scope.info.operationInfo.crtItemIndex - 1].username, newEmail : item.newEmail, confNewEmail : item.confNewEmail }; ajaxService.sendXTR($scope.info.jsonRequestUrl,$.param($params)) .then(function(result){ item.saveStatus.code = result.additInfo["code"]; item.saveStatus.message = result.additInfo["message"]; if(item.saveStatus.code == 'SUCCESS'){ $scope.info.table.items[$scope.info.operationInfo.crtItemIndex - 1].username = item.newEmail; $scope.info.table.items[$scope.info.operationInfo.crtItemIndex - 1].activ = false; } item.btnDisabled = false; }, function(status){ console.log("Eroare la request XHR trimis in functia 'onSaveChangeEmail' :"); console.log(status); }); } }; $scope.onSaveDeactivateAcc = function(){ var item = $scope.info.table.items[$scope.info.operationInfo.crtItemIndex]; item.btnDisabled = true; $params = { type : "isDeactivateAcc", username : $scope.info.table.items[$scope.info.operationInfo.crtItemIndex - 1].username, reason : item.reason }; ajaxService.sendXTR($scope.info.jsonRequestUrl,$.param($params)) .then(function(result){ item.saveStatus.code = result.additInfo["code"]; item.saveStatus.message = result.additInfo["message"]; if(item.saveStatus.code == 'SUCCESS') $scope.info.table.items[$scope.info.operationInfo.crtItemIndex - 1].activ = false; item.btnDisabled = false; }, function(status){ console.log("Eroare la request XHR trimis in functia 'onSaveDeactivateAcc' :"); console.log(status); }); }; });