define(['app'], function (app) {
	app.controller('ContentReportCtrl', function ($scope, $rootScope, AppData, $location) {
		$rootScope.menucont = true;
		$scope.active = 1;
		$rootScope.pageLoadComp = false;
		$scope.formLoad = false;
		$scope.listFound = false;
		$scope.pageData = contentReportData[$rootScope.selLanguage];
		console.log($scope.pageData )
		$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.userReport=[];
		//pagination
		$scope.viewby = '10';
		$scope.currentPage = 1;
		$scope.itemsPerPage = $scope.viewby;
		$scope.maxSize = 5; //Number of pager buttons to show
		$scope.currentDate = moment().format("YYYY-MM-DD");
		$scope.startingDate = moment().startOf('month').format('YYYY-MM-DD');
		$scope.typeList = [
			{ id: "1", name: "PDF" },
			{ id: "2", name: "VIDEO" }
		]

		$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.pageTab();
				$scope.getZm();
				$scope.formData.cateid = '1'
				$scope.formData.type = '1'
				$scope.selectTab()
				
				$scope.setPage = function (pageNo) {
					$scope.currentPage = pageNo;
				};

				$scope.pageChanged = function (page) {
					$scope.currentPage = page;
					$(".pagination li:nth-child(3)").removeClass("active");
					console.log('Page changed to:' + $scope.currentPage);
					$scope.userViewData();	
				};
				$scope.modal_dismiss = function(){
					if($scope.currentPage>=2){
						$scope.currentPage = 1;
						$scope.setPage($scope.currentPage);
					}
					$(".pagination li").removeClass("active");
					// $('#modal').on('hidden.bs.modal', function() {
					// 	$(this).removeData('bs.modal');
					// });
					$('.myViewModal').modal('hide');
					console.log('page'+$scope.currentPage )
				}
				$scope.setItemsPerPage = function (num) {
					$scope.itemsPerPage = num;
					$scope.currentPage = 1; 
					$scope.userViewData();
				}

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

		///////////// Zone Data /////////////////
		$scope.getZm = function () {
			$scope.formData = {}
			// $scope.formData.from_date = $scope.startingDate;
			// $scope.formData.to_date = $scope.currentDate;
			$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) {
				$scope.zmuser_list = res.item;
				$scope.zmChange()
			})
		}
		$scope.zmChange = function () {
			$scope.formData.rm_email = "all";
			$scope.formData.am_email = "all";
			if ($scope.formData.zm_email == "all") {
				$scope.rmInactive = true;
				$scope.amInactive = true;
				$scope.userContentData();
			} 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) {
				$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.userContentData()
			} 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) {
				$scope.amuser_list = res.item;
				$scope.amChange()
			})
		}
		$scope.amChange = function () {
			$scope.userContentData()
		}
		$scope.pageTab = function () {
			var obj = {};
			obj.funName = "category";
			obj.lang = $rootScope.selLanguage;
			AppData.sendData($scope.commonurl, obj, function (res) {
				if (res.cont) {
					$scope.catList = res.item;
					$scope.catList[0].active = true;
				}

			})
		}
		$scope.chartData = function () {
			$scope.userContentData();
		}
		//////////////// User Registration Report ////////////////////
		$scope.userContentData = function () {
			$scope.totalViewCount = 0;
			$scope.formData.funName = "content_report_v2";
			$scope.formData.pn = $scope.currentPage;
			$scope.formData.nr = $scope.itemsPerPage;
			$scope.formData.key = $scope.searchvalue;
			AppData.sendData($scope.pageurl, $scope.formData, function (res) {
				if (res.cont) {
					$scope.contentReport = res.item;
					$rootScope.modulename =  'report';
					$scope.contentValueReport = $scope.contentReport.report;
					$scope.labelData = $scope.contentReport.label;
					$scope.totalValue = $scope.contentReport.report;
					for(let i=0;i<$scope.totalValue.length;i++){
						$scope.totalViewCount += parseInt($scope.totalValue[i].total_view);
					}
					$scope.arr = []
					$scope.bg = ["#00344C","#0B59AC","#859BD4","#B4C1E3"]
					$scope.barValueData = {}
					$scope.contentDatasets = []
					var dataArr=[];
					for (var i = 0; i < $scope.contentValueReport.length; i++) {
						console.log("here"+i)
						$scope.arr.push($scope.contentValueReport[i].title);
						$scope.barValueData =
								{
									borderColor: "rgba(0,0,0,0.5)",
									borderWidth: 1,
									data: $scope.contentValueReport[i].value
								}
						// for (var j = 0; j < $scope.contentValueReport[i].length; j++) {
							// {+
							// label: "Mastercard",
							// backgroundColor: "lightblue",
							// borderColor: $scope.getRandomColor(),
							// borderWidth: 1,
							// data: [4, 7, 3, 6, 10,7,4,6]
							// },
							// {
							// label: "Paypal",
							// backgroundColor: "lightgreen",
							// borderColor: $scope.getRandomColor(),
							// borderWidth: 1,
							// data: [10,7,4,6,9,7,3,10]
							// },
							// {
							// label: "Visa",
							// backgroundColor: "yellow",
							// borderColor: $scope.getRandomColor(),
							// borderWidth: 1,
							// data: [6,9,7,3,10,7,4,6]
							// }

						// }

						$scope.contentDatasets.push($scope.barValueData)
						$scope.contentDatasets[i].label = [];
						$scope.contentDatasets[i].backgroundColor = [];
					}
					console.log($scope.contentDatasets)
					$scope.barChart();
				}

			})
		}
		$scope.selectNewTab = function(tab,index){
			for(let i=0; i<$scope.catList.length; i++){
				if(i==index){
					$scope.catList[i].active = true;
				}else{
					$scope.catList[i].active = false;
				}
			}
			$scope.contentReport = [];
			$scope.currentPage = 1;
			$scope.formData.cateid = tab;
			$scope.userContentData();
		}
		$scope.viewdetails = function(topicdata){
			console.log(topicdata)
			// $('.myViewModal').modal('show');
			$(".pagination li:nth-child(3)").addClass("active");
			$scope.particular_contentdata = topicdata
			$scope.formData.assettopicid = $scope.particular_contentdata.assettopicid;
			$scope.formData.contentid = $scope.particular_contentdata.contentid ;
			$scope.userViewData();
		}
		$scope.selectTab = function () {
			$scope.contentReport = [];
			$scope.currentPage = 1;
			$scope.userContentData();
		}
		$scope.selectType = function () {
			$scope.userContentData();
		}
		$scope.userViewData = function(){
			$scope.formData.funName = "content_report_list_v2";
			$scope.formData.pn = $scope.currentPage;
			$scope.formData.nr = $scope.itemsPerPage;
			// $scope.formData.key = $scope.searchvalue;
			AppData.sendData($scope.pageurl, $scope.formData, function (res) {
				if (res.cont) {
					$scope.contentReport = res.item;
					$scope.totalItems = $scope.contentReport.total_records;
					$rootScope.modulename =  'report';
					$scope.contentValue = $scope.contentReport.content;
					$scope.contentViewValueReport = $scope.contentReport.report;
				}
			});
		}

		$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 r =(Math.random() * (110 - 0) + 0).toFixed(0);
			// var g =(Math.random() * (110 - 0) + 0).toFixed(0);
			// var b =(Math.random() * (110 - 0) + 0).toFixed(0);
			// //var arr =  "rgba("+hexToRgb(color).r+","+hexToRgb(color).g+","+hexToRgb(color).b+",0.2)";
			// var arr =  "rgba("+r+","+g+","+b+",0.8)";
			var arr=colors[Math.floor(Math.random() * colors.length)]
			return arr;
		}
		$scope.barChart = function () {
			
			for (var l = 0; l < $scope.contentDatasets.length; l++) {
				$scope.contentDatasets[l].label.push($scope.arr[l])
				$scope.contentDatasets[l].backgroundColor=($scope.bg[l])
				console.log($scope.contentDatasets[l].data)
			}
			$('.inner-container').html('');
			$('.inner-container').html('<canvas id="contentChart"></canvas>');
			Chart.defaults.global.legend.display = true;
			var ctx = document.getElementById("contentChart").getContext("2d");
			var barChartData = {
				labels: $scope.labelData,
				datasets: $scope.contentDatasets,
			};

			var chartOptions = {
				responsive: true,
				legend: {
					position: "bottom"
				},
				title: {
					display: false,
					text: "Chart.js Bar Chart"
				},
				scales: {
					yAxes: [{
						ticks: {
							min: 0,
							suggestedMin: 0, 
							fontColor: "rgba(0,0,0,0.5)",
							fontStyle: "bold",
							maxTicksLimit: 3,
							padding: 20,
						},
						gridLines: {
							drawTicks: false,
							display: false
						}
					}],
				}
			}
			var barChart = new Chart(ctx, {
				type: 'bar',
				data: barChartData,
				options: chartOptions
			});
			// window.myBar = new Chart(ctx, {
			//   type: "bar",
			//   data: barChartData,
			//   options: chartOptions
			// });

		}
		$scope.exportTableData = function(val,id){
			console.log(val);
			if(val == 1){
				$scope.formData.funName="content_report_v2"
			}else{
				$scope.formData.funName="content_report_list_v2"
				$scope.assettopicid=id
			}
			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);
				}
				
			})
		}
	});

}); 
