define(['app'], function (app) {
	app.controller('AttritionReportCtrl', function ($scope, $rootScope, AppData, $location) {
		$rootScope.menucont = true;
		$rootScope.pageLoadComp = false;
		$scope.formLoad = false;
		$scope.listFound = false;
		$scope.pageData = attritionData[$rootScope.selLanguage];
		$scope.submenu = $scope.pageData.sidemenu;
		$scope.url = "../senddata/log/";
		$scope.pageurl = "../senddata/report/";
		$scope.exporturl = "../senddata/reportdownload/";
		$scope.commonurl = "../senddata/common/";
		$rootScope.sideselect = $scope.pageData.sidemenu[0];
		//Check Session//
		$scope.formData = {}
		$rootScope.formData = {}

		$scope.currentDate  =  moment().format("YYYY-MM-DD");
		console.log($scope.currentDate)
		$scope.startingDate  =  moment().startOf('month').format('YYYY-MM-DD');
		$scope.formData.ismenuload = "Yes";
		if ($rootScope.items == undefined || $rootScope.items == null) {
			$scope.formData.ismenuload = "No";
		}

		$scope.formData.funName = "checksession";
		$scope.formData.lang = $rootScope.selLanguage;
		$scope.formData.start = 0;
		$scope.formData.limit = 2;

		AppData.sendData($scope.url, $scope.formData, function (res) {
			if (res.cont) {
				if ($rootScope.items == undefined || $rootScope.items == null) {
					$rootScope.items = res.item.control;

				}
				for (var i = 0; i < $rootScope.items.length; i++) {
					if ($location.path() == $rootScope.items[i].modulepath) {
						$rootScope.modulename = $rootScope.items[i].modulename;
						$scope.usercontrol = String($rootScope.items[i].permission).split("|");
					}
				}
				angular.element(document.querySelector('.table_area')).bind('scroll', function () {
					$('.datepicker').datepicker("hide")
				})
				$scope.getZm();

			} else {
				$location.path("/login");
			}
		})

		///////////// Zone Data /////////////////
		$scope.getZm = function () {
			$scope.formData = {}
			$scope.formData.zm_email = "all";
			$scope.rmInactive = true;
			$scope.amInactive = true;
			var zobj = {};
			zobj.funName = "zmlist";
			zobj.lang = $rootScope.selLanguage;
			AppData.sendData($scope.commonurl, zobj, function (res) {
				console.log(res)
				$scope.zmuser_list = res.item;
				$scope.zmChange()
			})
		}
		$scope.zmChange = function () {
			$scope.formData.from_date = $scope.startingDate;
			$scope.formData.to_date = $scope.currentDate;
			console.log($scope.formData.zm_email)
			$scope.formData.rm_email = "all";
			$scope.formData.am_email = "all";
			if ($scope.formData.zm_email == "all") {
				$scope.rmInactive = true;
				$scope.amInactive = true;
				$scope.attritionReportData()

			} else {
				$scope.rmInactive = false;
				$scope.amInactive = true;
				$scope.getRm();

			}

		}
		///////////// Regional Data //////////////
		$scope.getRm = function () {
			$scope.formData.rm_email = "all";
			var robj = {};
			robj.funName = "rmlist";
			robj.lang = $rootScope.selLanguage;
			robj.zm_email = $scope.formData.zm_email;
			AppData.sendData($scope.commonurl, robj, function (res) {
				console.log(res)
				$scope.rmuser_list = res.item;
				$scope.rmChange()
			})
		}
		$scope.rmChange = function () {
			$scope.formData.am_email = "all"
			if ($scope.formData.rm_email == "all") {
				$scope.amInactive = true;
				$scope.attritionReportData()
			} else {
				$scope.amInactive = false;
				$scope.getAm();
			}

		}
		//////////// Area Manager Data ///////////////////
		$scope.getAm = function () {
			var aobj = {};
			aobj.funName = "amlist";
			aobj.lang = $rootScope.selLanguage;
			aobj.rm_email = $scope.formData.rm_email;
			AppData.sendData($scope.commonurl, aobj, function (res) {
				console.log(res)
				$scope.amuser_list = res.item;
				$scope.amChange()
			})
		}
		$scope.amChange = function () {
			$scope.attritionReportData()
		}
		$scope.chartData = function () {
			$scope.attritionReportData()
		}
		$scope.getRandomColor = function () {
			var letters = '0123456789ABCDEF';
			var color = '#';
			var colors = ["rgb(0,0,128)", "rgb(0,51,76)", "rgb(51,51,153)", "rgb(204,204,230)", "rgb(229,229,242)"];
			var arr = colors[Math.floor(Math.random() * colors.length)]
			return arr;
		}
		//////////////// attrition Report ////////////////////
		$scope.attritionReportData = function () {
			$scope.userLabel = [];
			$scope.userValue = [];
			$scope.formData.funName = "attrition_report";
			AppData.sendData($scope.pageurl, $scope.formData, function (res) {
				if (res.cont) {
					console.log(res.item)
					$scope.userReport = res.item;
					$rootScope.modulename = 'report';
					$scope.labelData = $scope.userReport.label
					$scope.totalValue = $scope.userReport.total;
					$scope.salesData = []
					for (var i = 0; i < $scope.userReport.value.length; i++) {
						console.log($scope.userReport.value[i])
						$scope.salesData.push($scope.userReport.value[i].rate)

					}
					console.log($scope.salesData)
					if (Math.max(...$scope.salesData) != 0) {
						$scope.barChart();
					}else{
						$('.inner-container').html('');
						$('.inner-container').html("<h4 class='text-center' >No data found</h4>");
					}
					
				}

			})
		}
		$scope.barChart = function () {
			var maxValue = 0;
			if (Math.max.apply(Math, $scope.salesData) == 0) {
				maxValue += 10
			} else {
				maxValue += Math.max.apply(Math, $scope.salesData) + 1;
			}

			$('.inner-container').html('');
			$('.inner-container').html('<canvas id="salesChart"></canvas>');
			Chart.defaults.global.legend.display = true;
			var ctx = document.getElementById("salesChart").getContext("2d");

			var data = {
				labels: $scope.labelData,
				datasets: [
					{
						label: "Attrition Rate",
						data: $scope.salesData,
						backgroundColor: '#014DAF',
						borderWidth: 1,
					}
				]
			};

			var options = {
				responsive: true,
				legend: {
					position: "bottom"
				},

				scales: {
					yAxes: [{
						ticks: {
							min: 0,
							suggestedMin: 0,
							fontColor: "rgba(0,0,0,0.5)",
							fontStyle: "bold",
							maxTicksLimit: 5,
							padding: 20,
							max: maxValue,
						}
					}]
				}
			};

			var chart = new Chart(ctx, {
				type: "bar",
				data: data,
				options: options
			});

		}
		$scope.exportData = function () {
			AppData.sendData($scope.exporturl, $scope.formData, function (res) {
				if (res.cont) {
					$('body').append("<iframe id='fileDownload' src='" + res.item.report_url + "' ></iframe>");
					var setO = setTimeout(function () {
						clearTimeout(setO);
						$('#fileDownload').remove();
					}, 2000);
				}

			})
		}
	});

}); 
