'use strict'

angular.module('JBREST').controller('SendMaster', ['$scope', '$http', 'MasterCtrl', '$q', 'UploadService', function ($scope, $http, MasterCtrl, $q, UploadService) {
  $scope.lang = getLang()
  $scope.tmpdata = [];
  $scope.state = {}
  $scope.textStatus = {}
  $scope.sendingText = {
    tha: 'กำลังโหลด...',
    eng: 'Sending...'
  }
  $scope.successText = {
    tha: 'ส่งข้อมูลสำเร็จ !',
    eng: 'Success !'
  }
  $scope.errorText = {
    tha: 'ไม่สามารถเชื่อมต่อกับ Jobboard Service !',
    eng: 'Can\'t connect Jobboard Service !'
  }
  $scope.allConf = [
    {
      name : 'province',
      swcode : 'SW013052',
      functionName : function () {
        $scope.getProvince()
      }
    },
    {
      name : 'zipcode',
      swcode : 'SW013053',
      functionName : ()=> {
        $scope.getZipcode()
      }
    },
    {
      name : 'job',
      swcode : 'SW013054',
      functionName : ()=> {
        $scope.getJob()
      }
    },
    {
      name : 'education',
      swcode : 'SW013055',
      functionName : ()=> {
        $scope.getEdu()
      }
    },
    {
      name : 'faculty',
      swcode : 'SW013056',
      functionName : ()=> {
        $scope.getFaculty()
      }
    },
    {
      name : 'nationality',
      swcode : 'SW013057',
      functionName : ()=> {
        $scope.getNationality()
      }
    },
    {
      name : 'prefix',
      swcode : 'SW013058',
      functionName : ()=> {
        $scope.getPrefix()
      }
    },
    {
      name : 'race',
      swcode : 'SW013059',
      functionName : ()=> {
        $scope.getRace()
      }
    },
    {
      name : 'religion',
      swcode : 'SW013060',
      functionName : ()=> {
        $scope.getReligion()
      }
    },
    {
      name : 'skilllevel',
      swcode : 'SW013061',
      functionName : ()=> {
        $scope.getSkillLevel()
      }
    },
    {
      name : 'config',
      swcode : 'SW013086',
      functionName : ()=> {
        $scope.getConfig()
      }
    },
    {
      name : 'district',
      swcode : 'SW013088',
      functionName : ()=> {
        $scope.getDistrict()
      }
    },
    {
      name : 'major',
      swcode : 'SW013157',
      functionName : ()=> {
        $scope.getMajor()
      }
    },
    {
      name : 'institute',
      swcode : 'SW013158',
      functionName : ()=> {
        $scope.getMinstitute()
      }
    },
    {
      name : 'country',
      swcode : 'SW013159',
      functionName : ()=> {
        $scope.getMcountry()
      }
    },
    {
      name : 'sourceJob',
      swcode : 'SW013160',
      functionName : ()=> {
        $scope.getMsourceJob()
      }
    },
    {
      name : 'relations',
      swcode : 'SW013239',
      functionName : ()=> {
        $scope.getMRelations()
      }
    },
    {
      name : 'bloodgroup',
      swcode : 'SW013240',
      functionName : ()=> {
        $scope.getMbloodGroup()
      }
    },
    {
      name : 'gender',
      swcode : 'SW013241',
      functionName : ()=> {
        $scope.getMgender()
      }
    },
    {
      name : 'maritalStatus',
      swcode : 'SW013242',
      functionName : ()=> {
        $scope.getMmaritalStatus()
      }
    },
    {
      name : 'militaryStatus',
      swcode : 'SW013243',
      functionName : ()=> {
        $scope.getMmilitaryStatus()
      }
    },
    {
      name : 'peopleStatus',
      swcode : 'SW013244',
      functionName : ()=> {
        $scope.getMpeopleStatus()
      }
    },
    {
      name : 'honourably',
      swcode : 'SW013245',
      functionName : ()=> {
        $scope.getMhonourably()
      }
    },
    {
      name : 'occupation',
      swcode : 'SW013269',
      functionName : ()=> {
        $scope.getMoccupation()
      }
    },
    {
      name : 'language',
      swcode : 'SW013270',
      functionName : ()=> {
        $scope.getMlanguage()
      }
    },
    {
      name : 'mjobgroup',
      swcode : 'SW000723',
      functionName : ()=> {
        $scope.getMJobGroup()
      }
    },
    {
      name : 'mpaperlist',
      swcode : 'SW012811',
      functionName : ()=> {
        $scope.getMPaperList()
      }
    },
    {
      name : 'mresidence',
      swcode : 'SW013831',
      functionName : ()=> {
        $scope.getMResidence()
      }
    }
  ]
  $scope.promt = {}

  $scope.getProvince = function() {
    return $q(function(resolve, reject) {
      $scope.promt.province = true
      $scope.state.province = false
      $scope.textStatus.province = $scope.sendingText[$scope.lang]
      MasterCtrl.getProvince.query().$promise.then(function (data) {
        MasterCtrl.sendProvince.save(data.mprovince).$promise.then(function () {
          $scope.state.province = true
          $scope.textStatus.province = $scope.successText[$scope.lang]
          resolve()
        }, function () {
          $scope.state.province = true
          $scope.textStatus.province = $scope.errorText[$scope.lang]
          reject()
        })
      })
    })
  };
  $scope.getZipcode = function() {
    return $q(function(resolve, reject) {
      $scope.promt.zipcode = true
      $scope.state.zipcode = false
      $scope.textStatus.zipcode = $scope.sendingText[$scope.lang]
      MasterCtrl.getZipcode.query().$promise.then(function (data) {
        MasterCtrl.sendZipcode.save(data.csczipcode).$promise.then(function () {
          $scope.state.zipcode = true
          $scope.textStatus.zipcode = $scope.successText[$scope.lang]
          resolve()
        }, function () {
          $scope.state.zipcode = true
          $scope.textStatus.zipcode = $scope.errorText[$scope.lang]
          reject()
        })
      })
    })
  };
  $scope.getJob = function() {
    return $q(function(resolve, reject) {
      $scope.promt.job = true
      $scope.state.job = false
      $scope.textStatus.job = $scope.sendingText[$scope.lang]
      MasterCtrl.getJob.query({jobid: 0}).$promise.then(function (data) {
        MasterCtrl.sendJob.save(data.job).$promise.then(function () {
          $scope.state.job = true
          $scope.textStatus.job = $scope.successText[$scope.lang]
          resolve()
        }, function () {
          $scope.state.job = true
          $scope.textStatus.job = $scope.errorText[$scope.lang]
          reject()
        })
      })
    })
  };
  $scope.getEdu = function() {
    return $q(function(resolve, reject) {
      $scope.promt.education = true
      $scope.state.education = false
      $scope.textStatus.education = $scope.sendingText[$scope.lang]
      MasterCtrl.getEdu.query().$promise.then(function (data) {
        MasterCtrl.sendEdu.save(data.edu).$promise.then(function () {
          $scope.state.education = true
          $scope.textStatus.education = $scope.successText[$scope.lang]
          resolve()
        }, function () {
          $scope.state.education = true
          $scope.textStatus.education = $scope.errorText[$scope.lang]
          reject()
        })
      })
    })

  };

  $scope.getFaculty = function() {
    return $q(function(resolve, reject) {
      $scope.promt.faculty = true
      $scope.state.faculty = false
      $scope.textStatus.faculty = $scope.sendingText[$scope.lang]
      MasterCtrl.getFaculty.query().$promise.then(function (data) {
        MasterCtrl.sendFaculty.save(data.faculty).$promise.then(function () {
          $scope.state.faculty = true
          $scope.textStatus.faculty = $scope.successText[$scope.lang]
          resolve()
        }, function () {
          $scope.state.faculty = true
          $scope.textStatus.faculty = $scope.errorText[$scope.lang]
          reject()
        })
      })
    })
  };
  $scope.getNationality = function() {
    return $q(function(resolve, reject) {
      $scope.promt.nationality = true
      $scope.state.nationality = false
      $scope.textStatus.nationalilty = $scope.sendingText[$scope.lang]
      MasterCtrl.getNationality.query().$promise.then(function (data) {
        MasterCtrl.sendNationality.save(data.nationality).$promise.then(function () {
          $scope.state.nationality = true
          $scope.textStatus.nationality = $scope.successText[$scope.lang]
          resolve()
        }, function () {
          $scope.state.nationality = true
          $scope.textStatus.nationality = $scope.errorText[$scope.lang]
          reject()
        })
      })
    })
  };


  $scope.getPrefix = function() {
    return $q(function(resolve, reject) {
      $scope.promt.prefix = true
      $scope.state.prefix = false
      $scope.textStatus.prefix = $scope.sendingText[$scope.lang]
      MasterCtrl.getPrefix.query().$promise.then(function (data) {
        MasterCtrl.sendPrefix.save(data.prefix).$promise.then(function () {
          $scope.state.prefix = true
          $scope.textStatus.prefix = $scope.successText[$scope.lang]
          resolve()
        }, function () {
          $scope.state.prefix = true
          $scope.textStatus.prefix = $scope.errorText[$scope.lang]
          reject()
        })
      })
    })

  };


  $scope.getRace = function() {
    return $q(function(resolve, reject) {
      $scope.promt.race = true
      $scope.state.race = false
      $scope.textStatus.race = $scope.sendingText[$scope.lang]
      MasterCtrl.getRace.query().$promise.then(function (data) {
        MasterCtrl.sendRace.save(data.race).$promise.then(function () {
          $scope.state.race = true
          $scope.textStatus.race = $scope.successText[$scope.lang]
          resolve()
        }, function () {
          $scope.state.race = true
          $scope.textStatus.race = $scope.errorText[$scope.lang]
          reject()
        })
      })
    })

  };

  $scope.getReligion = function() {
    return $q(function(resolve, reject) {
      $scope.promt.religion = true
      $scope.state.reiigion = false
      $scope.textStatus.religion = $scope.sendingText[$scope.lang]
      MasterCtrl.getReligion.query().$promise.then(function (data) {
        MasterCtrl.sendReligion.save(data.religion).$promise.then(function () {
          $scope.state.religion = true
          $scope.textStatus.religion = $scope.successText[$scope.lang]
          resolve()
        }, function () {
          $scope.state.religion = true
          $scope.textStatus.religion = $scope.errorText[$scope.lang]
          reject()
        })
      })
    })
  };

  $scope.getSkillLevel = function() {
    return $q(function(resolve, reject) {
      $scope.promt.skilllevel = true
      $scope.state.skilllevel = false
      $scope.textStatus.skilllevel = $scope.sendingText[$scope.lang]
      MasterCtrl.getSkillLevel.query().$promise.then(function (data) {
        MasterCtrl.sendSkillLevel.save(data.skilllevel).$promise.then(function () {
          $scope.state.skilllevel = true
          $scope.textStatus.skilllevel = $scope.successText[$scope.lang]
          resolve()
        }, function () {
          $scope.state.skilllevel = true
          $scope.textStatus.skilllevel = $scope.errorText[$scope.lang]
          reject()
        })
      })
    })
  };
  
  $scope.sendAnnounce = ($fileName) => {
    return $q(function(resolve, reject) {
      let model = {
        rootpath: 'RECRUIT_UPLOAD',
        subpath: 'PDF/',
        filename: $fileName
      }
      UploadService.ManageFileUpload.read(model).$promise.then(function ($res) {
        let splitFile = $fileName.split('.')
        let dataToSend = {
          filedata: $res.data,
          filename: splitFile[0],
          despath: '',
          filetype: splitFile[1]
        }
        MasterCtrl.MPictureController.save(dataToSend).$promise.then(function () {
          resolve()
        }, () => {
          reject()
        })
      }, () => {
        reject()
      })
    })
  }
  
  $scope.getConfig = function() {
    return $q(function(resolve, reject) {
      $scope.promt.config = true
      $scope.state.config = false
      $scope.textStatus.config = $scope.sendingText[$scope.lang]
      MasterCtrl.getConfig.query().$promise.then(function (data) {
        let confAnnounce = data.config.find(($item) => {
          return $item.name === "ANNOUNCE"
        })
        let promSendFile = [Promise]
        if (confAnnounce) {
          let lstAnnounce = JSON.parse(confAnnounce.object).announce
          promSendFile = lstAnnounce.map(($announce) => {
            return new Promise((resolve, reject) => {
              if ($announce.file !== '') {
                $scope.sendAnnounce($announce.file).then(() => {
                  resolve()
                }, () => {
                  reject()
                })
              } else {
                resolve()
              }
            })
          })
        }
        Promise.all(promSendFile).then(() => {
          MasterCtrl.sendConfig.save(data.config).$promise.then(function () {
            $scope.state.config = true
            $scope.textStatus.config = $scope.successText[$scope.lang]
            resolve()
          }, function () {
            $scope.state.config = true
            $scope.textStatus.config = $scope.errorText[$scope.lang]
            reject()
          })
        })
      })
    })
  }

  $scope.getDistrict = function() {
    return $q(function(resolve, reject) {
      $scope.promt.district = true
      $scope.state.district = false
      $scope.textStatus.district = $scope.sendingText[$scope.lang]
      MasterCtrl.getDistrict.query().$promise.then(function (data) {
        MasterCtrl.sendDistrict.save(data.district).$promise.then(function () {
          $scope.state.district = true
          $scope.textStatus.district = $scope.successText[$scope.lang]
          resolve()
        }, function () {
          $scope.state.district = true
          $scope.textStatus.district = $scope.errorText[$scope.lang]
          reject()
        })
      })
    })
  }

  $scope.getMajor = function(){
    return $q(function(resolve, reject) {
      $scope.promt.major = true
      $scope.state.major = false
      $scope.textStatus.major = $scope.sendingText[$scope.lang]
      MasterCtrl.RestMmajor.getQuery().$promise.then((p_data) => {
        MasterCtrl.MMajorController.save(p_data.mmajor).$promise.then((p_result) => {
          if (p_result.status === 200){
            $scope.state.major = true
            $scope.textStatus.major = $scope.successText[$scope.lang]
          } else {
            $scope.state.major = true
            $scope.textStatus.major = $scope.errorText[$scope.lang]
          }
          resolve()
        }, () => {
          $scope.state.major = true
          $scope.textStatus.major = $scope.errorText[$scope.lang]
          reject()
        })
      })
    })
  }

  $scope.getMinstitute = function(){
    return $q(function(resolve, reject) {
      $scope.promt.institute = true
      $scope.state.institute = false
      $scope.textStatus.institute = $scope.sendingText[$scope.lang]
      MasterCtrl.RestMinstitute.getQuery().$promise.then((p_data) => {
        MasterCtrl.MInstituteController.save(p_data.minstitute).$promise.then((p_result) => {
          if (p_result.status === 200){
            $scope.state.institute = true
            $scope.textStatus.institute = $scope.successText[$scope.lang]
          } else {
            $scope.state.institute = true
            $scope.textStatus.institute = $scope.errorText[$scope.lang]
          }
          resolve()
        }, () => {
          $scope.state.institute = true
          $scope.textStatus.institute = $scope.errorText[$scope.lang]
          reject()
        })
      })
    })
  }

  $scope.getMcountry = function(){
    return $q(function(resolve, reject) {
      $scope.promt.country = true
      $scope.state.country = false
      $scope.textStatus.country = $scope.sendingText[$scope.lang]
      MasterCtrl.RestMcountry.getQuery().$promise.then((p_data) => {
        MasterCtrl.MCountryController.save(p_data.mcountry).$promise.then((p_result) => {
          if (p_result.status === 200){
            $scope.state.country = true
            $scope.textStatus.country = $scope.successText[$scope.lang]
          } else {
            $scope.state.country = true
            $scope.textStatus.country = $scope.errorText[$scope.langs]
          }
          resolve()
        }, () => {
          $scope.state.country = true
          $scope.textStatus.country = $scope.errorText[$scope.lang]
          reject()
        })
      })
    })
  }

  $scope.getMsourceJob = function(){
    return $q(function(resolve, reject) {
      $scope.promt.sourceJob = true
      $scope.state.sourceJob = false
      $scope.textStatus.sourceJob = $scope.sendingText[$scope.lang]
      MasterCtrl.RestMsourceJob.getQuery().$promise.then((p_data) => {
        MasterCtrl.MSourceJobController.save(p_data.msourcejob).$promise.then((p_result) => {
          if (p_result.status === 200){
            $scope.state.sourceJob = true
            $scope.textStatus.sourceJob = $scope.successText[$scope.lang]
          } else {
            $scope.state.sourceJob = true
            $scope.textStatus.sourceJob = $scope.errorText[$scope.lang]
          }
          resolve()
        }, () => {
          $scope.state.sourceJob = true
          $scope.textStatus.sourceJob = $scope.errorText[$scope.lang]
          reject()
        })
      })
    })
  }
  $scope.getMgender = function(){
    return $q(function(resolve, reject) {
      $scope.promt.gender = true
      $scope.state.gender = false
      $scope.textStatus.gender = $scope.sendingText[$scope.lang]
      MasterCtrl.RestMgender.getQuery().$promise.then((p_data) => {
        MasterCtrl.MGenderController.save(p_data.result).$promise.then((p_result) => {
          if (p_result.status === 200){
            $scope.state.gender = true
            $scope.textStatus.gender = $scope.successText[$scope.lang]
          } else {
            $scope.state.gender = true
            $scope.textStatus.gender = $scope.errorText[$scope.lang]
          }
          resolve()
        }, () => {
          $scope.state.gender = true
          $scope.textStatus.gender = $scope.errorText[$scope.lang]
          reject()
        })
      })
    })
  }
  $scope.getMRelations = function(){
    return $q(function(resolve, reject) {
      $scope.promt.relations = true
      $scope.state.relations = false
      $scope.textStatus.relations = $scope.sendingText[$scope.lang]
      MasterCtrl.RestMrelations.getQuery().$promise.then((p_data) => {
        MasterCtrl.MRelationsController.save(p_data.result).$promise.then((p_result) => {
          if (p_result.status === 200){
            $scope.state.relations = true
            $scope.textStatus.relations = $scope.successText[$scope.lang]
          } else {
            $scope.state.relations = true
            $scope.textStatus.relations = $scope.errorText[$scope.lang]
          }
          resolve()
        }, () => {
          $scope.state.relations = true
          $scope.textStatus.relations = $scope.errorText[$scope.lang]
          reject()
        })
      })
    })
  }
  $scope.getMbloodGroup = function(){
    return $q(function(resolve, reject) {
      $scope.promt.bloodgroup = true
      $scope.state.bloodgroup = false
      $scope.textStatus.bloodgroup = $scope.sendingText[$scope.lang]
      MasterCtrl.RestMbloodgroup.getQuery().$promise.then((p_data) => {
        MasterCtrl.MBloodGroupController.save(p_data.result).$promise.then((p_result) => {
          if (p_result.status === 200){
            $scope.state.bloodgroup = true
            $scope.textStatus.bloodgroup = $scope.successText[$scope.lang]
          } else {
            $scope.state.bloodgroup = true
            $scope.textStatus.bloodgroup = $scope.errorText[$scope.lang]
          }
          resolve()
        }, () => {
          $scope.state.bloodgroup = true
          $scope.textStatus.bloodgroup = $scope.errorText[$scope.lang]
          reject()
        })
      })
    })
  }
  
  $scope.getMmaritalStatus = function(){
    return $q(function(resolve, reject) {
      $scope.promt.maritalStatus = true
      $scope.state.maritalStatus = false
      $scope.textStatus.maritalStatus = $scope.sendingText[$scope.lang]
      MasterCtrl.RestMmaritalStatus.getQuery().$promise.then((p_data) => {
        MasterCtrl.MMaritalStatusController.save(p_data.result).$promise.then((p_result) => {
          if (p_result.status === 200){
            $scope.state.maritalStatus = true
            $scope.textStatus.maritalStatus = $scope.successText[$scope.lang]
          } else {
            $scope.state.maritalStatus = true
            $scope.textStatus.maritalStatus = $scope.errorText[$scope.lang]
          }
          resolve()
        }, () => {
          $scope.state.maritalStatus = true
          $scope.textStatus.maritalStatus = $scope.errorText[$scope.lang]
          reject()
        })
      })
    })
  }
  $scope.getMmilitaryStatus = function(){
    return $q(function(resolve, reject) {
      $scope.promt.militaryStatus = true
      $scope.state.militaryStatus = false
      $scope.textStatus.militaryStatus = $scope.sendingText[$scope.lang]
      MasterCtrl.RestMmilitaryStatus.getQuery().$promise.then((p_data) => {
        MasterCtrl.MMilitaryStatusController.save(p_data.result).$promise.then((p_result) => {
          if (p_result.status === 200){
            $scope.state.militaryStatus = true
            $scope.textStatus.militaryStatus = $scope.successText[$scope.lang]
          } else {
            $scope.state.militaryStatus = true
            $scope.textStatus.militaryStatus = $scope.errorText[$scope.lang]
          }
          resolve()
        }, () => {
          $scope.state.militaryStatus = true
          $scope.textStatus.militaryStatus = $scope.errorText[$scope.lang]
          reject()
        })
      })
    })
  }
  $scope.getMpeopleStatus = function(){
    return $q(function(resolve, reject) {
      $scope.promt.peopleStatus = true
      $scope.state.peopleStatus = false
      $scope.textStatus.peopleStatus = $scope.sendingText[$scope.lang]
      MasterCtrl.RestMpeopleStatus.getQuery().$promise.then((p_data) => {
        MasterCtrl.MPeopleStatusController.save(p_data.result).$promise.then((p_result) => {
          if (p_result.status === 200){
            $scope.state.peopleStatus = true
            $scope.textStatus.peopleStatus = $scope.successText[$scope.lang]
          } else {
            $scope.state.peopleStatus = true
            $scope.textStatus.peopleStatus = $scope.errorText[$scope.lang]
          }
          resolve()
        }, () => {
          $scope.state.peopleStatus = true
          $scope.textStatus.peopleStatus = $scope.errorText[$scope.lang]
          reject()
        })
      })
    })
  }

  $scope.getMhonourably = function(){
    return $q(function(resolve, reject) {
      $scope.promt.honourably = true
      $scope.state.honourably = false
      $scope.textStatus.honourably = $scope.sendingText[$scope.lang]
      MasterCtrl.RestMhonourably.getQuery().$promise.then((p_data) => {
        MasterCtrl.MHonourablyController.save(p_data.result).$promise.then((p_result) => {
          if (p_result.status === 200){
            $scope.state.honourably = true
            $scope.textStatus.honourably = $scope.successText[$scope.lang]
          } else {
            $scope.state.honourably = true
            $scope.textStatus.honourably = $scope.errorText[$scope.lang]
          }
          resolve()
        }, () => {
          $scope.state.honourably = true
          $scope.textStatus.honourably = $scope.errorText[$scope.lang]
          reject()
        })
      })
    })
  }

  $scope.getMoccupation = function(){
    return $q(function(resolve, reject) {
      $scope.promt.occupation = true
      $scope.state.occupation = false
      $scope.textStatus.occupation = $scope.sendingText[$scope.lang]
      MasterCtrl.RestMoccupation.getQuery().$promise.then((p_data) => {
        MasterCtrl.MOccupationController.save(p_data.result).$promise.then((p_result) => {
          if (p_result.status === 200){
            $scope.state.occupation = true
            $scope.textStatus.occupation = $scope.successText[$scope.lang]
          } else {
            $scope.state.occupation = true
            $scope.textStatus.occupation = $scope.errorText[$scope.lang]
          }
          resolve()
        }, () => {
          $scope.state.occupation = true
          $scope.textStatus.occupation = $scope.errorText[$scope.lang]
          reject()
        })
      })
    })
  }

  $scope.getMlanguage = function(){
    return $q(function(resolve, reject) {
      $scope.promt.language = true
      $scope.state.language = false
      $scope.textStatus.language = $scope.sendingText[$scope.lang]
      MasterCtrl.RestMlanguage.getQuery().$promise.then((p_data) => {
        MasterCtrl.MSpokenLanguageController.save(p_data.result).$promise.then((p_result) => {
          if (p_result.status === 200){
            $scope.state.language = true
            $scope.textStatus.language = $scope.successText[$scope.lang]
          } else {
            $scope.state.language = true
            $scope.textStatus.language = $scope.errorText[$scope.lang]
          }
          resolve()
        }, () => {
          $scope.state.language = true
          $scope.textStatus.language = $scope.errorText[$scope.lang]
          reject()
        })
      })
    })
  }

  $scope.getMJobGroup = function(){
    return $q(function(resolve, reject) {
      $scope.promt.mjobgroup = true
      $scope.state.mjobgroup = false
      $scope.textStatus.mjobgroup = $scope.sendingText[$scope.lang]
      MasterCtrl.RestMjobGroup.getQuery().$promise.then((p_data) => {
        MasterCtrl.MJobGroupController.save(p_data.result).$promise.then((p_result) => {
          if (p_result.status === 200){
            $scope.state.mjobgroup = true
            $scope.textStatus.mjobgroup = $scope.successText[$scope.lang]
          } else {
            $scope.state.mjobgroup = true
            $scope.textStatus.mjobgroup = $scope.errorText[$scope.lang]
          }
          resolve()
        }, () => {
          $scope.state.mjobgroup = true
          $scope.textStatus.mjobgroup = $scope.errorText[$scope.lang]
          reject()
        })
      })
    })
  }

  $scope.getMPaperList = function(){
    return $q(function(resolve, reject) {
      $scope.promt.mpaperlist = true
      $scope.state.mpaperlist = false
      $scope.textStatus.mpaperlist = $scope.sendingText[$scope.lang]
      MasterCtrl.RestMpaperList.getQuery().$promise.then((p_data) => {
        MasterCtrl.MPaperListController.save(p_data.result).$promise.then((p_result) => {
          if (p_result.status === 200){
            $scope.state.mpaperlist = true
            $scope.textStatus.mpaperlist = $scope.successText[$scope.lang]
          } else {
            $scope.state.mpaperlist = true
            $scope.textStatus.mpaperlist = $scope.errorText[$scope.lang]
          }
          resolve()
        }, () => {
          $scope.state.mpaperlist = true
          $scope.textStatus.mpaperlist = $scope.errorText[$scope.lang]
          reject()
        })
      })
    })
  }

  $scope.getMResidence = function(){
    return $q(function(resolve, reject) {
      $scope.promt.mresidence = true
      $scope.state.mresidence = false
      $scope.textStatus.mresidence = $scope.sendingText[$scope.lang]
      MasterCtrl.RestMresident.getQuery().$promise.then((p_data) => {
        MasterCtrl.MResidentController.save(p_data.result).$promise.then((p_result) => {
          if (p_result.status === 200){
            $scope.state.mresidence = true
            $scope.textStatus.mresidence = $scope.successText[$scope.lang]
          } else {
            $scope.state.mresidence = true
            $scope.textStatus.mresidence = $scope.errorText[$scope.lang]
          }
          resolve()
        }, () => {
          $scope.state.mresidence = true
          $scope.textStatus.mresidence = $scope.errorText[$scope.lang]
          reject()
        })
      })
    })
  }

  $scope.sendAllConfig = function () {
    return [
      this.getProvince(),
      this.getZipcode(),
      this.getJob(),
      this.getEdu(),
      this.getFaculty(),
      this.getNationality(),
      this.getPrefix(),
      this.getRace(),
      this.getReligion(),
      this.getSkillLevel(),
      this.getConfig(),
      this.getDistrict(),
      this.getMajor(),
      this.getMinstitute(),
      this.getMcountry(),
      this.getMsourceJob(),
      this.getMbloodGroup(),
      this.getMmaritalStatus(),
      this.getMmilitaryStatus(),
      this.getMpeopleStatus(),
      this.getMRelations(),
      this.getMgender(),
      this.getMhonourably(),
      this.getMoccupation(),
      this.getMlanguage(),
      this.getMJobGroup(),
      this.getMPaperList(),
      this.getMResidence()
    ]
  }
  $scope.sendAll = function () {
   let execAll = $scope.sendAllConfig()
    Promise.all(execAll).then(function () {
      MasterCtrl.GeneratorController.auto().$promise.then(($res) => {
        // console.log($res)
      })
    })
  }
}]);