"use strict";
appCore.controller("ResourcePoolListCtrl", ["$state", "$scope", "$http", "$timeout", "vServices", "filterFilter",
	function ($state, $scope, $http, $timeout, vServices, filterFilter) {
		$scope.poolDatas = [];
		$scope.allshadow = [];
		$scope.allallocation = [];
		$scope.allHeroes=[];
		$scope.$root.page.title = "List of Pool(Heroes) ";
		$scope.colOptions = {
			Pool100:{skills:true,IsCertified:false},
			PartialPool1:{skills:true},
			PartialPool2:{skills:true},
			AllocatedButNonBillable:{email:false,skills:true},
			BusinessUnitAllocation:{email:false,mobile:false,code:true},
			HeroCurrentAllocation:{email:false,mobile:false,code:true},
			newJoinee:{email:false,mobile:false,code:false,skills:false,IsCertified:false},
		};
		$scope.newJoinee = [];
		$scope.selectMonth = 3;
		$scope.today = moment().format("DD-MM-YYYY");
		$scope.lastDate = moment().subtract(3, 'months').format("DD-MM-YYYY");
		$scope.dTable = null;
		$scope.allRCs = [];
		$scope.selRevenueCenter = [];
		$scope.tempPoolData = [];
		$scope.tempArr = [];
		$scope.tArray = [];
		$scope.rv = '';

		
		$scope.poolStatus = (level) => level > 50 ? "badge-danger" : level > 20 ? "badge-warning" : "badge-success";
		
		$scope.selRevenue = (rc) =>{
			if($scope.dTable2)$scope.dTable2.destroy();
			var poolInd = $scope.poolDatas.findIndex(x => x.name == '100% Pool');
			var poolData = [];
			if(rc==0){
				poolData = $scope.tempArr;
				$scope.poolDatas[poolInd].data = poolData.length > 0 ? poolData : [];
			}else{
				poolData = $scope.tempArr.filter(x=> x.emp_revenue_center==rc);
				// $scope.filter.revenueCenters.includes(o.acc_revenue_center)
				$scope.poolDatas[poolInd].data = poolData.length > 0 ? poolData : [];
			}
			$scope.$root.callDataTable(".poolList", 500, (t) => {
				$scope.dTable2 = t;
			}, {orientation: "landscape"});	
			
		}

		$scope.getRCIds = () =>{
			if($scope.selRevenueCenter['ids']){
				selRevenue2();
			}else{
				$scope.refreshData();
			}
		}

		$scope.refreshData = () =>{
			if($scope.dTable2)$scope.dTable2.destroy();
			var poolInd = $scope.poolDatas.findIndex(x => x.name == '100% Pool');
			var poolData = [];
			poolData = $scope.tempArr;
			$scope.poolDatas[poolInd].data = poolData.length > 0 ? poolData : [];
			$scope.selRevenueCenter['ids'] = [];
			$scope.$root.callDataTable(".poolList", 500, (t) => {
				$scope.dTable2 = t;
			}, {orientation: "landscape"});	
		}

		const selRevenue2 = () =>{
			if($scope.dTable2)$scope.dTable2.destroy();
			var poolInd = $scope.poolDatas.findIndex(x => x.name == '100% Pool');
			var poolData = [];
			// $scope.poolDatas[poolInd].data = [];
			poolData = $scope.tempArr.filter(function(e) {
				return $scope.selRevenueCenter['ids'].indexOf(e.emp_revenue_center) != -1
			})
			$scope.poolDatas[poolInd].data = poolData;
			$scope.$root.callDataTable(".poolList", 500, (t) => {
				$scope.dTable2 = t;
			}, {orientation: "landscape"});	
			
		}

		$scope.selectAllrevenueCenters = () => $scope.selRevenueCenter['ids'] = $scope.allRCs.map(o => o.revenuecenterid);

		const init = function () {
			$scope.poolDatas = [];
			$scope.allshadow = [];
			var certifiesHeroes = [];
            vServices.callApi("employee/IsHeroCertified", "GET", {}).then(function (getRes) {
                certifiesHeroes = getRes;
            });
			vServices.callApi("/account/all_revenue_centers", "GET", {}).then(function (res) {
				if (res && res.length > 0) {
					$scope.allRCs = res;
					// $scope.selRevenueCenter['ids'] = res.map(o => o.revenuecenterid);
				}
			});
			vServices.callApi("employee/all", "GET", {}).then((res) => {
				res.forEach(o => {
					try{
						o.linemgr = res.filter(k => k.employeeid === o.emp_lm).map(k2 => ({
							employeeid: k2.employeeid,
							emp_first_name: k2.emp_first_name,
							emp_last_name: k2.emp_last_name
						}));
						if(o.linemgr) o.linemgr = o.linemgr[o.linemgr.length-1];
					}catch(e){}
					return o;
				});
				vServices.callApi("allocation/all", "GET", {}).then((allallocation) => {
					if (allallocation) $scope.allallocation = allallocation;
					vServices.callApi("allocation/allshadow", "GET", {}).then((allshadow) => {
						if (allshadow) $scope.allshadow = allshadow;
						if (res && res.length > 0) {
							$scope.allHeroes=res;
							$scope.allHeroes.forEach((o)=>{
								o.cert_hero_id = certifiesHeroes.includes(o.employeeid) ? '1' : '0';
							});
							if ($scope.$root.myAccess.resourcePoolList) {
								var getAllPoolData = $scope.allallocation.filter(o=> o.emp_pool > 7 & o.emp_pool < 9).map(o=> o.emp_id).filter((v, i, a) => a.indexOf(v) === i);
								var rData = [];
								vServices.callApi("employee/agingForHeroes", "POST", {'myVal':JSON.stringify(getAllPoolData)}).then((agingForHeroes) => {
									rData = res.filter(o => o.emp_pool > 7 & o.emp_pool < 9);
									rData.forEach(i=>{
										var dated = agingForHeroes.find(x=> x.emp_id == i.employeeid);
										i.lastAlloDate =  dated ? dated.toDate : moment(i.emp_doj);
										i.aging = dated ? moment().diff(dated.toDate,'days') : moment().diff(i.emp_doj, 'days');
										
									})
									$scope.tempPoolData = rData;
									$scope.tempArr = rData;
									$scope.tArray = rData;
									$scope.poolDatas.push({cardOutline: "card-danger", name: "100% Pool", data: rData});
								});
							}
							if ($scope.$root.myAccess.resourcePoolList) $scope.poolDatas.push({cardOutline: "card-danger", name: "Partial Pool", data: res.filter(o => o.emp_pool < 8 & o.emp_pool > 0)});
							// if ($scope.$root.myAccess.poolListOverAllocated) $scope.poolDatas.push({cardOutline: "card-success", name: "Over Allocated", data: res.filter(o => o.emp_pool < 0)});
							
							$scope.$root.callDataTable(".poolList", 1000);
						}
					});
				});
				if($scope.dTable)$scope.dTable.destroy();
				vServices.callApi("employee/newJoinee?lastDate="+$scope.lastDate + "&today="+$scope.today, "GET", {}).then((res) => {
					if(res){
						$scope.newJoinee = res;
						$scope.$root.callDataTable(".newJoinee");
					}
				});
			});
		};

		$scope.selectMonthData = (month) =>{
			if($scope.dTable)$scope.dTable.destroy();
			$scope.lastDate = moment().subtract(month, 'months').format("DD-MM-YYYY");
			vServices.callApi("employee/newJoinee?lastDate="+$scope.lastDate + "&today="+$scope.today, "GET", {}).then((res) => {
				if(res){
					$scope.newJoinee = res;
					$scope.$root.callDataTable(".newJoinee", 500, (t) => {
						$scope.dTable = t;
					}, {orientation: "landscape"});
				}
			});
		}
		init();
	}
]);
