define(['app'], function (app) {

	app.controller('UserCtrl', function ($scope, $rootScope, AppData, $location) {
		//Every module Set Same Variable//

		$rootScope.menucont = true;
		$rootScope.pageLoadComp = false;
		$scope.pageData = userData[$rootScope.selLanguage];
		//console.log("$scope.pageData-->",$scope.pageData)
		$scope.formLoad = false;
		$scope.search_box = true;
		$scope.filerequired = true;
		$scope.url = "../senddata/log/";
		$scope.pageurl = "../senddata/user/";
		$scope.commonurl = "../senddata/common/";
		$scope.formAction = "Add";
		$rootScope.sideselect = $scope.pageData.sidemenu[0];
		$scope.submenu = [];
		$scope.selectedIndex = null;
		$rootScope.cofirmWindow = false;
		$scope.tblheading = $rootScope.sideselect.headingText;
		$rootScope.imageFormat = ["image/jpeg", "image/png"];
		//add form
		$scope.role_error = '';
		$scope.smpin_error = '';
		$scope.empcode_error = '';
		$scope.fname_error = '';
		$scope.email_error = '';
		$scope.mobile_error = '';
		$scope.dealer_error = '';
		$scope.segment_error = '';
		$scope.dob_error = '';
		$scope.zone_error = '';

		//Check Session//
		$scope.formData = {}
		$scope.formData.ismenuload = "Yes";
		if ($rootScope.items == undefined || $rootScope.items == null) {
			$scope.formData.ismenuload = "No";
		}
		$scope.viewby = '10';
		$scope.itemsPerPage = $scope.viewby;
		$scope.maxSize = 5; //Number of pager buttons to show
		$scope.currentPage = 1;

		$scope.formData.funName = "checksession";
		$scope.formData.lang = $rootScope.selLanguage;
		$scope.formData.start = 0;
		$scope.formData.limit = 2;
		$rootScope.log = true;
		// $scope.$on('$locationChangeStart', function(event, next, current) {

		// 	var nextRoute = next.split('#')[1];
		// 	console.log(nextRoute)
		// 	var currentPath = current.split('#')[1];
		// 	console.log(currentPath)

		// 	if(currentPath=='/user'){
		// 		if(nextRoute=='/login'){
		// 			console.log($rootScope.log)
		// 			//alert('You are Logged In!!!!');
		// 			if($rootScope.log){
		// 				$location.path('/user')
		// 			}

		// 		}

		// 	}
		//   });
		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.issa = res.item.role
				if ($scope.issa != 1) {
					for (var id = 0; id < $scope.pageData.sidemenu.length - 3; id++) {
						$scope.submenu.push($scope.pageData.sidemenu[id]);
					}
				} else {
					$scope.submenu = $scope.pageData.sidemenu;
				}


				$scope.userList = function () {
					$scope.searchvalue = ($scope.formData.mymodel != '') ? $scope.formData.mymodel : "";
					var fobj = {};
					fobj.funName = "userslist";
					fobj.lang = $rootScope.selLanguage;
					fobj.pn = $scope.currentPage;
					fobj.nr = $scope.itemsPerPage;
					fobj.key = $scope.searchvalue

					AppData.sendData($scope.pageurl, fobj, function (res) {
						$rootScope.pageLoadComp = true;
						if (res.cont) {
							$scope.tblheading = $scope.pageData.sidemenu[0].headingText;
							$scope.itemuserdetail = res.item.users;
							$scope.totalItems = res.item.total_records;
							$scope.listFound = true;
							$scope.search_box = true;
							// $scope.formData = {}
						} else {
							$scope.listFound = false;
							$scope.errorMsg = res.mess;
						}
					})
				}

				$scope.userList();

				$scope.setPage = function (pageNo) {
					$scope.currentPage = pageNo;
				};

				$scope.pageChanged = function () {
					console.log('Page changed to: ' + $scope.currentPage);
					$scope.userList();
				};

				$scope.setItemsPerPage = function (num) {
					$scope.itemsPerPage = num;
					$scope.currentPage = 1; //reset to first page
					$scope.userList();
				}
				$scope.filterData = function () {
					console.log($scope.searchtext)
					$scope.userList();
				}
			} else {
				$location.path("/login");
			}
		})
		$scope.roleList = function () {
			var robj = {};
			robj.funName = "role";
			robj.lang = $rootScope.selLanguage;
			robj.type = "";
			AppData.sendData($scope.commonurl, robj, function (res) {
				$rootScope.pageLoadComp = true;
				if (res.cont) {
					$scope.roledetail = res.item;

				} else {

				}
			})
		}

		$scope.onSearchChange = function () {
			console.log($scope.formData.mymodel)
			$scope.userList();
		}
		// $scope.roleList = function (){
		// 	var fobj = {};
		// 	fobj.funName = "userrolelist";
		// 	AppData.sendData($scope.pageurl,fobj,function(res){
		// 		$rootScope.pageLoadComp = true;
		// 		if(res.cont){
		// 			$scope.rolelist = res.item;
		// 			$scope.listFound = true;

		// 		}else{
		// 			$scope.listFound = false;
		// 			$scope.errorMsg = res.mess;
		// 		}
		// 	})
		// }
		$scope.dealerList = function () {
			var dobj = {};
			dobj.funName = "dealer";
			dobj.lang = $rootScope.selLanguage;
			dobj.city = "chennai";
			AppData.sendData($scope.commonurl, dobj, function (res) {
				$rootScope.pageLoadComp = true;
				if (res.cont) {
					$scope.dealerlist = res.item;
				} else {

				}
			})
		}
		$scope.addFun = function () {
			$scope.formData = {};
			$scope.formLoad = true;
			$scope.listFound = false;
			$scope.formAction = "Add";
			$scope.formTitle = "ADD USER";
			$scope.formData.funName = "adduser";
			$scope.selectedIndex = null;
			$scope.roleList();
			$scope.dealerList();

		}
		$scope.zoneList = function () {
			var obj = {};
			obj.funName = "zone";
			obj.lang = $rootScope.selLanguage;
			AppData.sendData($scope.commonurl, obj, function (res) {
				if (res.cont) {
					$scope.zoneData = res.item;
				} else {

				}
			})

		}
		$scope.changeRole = function () {
			console.log("here1")
			if ($scope.formData.role == '5') {
				console.log("here")
				$scope.zoneList();
			}
		}
		$scope.closeForm = function () {
			$scope.formLoad = false;
			$scope.listFound = true;
			$scope.formData = {};
			$scope.selectedIndex = null
			$scope.clearErrorMsg();
			$scope.userList();
		}
		$scope.listClick = function (obj) {
			$scope.formLoad = false;
			$rootScope.sideselect = obj;
			$scope.itemdetail = [];
			$scope.selectedIndex = null;
			var fobj = {};
			fobj.lang = $rootScope.selLanguage;
			if (obj.id == 1) {
				fobj.funName = "userslist";
			} else if (obj.id == 2) {
				fobj.funName = "userrolelist";
			} else if (obj.id == 3) {
				fobj.funName = "permission";
			} else if (obj.id == 4) {
				fobj.funName = "module";
			}


			$scope.tblheading = obj.headingText;
			AppData.sendData($scope.pageurl, fobj, function (res) {
				if (res.cont) {
					if (obj.id != 3) {
						if (res.item != undefined) {
							$scope.itemdetail = res.item;
						}
					} else {
						if (res.item != undefined) {
							$scope.roleitem = res.item.role;
							$scope.moduleitem = res.item.module;
							$scope.itemdetail = res.item.item
						}
					}
					$scope.listFound = true;
				} else {
					if (obj.id == 3) {
						if (res.item != undefined) {
							$scope.roleitem = res.item.role;
							$scope.moduleitem = res.item.module;
						}
					}
					$scope.listFound = false;
					$scope.errorMsg = res.mess;
				}
			})

		}
		$scope.editFun = function (obj, i) {
			$scope.roleList();
			$scope.dealerList();
			$scope.formData = obj;
			$scope.formData.role = obj.roleid;
			$scope.formTitle = "EDIT USER";
			console.log($scope.formData)
			$scope.selectedIndex = i;
			$scope.listFound = false;
			$scope.formLoad = true;
			$scope.formAction = "Edit";
			if ($rootScope.sideselect.id == 1) {
				$scope.formData.funName = "edituser";
			} else if ($rootScope.sideselect.id == 2) {
				$scope.formData.funName = "editrole";
			} else if ($rootScope.sideselect.id == 3) {
				$scope.formData.funName = "addpermission";
				loadPermission($scope.formData.child);
			} else if ($rootScope.sideselect.id == 4) {
				$scope.formData.funName = "editmodule";
				if ($scope.formData.issubmodule == 1) {
					$scope.enableParent = true;
				} else {
					$scope.enableParent = false;
				}
			}

		}

		$scope.deleteFun = function () {
			console.log($scope.selectedIndex)
			if ($scope.selectedIndex != null) {
				$rootScope.cofirmWindow = true;
				$rootScope.modalShown = true;
				$rootScope.message = $scope.pageData.errorMsg[1];

			} else {
				$rootScope.modalShown = true;
				$rootScope.message = $scope.pageData.errorMsg[0];
			}
		}

		$scope.$on("confirmDelete", function () {
			$scope.formData.funName = "deleteuser";
			AppData.sendData($scope.pageurl, $scope.formData, function (res) {
				if (res.cont) {
					var index = $scope.itemuserdetail.indexOf($scope.formData);
					if (index != -1) {
						$scope.itemuserdetail.splice(index, 1);
					}
					$rootScope.modalShown = true;
					$rootScope.cofirmWindow = false;
					$rootScope.message = (res.mess);
					$scope.userList();

				} else {
					$rootScope.modalShown = true;
					$rootScope.message = (res.mess);
				}
			})
		})

		$scope.deleteRow = function (obj, i) {
			console.log(obj)
			$scope.formData = obj;
			$scope.selectedIndex = i;
			$scope.deleteFun();

		}

		$scope.fieldValidation = function () {
			$scope.role_error = ($scope.formData.role == undefined || $scope.formData.role == '') ? $scope.pageData.errorMsg[0] : '';
			$scope.smpin_error = ($scope.formData.smpin == undefined || $scope.formData.smpin == '') ? $scope.pageData.errorMsg[2] : '';
			$scope.empcode_error = ($scope.formData.emp_code == undefined || $scope.formData.emp_code == '') ? $scope.pageData.errorMsg[3] : '';
			console.log($scope.formData.emp_code, $scope.empcode_error)
			$scope.fname_error = ($scope.formData.fname == undefined || $scope.formData.fname == '') ? $scope.pageData.errorMsg[4] : '';
			$scope.email_error = ($scope.formData.emailid == undefined || $scope.formData.emailid == '') ? $scope.pageData.errorMsg[5] : '';
			$scope.mobile_error = ($scope.formData.mobile == undefined || $scope.formData.mobile == '') ? $scope.pageData.errorMsg[6] : '';
			$scope.dealer_error = ($scope.formData.dealer_code == undefined || $scope.formData.dealer_code == '') ? $scope.pageData.errorMsg[7] : '';
			$scope.segment_error = ($scope.formData.segment == undefined || $scope.formData.segment == '') ? $scope.pageData.errorMsg[10] : '';
			$scope.dob_error = ($scope.formData.date_of_join == undefined || $scope.formData.date_of_join == '') ? $scope.pageData.errorMsg[8] : '';
			$scope.zone_error = ($scope.formData.zone == undefined || $scope.formData.zone == '') ? $scope.pageData.errorMsg[9] : '';
			if ($scope.formData.role == 2) {
				return ($scope.role_error == '' && $scope.empcode_error == '' && $scope.fname_error == '' && $scope.email_error == '' && $scope.mobile_error == '') ? true : false;
			} else if ($scope.formData.role == 3 || $scope.formData.role == 4 || $scope.formData.role == 6 || $scope.formData.role == 7) {
				return ($scope.role_error == '' && $scope.empcode_error == '' && $scope.fname_error == '' && $scope.email_error == '' && $scope.mobile_error == '' && $scope.dob_error == '') ? true : false;
			} else if ($scope.formData.role == 5) {
				return ($scope.role_error == '' && $scope.empcode_error == '' && $scope.fname_error == '' && $scope.email_error == '' && $scope.mobile_error == '' && $scope.dob_error == '' && $scope.zone_error == '') ? true : false;
			} else {
				return ($scope.role_error == '' && $scope.smpin_error == '' && $scope.empcode_error == '' && $scope.fname_error == '' && $scope.email_error == '' && $scope.mobile_error == '' && $scope.dealer_error == '' && $scope.dob_error == '') ? true : false;
			}

		}
		$scope.clearErrorMsg = function () {
			$scope.role_error = '';
			$scope.smpin_error = '';
			$scope.empcode_error = '';
			$scope.fname_error = '';
			$scope.email_error = '';
			$scope.mobile_error = '';
			$scope.dealer_error = '';
			$scope.dob_error = '';
			$scope.zone_error = '';
			$scope.segment_error = '';
		}
		///////// User Submit //////////////
		$scope.userSubmit = function () {
			$scope.clearErrorMsg();
			console.log($scope.fieldValidation());
			if ($scope.fieldValidation()) {
				AppData.sendData($scope.pageurl, $scope.formData, function (res) {
					if (res.cont) {
						$rootScope.modalShown = true;
						$rootScope.message = (res.mess);
						$scope.userList();
						$scope.selectedIndex = null;
						$scope.formLoad = false;
						$scope.listFound = true;
					} else {
						$rootScope.modalShown = true;
						$rootScope.message = (res.mess);
					}
				})
			}
		}
		$scope.roleSubmit = function () {
			AppData.sendData($scope.pageurl, $scope.formData, function (res) {
				if (res.cont) {
					$rootScope.modalShown = true;
					$rootScope.message = (res.mess);
					if ($scope.formAction == "Add") {
						$scope.itemdetail.push(res.item);

					} else {
						$scope.itemdetail[$scope.selectedIndex] = res.item
					}
					$scope.selectedIndex = null;
					$scope.formLoad = false;
					$scope.listFound = true;
				} else {
					$rootScope.modalShown = true;
					$rootScope.message = (res.mess);
				}
			})

		}
		$scope.moduleSubmit = function () {
			AppData.sendData($scope.pageurl, $scope.formData, function (res) {
				if (res.cont) {
					$rootScope.modalShown = true;
					$rootScope.message = (res.mess);
					if ($scope.formAction == "Add") {
						$scope.itemdetail.push(res.item);
						$scope.listFound = true;
					} else {
						$scope.itemdetail[$scope.selectedIndex] = res.item
					}
					$scope.selectedIndex = null;
					$scope.formLoad = false;
				} else {
					$rootScope.modalShown = true;
					$rootScope.message = (res.mess);
				}
			})
		}
		$scope.permissionSubmit = function () {
			var sobj = {}
			var check = document.getElementsByClassName("clscheckbox");
			for (var i = 0; i < check.length; i++) {
				var obj = check[i];
				if (obj.checked) {
					checkAll(sobj, obj)
				}
			}
			$scope.formData.items = JSON.stringify(sobj);
			AppData.sendData($scope.pageurl, $scope.formData, function (res) {
				if (res.cont) {
					$rootScope.modalShown = true;
					$rootScope.cofirmWindow = false;
					$rootScope.message = (res.mess);
					$scope.selectedIndex = null;
					$scope.formLoad = false;
					$scope.listFound = true;
				} else {
					$rootScope.modalShown = true;
					$rootScope.cofirmWindow = false;
					$rootScope.message = (res.mess);
				}
			});

		}

		$scope.findParentname = function (g) {
			var str = "";
			if (g != 0) {
				for (var id = 0; id < $scope.itemdetail.length; id++) {
					if ($scope.itemdetail[id].moduleid == g) {
						str = $scope.itemdetail[id].modulename;
						break;
					}
				}
			}
			return str;
		}

	});
});
function loadPermission(arr) {

	for (var id = 0; id < arr.length; id++) {
		var parr = String(arr[id].permission).split("|");
		var mid = arr[id].moduleid;

		if (parr[0] == "1") {
			var obj = document.getElementById("r_" + mid);
			obj.checked = true;
		}
		if (parr[1] == "1") {
			var obj2 = document.getElementById("w_" + mid);
			obj2.checked = true;
		}
		if (parr[2] == "1") {
			var obj3 = document.getElementById("d_" + mid);
			obj3.checked = true;
		}
	}
}
function checkAll(sobj, ele) {
	var str = String(ele.id).split("_");
	if (sobj[str[1]] == undefined) {
		sobj[str[1]] = [0, 0, 0]
	}
	if (str[0] == "r") {
		sobj[str[1]][0] = 1;
	} else if (str[0] == "w") {
		sobj[str[1]][1] = 1;
	} if (str[0] == "d") {
		sobj[str[1]][2] = 1;
	}

}