'use strict';
angular.module('qrzk')

/** 用户管理 **/
.controller('UserListCtrl',function($cookieStore,$scope,$http,appConst,toaster){
	//
	$scope.pageModel = {};
	$scope.pageModel.pageSize = 10;
	$scope.search = {};
	$scope.load = function(){
		_goPage(1);
	};
	$scope.load();
	$scope.goPage = function(pageNum){
		_goPage(pageNum);
	}
	
	function _goPage(pageNum){
		if(pageNum < 1 || (pageNum > $scope.pageModel.totalPage && $scope.pageModel.totalPage > 0)){
			return;
		}
		
		$scope.pageModel.curPage = pageNum;
		var params = {'page': pageNum
				, 'rows': $scope.pageModel.pageSize
				, 'keyword':$scope.search.loginName};
		$http.post('user/queryUsers' , params).success(function(data){
			$scope.pageModel.array = data.rows;
			$scope.pageModel.total = data.total;
			$scope.pageModel.totalPage = Math.ceil(data.total / $scope.pageModel.pageSize);
		})
		.error(function(data, code){alert(code);});
	}
	//
	
//	$scope.load = function(){
//		var url = 'user/queryUsers';
//		$http.get(url).success(function(data){
//			$scope.array = data;
//		})
//		.error(function(data, code){alert(code);});
//	};
//	$scope.load();
	
	$scope.updateStatus = function(userId, status){
		$http.post('user/updateStatus/'+userId + '/' + status)
		.success(function(data){
			if(!data.result){
				alert(data.msg);
			}
			else{
				toaster.pop('success',name,'更新成功！');
				$scope.load();
			}
		})
		.error(function(data,code){
			alert(code);
		});	
	};
	
	$scope.resetPassword = function(userId){
		var obj={
				"id" :userId	 
			};
		var user = JSON.stringify(obj);
			
		$http.post('user/resetPwd', user)
		.success(function(data){
			if(!data.result){
				alert(data.msg);
			}
			else{
				toaster.pop('success',name,'密码重置为123456！');
				$scope.load();
			}
		})
		.error(function(data,code){
			alert(code);
		});	
	};
})

/** 新增用户 **/
.controller('UserAddCtrl',function($scope,$http,$state,toaster){
	$scope.load = function(){
		var url = 'user/getAllRole';
		$http.get(url).success(function(data){
			$scope.roleArray = data;
		})
		.error(function(data, code){alert(code);});
	};
	$scope.load();
	
	$scope.model = {};
	$scope.submit = function(){
		var obj = angular.toJson($scope.model);
		$scope.myPromise = $http.post('user/addUser',$scope.model)
		.success(function(data){
			if(!data.result){
				alert(data.msg);
			}
			else{
				toaster.pop('success',name,'保存成功！');
				$state.go('userList');
			}
		})
		.error(function(data,code){
			alert(code);
		});
	};
})

/** 编辑用户 **/
.controller('UserEditCtrl',function($scope,$http,$state,$stateParams,toaster){
	$scope.load = function(){
		var url = 'user/getAllRole';
		$http.get(url).success(function(data){
			$scope.roleArray = data;
			
		})
		.error(function(data, code){alert(code);});
		
		var url = 'user/editUser/'+ $stateParams.id;
		$http.get(url).success(function(data){
			$scope.model = data;
			if(data.roles.length > 0){
				$scope.model.role = data.roles[0].id;
			}
		})
		.error(function(data, code){alert(code);});
	};
	$scope.load();
	
	$scope.submit = function(){
		var obj = angular.toJson($scope.model);
		$scope.myPromise = $http.post('user/editUserInfo',$scope.model)
		.success(function(data){
			if(!data.result){
				alert(data.msg);
			}
			else{
				toaster.pop('success',name,'保存成功！');
				$state.go('userList');
			}
		})
		.error(function(data,code){
			alert(code);
		});
	};
})