/**
 * Created by PPMaj on 11/24/2016.
 */

'use strict'
recModule.controller('REC_REQUEST', ['$scope', '$mdDialog', 'RecService', 'SwapLang', function ($scope, $mdDialog, RecService, SwapLang) {
  $scope.swlang = new swaplang()
  $scope.lang = getLang();
  $scope.now = ''
  $scope.employeeid = ''
  $scope.listReqs = []
  $scope.basicConfig = {}

  RecService.ManageRequestForm.query({funcname: 'getAllMrequest', reqid: '0'}).$promise.then(function (data) {
    $scope.listReqs = JSON.parse(data.result);
    // console.log($scope.listReqs);
    // $scope.isReady = true
  })

  RecService.ManageConfig.query({cname: 'BASIC'}).$promise.then(function (data) {
    $scope.basicConfig = data.result.object
  })


  $scope.availableRequest = ($item) => {
    let today = new Date($scope.now);
    let endDate = new Date($item.enddate);
    let availableNow = daysBetween(today, endDate) >= 0
    let forInternal = $item.req_type == '2' || $item.req_type == '3'
    return availableNow && forInternal
  }

  $scope.goFillFormApplicant = ($jobid, $requestid) => {
    let isPass = true
    let serverDate = new Date($scope.now)
    let restrictAfterApply = $scope.basicConfig.hasOwnProperty('restrictAfterApply') ? $scope.basicConfig.restrictAfterApply : '0'
    RecService.RestMapplicant.getLastApply({employeeId: $scope.employeeid}).$promise.then(function ($res) {
      let diffDays = daysBetween(new Date($res.result), serverDate)
      const promCheck = new Promise((resolve, reject) => {
        if (!parseInt(restrictAfterApply) || diffDays > parseInt(restrictAfterApply)) {
          RecService.RestMapplicant.existsByEmployeeIdAndJobId({employeeid: $scope.employeeid, jobid: $jobid}).$promise.then(($res) => {
            //console.log($res, $scope.basicConfig.duplicateResume)
            let isExist = $res.result === 'true'
            if (isExist) {
              if (!$scope.basicConfig.duplicateResume) {
                // alert can't duplicate
                isPass = false
                alert(MyCode[252])
              }
              resolve()
            } else {
              if (!$scope.basicConfig.multipleResume) {
                RecService.RestMapplicant.countByEmployeeId({employeeid: $scope.employeeid}).$promise.then(($res) => {
                  let counted = parseInt($res.result)
                  if (counted > 0) {
                    // alert can't multiple
                    isPass = false
                    alert(MyCode[251])
                  }
                })
              }
              resolve()
            }
          })
        } else {
          // alert recently apply
          isPass = false
          alert(MyCode[253])
          resolve()
        }
      })
      Promise.all([promCheck]).then(() => {
        if (isPass) {
          cscform.action = "../RECRUIT/FillFormApplicant.jsp"
          cscform.target = "_blank"
          cscform.__internal.value = 'true'
          cscform.__jobid.value = $jobid
          cscform.__requestid.value = $requestid
          cscform.submit()
        }
      })
    })
  }


  $scope.replaceNewLine = ($text) => {
    return $text.replace(/\n/g, "<br/>");
  }


}])