"use strict";
appCore.controller("AccountViewCtrl", ["$state", "$scope", "$http", "$timeout", "vServices", function ($state, $scope, $http, $timeout, vServices) {
  $scope.param = $state.params;
  $scope.account = [];
  $scope.projects = [];
  $scope.accountAllocation = [];
  $scope.myHeroesProjects = [];

  // $scope.getNonBillableHours = (data) => data ? data.map(o=>parseFloat(o.partialHours > o.billableHours  ? o.partialHours - o.billableHours : 0)).reduce((a, b)=>a+b) : 0;
  // $scope.getBillableHours = (data) => data ? data.map(o=>parseFloat(o.billableHours)).reduce((a, b)=>a+b) : 0;
  $scope.getNonBillableHours = (data) => data ? data.map(o=>parseFloat(o.partialHoursNonBillable)).reduce((a, b)=>a+b) : 0;
  $scope.getBillableHours = (data) => data ? data.map(o=>parseFloat(o.partialHoursBillable)).reduce((a, b)=>a+b) : 0;
  $scope.statusTrueFilter = (o) => o.proj_status === "active";
  $scope.statusFalseFilter = (o) => o.proj_status === "inactive";
  const init = function () {
    if ($scope.param && typeof $scope.param.accountid !== "undefined") {
      vServices.callApi("account/" + $scope.param.accountid, "GET", {}).then((res) => {
        if (res) {
          $scope.account = res;
          $scope.$root.page.title = "Account view : " + $scope.account.acc_company_name;
        }

      });
      vServices.callApi("project/account/" + $scope.param.accountid, "GET", {}).then((res) => {
        if (res && typeof res !== 'string') {
          $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) {
                if (res) $scope.myHeroesProjects.push({proj: projInfo, data: res});
                resolve(true);
              });
            }));
          });
          Promise.all(allTeam).then(function(data) {
            try {$scope.$root.callDataTable(".teamView", 2000);}catch (e){console.error(e);}
          });*/
        }
        $scope.$root.callDataTable("#accountView");
      });
      vServices.callApi("allocation/listbyaccid/" + $scope.param.accountid, "GET", {}).then((res) => {
        if (res) {
          res.partialHoursNonBillable = res.partialHoursNonBillable ? parseFloat(res.partialHoursNonBillable) : 0;
          res.partialHoursBillable = res.partialHoursBillable ? parseFloat(res.partialHoursBillable) : 0;
          res.heroCount = res.heroCount ? parseFloat(res.heroCount) : 0;
          res['totalAllocation'] = res.partialHoursNonBillable + res.partialHoursBillable;
          $scope.accountAllocation = res;
          $scope.$root.callDataTable(".teamView", 1000);
          //teamView
        }
      });
    }
  };
  init();
}]);
