'use strict'

angular.module('APP_BU').controller('ApsBuData', ['$scope', '$http', 'ApsManage', function($scope, $http, ApsManage){
   $scope.apsbudata = [];
   $scope.assdata = [];
   $scope.empdata = 'emp =';
   $scope.allitem = {};
   $scope.selectnumber =[{
      score: "5.00",
      show: {
         tha: 5,
         eng: 5
      }
   },{
      score: "4.00",
      show: {
         tha: 4,
         eng: 4
      }
   },{
      score: "3.00",
      show: {
         tha: 3,
         eng: 3
      }
   },{
      score: "2.00",
      show: {
         tha: 2,
         eng: 2
      }
   },{
      score: "1.00",
      show: {
         tha: 1,
         eng: 1
      }
   },{
      score: "0.00",
      show: {
         tha: '-',
         eng: '-'
      }
   }];

   $scope.getApsData = function($empid){

      $scope.empdata = $scope.empdata+" "+$empid;
      ApsManage.manageAps360.query({employeeid: $empid})
      .$promise.then(function(data){
         $scope.apsbudata = data.tasdata;
         $scope.assdata = data.assessdata;
         //console.log($scope.apsbudata);
      })
   }

    $scope.getLabel = function($p_obj){
      if (lang == 'eng'){
         return $p_obj.eng;
      }else if (lang == 'tha'){
         return $p_obj.tha;
      }
   }

   $scope.calScore = function($p_index){
      var tmpscore = 0.00;
      $scope.apsbudata[$p_index].scorelist.forEach(function($data){
         tmpscore += parseFloat($data.score);
      });

      if (tmpscore == 0.00){
         return '-';
      }else{
         return tmpscore;
      }
   }

    $scope.save = function(){
        if($scope.chkcomment()){
            alert("กรุณากรอกความคิดเห็นให้ครบถ้วน");
        }else if (confirm(MyCode[1])){      
            $scope.alldata = $scope.apsbudata;
            $scope.alldata.forEach(function(data){
                 data.scorelist.forEach(function(list){
                    if((list.score !="1.00" && list.score !="5.00") && (list.comment !="")){
                        list.comment ="";
                    }                    
                 });
            });
            $scope.allitem = {budata: $scope.alldata};

            
            ApsManage.manageAps360.save(JSON.stringify($scope.allitem))
            .$promise.then(function(data){
                alert(MyCode[18]);
            });
         }
    }
 
    $scope.chkcomment = function(){
        var result = false;
        $scope.apsbudata.forEach(function(data){
            data.scorelist.forEach(function(list){
                if((list.score =="1.00" || list.score =="5.00") && (list.comment==="")){
                    result = true;
                }
            });
        });
        return result;
    }
}]);