define(['app'], function (app) {
	app.controller('RedeemReportCtrl', function ($scope, $rootScope, AppData, $location) {
		$rootScope.menucont = true;
		$rootScope.pageLoadComp = false;
		$scope.formLoad = false;
		$scope.listFound = false;
		$scope.pageData = redeemReportData[$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];
		$scope.active_product;
		//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");
			}
		})
		$scope.listClick = function (obj) {
			$scope.formData = {}
			$scope.currentPage = 1;
			$rootScope.sideselect = obj;
			$rootScope.sideselect.name = obj.name;
			$scope.getZm();
		}
		///////////// Zone Data /////////////////
		$scope.getZm = function () {
			$scope.showaccordian = false;
			$scope.formData = {}
			$scope.formData.zm_email = "all";
			$scope.formData.regtype = '1';
			$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.showaccordian = false;
			// $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;
				if($rootScope.sideselect.id== 1){
					$scope.redeemAccumulationData()
				}else if($rootScope.sideselect.id== 2){
					$scope.redeemRedemptionData()
				}else if($rootScope.sideselect.id== 3){
					$scope.redeemApprovalData()
				}
			} else {
				$scope.rmInactive = false;
				$scope.amInactive = true;
				$scope.getRm();

			}
			
		}
		///////////// Regional Data //////////////
		$scope.getRm = function () {
			$scope.showaccordian = false;
			$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.showaccordian = false;
			$scope.formData.am_email = "all"
			if ($scope.formData.rm_email == "all") {
				$scope.amInactive = true;
				if($rootScope.sideselect.id== 1){
					$scope.redeemAccumulationData()
				}else if($rootScope.sideselect.id== 2){
					$scope.redeemRedemptionData()
				}else if($rootScope.sideselect.id== 3){
					$scope.redeemApprovalData()
				}
			} else {
				$scope.amInactive = false;
				$scope.getAm();
			}
			
		}
		//////////// Area Manager Data ///////////////////
		$scope.getAm = function () {
			$scope.showaccordian = false;
			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.showaccordian = false;
			if($rootScope.sideselect.id== 1){
				$scope.redeemAccumulationData()
			}else if($rootScope.sideselect.id== 2){
				$scope.redeemRedemptionData()
			}else if($rootScope.sideselect.id== 3){
				$scope.redeemApprovalData()
			}
			
		}
		$scope.chartDataAccumulate = function(){
			$scope.redeemAccumulationData()
		}
		$scope.chartDataRedemption = function(){
			$scope.redeemRedemptionData()
		}
		$scope.chartDataApproval = function(){
			$scope.redeemApprovalData()
		}
		//////////////// User Registration Report ////////////////////
		$scope.redeemAccumulationData = function () {
			$scope.showaccordian = false;
			$scope.userLabel = [];
			$scope.userValue = [];
			$scope.formData.funName = "points_report_v2";
			console.log($scope.formData) 
			if($scope.formData.am_email!='all'){
				$scope.show_hyperlink = true;
			}else{
				$scope.show_hyperlink = false;
			}
//			$scope.formData.funName = "accumulation_report";
			AppData.sendData($scope.pageurl, $scope.formData, function (res) {
				console.log(res);
				if (res.cont) {
					console.log(res.item)
					$scope.userReport = res.item;
					$rootScope.modulename =  'report';
					$scope.userReport = $scope.userReport;
				}

			})
		}
		$scope.areaChart = function (chart_title) {
			var maxValue=0;
			if(Math.max.apply(Math,$scope.userValue)==0){
				maxValue+=10
			}else{
				maxValue+=Math.max.apply(Math,$scope.userValue);
			}
			console.log(maxValue)
			$('.inner-container').html('');
		   $('.inner-container').html('<canvas id="registrationChart"></canvas>');
		   Chart.defaults.global.legend.display = true;
			var ctx = document.getElementById('registrationChart').getContext("2d");
			var gradientStroke = ctx.createLinearGradient(500, 0, 100, 0);
			gradientStroke.addColorStop(0, '#80b6f4');
			gradientStroke.addColorStop(1, '#f49080');

			var gradientFill = ctx.createLinearGradient(500, 0, 100, 0);
			gradientFill.addColorStop(0, "rgba(128, 182, 244, 0.6)");
			gradientFill.addColorStop(1, "rgba(244, 144, 128, 0.6)");

			var registrationChart = new Chart(ctx, {
				type: 'line',
				data: {
					labels: $scope.userLabel,
					datasets: [{
						label: chart_title,
						borderColor: gradientStroke,
						pointBorderColor: gradientStroke,
						pointBackgroundColor: gradientStroke,
						pointHoverBackgroundColor: gradientStroke,
						pointHoverBorderColor: gradientStroke,
						pointBorderWidth: 10,
						pointHoverRadius: 10,
						pointHoverBorderWidth: 1,
						pointRadius: 2,
						fill: true,
						backgroundColor: gradientFill,
						borderWidth: 4,
						lineTension: 0,
						scaleStartValue : 0 ,
						data: $scope.userValue
					}]
				},
				options: {
					beginAtZero: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,
								
							},
							gridLines: {
								drawTicks: false,
								display: false
							}
			
						}],
						xAxes: [{
							gridLines: {
								zeroLineColor: "transparent"
							},
							ticks: {
								padding: 20,
								fontColor: "rgba(0,0,0,0.5)",
								fontStyle: "bold",
							}
						}]
					},
					
				}
			});
		}
		$scope.exportData = function(){
		//	$scope.formData.top_50=''
			$scope.formData.funName="points_report_list_v2";
			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);
				}
				
			})
		}
		$scope.selectStatus = function () { 
			console.log($scope.formData.regtype);
			$scope.redeemAccumulationData();

		}
		$scope.exportTableData = function(){
				$scope.formData.regtype=$scope.formData.regtype;
				console.log($scope.formData.regtype);
				$scope.formData.funName="points_report_v2"
				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);
					}
					
				})
			}

			$scope.exportTable = function(pin){
				console.log(pin)
				$scope.formData.regtype=$scope.formData.regtype;
				console.log($scope.formData.regtype);
				$scope.formData.funName="rewardpoint_data"
				$scope.formData.smpin=pin;
				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);
					}
					
				})
			}
		$scope.topFiftyData = function(){
			$scope.formData.top_50='1'
			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);
				}
				
			})
		}
		///////////////////////// Redemption Report /////////////////////
		
		$scope.selectTab = function(tab){
			$scope.tabActive=tab;
		}
		$scope.selectTab('1')
		$scope.redeemRedemptionData = function () {
			$scope.userLabel = [];
			$scope.userValue = [];
			$scope.formData.funName = "redemption_report";
			AppData.sendData($scope.pageurl, $scope.formData, function (res) {
				if (res.cont) {
					console.log(res.item)
					$scope.userReport = res.item;
				    for(var i=0;i<$scope.userReport.value.length;i++){
						$scope.userLabel.push($scope.userReport.value[i].title)
						$scope.userValue.push(Number($scope.userReport.value[i].value))
					}
					console.log($scope.userLabel)
					console.log($scope.userValue)
					console.log(Math.max.apply(Math,$scope.userValue))
					if (Math.max(...$scope.userValue) == 0) {
						$('.inner-container').html('');
						$('.inner-container').html("<h4 class='text-center' >No data found</h4>");
					}else {
						$scope.areaChart('Redemption Report');
					}
					
				}
			})
		}

		///////////////////////// Reward Point ////////////////////////
		$scope.getdsedetails = function(code,val){
			console.log(code);
			if(code != undefined && code!= null && code!=''){
				$scope.showaccordian = true;
				var obj = {};
				obj.funName = "reward_point_dse";
				obj.lang = "en_us";
				obj.dealer_code = code;
				obj.regtype = $scope.formData.regtype;
				AppData.sendData($scope.pageurl, obj, function (res) {
					console.log(res);
					if (res.cont) {
						$scope.user_reward_point_Report = res.item;
					}
				})
			}else{

			}
		}

		$scope.arrowfunction = function(i){
			console.log('arrow')
			if(i!=undefined){
				$(".listitem").each(function(){
					$(this).find(".arrow").removeClass("right");
					$(this).find(".arrow").addClass("down");
				});
			}
			
			
			// Toggle plus minus icon on show hide of collapse element
			// $(".listitem").on('show.bs.listitem', function(){
			// 	$(this).parent().find(".arrow").addClass("down");
			// }).on('hide.bs.listitem', function(){
			// 	$(this).parent().find(".arrow").removeClass("down");
			// });
		}

		// .$scope.toggleGroup(group) = function{
		// 	console.log(group);
		// 	this.removefilterstorage();
		// 	if (this.isGroupShown(group)) {
		// 	  this.shownGroup = null;
		// 	} else {
		// 	  this.shownGroup = group;
		// 	}
		//   };
		//   $scope.isGroupShown(group) {
		// 	return this.shownGroup === group;
		//   };

		$scope.viewdetails = function(data){
			console.log(data);
			$scope.reward_user_data = data;
			var obj = {};
			obj.funName = "rewardpoint_data";
			obj.lang = "en_us";
			obj.smpin = data.smpin;
			AppData.sendData($scope.pageurl, obj, function (res) {
				console.log(res);
				if (res.cont) {
					$scope.user_reward_point_history = res.item;
				}
			})
		}
		///////////////////////// Approval Report /////////////////////
				
	
		$scope.redeemApprovalData = function () {
			$scope.userLabel = [];
			$scope.userValue = [];
			$scope.formData.funName = "redemptionapproved_report";
			AppData.sendData($scope.pageurl, $scope.formData, function (res) {
				if (res.cont) {
					console.log(res.item)
					$scope.userReport = res.item;
					for(var i=0;i<$scope.userReport.value.length;i++){
						$scope.userLabel.push($scope.userReport.value[i].title)
						$scope.userValue.push(Number($scope.userReport.value[i].value))
					}
					console.log($scope.userLabel)
					console.log($scope.userValue)
					console.log(Math.max.apply(Math,$scope.userValue))
					
					if (Math.max(...$scope.userValue) == 0) {
						$('.inner-container').html('');
						$('.inner-container').html("<h4 class='text-center' >No data found</h4>");
					}else {
						$scope.areaChart('Approval Report');
					}
					
				}
			})
		}
	});

}); 
