"use strict";

appCore.controller("MyTeamListCtrl", ["$state", "$scope", "$http", "$timeout", "vServices", "filterFilter", function ($state, $scope, $http, $timeout, vServices, filterFilter) {
  $scope.$root.page.title = "My Teams";
  $scope.projects = [];
  $scope.myHeroesProjects = [];

  const init = function () {
    vServices.callApi("allocation/listbyempid/" + $scope.$root.gUser.id, "GET", {}).then(function (res) {
      if (res) {
        $scope.projects = res.length > 0 ? res : [res];
        let allTeam = [];
        $scope.projects.forEach((o) => {
          allTeam.push(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) {
              try {
                if (res) $scope.myHeroesProjects.push({proj: projInfo, data: res});
                resolve(true);
              }catch (e){
                resolve(false);
              }
            });
          }));
        });

        Promise.all(allTeam).then(function(data) {
          try {$scope.$root.callDataTable(".myTeamList", 2000);}catch (e){console.error(e);}
        });
      }
    });
  };
  init();
}]);
