define(['app'], function (app) {
    app.controller('SurveyCtrl', function ($scope,$rootScope,AppData,$location) {
		$rootScope.menucont = true;
		$rootScope.pageLoadComp = false;
		$scope.formLoad = false;
		$scope.listFound = false;
		$scope.pageData = surveyData[$rootScope.selLanguage];
		$scope.itemdetail = [];
		$scope.selectedList = [];
		//$scope.modelData = [];
		$scope.zoneList = [];
		$scope.stateList = [];
		$scope.cityList = [];
		$scope.dealerList = [];
		$scope.expiredtype = [];
		$scope.stateVisible = false;
		$scope.cityVisible = false;
		$scope.dealerVisible = false;
		//$scope.isCategoryVisible = true;
		$scope.isExpiredRowVisible = false;
		$scope.isExpiredDaysVisible = false;
		$scope.isfirst = false;
		$scope.selectedIndex = null;
		$rootScope.cofirmWindow = false;
		$rootScope.modalpublishShown =false;
		$scope.search_show = true;
		$scope.url = "../senddata/log/";
		$scope.pageurl = "../senddata/survey/";
		$scope.exporturl = "../senddata/export/";
		$scope.commonurl = "../senddata/common/";
		$scope.search_box = true;
		$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
		// $scope.formtitle = "Add Survey";
		//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 = 2;
		$scope.formData.limit = 5;
		
		//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.surveyList();
			}else{
				$location.path("/login");
			}
		})
		$scope.surveyList = function(){
			$scope.search_show = true;
			$scope.searchvalue = ($scope.formData.mymodel!='')?$scope.formData.mymodel : "";
		    var fobj = {};
				fobj.funName = "surveylist";
				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.survey;
							$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.setPage = function (pageNo) {
				$scope.currentPage = pageNo;
				};
			
				$scope.pageChanged = function() {
					$scope.surveyList();
				};
			
			$scope.setItemsPerPage = function(num) {
				$scope.itemsPerPage = num;
				$scope.currentPage = 1; //reset to first page
				$scope.surveyList();
			}
			$scope.filterData = function() {
				$scope.surveyList();
			}
			
			$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;
				$scope.surveyList();
			}
			
		$scope.deleterow = function(obj, i){
			$scope.formData = obj;
			$scope.selectedIndex = i;
			$scope.deleteFun();
		}
		$scope.exportrow = function(obj, i){
			$scope.formData = obj;
			$scope.selectedIndex = i;
			$scope.exportFun();
		}
		$scope.editrow = function(obj, i){
			$scope.formData = obj;
			$scope.selectedIndex = i;
			$scope.search_box = false;
			$scope.listFound = false;
			$scope.formLoad = true;
			$scope.editFun();
		}
		$scope.viewTiprow = function(obj, i){
			$scope.formData = obj;
			$scope.selectedIndex = i;
			$scope.viewTipFun();
		}
		$scope.viewrow = function(obj, i){
			$scope.formData = obj;
			$scope.selectedIndex = i;
			$scope.viewFun();
		}
		$scope.onSearchChange = function(){
			// console.log($scope.formData.mymodel)
			$scope.surveyList();
		  }
		
		$scope.editFun = function(){
			$(".clsbuttoncont .clsbutton").addClass("disable_btn");
			if($scope.selectedIndex != null){
				$scope.formLoad = true;
				$scope.formAction = 'Edit';
				if($scope.sideselect.id==1){
					// $scope.formtitle = "Edit Survey";
					$scope.modalTitle = "Edit Survey";
					$scope.questionList();
					$scope.getRole();
					$scope.getZone();
					$scope.regionList=[]
					$scope.formData.zoneList = {};
					// $scope.formData.regionList = {};
					$scope.formData.dealerList = {};
					var obj = {};
					obj.funName = "surveydetails";
					obj.lang = $rootScope.selLanguage;
					obj.assessmentid = $scope.formData.assessmentid;
				  AppData.sendData($scope.pageurl,obj,function(res){
					  $rootScope.pageLoadComp = true;
					  if(res.cont){
						  if(res.cont){
							//   console.log(res.item)
							  $scope.formData=res.item;
							  $scope.formData.zoneList={}
							 
								// $scope.formData.dealerList = {};
								// $scope.formData.roleList={};
							//   $scope.formData.filter_zone=res.item.filter_zone;
							  if ($scope.formData.filter_zone.length > 0) {
								angular.forEach($scope.formData.filter_zone, function (element,index) {
									// console.log(element)
									$scope.formData.zoneList[element.zone]= true;
									if (element != '') {
										$scope.zoneChanged(element,index);
									}
								});
							} else {
								$scope.stateVisible = false;
								$scope.dealerVisible = false;
		
							}
							console.log($scope.formData.filter_region,"$scope.formData.filter_region",$scope.formData.filter_dealer,$scope.formData.regionList)
							$scope.formData.regionList = {};
							if ($scope.formData.filter_region.length > 0) {
								angular.forEach($scope.formData.filter_region, function (element,index) {
									console.log(element,"region")
									$scope.formData.regionList[element.city] = true;
									
									if (element != '') {
										$scope.regionChanged(element.city ,index);
									}
								});
								console.log($scope.formData.regionList,"$scope.formData.regionList")
							} else {
								$scope.dealerVisible = false;
							}
							$scope.formData.dealerList = {};
							if ($scope.formData.filter_dealer.length > 0) {
								angular.forEach($scope.formData.filter_dealer, function (element) {
									$scope.formData.dealerList[element.dealer_code] = true;
								});
		
							}
							 $scope.formData.roleList = $scope.formData.filter_role_name;
							  	console.log($scope.formData.filter_role_name,"filter_role_name")
						  }
					  }else{
						  
					  }
				  })
					// console.log($scope.formData.filter_zone.length)
					 
				}
			}else{
				$rootScope.modalShown =true;
				$rootScope.message=$scope.pageData.errorMsg[0];
			}
		}
		$scope.deleteFun = function(){
			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 = "deletesurvey";
			AppData.sendData($scope.pageurl,$scope.formData,function(res){
				if(res.cont){
					if($scope.sideselect.id==1){
						var index = $scope.itemdetail.indexOf($scope.formData);
						if(index != -1){
							$scope.itemdetail.splice(index,1);
						}
				  }
					$rootScope.modalShown =true;
					$rootScope.cofirmWindow = false;
					$rootScope.message=(res.mess);
				}else{
					$rootScope.modalShown =true;
					$rootScope.message=(res.mess);
				}
			})
		})
		$scope.questionList = function(){
			var fobj = {};
			fobj.funName = "questions";
			fobj.lang = $rootScope.selLanguage;
			// fobj.certified=1;
			fobj.is_survey=1;
			AppData.sendData($scope.commonurl,fobj,function(res){
				$rootScope.pageLoadComp = true;
				if(res.cont){
					if(res.item!= undefined){
						$scope.assessment_question = res.item;
						console.log($scope.assessment_question)
					}
				}else{
					//$scope.listFound = false;
					$scope.errorMsg = res.mess;
				}
			})
		}
		
		$scope.getRole = function () {
			var obj = {};
			obj.funName = "role";
			obj.lang = $rootScope.selLanguage;
			obj.type = "dse"
			AppData.sendData($scope.commonurl, obj, function (res) {
				if (res.cont) {
					if (res.item != undefined) {
						$scope.role = res.item;
					}
				}
			})
		}
		$scope.getZone = function () {
			var zobj = {};
			zobj.funName = "zone";
			zobj.lang = $rootScope.selLanguage;
			AppData.sendData($scope.commonurl, zobj, function (res) {
				if (res.cont) {
					if (res.item != undefined) {
						$scope.zoneList = res.item;
					}
				}
			})
		}
		$scope.zoneChanged = function (item,i) {
			console.log(item,i,$scope.regionList)
			var obj = {};
		 	obj.cont = true;
			angular.forEach($scope.regionList,function(cityname,index){
				if(item.zone == cityname.title){
					angular.forEach(cityname.child,function(city,ind){
						for (var k in $scope.formData.regionList) {
							if(city == k){
							$scope.formData.regionList[k] = false;
						   }
						}
					})
					$scope.regionList.splice(index,1);
					obj.cont = false;
				}
			})
			// $scope.regionList=[]
			var fobj = {};
			fobj.lang = $rootScope.selLanguage;
			fobj.funName = "city";
			var zonename = item.zone;
			// if($scope.is_edit_region==false){
				
			// }else{
			// 	var zonename = item.toString();
			// 	console.log(zonename)
			// 	$scope.is_edit_region=false;
			// }
			// console.log(zonename)
			// $scope.regionList[zonename] = {};
			fobj.zone = (zonename == undefined) ? "" :zonename;
			if(obj.cont){
				AppData.sendData($scope.commonurl, fobj, function (res) {
					$rootScope.pageLoadComp = true;
					if (res.cont) {
						if (res.item != undefined) {
							$scope.stateVisible = true;
							// $scope.regionList=[]
							var arr=[];
							for (var id = 0; id < res.item.length; id++) {
								arr.push(res.item[id].city);
							}
							console.log(res.item,arr,"helloooo",$scope.regionList)
							$scope.regionList.push({"title":zonename,"child":arr}) ;
							// console.log($scope.regionList)
						} else {
							$scope.regionList = [];
							$scope.dealerList = [];
							$scope.stateVisible = false;
							$scope.dealerVisible = false;
						}
					}
				});
			}else{
				// console.log("here")
				angular.forEach($scope.dealerList,function(item,index){
					for (var k in $scope.formData.regionList) {
						if(k == item.title){
							// console.log(k,item)
							if($scope.formData.regionList[k]==false){
								angular.forEach(item.child,function(dealer,ind){
									for (var j in $scope.formData.dealerList) {
										// console.log(j,dealer)
									  if(j==dealer.dealer_code){
										$scope.formData.dealerList[j]=false;
									  }
								   }
								})
								$scope.dealerList.splice(index,1);
							}
						}
					}
				})
			}
				
			
		}
		$scope.regionChanged = function (item,i) {
			// console.log(item)
			var dobj = {};
			dobj.cont = true;
			console.log($scope.dealerList,"dealerList")
			if($scope.dealerList!=undefined){
				angular.forEach($scope.dealerList,function(dealername,index){
					// console.log(dealername)
					if(item == dealername.title){
						angular.forEach(dealername.child,function(dealer,ind){
							for (var k in $scope.formData.dealerList) {
								// console.log(k,dealer)
								if(dealer.dealer_code == k){
								$scope.formData.dealerList[k] = false;
							   }
							}
						})
						$scope.dealerList.splice(index,1);
						// dobj.cont = false;
					}
				})
			}
			// $scope.dealerList=[]
			var fobj = {};
			fobj.lang = $rootScope.selLanguage;
			fobj.funName = "dealer";
			var dealer_name = item;
			// if($scope.is_edit_dealer==false){
				
			// }else{
			// 	var dealer_name = item.toString();
			// 	$scope.is_edit_dealer= false;
			// }
			
			fobj.city = (dealer_name == undefined) ? "" :dealer_name;
			if (dobj.cont) {
				AppData.sendData($scope.commonurl, fobj, function (res) {
					$rootScope.pageLoadComp = true;
					if (res.cont) {
						if (res.item != undefined) {
							$scope.dealerVisible = true;
							var arr=[];
							for (var id = 0; id < res.item.length; id++) {
								var obj={};
								obj.dealer_name = res.item[id].dealer_name;
								obj.dealer_code = res.item[id].dealer_code;
								arr.push(obj);
							}
							// console.log(obj)
							$scope.dealerList.push({"title":dealer_name,"child":arr});
							// console.log($scope.dealerList)
						} else {
							$scope.dealerVisible = false;
							$scope.dealerList = [];
						}
					}
				});
			}else{
				alert("error")
			}

		}
		$scope.addFun = function(){
			// $scope.formtitle = "Add Survey";
			$scope.modalTitle = "Add survey";
			$scope.formData = {}
			$scope.formData.assessmenttime = 30;
			$scope.formData.questionList = {};
			$scope.zoneList = [];
			$scope.regionList = [];
			$scope.dealerList = [];
			$scope.formData.regionList = {};
			$scope.formData.dealerList = {};
			$scope.search_box = false;
			$scope.formLoad = true;
			$scope.listFound = false;
			$("input#myCheck").attr("disabled", true);
			$(".clsbuttoncont .clsbutton").addClass("disable_btn");
			$scope.formAction = 'Add';
			$scope.formLoad = true;
			$scope.formData.funName = "addsurvey";
			
			console.log('$scope.formtitle = "Add Survey";',$scope.formtitle)
			$scope.question_json = [];
			$scope.formData.random_question={};
			$scope.formData.is_mandatory={};
			$scope.questionList();
			$scope.getRole();
		    $scope.getZone();
		}
		$scope.quesChange = function(item,i){
			// console.log(item)
			if(item!=undefined){
					angular.forEach($scope.assessment_question,function(val) {
						if(item.id == val.id){
							// console.log($scope.formData.questionList,val)
							$scope.formData.is_mandatory[val.id]='0';
							// $scope.formData.questionList[item.id]=true;
						}
					})
				}
		}
		$scope.totalques = [];
		$scope.closeForm = function(){
			$scope.formLoad = false;
			$scope.formData = {};
			$scope.selectedIndex = null;
			$scope.search_box = true;
			$scope.listFound = true;
			$scope.surveyList();
		}
		$scope.surveySubmit = function(){
			if($scope.formAction=="Add"){
				$scope.formData.funName = "addsurvey";
			}else{
				$scope.formData.funName = "editsurvey";
				$scope.formData.assessmentid = $scope.formData.assessmentid;
			}
            
            $scope.formData.segment=$scope.formData.segment;
			if ($scope.formData.cityList != undefined) {
				var cityarr = [];
				for (k in $scope.formData.cityList) {
					if ($scope.formData.cityList[k] == true) {
						cityarr.push(k)
					}
				}
				if (cityarr.length != 0) {
					$scope.formData.filter_city = cityarr.toString();
				}
			}
			if ($scope.formData.regionList != undefined) {
				var statearr = [];
				for (k in $scope.formData.regionList) {
					if ($scope.formData.regionList[k] == true) {
						statearr.push(k)
					}
				}
				if (statearr.length != 0) {
					$scope.formData.filter_region = statearr.toString();
				}
			}
			if ($scope.formData.dealerList != undefined) {
				var dealerarr = [];
				for (k in $scope.formData.dealerList) {
					if ($scope.formData.dealerList[k] == true) {
						dealerarr.push(k)
					}
				}
				if (dealerarr.length != 0) {
					$scope.formData.filter_dealer = dealerarr.toString();
				}
			}
			if ($scope.formData.zoneList != undefined) {
				var zonearr = [];
				for (k in $scope.formData.zoneList) {
					if ($scope.formData.zoneList[k] == true) {
						zonearr.push(k)
					}
				}
				if (zonearr.length != 0) {
					$scope.formData.filter_zone =  zonearr.toString();
				}
			}
			if ($scope.formData.roleList.length != 0) {
				var rolearr = [];
				for (var id = 0; id < $scope.formData.roleList.length; id++) {
					rolearr.push($scope.formData.roleList[id].id)
				}
				$scope.formData.filter_role = rolearr.toString();
			}
			var arr=[];
			$scope.question_json=[];
			
			for(var h in $scope.formData.questionList){
				
				if($scope.formData.questionList[h]){
					$scope.question_json.push({qid:h,is_mandatory:$scope.formData.is_mandatory[h]});
				}
			}
			$scope.formData.question_json = JSON.stringify($scope.question_json);
			//Set notification as 1 default
			$scope.formData.assessmentnotification = "1"

			AppData.sendData($scope.pageurl,$scope.formData,function(res){
				if(res.cont){
					$rootScope.modalShown =true;
					$rootScope.message=(res.mess);
					$scope.surveyList();
					
					$scope.selectedIndex = null;
					$scope.formLoad = false;
					$scope.listFound = true;
				}else{
					$rootScope.modalShown =true;
					$rootScope.message=(res.mess);
				}
			})
		}
		
	
 });
	
}); 
