"use strict";
appCore.controller("MyProjectListCtrl", ["$state", "$scope", "$http", "$timeout", "vServices", function ($state, $scope, $http, $timeout, vServices) {
  $scope.$root.page.title = "My Projects";
  $scope.projects = [];
  $scope.myHeroesProjects = [];
  $scope.empACL = $scope.$root.gUser.emp_acls;
  $scope.projectWithRCAdmin = [];

  const init = function () {
    if ($scope.$root.gUser.emp_acls == '') {
      window.location.reload();
    }
    if ($scope.empACL != null) {
      if ($scope.empACL.search("63") != -1 || $scope.empACL.search("64") != -1 || $scope.empACL.search("65") != -1) {
        vServices.callApi("project/getAllRevenueCenterAdmin?emp_acls=" + $scope.empACL, "GET", {}).then(function (res) {
          if (res) {
            $scope.projects = res;
            console.log(res, $scope.empACL);
            $scope.$root.callDataTable("#myProjectList");
          }
        });
      } else {
        vServices.callApi("allocation/listbyempid/" + $scope.$root.gUser.id, "GET", {}).then(function (res) {
          if (res) {
            $scope.projects = res.length > 0 ? res : [res];
            $scope.projects.forEach((o) => {
              new Promise((resolve, reject) => {
                const projInfo = { id: o.projectid, name: o.proj_name, cdate: o.proj_created_date };
                vServices.callApi("allocation/list/" + o.projectid, "GET", {}).then(function (res) {
                  if (res) $scope.myHeroesProjects.push({ proj: projInfo, data: res });
                  $scope.$root.callDataTable("#myProjectList");
                  resolve(true);
                });
              });
            });
          }
        });
      }
    }
    else {
      vServices.callApi("allocation/listbyempid/" + $scope.$root.gUser.id, "GET", {}).then(function (res) {
        if (res) {
          $scope.projects = res.length > 0 ? res : [res];
          $scope.projects.forEach((o) => {
            new Promise((resolve, reject) => {
              const projInfo = { id: o.projectid, name: o.proj_name, cdate: o.proj_created_date };
              vServices.callApi("allocation/list/" + o.projectid, "GET", {}).then(function (res) {
                if (res) $scope.myHeroesProjects.push({ proj: projInfo, data: res });
                $scope.$root.callDataTable("#myProjectList");
                resolve(true);
              });
            });
          });
        }
      });
    }

  };
  init();
}]);
