define(['app'], function (app) {
	app.controller('CurrentloginReportCtrl', function ($scope, $rootScope, AppData, $location) {
		$rootScope.menucont = true;
		$rootScope.pageLoadComp = false;
		$scope.formLoad = false;
		$scope.listFound = false;
		$scope.pageData = certificationReportData[$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("|");
					}
				}
				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) {
				$scope.zmuser_list = res.item;
				$scope.zmChange()
			})
		}
		$scope.zmChange = function () {
			$scope.formData.regtype=1;
			$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.selectStatus = function(){
			$scope.userAssessmentData()
		}
		$scope.setPage = function (pageNo) {
			console.log(pageNo)
			$scope.currentPage = pageNo;
		};

		$scope.pageChanged = function (tempval) {
			$scope.currentPage=tempval
			console.log($scope.currentPage)
			$scope.userAssessmentData();
		};

		$scope.setItemsPerPage = function (num) {
			$scope.itemsPerPage = num;
			$scope.currentPage = 1; //reset to first page
			$scope.userAssessmentData();
		}
		//////////////// User Certification Report ////////////////////
		$scope.userAssessmentData = function () {
			$scope.formData.regtype=$scope.formData.regtype;
			$scope.formData.funName = "useraccess_data";
			$scope.formData.pn = $scope.currentPage;
			$scope.formData.nr = $scope.itemsPerPage;
			// $scope.formData.lang = $rootScope.selLanguage;
			AppData.sendData($scope.pageurl, $scope.formData, function (res) {
				if (res.cont) {
					$scope.assessmentReport = res.item;
					$scope.totalItems = res.item.total_records;
					$rootScope.modulename =  'report';	
				}
			})
		}

		$scope.exportData = function () {
			$scope.formData.regtype=$scope.formData.regtype;
			$scope.formData.funName = "useraccess_data";
			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);
				}
			})
		}
	});
}); 
