'use strict'

angular.module('myHR').controller('JobRouter', ['$scope', '$http', 'SwapLang', '$mdDialog', 'RootServices', function($scope, $http, SwapLang, $mdDialog, RootServices){
  $scope.swlang = SwapLang

  $scope.checkState = function(rstate){
    let myclass = ''
    switch (rstate.status) {
      case 1:
        myclass = 'circle-green'
        break
      case -1:
        myclass = 'circle-red'
        break
      case -2:
        myclass = 'circle-orange'
        break
      case 2:
        myclass = 'circle-darkslate'
        break
      case 3:
        myclass = 'circle-pink'
        break
      case -10:
        myclass = 'circle-yellow'
        break
      case 10:
        myclass = 'circle-blue'
        break
    }

    return myclass
  }

  $scope.checkPointer = function(rstep){
    let cursorclass = ''
    if (rstep === 2){
      cursorclass = 'showpointer'
    }

    return cursorclass
  }

  $scope.openExam = function(router){
    if (router.step === 2){
      $mdDialog.show({
        controller: 'PreviewExam',
        templateUrl: '../JS/AngularTools/JobRouter/preview.exam.html',
        clickOutsideToClose: false,
        locals: {router: router, allobj: $scope.obj}
      }).then(function(result){
        router.status = result.state
        $scope.saveRouter()
      })
    }
  }

  $scope.saveRouter = function(){
    let objsend = {
      appid: $scope.appid,
      router: $scope.obj
    }

    let objjb = {
      appid: $scope.appid.replace('JB_', ''),
      router: $scope.obj
    }

    RootServices.RestMapplicant.updateRouter(objsend).$promise.then(function(data){

      RootServices.JbTapplicantController.updateApplicant(objjb).$promise.then(function(data){
        $mdDialog.alert().clickOutsideToClose(true).textContent('Update State complete')
      })

    })

  }
}])