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

'use strict'
recModule.controller('REC_CANDIDATE', ['$scope', '$mdDialog', 'RecService', function ($scope, $mdDialog, RecService) {
  $scope.isReady = false
  $scope.lang = getLang();
  $scope.now = ''
  $scope.userJobid = ''
  $scope.userBu3 = ''
  $scope.userBu2 = ''
  $scope.isActPosition = ''
  $scope.employeeid = ''
  $scope.applicants = []
  $scope.jobs = []
  $scope.basicConfig = {}
  $scope.propertyNameIn = ''
  $scope.reverseIn = true
  $scope.propertyNameEx = ''
  $scope.reverseEx = true
  $scope.openReqs = []
  $scope.buFilter = {}
  let cscForm = document.cscform
  
  RecService.RestMrequest.getManPowerRequest().$promise.then(function (data) {
    $scope.openReqs = data.result.filter((item) => {
      return item.requestStatus === "2"
    })
  })


  let jobSetOne = ['S1001', 'S2001', 'O1001', 'O2001', 'O3001', 'O4001', 'E1001',' M1002']
  let jobSetTwo = ['M2002', 'M1003', 'M2001', 'E2001', 'M2003']
  let jobSetThree = ['T1001', 'T1002']
  $scope.filterDirector = ($item) => {
    if (jobSetOne.includes($item.jobid)) {
      if ($scope.userJobid === 'M1003' || $scope.userJobid === 'M2002' || $scope.userJobid === 'M2001') {
        return $item.bu3.idFromMreq === $scope.userBu3
      } else {
        return false
      }
    } else if (jobSetTwo.includes($item.jobid)) {
      return $scope.isActPosition === '1'
    } else if (jobSetThree.includes($item.jobid)) {
      return $scope.userJobid === 'V0001'
    } else {
      return false
    }
  }
  
  // $scope.$watch('user', ($res) => {
  //   if ($res !== '') {
  //     RecService.RestEmpCenter.getQuery({usernameid: $res}).$promise.then(function ($data) {
  //       if ($data.result.length > 0) {
  //         let data = $data.result[0]
  //         for (let i = 1; i <= 4; i++) {
  //           $scope.buFilter.viewAll = data.isviewall == '1'
  //           let currentBu = $scope.buFilter['bu'+i] = {}
  //           currentBu.active = data['isbu'+i] == '1'
  //           currentBu.show = data['bu'+i+'status'] == '1'
  //           currentBu.list = (data['bu'+i+'list'] != '') ? data['bu'+i+'list'].split(',') : []
  //         }
  //       }
  //     })
  //   }
  // })
  // $scope.filterBu = ($item) => {
  //   let chkBu = ($index) => {
  //     if ($index && !angular.equals($scope.buFilter, {})) {
  //       if ($scope.buFilter['bu'+$index].active) {
  //         if ($scope.buFilter['bu'+$index].list.length > 0) {
  //           if ($scope.buFilter['bu'+$index].show) {
  //             return $scope.buFilter['bu'+$index].list.includes($item['bu'+$index].id)
  //           } else {
  //             return !$scope.buFilter['bu'+$index].list.includes($item['bu'+$index].id)
  //           }
  //         } else {
  //           return true
  //         }
  //       } else {
  //         return true
  //       }
  //     }
  //   }
  //   if ($scope.buFilter.viewAll) {
  //     return true
  //   } else {
  //     return chkBu(1) && chkBu(2) && chkBu(3) && chkBu(4) && chkBu(5)
  //   }
  // }
  
  $scope.goPages = ($candidateid) => {
    cscForm.action = "../RECRUIT/FillFormApplicant.jsp"
    cscForm.__candidateid.value = $candidateid
    cscForm.target = "_blank"
    cscForm.submit()
  }
  
  $scope.sortByIn = function(propertyName) {
    $scope.reverseIn = ($scope.propertyNameIn  === propertyName) ? !$scope.reverseIn : false;
    $scope.propertyNameIn = propertyName;
  }
  
  $scope.sortByEx = function(propertyName) {
    $scope.reverseEx = ($scope.propertyNameEx === propertyName) ? !$scope.reverseEx : false;
    $scope.propertyNameEx = propertyName;
  }
  
  $scope.availableRequest = ($app) => {
    let result = false
    const exist = $scope.openReqs.find(($item) => {
      return $app.requestid === $item.requestid
    })
    if (exist) {
      let today = new Date($scope.now);
      let endDate = new Date(exist.lastDate)
      result = daysBetween(today, endDate) >= 0
    }
    return result
  }
  
  RecService.RestMcandidate.getQuery().$promise.then(function ($res) {
    $scope.applicants = $res.result
    // console.log($res.result);
    $scope.isReady = true
  })
  
  RecService.ManageConfig.query({cname: 'BASIC'}).$promise.then(function (data) {
    $scope.basicConfig = data.result.object
  })
  
  $scope.updateWantInterview = ($applicantid, $event) => {
    RecService.RestMapplicant.updateWantInterview({applicantid: $applicantid, wantinterview: $event.target.value}).$promise.then(function ($res) {
    
    })
  }
  
  $scope.filterConditions = ($app) => {
    return $app.applicantid === $scope.conditions.filter.applicantid &&
      ($app.personal.fname === $scope.conditions.filter.name || $app.personal.fname == $scope.conditions.filter.name) &&
      $app.jobid === $scope.conditions.filter.jobid &&
      $app.wantinterview === $scope.conditions.filter.wantinterview
  }
  
  $scope.filterInternal = ($app) => {
    return $app.internal == '1'
  }
  
  $scope.filterExternal = ($app) => {
    return $app.internal == '0'
    
  }
  
  
  // Jquery
  
  
}
])