"use strict";
appCore.controller("AssignRequisitionFormCtrl", ["$state", "$scope", "$http", "$timeout", "vServices",
  function ($state, $scope, $http, $timeout, vServices) {
    $scope.$root.page.title = "Requisition Assign ";
    $scope.btn = {text: "Save"};
    $scope.params = $state.params;
    $scope.heroes = [];
    $scope.heroesAdmin = [];
    $scope.aclProfileList = [];
  
    $scope.hero = {heroes:'',req_id:$scope.params.requisition_id};

    $scope.save = (hero) => {
        vServices.callApi("requi/assignReqFormToAuth", "POST",hero)
        .then(function (res) {
          if(res){
            toastr.success(res, "Success!", {timeOut: 5000});
            $state.go('requisitionDashboard', {}, {reload: true});          
          }
        });
    };

    $scope.getOnchangeVal = (req_id) =>{
      vServices.callApi("requi/assignedReqAuth?req_id="+req_id, "GET")
      .then(function (res) {
          if(res){
            $scope.hero.heroes = res.map(x=>x.hero_id);
          }
      });
    }

    const init = function () {
        vServices.callApi("department/all", "GET", {}).then((res) => {
            $scope.departmentOptions = res;
          });
          vServices.callApi("designation/all", "GET", {}).then((res) => {
            if (res && res.length > 0) {
              $scope.designationOptions = res;
              $scope.designationOptions.unshift({"id": 0, "desg_name": "Select", "desg_description": null});
            }
        });
            /* Get requisition list*/
            vServices.callApi("requi/all", "GET", {}).then(res => {
              if (res) {
                  $scope.requisitionList = res.filter(x=>x.req_status==1);
              }
          });
      // console.log($scope.$root.gUser);
      vServices.callApi("requi/getAllHeroes", "GET", {}).then(function (res) {
        if (res && res.length > 0) {
          res.sort((a, b) => {
            let prop = "emp_first_name";
            return a[prop] > b[prop] ? 1 : a[prop] < b[prop] ? -1 : 0;
          });
          res.forEach((o) => o.is_admin ? $scope.heroesAdmin.push(o) : "");
          $scope.heroes = res.filter(x=>x.emp_status=='active' && (x.id==22 || x.id==182 || x.id==163 || x.id==186));
          // $scope.reqData.emp_id = $scope.heroes[0].employeeid;
        }
      });
    };
    init();
  }
]);



