angular.module('ASP').controller('ObjectiveAppraisal',['$scope','$http','KPISetup',function($scope,$http,KPISetup){
	$scope.item=[];
	$scope.itemkpi=[];
	$scope.amountitem=0;
	$scope.tmpitem=[];
	$scope.tmpitemkpi=[];

	$scope.addNewGroup = function(groupid,groupname){
		$scope.itemstatus=true;
		$scope.item.forEach(function($key){
			if ($key.groupid == groupid){
				$scope.itemstatus = false;
			}
		});
		if ($scope.itemstatus){
			$scope.item.push({'groupid':groupid,'groupname':groupname});
			$scope.updateCateg(groupid,groupname,1);
		}
	}

	$scope.calAmount = function(){
		$scope.amountitem = $scope.item.length;
	}

	$scope.addNewCateg = function(kpicatid,kpicatdesc){
		$scope.kpistatus=true;
		$scope.itemkpi.forEach(function($key){
			if ($key.kpicatid == kpicatid){
				$scope.kpistatus = false;
			}
		});

		if ($scope.kpistatus){
			$scope.kpiweight=[];
			$scope.kpiweight = $scope.addKpiWeight(1);
			$scope.itemkpi.push({'kpicatid':kpicatid,'kpicatdesc':kpicatdesc,'minkpi':'1','maxkpi':'1','kpiweight':$scope.kpiweight});
		}
	}

	$scope.addKpiWeight = function(kpiweight){
		$scope.kpiweight=[];
		$scope.item.forEach(function($key){
			$scope.kpiweight.push({'groupid':$key.groupid,'groupname':$key.groupname,'kpival':kpiweight});
		});

		return $scope.kpiweight;
	}

	$scope.updateCateg = function(groupid,groupname,kpiweight){
		$scope.kpiweight=[];
		if ($scope.itemkpi.length>0){
			$scope.itemkpi.forEach(function($key){
				$scope.kpiweight = $key.kpiweight;
				$scope.kpiweight.push({'groupid':groupid,'groupname':groupname,'kpival':kpiweight});
				console.log($scope.kpiweight);
				$key.kpiweight = $scope.kpiweight;

			});
			/*console.log($scope.itemkpi);*/
		}
	}

	// for get data in first coming this page
	KPISetup.getObjectiveKpiCategory.query()
	.$promise.then(function(data){
		console.log(data);
		$scope.tmpitemkpi = data.ObjectiveKpiCategory;
		$scope.tmpitem = data.ObjectiveKpiGroup;

		if ($scope.tmpitemkpi!=undefined){
			$scope.tmpitemkpi.forEach(function($key){
				$scope.updateKpiCateg($key.kpicatid,$key.kpicatdesc,$key.minkpi,$key.maxkpi);
			});

			$scope.tmpitem.forEach(function($key){
				$scope.updateGroup($key.groupid,$key.groupname);
				$scope.updateWeight($key.groupid,$key.kpicatid,$key.groupname,$key.kpiweight);
			});

			$scope.calAmount();
		}
	});


	$scope.checkTmpKpi = function (){
		console.log($scope.itemkpi);
	}

	$scope.updateGroup = function (groupid,groupname){
		$scope.itemstatus=true;
		$scope.item.forEach(function($key){
			if ($key.groupid == groupid){
				$scope.itemstatus=false;
			}
		});

		if ($scope.itemstatus){
			$scope.item.push({'groupid':groupid,'groupname':groupname});
		}
	}

	$scope.updateKpiCateg = function (kpicatid,kpicatdesc,minkpi,maxkpi){
		$scope.itemstatus=true;

		$scope.itemkpi.forEach(function($key){
			if ($key.kpicatid == kpicatid){
				$scope.itemstatus=false;
			}
		});

		if ($scope.itemstatus){
			$scope.kpiweight=[];
			$scope.itemkpi.push({'kpicatid':kpicatid,'kpicatdesc':kpicatdesc,'minkpi':minkpi,'maxkpi':maxkpi,'kpiweight':$scope.kpiweight});
		}
	}

	$scope.updateWeight = function(groupid,kpicatid,groupname,kpiweight){
		$scope.kpiweight=[];
		$scope.itemkpi.forEach(function($key){
			if ($key.kpicatid == kpicatid){
				$scope.kpiweight = $key.kpiweight;
				$scope.kpiweight.push({'groupid':groupid,'groupname':groupname,'kpival':kpiweight});
				$key.kpiweight = $scope.kpiweight;
			}
		});
	}

	$scope.calScore = function(item){
		var score = 0;
		$scope.itemkpi.forEach(function(data){
			var myweight = data.kpiweight;
			var obj = myweight.filter(myweight => myweight.groupid == item.groupid);
			score += parseInt(obj[0].kpival);
		});
		return score;
	}
}]);