"use strict";
appCore.controller("AclListCtrl", ["$state", "$scope", "$http", "$timeout", "vServices",
  function ($state, $scope, $http, $timeout, vServices) {
    $scope.$root.page.title = "Manage ACL";
    $scope.btn = {text: "Save"};
    $scope.cCode = ["primary", "success", "warning", "danger", "indigo", "navy", "purple", "maroon", "lime", "teal", "olive", "orange", "fuchsia"];

    // TODO To make this dynamic
    $scope.masters = [];
    $scope.heroes = [];
    $scope.heroesAdmin = [];
    $scope.acllist = [];
    $scope.acls = [];
    $scope.selHeroes = {model:[],searchStr: ""};
    $scope.reqData = {is_admin: true, emp_id: null, emp_acls: []};

    $scope.onHeroSelect = (d) => {
      if($scope.selHeroes.model.length === 1){
        vServices.callApi("acl/" + $scope.selHeroes.model.toString(), "GET", {}).then(function (res) {
          if (res && res.emp_acls)
            $scope.acls.forEach((value, key) => value["selected"] = ("," + res.emp_acls + ",").indexOf("," + value.acl_id + ",") > -1);
          else
            $scope.acls.forEach((value, key) => value["selected"] = false);
        });
      }
    };

    $scope.searchHeroes = function(hero) {
      // If there's no search term, show everything
      if (!$scope.selHeroes.searchStr) {
        return true;
      }

      var search = $scope.selHeroes.searchStr.toLowerCase();
      var firstName = (hero.emp_first_name || "").toLowerCase();
      var lastName = (hero.emp_last_name || "").toLowerCase();
      var code = (hero.emp_code || "").toLowerCase();

      // Check if the search string exists in any of the three fields
      return firstName.indexOf(search) > -1 || 
            lastName.indexOf(search) > -1 || 
            code.indexOf(search) > -1;
  };

    $scope.save = () => {
      if ($scope.selHeroes.model.length > 0) {
        $scope.reqData.emp_acls=[];
        $scope.acls.forEach((o) => o.selected ? $scope.reqData.emp_acls.push(o.acl_id) : "");
        $scope.reqData.emp_acls = $scope.reqData.emp_acls.toString();
        $scope.reqData.emp_id = $scope.selHeroes.model.toString();
        vServices.callApi("acl/edit", "PUT", $scope.reqData).then(function (res) {
          toastr.success(res, "Success!", {timeOut: 5000});
          $(document).find("#emp_id").focus();
        });
      }
    };

    $scope.selectAll = (moduleType, idx) => {
      $scope.masters[idx].selected = !$scope.masters[idx].selected;
      $scope.acls.forEach((value, key) => {
        if (value.acl_module === moduleType)
          value["selected"] = $scope.masters[idx].selected;
      });
    };
    const init = function () {
      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.heroes.unshift({"employeeid": 0, "emp_first_name": "Select", "emp_last_name": ""});
          //  $scope.heroesAdmin.unshift({"employeeid": 0, "emp_first_name": "Select", "emp_last_name": ""});
          $scope.reqData.emp_id = $scope.heroes[0].employeeid;
        }
      });
      vServices.callApi("acl/all", "GET", {}).then(function (res) {
        if (res && res.length > 0) {
          $scope.masters = [];
          let tempDupHandler = [];
          for (let i = 0; i < res.length; i++) {
            const o = res[i];
            if (tempDupHandler.indexOf(o.acl_module) === -1) {
              tempDupHandler.push(o.acl_module);
              $scope.masters.push({name: o.acl_module, selected: false, count: 0, color: $scope.cCode[i % $scope.cCode.length]});
            }
            else {
              $scope.masters[$scope.masters.length - 1].count++;
            }
          }
          $scope.acls = res;
        }
      });
    };
    init();
  }
]);



