"use strict";
appCore.controller("assignAclProfile", ["$state", "$scope", "$http", "$timeout", "vServices",
  function ($state, $scope, $http, $timeout, vServices) {
    $scope.$root.page.title = "Manage ACL";
    $scope.btn = {text: "Save"};

    $scope.heroes = [];
    $scope.heroesAdmin = [];
    $scope.aclProfileList = [];
  
    $scope.hero = {heroes:[],emp_acl_profile:0,type:0, emp_designation:0, emp_department:0};

    $scope.save = (hero) => {
        vServices.callApi("acl/assignAclProfileToMultipleHeroes", "PUT",hero)
        .then(function (res) {
          toastr.success(res, "Success!", {timeOut: 5000});
          $state.go($state.current, {}, {reload: true});
          // $(document).find("#emp_id").focus();
        });
    };

    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});
            }
        });

        vServices.callApi("acl/aclProfileList", "GET", {}).then(function (res) {
            if (res && res.length > 0) 
            $scope.aclProfileList = res;
            // console.log($scope.aclProfileList);
          });
      // console.log($scope.$root.gUser);
      vServices.callApi("employee/allDataForACL", "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;
          // $scope.reqData.emp_id = $scope.heroes[0].employeeid;
        }
      });
    };
    init();
  }
]);



