define(['app'], function (app) {
	app.controller('RedeemCtrl', function ($scope, $rootScope, AppData, $location) {
		$rootScope.menucont = true;
		$rootScope.pageLoadComp = false;
		$scope.formLoad = false;
		$scope.listFound = false;
		$scope.pageData = redeemData[$rootScope.selLanguage];
		$scope.itemdetail = [];
		$scope.approvedListDetail = [];
		$scope.selectedList = [];
		$scope.expiredtype = [];

		$scope.isExpiredRowVisible = false;
		$scope.isExpiredDaysVisible = false;
		$scope.isfirst = false;
		$scope.selectedIndex = null;
		$rootScope.cofirmWindow = false;
		$rootScope.modalLogShown = false;
		$scope.search_show = true;
		$scope.url = "../senddata/log/";
		$scope.pageurl = "../senddata/redeem/";
		$scope.exporturl = "../senddata/export/";
		$scope.commonurl = "../senddata/common/";
		$scope.search_box = true;
		$scope.showButton = false;
		$scope.submenu = $scope.pageData.sidemenu;
		$rootScope.sideselect = $scope.pageData.sidemenu[0];
		$scope.tblheading = $rootScope.sideselect.headingText;
		//pagination
		$scope.viewby = '10';
		$scope.currentPage = 1;
		$scope.itemsPerPage = $scope.viewby;
		$scope.maxSize = 5; //Number of pager buttons to show

		//Check Session//
		$scope.formData = {}
		$rootScope.formData = {}

		$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;


		//this page Variable//
		//$scope.roleitem = null;
		$scope.cateitem = null;
		$scope.questionitem = null;
		$scope.assignquestionitem = [];
		$scope.assignquestionnum = [];
		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.statusobj = res.item.status;
				$rootScope.username = res.item.username;
				$scope.listFound = true;
				$scope.start_time = $scope.pageData.assessment_publish_time;
				$scope.end_time = $scope.pageData.assessment_publish_time;

				$scope.waitingList();
			} else {
				$location.path("/login");
			}
		})
		$scope.waitingList = function () {
			$scope.search_show = true;
			$scope.searchvalue = ($scope.formData.mymodel != '') ? $scope.formData.mymodel : "";
			var fobj = {};
			fobj.funName = "redeemlist";
			if ($rootScope.sideselect.id == 1) {
				fobj.pn = $scope.currentPage;
				fobj.nr = $scope.itemsPerPage;
			} else {
				fobj.pn = '';
				fobj.nr = '';
			}
			fobj.lang = $rootScope.selLanguage;
			fobj.key = $scope.searchvalue;
			$rootScope.pageLoadComp = true;
			AppData.sendData($scope.pageurl, fobj, function (res) {
				$rootScope.pageLoadComp = true;
				if (res.cont) {
					if (res.item != undefined) {
						$scope.itemdetail = res.item.users;
						$scope.totalItems = res.item.total_records;
						$scope.listFound = true;
						$scope.formLoad = false;
						$scope.search_box = true;
					}
					//$scope.formData = {}
				} else {
					$scope.listFound = false;
					$scope.errorMsg = res.mess;
				}
				//$scope.roleitem=res.item.roleitem;
			})
		}
		$scope.approvedList = function () {
			$scope.search_show = true;
			$scope.searchvalue = ($scope.formData.mymodel != '') ? $scope.formData.mymodel : "";
			var fobj = {};
			fobj.funName = "redeemapprovedlist";
			if ($rootScope.sideselect.id == 1) {
				fobj.pn = $scope.currentPage;
				fobj.nr = $scope.itemsPerPage;
			} else {
				fobj.pn = '';
				fobj.nr = '';
			}
			fobj.lang = $rootScope.selLanguage;
			fobj.key = $scope.searchvalue;
			$rootScope.pageLoadComp = true;
			AppData.sendData($scope.pageurl, fobj, function (res) {
				$rootScope.pageLoadComp = true;
				if (res.cont) {
					if (res.item != undefined) {
						$scope.approvedListDetail = res.item.users;
						$scope.totalItems = res.item.total_records;
						$scope.listFound = true;
						$scope.formLoad = false;
						$scope.search_box = true;
					}
					//$scope.formData = {}
				} else {
					$scope.listFound = false;
					$scope.errorMsg = res.mess;
				}
				//$scope.roleitem=res.item.roleitem;
			})
		}
		$scope.toggleSelect = function () {
			$scope.selectedList=[];
			$scope.isAllSelected =!event.target.checked
			console.log($scope.isAllSelected)
			if($scope.isAllSelected){
				console.log('if')
				angular.forEach($scope.itemdetail, function (item) {
					item.selected = false;
				});
				$scope.showButton = false;
			}else{
				console.log('ifdfsdf')
				angular.forEach($scope.itemdetail, function (item) {
					item.selected = event.target.checked;
					$scope.selectedList.push(item.id);
					console.log($scope.selectedList)
				});
				$scope.showButton = true;
			}
			
			// angular.forEach($scope.itemdetail, function (item) {
			// 	item.selected = event.target.checked;
			// 		var index = $scope.selectedList.indexOf(item.id)
			// 		console.log(index)
			// 		if(index>-1){
			// 			$scope.selectedList.splice(index,1)
			// 			$scope.showButton = false;
			// 		}else{
			// 			$scope.selectedList.push(item.id)
			// 			$scope.showButton = true;
			// 		}
				
			// 	console.log($scope.selectedList)
			// });
		}
		$scope.childSelect = function (id) {
			console.log($scope.isAllSelected)
			// if($scope.isAllSelected || $scope.isAllSelected==undefined){
			// 	$scope.itemdetail.every(function (itm) {
			// 		console.log(itm)
			// 		if(itm.selected){
			// 			$scope.showButton = true;
			// 		}else{
			// 			$scope.showButton = false;
			// 		}
			// 	})
			// }
		
			var index = $scope.selectedList.indexOf(id)
			console.log(index)
			if (index > -1) {
				$scope.selectedList.splice(index, 1)
				
			} else {
				$scope.selectedList.push(id)

			}
			if($scope.selectedList.length==0){
				$scope.showButton = false;
			}else{
				$scope.showButton = true;
			}
			console.log($scope.selectedList)
		}
		$scope.sendConfirmation = function () {
			console.log($scope.selectedList)
			var obj = {};
			obj.funName = "approveredeem";
			obj.lang = $rootScope.selLanguage;
			obj.id = $scope.selectedList.toString();
			$rootScope.pageLoadComp = true;
			console.log(obj)
			AppData.sendData($scope.pageurl, obj, function (res) {
				$rootScope.modalLogShown = true;
				$rootScope.message = res.mess;
				$scope.waitingList()
				
			})
		}
		$scope.setPage = function (pageNo) {
			$scope.currentPage = pageNo;
		};

		$scope.pageChanged = function () {
			if ($rootScope.sideselect.id == 1) {
				$scope.waitingList();
			} else if ($rootScope.sideselect.id == 2) {
				$scope.approvedList();
			}
		};

		$scope.setItemsPerPage = function (num) {
			$scope.itemsPerPage = num;
			$scope.currentPage = 1; //reset to first page
			if ($rootScope.sideselect.id == 1) {
				$scope.waitingList();
			} else if ($rootScope.sideselect.id == 2) {
				$scope.approvedList();
			}
		}
		$scope.filterData = function () {
			if ($rootScope.sideselect.id == 1) {
				$scope.waitingList();
			} else if ($rootScope.sideselect.id == 2) {
				$scope.approvedList();
			}
		}

		$scope.listClick = function (obj) {
			$scope.searchtext = '';
			$scope.formData = {};
			$scope.currentPage = 1;
			$(".clsbuttoncont .clsbutton").removeClass("disable_btn");
			$scope.formLoad = false;
			$rootScope.sideselect = obj;
			$scope.selectedIndex = null;
			$scope.tblheading = obj.headingText;
			if ($rootScope.sideselect.id == 1) {
				$scope.waitingList();
			} else if ($rootScope.sideselect.id == 2) {
				$scope.approvedList();
			}

		}

		$scope.onSearchChange = function () {
			// console.log($scope.formData.mymodel)
			if ($rootScope.sideselect.id == 1) {
				$scope.waitingList();
			} else if ($rootScope.sideselect.id == 2) {
				$scope.approvedList();
			}
		}


	});

}); 
