'use strict'
angular.module('TRN_FOLLOWUP').controller('FollowupForm',['$scope','$sce', '$filter', '$http', 'ListFollowupCtrl', '$q', function($scope, $sce, $filter, $http, ListFollowupCtrl, $q){
  $scope.apsid = $('input[name="apsid"]').val()
  $scope.trainingid = $('input[name="trainingid"]').val()
  $scope.apsassessor = $('input[name="apsassessor"]').val()
  $scope.me = $('input[name="me"]').val()
  $scope.headdata = {}
  $scope.apsform = {}



  $scope.myparam = {
    apsid: $scope.apsid
    ,trainingid: $scope.trainingid
    ,apsassessor: $scope.apsassessor
  };
  ListFollowupCtrl.manageFollowupForm.query($scope.myparam).$promise.then(function(data){
    $scope.headdata = data.header;
    $scope.apsform = data.body;
    //console.log($scope.headdata);
    console.log(data)
  });

  $scope.saveDraff = function(){
    if (confirm(MyCode[1])){
      $scope.sendparam = {
          apsid: $scope.headdata[0].apsid
          ,trainingid: $scope.headdata[0].trainingid
          ,headdata: $scope.headdata
          ,apsform: $scope.apsform
          ,savetype: '0'
      }
      ListFollowupCtrl.manageFollowupForm.save(JSON.stringify($scope.sendparam))
      .$promise.then(function(){
          alert(MyCode[18]);
          //window.opener.location.reload();
      });
    }
  }

  $scope.save = function(){
    if (checkCommnet()){
      alert("กรุณากรอกข้อมูลให้ครบถ้วน");
    }else if(checkScore()){
      alert("กรุณากรอกคะแนนให้ครบถ้วน");
    }else if (confirm(MyCode[1])){
      $scope.sendparam = {
        apsid: $scope.headdata[0].apsid
        ,trainingid: $scope.headdata[0].trainingid
        ,headdata: $scope.headdata
        ,apsform: $scope.apsform
        ,savetype: '1'
      }
      ListFollowupCtrl.manageFollowupForm.save(JSON.stringify($scope.sendparam))
      .$promise.then(function(){
          alert(MyCode[18]);
          closePage();
          //window.opener.location.reload();
      });
    }
  }

  $scope.chkboss = function(){
    let result = false
    if($scope.headdata.length > 0){
      if($scope.headdata[0].boss == $scope.me){
        result = true
      }
    }
    return result
  }

  function checkScore(){
    var result = false

    if($scope.chkboss()){
      $scope.apsform[1].groupAry.forEach(function(group){
        let objlist = group.masAry.filter((masobj) => masobj.score == 0.00)
        if (objlist.length>0){
          result = true
        }
      });
      if($scope.headdata[0].chk1 == '' || $scope.headdata[0].chk2 == ''){
        result = true
      }
    }else{
      $scope.apsform[0].groupAry.forEach(function(group){
        let objlist = group.masAry.filter((masobj) => masobj.score == 0.00)
        if (objlist.length>0){
          result = true
        }
      });
    }
    return result
  }

  function checkCommnet(){
    var result = false;
    if($scope.chkboss()){
      if(($scope.headdata[0].comment2 == "" ) || ($scope.headdata[0].chk1 == "" ) || ($scope.headdata[0].chk2 == "" )){
        result = true;
      }
    }else{
      if($scope.headdata[0].comment1 == "" ){
        result = true;
      }
    }
    return result;
  }

  $scope.checkprivilege = function(user){
    var result = false;
    if($scope.headdata.length > 0){
      if(user == 'emp'){
        if($scope.chkboss()){
          $("i[class='fa fa-upload'] , i[class='fa fa-trash-o']").parent().hide()
          result = true
        }else if($scope.headdata[0].apsstate == '1' || $scope.headdata[0].apsstate == 'D' || $scope.headdata[0].apsstate == '2'){
          $("i[class='fa fa-upload'] , i[class='fa fa-trash-o']").parent().hide()
          result = true
        }
      }else if(user == 'boss'){
        if(!$scope.chkboss()){
          result = true
        }else if($scope.headdata[0].apsstate == '0' || $scope.headdata[0].apsstate == 'C' || $scope.headdata[0].apsstate == '2'){
          $("i[class='fa fa-upload'] , i[class='fa fa-trash-o']").parent().hide()
          result = true
        }else{
          $("i[class='fa fa-upload'] , i[class='fa fa-trash-o']").parent().hide()
        }
      }
    }
    return result;
  }

  //alert($(window).height());            ==> check height of window
}]);