'use strict'
angular.module('Print').controller('PrintManagement', ['$scope', '$location', 'PrintServices', function($scope, $location, PrintServices){
  let params = $location.absUrl().substring($location.absUrl().indexOf('?')+1, $location.absUrl().length).split('=')
  $scope.today = new Date()
  $scope.lang = getLang()
  $scope.applicant = {}
  $scope.test = '02'
  $scope.marryprop = {
    name: '',
    job: '',
    childnum: '',
    position: '',
    tel: '',
    Office: ''
  }
  console.log(params)
  PrintServices.RestMapplicant.getQuery({appid: params[1]}).$promise.then((data) => {
    $scope.applicant = data.result
    // console.log($scope.applicant);
    if ($scope.applicant.hasOwnProperty('family')){
      let marryobj = $scope.applicant.family.family.filter((p_obj) => {
        return p_obj.relation == 1
      })

      if (marryobj.length > 0){
        let marrylast = marryobj[marryobj.length-1]
        $scope.marryprop = {
          name: $scope.getMarryName(marrylast),
          job: $scope.getMarryJob(marrylast),
          childnum: '',
          position: '',
          tel: $scope.getMarryPhone(marrylast),
          Office: $scope.getMarryOffice(marrylast)
        }
      }
    }
    // console.log($scope.applicant)

  })
  $scope.print = () => {
    window.print()
  }

  $scope.getDates = (p_datestr = '') => {
    let dates = new Date(p_datestr)
    let result = dates.getFullYear()

    if (p_datestr != ''){
      if (parseInt(dates.getMonth()+1) <= 9){
        result = result+'-0'+parseInt(dates.getMonth()+1)
      } else {
        result = result+'-'+parseInt(dates.getMonth()+1)
      }

      if (parseInt(dates.getDate()) <= 9){
        result = result+'-0'+dates.getDate()
      } else {
        result = result+'-'+dates.getDate()
      }
    }

    return result
  }

  $scope.getMarryName = (p_obj) => {
    return `${p_obj.fname} ${p_obj.lname}`
  }

  $scope.getMarryJob = (p_obj) => {
    return p_obj.occupation
  }

  $scope.getMarryPhone = (p_obj) => {
    return p_obj.phone
  }

  $scope.getMarryOffice = (p_obj) => {
    return p_obj.workplace
  }

  $scope.getCurrentAge = (birthdate) => {
    if (birthdate) {
      let birthDate = new Date(birthdate)
      let ageDifMs = new Date() - birthDate.getTime();
      let ageDate = new Date(ageDifMs); // miliseconds from epoch
      return Math.abs(ageDate.getUTCFullYear() - 1970);
    }
  }

  $scope.getFullAddr = (data) => {
    if(data != undefined){
      // console.log(data)
      let textResult = ""
      if (data.addr) {
        textResult += `เลขที่ ${data.addr}`
      }
      if (data.roomNo) {
        textResult += ` ห้อง ${data.roomNo}`
      }
      if (data.floor) {
        textResult += ` ชั้น ${data.floor}`
      }
      if (data.moo) {
        textResult += ` หมู่ ${data.moo}`
      }
      if (data.village) {
        textResult += ` หมู่บ้าน ${data.village}`
      }
      if (data.soi) {
        textResult += ` ซอย ${data.soi}`
      }
      if (data.road) {
        textResult += ` ถนน ${data.road}`
      }
      if (data.district) {
        textResult += ` ตำบล ${data.district}`
      }
      if (data.zipcode) {
        textResult += ` อำเภอ ${data.zipcode.disname.tha} จังหวัด ${data.zipcode.proname.tha}`
      }
      return textResult
    }

  }

  $scope.getInstituteName = (objIns) => {
    if (objIns.universitylist !== null) {
      return objIns.universitylist.name[lang]
    } else {
      return objIns.searchtext
    }
  }

  $scope.getMajorName = (objMajor) => {
    if (objMajor.majorlist !== null) {
      return objMajor.majorlist.name[lang]
    } else {
      return objMajor.searchtext
    }
  }

  // $scope.myNumber = 3;
  $scope.getNumber = function(num) {
    if(num<0) {
      num = 0
    }
    return new Array(num);
  }
  $scope.getRow = function(length) {
    let num = length + 1
    return new Array(num);
  }

  $scope.famrelation = [{
    id: '1',
    name: {
      tha: 'คู่สมรส',
      eng: 'Spouse'
    }
  },{
    id: '2',
    name: {
      tha: 'พ่อ',
      eng: 'Father'
    }
  },{
    id: '3',
    name: {
      tha: 'แม่',
      eng: 'Mother'
    }
  },{
    id: '4',
    name: {
      tha: 'พี่ชาย',
      eng: 'Older Brother'
    }
  },{
    id: '5',
    name: {
      tha: 'น้องชาย',
      eng: 'Younger Brother'
    }
  },{
    id: '6',
    name: {
      tha: 'พี่สาว',
      eng: 'Older Sister'
    }
  },{
    id: '7',
    name: {
      tha: 'น้องสาว',
      eng: 'Younger Sister'
    }
  },{
    id: '8',
    name: {
      tha: 'บุตร',
      eng: 'Child'
    }
  }]

  $scope.getFamilyRelation = (id) => {
    let relation = $scope.famrelation.find((item) => {
      return item.id === id
    })

    if (relation) {
      return relation.name[lang]
    }

  }
}])