define(['app'], function (app) {
	app.controller('UserReportCtrl', function ($scope, $rootScope, AppData, $location) {
		$rootScope.menucont = true;
		$rootScope.pageLoadComp = false;
		$scope.formLoad = false;
		$scope.listFound = false;
		$scope.pageData = userreportData[$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");
		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.pageTab();
				$scope.getZm();
				$scope.selectTab('Dashboard',0)
				$scope.setPage = function (pageNo) {
					$scope.currentPage = pageNo;
				  };
				 
				$scope.pageChanged = function() {
					console.log($scope.tabActive)
					$scope.tabActive=($scope.tabActive=='')?'Dashboard':$scope.tabActive;
					console.log('Page changed to: ' + $scope.currentPage);
					$scope.userReportData()
				  };
				  $scope.setItemsPerPage = function (num) {
					$scope.itemsPerPage = num;
					$scope.currentPage = 1; //reset to first page
					$scope.userReportData()
				}
				
			} else {
				$location.path("/login");
			}
		})
		// $scope.onSearchChange = function(){
		// 	console.log($scope.formData.mymodel)
		// 	$scope.selectTab($scope.tabActive,0)
		//   }
		
		///////////// 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.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 () {
			
			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.userReportData()
			
			} 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.userReportData()
			} 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.userReportData()
		}
		$scope.pageTab = function(){
			var obj={};
			obj.funName = "accesspage";
			obj.lang = $rootScope.selLanguage;
			AppData.sendData($scope.commonurl, obj, function (res) {
				if (res.cont) {
					$scope.pageRedirect = res.item;
					// $scope.userReport = res.item.report;
				}
			})
		}
		$scope.selectStatus = function () { 
			$scope.userReportData();
		}
		//////////////// User Registration Report ////////////////////
		$scope.userReportData = function () {
			$scope.searchvalue = ($scope.formData.mymodel!='')?$scope.formData.mymodel : "";
			// $scope.formData.funName = "page_access";
			$scope.formData.funName="page_access_v2"
			$scope.formData.page = $scope.tabActive;
			$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) {
					console.log(res.item)
					$rootScope.modulename =  'report';
					$scope.userReport = res.item.report;
					$scope.totalItems  = res.item.total_records;
				}
			})
		}
		$scope.selectTab = function(tab,index){
			console.log(tab)
			$scope.userReport=[];
			$scope.tabActive=tab;
			$scope.tabId=index;
			$scope.currentPage = 1;
			$scope.userReportData();
		}
		$scope.exportData = function () {
			// $scope.formData.funName="page_access"
			$scope.formData.funName="page_access_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);
				}

			})
		}
	});
}); 
