'use strict'

angular.module('EMPVIEW').controller('Survey', [ '$scope', '$http', 'SurveyControl', function($scope, $http, SurveyControl){
   $scope.allitem = {};
   $scope.myLang = "";
   $scope.formData = {};
   $scope.questionData = [];
   $scope.surveyData = {
      'surveyid': '0'
   };
   $scope.require_status = false;
   $scope.waitingload = true;

   SurveyControl.manageSurvey.query({gettype: 'query'})
   .$promise.then(function(data){
      $scope.formData = data.formData;
      $scope.questionData = data.questionData;
      $scope.surveyData = data.objtsurvey;
      $scope.setDisableLoad();
   })

   $scope.setDisableLoad = function(){
      setTimeout(function(){
         $scope.waitingload = false;
      }, 100);
   }

   $scope.initLang = function($p_lang){
      $scope.myLang = $p_lang.toString();
   }

   $scope.getFormData = function($p_object){
      if ($p_object != undefined){
         if ($scope.myLang == "eng"){
            return $p_object["eng"];
         }else if ($scope.myLang == "tha"){
            return $p_object["tha"];
         }
      }
   }

   $scope.getQuestionData = function($p_object){
      if ($p_object != undefined){
         if ($scope.myLang == "eng"){
            return $p_object["eng"];
         }else if ($scope.myLang == "tha"){
            return $p_object["tha"];
         }
      }
   }

   $scope.saveDraff = function(){
      $scope.checkFormRequire('0');
   }

   $scope.saveSubmit = function(){
      if ($scope.myLang == 'eng'){
         if (confirm("If you submit on your data, you can't edit your data anymore")){
            $scope.checkFormRequire('1');
         }
      }else if ($scope.myLang == 'tha'){
         if (confirm('หากทำการยืนยัน จะไม่สามารถแก้ไขข้อมูลแบบสำรวจได้อีก')){
            $scope.checkFormRequire('1');
         }
      }
   }

   $scope.checkFormRequire = function($p_savestatus){
      $scope.require_status = false;
      $scope.questionData.forEach(function($qkey){
         if ($qkey.tqrequire == 1){

            if ($qkey.type == 1){
               $scope.checkTextValue($qkey);
            }else if ($qkey.type == 2){
               $scope.checkRadioValue($qkey);
            }else if ($qkey.type == 3){
               $scope.checkCheckBoxValue($qkey);
            }else if ($qkey.type == 4){
               $scope.checkTableChoiceValue($qkey);
            }else if ($qkey.type == 5){
               $scope.checkTableCheckBoxValue($qkey);
            }else if ($qkey.type == 7){
               $scope.checkTableTextValue($qkey);
            }
         }else if ($qkey.type == 6){
            $scope.checkSubQuestionValue($qkey);
         }
      });

      if ($scope.require_status){
         //alert("Require cannot be null");
         if ($scope.myLang == 'eng'){
            alert('Please check your data again');
         }else if ($scope.myLang == 'tha'){
            alert('กรุณากรอกข้อมูลที่จำเป็นให้ครบถ้วน')
         }

      }else{
         //console.log($scope.surveyData);
         $scope.allitem = {"formData": $scope.formData, "questionData": $scope.questionData, "surveyData": $scope.surveyData, "savestatus": $p_savestatus};
         SurveyControl.manageSurvey.save(JSON.stringify($scope.allitem))
         .$promise.then(function(data){
            $scope.surveyData.surveyid = data.surveyid;
            if ($scope.myLang == 'eng'){
               alert('Save data complete');
            }else if ($scope.myLang == 'tha'){
               alert('บันทึกข้อมูลเสร็จสิ้น');
            }
         })
      }
   }

   $scope.checkTextValue = function($p_object){
      if ($p_object.structure.value == undefined){
         $scope.require_status = true;
         $p_object.showrequire = true;
      }else if ($p_object.structure.value == ""){
         $scope.require_status = true;
         $p_object.showrequire = true;
      }else{
         $p_object.showrequire = false;
      }
   }

   $scope.checkRadioValue = function($p_object){
      if ($p_object.structure.value == undefined){
         $scope.require_status = true;
         $p_object.showrequire = true;
      }else if ($p_object.structure.value == ""){
         $scope.require_status = true;
         $p_object.showrequire = true;
      }else{
         $p_object.showrequire = false;
      }
   }

   $scope.checkCheckBoxValue = function($p_object){
      var tmpvalue = true;
      $p_object.structure.list.forEach(function($ckey){
         if ($ckey.isSelect){
            tmpvalue = false;
         }
      });

      if (tmpvalue){
         $scope.require_status = true;
         $p_object.showrequire = true;
      }else{
         $p_object.showrequire = false;
      }
   }

   $scope.checkTableChoiceValue = function($p_object){
      $p_object.structure.list.grouprow.forEach(function($gkey){
         $gkey.listrow.forEach(function($lkey){
            if ($lkey.value == undefined){
               $scope.require_status = true;
               $p_object.showrequire = true;
               return;
            }else if($lkey.value == ""){
               $scope.require_status = true;
               $p_object.showrequire = true;
               return;
            }else{
               $p_object.showrequire = false;
               return;
            }
         });
      });
   }

   $scope.checkTableCheckBoxValue = function($p_object){
      $p_object.showrequire = false;
      var listlength = $p_object.structure.list.column.length;
      $p_object.structure.list.grouprow.forEach(function($gkey){
         $gkey.listrow.forEach(function($lkey){
            if ($lkey.isSelect == undefined){
               $scope.require_status = true;
               $p_object.showrequire = true;
               return;
            }else{
               var tmpvalue = true;
               for (var i=0; i<listlength; i++){
                  if ($lkey.isSelect[i] != undefined && $lkey.isSelect[i] == true){
                     tmpvalue = false;
                  }
               }

               if (tmpvalue){
                  $scope.require_status = true;
                  $p_object.showrequire = true;
                  return;
               }
            }
         });
      });
   }

   $scope.checkTableTextValue = function($p_object){
      $p_object.showrequire = false;
      var listlength = $p_object.structure.list.column.length;
      $p_object.structure.list.grouprow.forEach(function($gkey){

         $gkey.listrow.forEach(function($lkey){

            if ($lkey.textvalue == undefined){
               $p_object.showrequire = true;
               $scope.require_status = true;
               return;
            }else{
               var tmpvalue = true;
               for (var i=0; i<listlength; i++){
                  if ($lkey.textvalue[i] == undefined ){
                     $p_object.showrequire = true;
                     $scope.require_status = true;
                     return;
                  }else if ($lkey.textvalue[i] == ""){
                     $p_object.showrequire = true;
                     $scope.require_status = true;
                     return;
                  }
               }
            }

         });
      });
   }

   $scope.checkSubQuestionValue = function($p_object){
      $p_object.structure.list.forEach(function($subqkey){
         if ($subqkey.tqrequire == 1){
            if ($subqkey.type == 1){
               $scope.checkTextValue($subqkey);
            }else if ($subqkey.type == 2){
               $scope.checkRadioValue($subqkey);
            }else if ($subqkey.type == 3){
               $scope.checkCheckBoxValue($subqkey);
            }else if ($subqkey.type == 4){
               $scope.checkTableChoiceValue($subqkey);
            }else if ($subqkey.type == 5){
               $scope.checkTableCheckBoxValue($subqkey);
            }else if ($subqkey.type == 7){
               $scope.checkTableTextValue($subqkey);
            }
         }
      });
   }

   $scope.clearData = function(){
      SurveyControl.manageSurvey.query({gettype: 'clear'})
      .$promise.then(function(data){
         $scope.formData = data.formData;
         $scope.questionData = data.questionData;
      })
   }
}])
.directive('showtext', function(){
   return {
      templateUrl: 'SHOWTEXTQUESTION.html'
   };
})
.directive('showchoice', function(){
   return {
      templateUrl: 'SHOWCHOICEQUESTION.html'
   };
})
.directive('showsubquestion', function(){
   return {
      templateUrl: 'SHOWSUBQUESTION.html'
   };
})
.directive('showtable', function(){
   return {
      templateUrl: 'SHOWTABLEQUESTION.html'
   };
});