/**
 * Created by PPMaj on 12/20/2016.
 */
'use strict'

angular.module('RECRUIT').controller('ManpowerRequest', ['$scope', '$http', 'RecService', '$log', '$filter', '$mdDialog', '$window', function ($scope, $http, RecService, $log, $filter, $mdDialog, $window) {
  $scope.isReady = false
  let cscForm = document.cscform
  $scope.budesc = {};
  $scope.mreqs = [];
  $scope.swlang = new swaplang()
  $scope.basicConfig = {}
  $scope.conditions = {
    'filter': {
      'all': '',
      'status': {
        'normal': '',
        'inProcess': '',
        'completed': '',
        'cancel': ''
      },
      'date': {
        'start': '',
        'eng': ''
      }
    }
  };
  $scope.cardStatusClass = function (mreq) {
    return {
      'card-request-normal': mreq.requestStatus == '1',
      'card-request-inprocess': mreq.requestStatus == '2',
      'card-request-completed': mreq.requestStatus == '3',
      'card-request-cancel': mreq.requestStatus == '4'
    }
  }
  $scope.filterStatus = function (item) {
    let empty = {
      normal : $scope.conditions.filter.status.normal === '',
      inProcess: $scope.conditions.filter.status.inProcess === '',
      completed: $scope.conditions.filter.status.completed === '',
      cancel: $scope.conditions.filter.status.cancel === ''
    }
    if (empty.normal && empty.inProcess && empty.completed && empty.cancel) {
      return item
    }
    else {
      return item.requestStatus === $scope.conditions.filter.status.normal ||
        item.requestStatus === $scope.conditions.filter.status.inProcess ||
        item.requestStatus === $scope.conditions.filter.status.completed ||
        item.requestStatus === $scope.conditions.filter.status.cancel;
    }
  };
  $scope.filterDate = function (item) {
    console.log(item)
    // let startDate = $scope.conditions.filter.date.start
    // let endDate = $scope.conditions.filter.date.end
    // if (startDate != '' && endDate != '') {
    //   return item.createDate >= startDate && item.createDate <= endDate
    // }
    // else {
    //   return item
    // }
  }
  $scope.showDetail = function (event, mreq) {
    $mdDialog.show({
      controller: DialogController,
      templateUrl: 'HTML/ManpowerRequest/DIALOGDETAILMREQ.html',
      parent: angular.element(document.body),
      targetEvent: event,
      clickOutsideToClose: true,
      locals: {
        mreq: mreq
      }
    })
  };
  function DialogController($scope, $mdDialog, mreq) {
    $scope.mreq = mreq;
    $scope.lang = getLang();
    $scope.cancel = function () {
      $mdDialog.cancel();
    };
  }

  $scope.goPages = function (mreq) {
    cscForm.__requestid.value = mreq.requestid
    cscForm.target = "_blank"
    cscForm.action = "REC101.jsp"
    cscForm.submit()
  }

  $scope.goPagess = function () {
    cscForm.__requestid.value = ''
    cscForm.action = "REC101.jsp"
    cscForm.target = "_blank"
    cscForm.submit()
  }


  RecService.ManageRequestForm.query({funcname: 'getBuDesc', reqid: '0'}).$promise.then(function (data) {
    $scope.budesc = data.result;
  });

  RecService.RestMrequest.query({funcname: 'getManPowerRequest'}).$promise.then(function (data) {
    $scope.mreqs = data.result;
    $scope.isReady = true
  })

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

  $(window).keydown(function (event) {
    if (event.keyCode === 13) {
      event.preventDefault();
    }
  });
  let label = {
    dateRange: {
      'clear': $scope.swlang.swap2String('SW013047'),
      'custom': $scope.swlang.swap2String('SW012076'),
      'apply': $scope.swlang.swap2String('SW013071')
    }
  }
  let rangesOptionEng = {
    'Today': [moment(), moment()],
    'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
    'Last 7 Days': [moment().subtract(6, 'days'), moment()],
    'Last 30 Days': [moment().subtract(29, 'days'), moment()],
    'Last Week': [moment().subtract(1, 'week').startOf('week'), moment().subtract(1, 'week').endOf('week')],
    'This Month': [moment().startOf('month'), moment().endOf('month')],
    'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
  }
  let rangesOptionTha = {
    'วันนี้': [moment(), moment()],
    'เมื่อวาน': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
    '7 วันล่าสุด': [moment().subtract(6, 'days'), moment()],
    '30 วันล่าสุด': [moment().subtract(29, 'days'), moment()],
    'สัปดาห์ที่แล้ว': [moment().subtract(1, 'week').startOf('week'), moment().subtract(1, 'week').endOf('week')],
    'เดือนนี้': [moment().startOf('month'), moment().endOf('month')],
    'เดือนที่แล้ว': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
  }
  $('input[name="dateFilter"]').daterangepicker({
    autoUpdateInput: false,
    locale: {
      cancelLabel: label.dateRange.clear,
      customRangeLabel: label.dateRange.custom,
      applyLabel: label.dateRange.apply
    },
    ranges: ($scope.lang == 'eng') ? rangesOptionEng : rangesOptionTha
  })
    .on('keydown', function (e) {
      e.preventDefault();
    })
    .on('apply.daterangepicker', function (ev, picker) {
      let strStart = picker.startDate.format('YYYY-MM-DD')
      let strEnd = picker.endDate.format('YYYY-MM-DD')
      let strShowDateRange = `${$filter('ddmmyyyyDate')(strStart)} - ${$filter('ddmmyyyyDate')(strEnd)}`
      $scope.$apply(function () {
        $scope.conditions.filter.date.start = strStart
        $scope.conditions.filter.date.end = strEnd
      });
      $(this).val(strShowDateRange);
    })
    .on('cancel.daterangepicker', function (ev, picker) {
      $scope.$apply(function () {
        $scope.conditions.filter.date.start = ''
        $scope.conditions.filter.date.end = ''
      });
      $(this).val('')
    });


}]);