'use strict';

angular.module('CKLC').controller('ObjectiveCKLC', function($scope, $http, CKLCGetData, $state, $stateParams){

	$scope.employeeid  = $stateParams.employeeid;
	$scope.empdata =[];
	$scope.item=[];
	$scope.totalscore =0;
	$scope.selfScore=0;
	$scope.othcomment ='';
	$scope.overallresult =0;
	$scope.overallcomment = '';
	$scope.allitem={
		'tapstran_data' : {},
		'tastran_data': {},
		'manage_type' : ''
	};
	$scope.formitem=[]

	$scope.itemtapstran={};

	CKLCGetData.getObjectiveProfile.query({employeeid: $scope.employeeid })
	.$promise.then(function(data){
		$scope.empdata = data.ObjectiveProfile;
	})

	CKLCGetData.getObjectiveCKLC.query({employeeid: $scope.employeeid })
	.$promise.then(function(data){
		$scope.item = data.ObjectiveCKLC;
		$scope.itemtapstran = data.ObjectiveTapstran;
		$scope.formitem = data.ObjectiveForm;
		console.log($scope.item);
	})
	
	$scope.save = function (p_savetype){
		console.log('get');
		$scope.allitem.tapstran_data=$scope.itemtapstran;
		$scope.allitem.tastran_data=$scope.item;
		$scope.allitem.manage_type = p_savetype;

		CKLCGetData.getObjectiveCKLC.create( JSON.stringify($scope.allitem))
		.$promise.then(function(data){
			if (p_savetype == "1"){
				alert('Save draft complete');
			}else if (p_savetype == "2"){
				alert("Asessed succes");
				window.location.reload();
			}
		})
	}
	
	$scope.getTotalScore = function(){
		$scope.totalscore=0;
		for(var i=0;i<$scope.item.length;i++){
			$scope.totalscore += parseInt($scope.item[i].reviewscore);
		}
		$scope.itemtapstran.totalscore = parseInt($scope.totalscore);
	}

	$scope.getSelfScore = function(){
		$scope.selfScore=0;
		for(var i=0;i<$scope.item.length;i++){
			$scope.selfScore += parseInt($scope.item[i].selfscore);
		}

		return $scope.selfScore;
	}

	$scope.clear =function(empid){
		if ($scope.item[0].employeeid == empid){

			$scope.item.forEach(function($key){
				$key.selfscore ="";
			});

		}else if ($scope.item[0].reviewerid == empid){

			$scope.item.forEach(function($key){
				$key.reviewscore="";
			});
		}
		alert("clear data complete");
	}
});