'use strict';
/**
* controller for User Profile Example
*/
app.controller('adminUserListCtrl', ["$rootScope", "$scope", "$state", "$timeout", "$interval", "$modal", "SweetAlert", "i18nService", "uiGridConstants", "uiGridGroupingConstants", "Restangular", "api_user_data", "api_login", function ($rootScope, $scope, $state, $timeout, $interval, $modal, SweetAlert, i18nService, uiGridConstants, uiGridGroupingConstants, Restangular, api_user_data, api_login) {
$scope.langs = i18nService.getAllLangs();
$scope.lang = 'zh-cn';
i18nService.setCurrentLang($scope.lang);
var loginUser = $rootScope.user;
$scope.xinzeng = false;
$scope.shanchu = false;
$scope.bianji = false;
$scope.chongzhimima = false;
for (var i = 0; i < loginUser.menu.length; i++) {
if (loginUser.menu[i].link == "renyuanguanli_xinzeng") {
$scope.xinzeng = true
}
if (loginUser.menu[i].link == "renyuanguanli_shanchu") {
$scope.shanchu = true
}
if (loginUser.menu[i].link == "renyuanguanli_bianji") {
$scope.bianji = true
}
if (loginUser.menu[i].link == "renyuanguanli_chongzhimima") {
$scope.chongzhimima = true
}
}
//$scope.allright=false;
//$scope.allrightnot=true;
$scope.gridOptions = {};
$scope.gridOptions.data = 'myData';
$scope.gridOptions.enableColumnResizing = true;
$scope.gridOptions.enableFiltering = true;
$scope.gridOptions.enableGridMenu = true;
$scope.gridOptions.enableRowSelection = true;
$scope.gridOptions.showGridFooter = true;
$scope.gridOptions.showColumnFooter = false;
$scope.gridOptions.fastWatch = true;
$scope.gridOptions.useExternalFiltering = true;
$scope.gridOptions.useExternalPagination = true;
$scope.gridOptions.paginationPageSizes = [10];
$scope.gridOptions.paginationPageSize = 10;
$scope.gridOptions.multiSelect = false;
//$scope.gridOptions.rowTemplate= "
";
$scope.gridOptions.rowIdentity = function (row) {
return row.id;
};
$scope.gridOptions.getRowIdentity = function (row) {
return row.id;
};
$scope.transferRole = function (roles) {
var tempValue = "";
angular.forEach(roles, function (item) {
if (tempValue != "") {
tempValue = tempValue + "/";
}
tempValue = tempValue + item.role;
})
return tempValue;
}
$scope.transferStatus = function (flag) {
return (flag == 0) ? "有效" : "无效";
}
$scope.transfergroup = function (group) {
var groupData = '';
angular.forEach(group, function (item) {
if (groupData == '') {
groupData = item.groupName;
} else {
groupData = groupData + "/" + item.groupName;
}
})
return groupData;
}
//remote data
$scope.gridOptions.columnDefs = [{
name: 'item',
displayName: '序号',
width: 50,
enableFiltering: false,
cellTemplate: '' +
'
{{row.entity.item}}
' +
'
'
},
{
name: 'account',
displayName: '账号',
width: '8%',
minWidth: '105',
enableFiltering: false
},
{
name: 'name',
displayName: '姓名',
width: '6%',
minWidth: '100',
enableFiltering: false
},
{
name: 'weixin',
displayName: '微信',
width: '6%',
minWidth: '120',
enableFiltering: false
},
{
name: 'userType',
displayName: '人员类别',
width: '6%',
minWidth: '120',
enableFiltering: false,
cellTemplate: '' +
'
{{row.entity.userType.value == 2 ?row.entity.company.name:row.entity.userType.name}}
' +
'
'
},
// {
// name: 'gender.name',
// displayName: '性别',
// width: '5%',
// enableFiltering: false
// },
{
name: 'phone',
displayName: '联系电话',
width: '8%',
minWidth: '115',
enableFiltering: false
},
{
name: 'email',
displayName: '邮箱',
width: '14%',
minWidth: '150',
enableFiltering: false
},
{
name: 'dept.dept',
displayName: '科室',
width: '12%',
enableFiltering: false
},
{
name: 'dept.phone',
displayName: '科室电话',
width: '11%',
enableFiltering: false
},
{
name: 'extensionNo',
displayName: '分机号码',
width: '11%',
enableFiltering: false
},
{
name: 'group',
displayName: '所属工作组',
width: '15%',
minWidth: '150',
cellTemplate: '{{grid.appScope.transfergroup(row.entity.group)}}
',
enableFiltering: false
},
{
name: 'flag',
displayName: '状态',
cellTemplate: '{{grid.appScope.transferStatus(row.entity.flag)}}
',
width: '5%',
enableFiltering: false
},
{
name: '操作',
minWidth: '150',
cellTemplate: '',
enableFiltering: false
}
];
$scope.groupdata = {};
var apple_selected, tree, treedata_avm, treedata_geography;
// $scope.onFilterCallback = function(branch) {
// var filedata = $scope.memoryfilterData;
// if (filedata.user) {
// filedata.user.groupdata = { id: branch.id };
// } else {
// filedata.user = { groupdata: { id: branch.id } };
// }
// // filedata.user.group = branch.groupName;
// $scope.refreshData('expand-right', $scope.fileData);
// };
$scope.my_data = [];
$scope.deselectItem = function (item) {
console.log(item)
console.log($scope)
}
function convertListToTree(data, treeMap) {
var idToNodeMap = {}; //Keeps track of nodes using id as key, for fast lookup
var root = null; //Initially set our loop to null
var parentNode = null;
//loop over data
for (var i = 0; i < data.length; i++) {
var datum = data[i];
//each node will have children, so let's give it a "children" poperty
datum.children = [];
//add an entry for this node to the map so that any future children can
//lookup the parent
idToNodeMap[datum.id] = datum;
//Does this node have a parent?
if (typeof datum.parent === "undefined" || datum.parent == null) {
//Doesn't look like it, so this node is the root of the tree
root = datum;
treeMap[datum.id] = root;
} else {
//This node has a parent, so let's look it up using the id
parentNode = idToNodeMap[datum.parent.id];
//We don't need this property, so let's delete it.
delete datum.parent;
//Let's add the current node as a child of the parent node.
parentNode.children.push(datum);
}
}
return root;
}
function convertParentToChildList(data) {
var treeMap = {};
var list = [];
convertListToTree(data, treeMap);
angular.forEach(treeMap, function (item) {
list.push(item);
});
return list;
}
$scope.my_tree = tree = {};
$scope.try_async_load = function () {
$scope.my_data = [];
$scope.select_treedata = [];
$scope.doing_async = true;
api_user_data.fetchDataList('group', {
"idx": 0,
"sum": 1000
}).then(function (data) {
$scope.select_treedata = $scope.my_data = convertParentToChildList(data['list']);
$scope.doing_async = false;
// tree.expand_all();
//console.log(treelist);
});
};
$scope.select_treedata = [];
$scope.propTypeOptions = [];
$scope.try_async_load();
//注册
$scope.registerfunction = function (id) {
var modalInstance = $modal.open({
templateUrl: 'assets/views/system/tpl/register.html',
controller: function ($scope, scope, $modalInstance) {
$scope.ok = function (key) {
var filedata = {
'id': id,
'email': key
};
$modalInstance.dismiss('cancel');
api_user_data.register(filedata).then(function (response) {
var myData = Restangular.stripRestangular(response);
if (response.status == 200) {
SweetAlert.swal({
title: "注册成功!",
type: "success",
confirmButtonColor: "#007AFF"
}, function () {
scope.refreshData('expand-right', scope.fileData);
});
} else {
SweetAlert.swal({
title: "注册失败!",
text: response.msg,
type: "error"
});
}
})
};
$scope.cancel = function () {
$modalInstance.dismiss('cancel');
};
},
resolve: {
scope: function () {
return $scope;
}
}
})
}
$scope.resetpassword = function (id) {
var modalInstance = $modal.open({
templateUrl: 'assets/views/delete.html',
controller: function ($scope, $modalInstance) {
var data;
$scope.title = '重置密码';
$scope.connect = '确定要重置密码?';
$scope.ok = function () {
$modalInstance.dismiss('cancel');
api_login.resetpwd(id).then(function (response) {
var myData = Restangular.stripRestangular(response);
// console.log("myData=" + JSON.stringify(myData));
if (response.status == 200) {
SweetAlert.swal({
title: "重置成功!重置后密码 888888",
type: "success",
confirmButtonColor: "#007AFF"
});
} else {
SweetAlert.swal({
title: "操作异常!",
text: "系统异常,请稍后重试,或者联系管理员!",
type: "error"
});
}
})
};
$scope.cancel = function () {
$modalInstance.dismiss('cancel');
};
},
size: 'sm'
})
}
$scope.saveData = function (data) {
var modelData = {
model: {
user: data
}
};
if (modelData.model.user && modelData.model.user.item) {
delete modelData.model.user.item;
}
modelData.model.user.deptPhone=data.dept?data.dept.phone:''
$state.go('app.system.form', {
formKey: 'system_edit',
service: 'api_user_data',
model: JSON.stringify(modelData)
});
};
$scope.addData = function () {
var modelData = {
model: {
user: {
flag: "0"
}
}
};
$state.go('app.system.form', {
formKey: 'system_edit',
service: 'api_user_data',
model: JSON.stringify(modelData)
});
}
$scope.removeData = function () {
var modalInstance = $modal.open({
templateUrl: 'assets/views/delete.html',
controller: function ($scope, scope, $modalInstance, api_bpm_data) {
var rmvList = [];
$scope.title = '人员删除';
$scope.connect = '确定要删除此人员?';
angular.forEach(scope.selected.items, function (item) {
rmvList.push(item.id);
});
$scope.ok = function () {
$modalInstance.close(rmvList);
};
$scope.cancel = function () {
$modalInstance.dismiss('cancel');
};
},
size: 'sm',
resolve: {
scope: function () {
return $scope;
}
}
});
modalInstance.result.then(function (selectedItem) {
if (selectedItem) {
if (selectedItem.length > 0) {
api_user_data.rmvData('user', selectedItem).then(function (response) {
if (response.data) {
SweetAlert.swal({
title: "删除成功!",
type: "success",
confirmButtonColor: "#007AFF"
}, function () {
$scope.myData = _.reject($scope.myData, function (o) {
return _.includes(selectedItem, o.id);
});
$scope.selected = {
items: []
};
$scope.gridOptions.totalItems = $scope.gridOptions.totalItems - selectedItem.length;
$scope.gridApi.grid.selection.selectedCount = 0;
});
} else {
SweetAlert.swal({
title: "操作异常!",
text: "系统异常,请稍后重试,或者联系管理员!",
type: "error"
});
}
})
}
}
})
}
$scope.selected = {
items: []
}
$scope.editted = {
items: []
}
$scope.gridOptions.onRegisterApi = function (gridApi) {
$scope.gridApi = gridApi;
// gridApi.edit.on.afterCellEdit($scope,function(rowEntity, colDef, newValue, oldValue){
// // console.log(rowEntity);
// });
gridApi.pagination.on.paginationChanged($scope, function (newPage, pageSize) {
var filtersData = $scope.memoryfilterData;
filtersData.idx = newPage - 1;
filtersData.sum = pageSize;
$scope.fileData.idx = newPage - 1;
$scope.fileData.sum = pageSize;
defaultFilterData = filtersData;
$scope.refreshData('expand-right', $scope.fileData);
});
gridApi.selection.on.rowSelectionChanged($scope, function (scope) {
var j = 0;
for (var i = 0; i <= scope.grid.appScope.selected.items.length; i++) {
if (scope.grid.appScope.selected.items[i] == scope.entity) {
j++;
break;
}
}
// console.log("j="+j)
if (j == 1) {
scope.grid.appScope.selected.items.splice(i, 1);
} else {
scope.grid.appScope.selected.items.push(scope.entity)
}
});
// gridApi.core.on.filterChanged($scope, function() {
// var grid = this.grid;
// var filtersData = {
// idx: 0,
// sum: 10
// };
// angular.forEach(grid.columns, function(item) {
// if (item.enableFiltering) {
// if (angular.isDefined(item.filters[0].term) && item.filters[0].term != '') {
// if (angular.isUndefined(filtersData['requester'])) {
// filtersData['requester'] = {};
// }
// filtersData['requester'][item.field] = item.filters[0].term;
// }
// }
// });
// $scope.memoryfilterData = filtersData;
// $scope.refreshData('expand-right', filtersData);
// });
gridApi.core.on.filterChanged($scope, function () {
var grid = this.grid;
// var filtersData = {
// idx: 0,
// sum: 10
// };
var filtersData = $scope.memoryfilterData;
angular.forEach(grid.columns, function (item) {
if (item.enableFiltering) {
if (angular.isDefined(item.filters[0].term) && item.filters[0].term != '') {
if (angular.isUndefined(filtersData['user'])) {
filtersData['user'] = {};
filtersData['user']['flag'] = -1;
filtersData['user'][item.field] = item.filters[0].term;
} else {
filtersData.user.flag = -1;
filtersData.user[item.field] = item.filters[0].term;
}
// filtersData['user']['flag'] = -1;
// filtersData['user'][item.field] = item.filters[0].term;
}
}
});
$scope.memoryfilterData = filtersData;
$scope.refreshData('expand-right', $scope.fileData);
});
};
var defaultFilterData = {
"idx": 0,
"sum": 10
};
$scope.memoryfilterData = {
"idx": 0,
"sum": 10
}
$scope.ldloading = {};
$scope.fileData = {
"idx": 0,
"sum": 10,
"user": {}
}
$scope.treeItem = "";
$scope.onFilterCallback = function (item) {
$scope.treeItem = item
}
// 搜索
$scope.searchData = function () {
if ($scope.treeItem) {
$scope.fileData.user.groupdata = $scope.treeItem
}
if($scope.fileData.user.name){
$scope.fileData.user.selectType= "pinyin_qs"
}
$scope.refreshData('expand-right', $scope.fileData);
}
// 清空
$scope.clean = function () {
delete $scope.fileData.user.account;
delete $scope.fileData.user.name;
delete $scope.fileData.user.groupdata;
$scope.treeItem = "";
$scope.try_async_load();
$scope.refreshData('expand-right', $scope.fileData);
}
//刷新
$scope.refresh = function (style, filterData) {
$scope.selected = {
items: []
};
if ($scope.gridApi) {
// $scope.gridApi.grid.options.paginationCurrentPage = 0;
$scope.gridApi.grid.selection.selectedCount = 0;
}
$scope.refreshData('expand-right', $scope.fileData);
}
//获取列表数据
$scope.refreshData = function (style, filterData) {
$scope.ldloading[style.replace('-', '_')] = true;
if (angular.isUndefined(filterData)) {
filterData = defaultFilterData;
}
if (angular.isDefined($scope.searchTypes)) {
filterData['searchType'] = $scope.searchTypes;
}
$scope.myData = [];
filterData['flag'] = -1;
// if ($scope.gridApi) {
// $scope.gridApi.grid.selection.selectedCount = 0;
// $scope.selected = { items: [] };
// }
console.log("filterData=" + JSON.stringify(filterData))
api_user_data.fetchDataList('user', filterData).then(function (data) {
var myData = Restangular.stripRestangular(data);
$scope.gridOptions.totalItems = myData.totalNum;
$scope.myData = myData.list;
for (var i = 0; i < $scope.myData.length; i++) {
$scope.myData[i]['item'] = i + 1 + filterData.idx * filterData.sum
}
$scope.ldloading[style.replace('-', '_')] = false;
// console.log("$scope.myData="+JSON.stringify($scope.myData))
}, function () {
$scope.ldloading[style.replace('-', '_')] = false;
});
};
$scope.refreshData2 = function (style, filterData) {
$scope.ldloading[style.replace('-', '_')] = true;
if (angular.isUndefined(filterData)) {
filterData = defaultFilterData;
}
if (angular.isDefined($scope.searchTypes)) {
filterData['searchType'] = $scope.searchTypes;
}
// $scope.myData = [];
filterData['flag'] = -1;
// if ($scope.gridApi) {
// $scope.gridApi.grid.selection.selectedCount = 0;
// $scope.selected = { items: [] };
// }
console.log("filterData=" + JSON.stringify(filterData))
api_user_data.fetchDataList('user', filterData).then(function (data) {
var myData = Restangular.stripRestangular(data);
$scope.gridOptions.totalItems = myData.totalNum;
$scope.myData = myData.list;
for (var i = 0; i < $scope.myData.length; i++) {
$scope.myData[i]['item'] = i + 1 + filterData.idx * filterData.sum
}
$scope.ldloading[style.replace('-', '_')] = false;
// console.log("$scope.myData="+JSON.stringify($scope.myData))
}, function () {
$scope.ldloading[style.replace('-', '_')] = false;
});
};
$scope.refreshData('expand-right', $scope.fileData);
$scope.timer = $interval(function () {
$scope.refreshData2('expand-right', $scope.fileData);
}, $rootScope.refreshTime);
$scope.$on('$destroy', function () {
$interval.cancel($scope.timer)
});
}]);