define(['app'], function (app) {
	app.controller('AssessmentReportCtrl', function ($scope, $rootScope, AppData, $location) {
		$rootScope.menucont = true;
		$rootScope.pageLoadComp = false;
		$scope.formLoad = false;
		$scope.listFound = false;
		$scope.pageData = assessmentReportData[$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.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("|");
					}
				}
				$scope.assessmentList = function () {
					var obj = {};
					obj.funName = "assessment";
					obj.lang = $rootScope.selLanguage;
					obj.atype = 0;
                    obj.user_type=0;
					AppData.sendData($scope.commonurl, obj, function (res) {
						if (res.cont) {
							$scope.assessmentData = res.item;
							$rootScope.modulename =  'report';
							$scope.formData.assessmentid = res.item[0].assessmentid;
							console.log($rootScope.modulename,'$rootScope.modulename')
						}
					})
				}
				angular.element(document.querySelector('.table_area')).bind('scroll', function(){
					$('.datepicker').datepicker("hide")
				  })
				$scope.assessmentList()
				$scope.getZm();
				$scope.setPage = function (pageNo) {
					$scope.currentPage = pageNo;
				};
				$scope.pageChanged = function () {
					$scope.userAssessmentData()
				};
				$scope.setItemsPerPage = function (num) {
					$scope.itemsPerPage = num;
					$scope.currentPage = 1; //reset to first page
					$scope.userAssessmentData()
				}
				
			} 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.userAssessmentData();
			} 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.userAssessmentData()
			} 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.userAssessmentData()
		}
		
		$scope.chartData = function () {
			$scope.userAssessmentData();
		}
		//////////////// User Assessment Report ////////////////////
		$scope.userAssessmentData = function () {
			$scope.formData.funName = "assessment_report_v2";
			// $scope.formData.pn = $scope.currentPage;
			// $scope.formData.nr = $scope.itemsPerPage;
			$scope.formData.key = $scope.searchvalue;
			$scope.formData.lang = $rootScope.selLanguage;
			console.log($scope.formData)
			AppData.sendData($scope.pageurl, $scope.formData, function (res) {
				if (res.cont) {
					$scope.assessmentReport = res.item;
					// $scope.assessmentDataReport = $scope.assessmentReport.value;
					// $scope.dataValue=[];
					// $scope.labelData=[];
					// $scope.colorData=[];
					// for(var i=0;i<$scope.assessmentDataReport.length;i++){
					// 	$scope.dataValue.push($scope.assessmentDataReport[i].value);
					// 	$scope.labelData.push($scope.assessmentDataReport[i].title);
					// 	$scope.colorData.push($scope.getRandomColor());
					// }
					// var showChart;
					// console.log(Math.max(...$scope.dataValue) ,'mwth')
					// 	if (Math.max(...$scope.dataValue) == 0) {
					// 		console.log('false')
					// 		showChart = false;
					// 		$('.inner-container').html('');
					// 		$('.inner-container').html("<h4 class='text-center' >No data found</h4>");
					// 	} else {
					// 		showChart = true;
					// 		$scope.pieChart();
					// 	}
					
				}
			})
		}
		$scope.selectAssessment = function () {
			$scope.assessmentReport = [];
			$scope.currentPage = 1;
			$scope.userAssessmentData();

		}
		$scope.selectType = function () {
			$scope.userAssessmentData();
		}
		$scope.getRandomColor = function () {
			var color = Math.floor(0x1000000 * Math.random()).toString(16);
			return '#' + ('000000' + color).slice(-6);
		}
		$scope.pieChart = function () {
			console.log($scope.dataValue)
			console.log($scope.colorData)
			$('.inner-container').html('');
			$('.inner-container').html('<canvas id="assessmentChart"></canvas>');
			Chart.defaults.global.legend.display = true;
			var ctx = document.getElementById("assessmentChart").getContext('2d');
			var data = {
				labels: $scope.labelData,
				datasets: [{
					backgroundColor: $scope.colorData,
					data: $scope.dataValue
				}]
			}
			var chartOptions = {
				responsive: true,
				legend: {
					position: "bottom"
				}
			}
			var chart = new Chart(ctx, {
                type: "pie",
                data: data,
                options: chartOptions
            });
		}
		$scope.exportData = function () {
			$scope.formData.funName = "assessment_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.exportTableData = function(){
			$scope.formData.funName = "assessment_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);
				}
			})
		}
	});
}); 
