define(['app'], function (app) {
	app.controller('AssessmentCtrl', function ($scope, $rootScope, AppData, $location) {
		$rootScope.menucont = true;
		$rootScope.pageLoadComp = false;
		$scope.formLoad = false;
		$scope.listFound = false;
		$scope.pageData = assessmentData[$rootScope.selLanguage];
		console.log($scope.pageData.assessment_publish_time)
		$scope.itemdetail = [];
		$scope.selectedList = [];
		$scope.totalques = [];
		// $scope.totalAdded = false;
		$scope.quesionAdded = false
		//$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/assessment/";
		$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

		//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;
						console.log($rootScope.modulename, '$rootScope.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;
				//$rootScope.role = res.item.role;

				$scope.assessmentList();
				//$scope.moduleData();
			} else {
				$location.path("/login");
			}
		})
		$scope.assessmentList = function () {
			$scope.search_show = true;
			$scope.searchvalue = ($scope.formData.mymodel != '') ? $scope.formData.mymodel : "";
			var fobj = {};
			fobj.funName = "assessmentlist";
			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.assessment;
						$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.assessmentList();
		};

		$scope.setItemsPerPage = function (num) {
			$scope.itemsPerPage = num;
			$scope.currentPage = 1; //reset to first page
			$scope.assessmentList();
		}
		$scope.filterData = function () {
			$scope.assessmentList();
		}

		$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.assessmentList();
		}

		$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, type) {
			$scope.formData = obj;
			$scope.selectedIndex = i;
			$scope.search_box = false;
			$scope.listFound = false;
			$scope.formLoad = true;
			$scope.questionList();
			$scope.getTag();
			// $scope.getAssessmentReview();
			setTimeout(function () {
				$scope.editFun(type);
			}, 200)

		}
		$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.assessmentList();
		}

		$scope.onremoveQuestion = function (val) {
			console.log(val);

		}

		$scope.getAssessmentReview = function () {
			var obj = {};
			obj.funName = "assessmentdetails";
			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;
					}
				} else {

				}
			})
		}
		$scope.editFun = function (type) {
			$(".clsbuttoncont .clsbutton").addClass("disable_btn");
			if ($scope.selectedIndex != null) {
				$scope.random_question = {}
				$scope.formLoad = true;
				if(type==0){
					$scope.formAction = 'Edit';
				}
				else{
					$scope.formAction = 'Add';
				}
				if ($scope.sideselect.id == 1) {
					$scope.formtitle = "Edit Assessment";
					$scope.formData.zoneList = {};
					// $scope.formData.regionList = {};
					$scope.formData.dealerList = {};
					$scope.regionList = []
					$scope.questypeList();
					$scope.getRole();
					$scope.getZone();
					$scope.getUserType();
					$scope.scoreListFun();
					var obj = {};
					obj.funName = "assessmentdetails";
					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 = {}
								console.log(res.item.question_arr)
								$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;

								}
								$scope.formData.regionList = {};
								if ($scope.formData.filter_region.length > 0) {
									angular.forEach($scope.formData.filter_region, function (element, index) {
										console.log(element)
										$scope.formData.regionList[element.city] = true;
										if (element != '') {
											$scope.regionChanged(element.city, index);
										}
									});
								} 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;
									});

								}

								if (type == 0) {
									$scope.formData.roleList = $scope.formData.user_type;
								}
								else {
									$scope.formData.assessmentid = {}
									$scope.formData.roleList =''
									// // $scope.formData.funName = "addassessment";
									$scope.formAction == "Add"
								}
								// $scope.formData.roleList = $scope.formData.user_type;
								console.log($scope.formData.questionList)
								for (var key in $scope.formData.questionList) {
									$scope.formData.questionList[key] = false;
								}

								// for(i=0;i<$scope.formData.questionList.length;i++){
								// 	$scope.formData.questionList[$scope.formData.questionList[i].id]=false;
								// }

								// $scope.formData.roleList = $scope.formData.filter_role_name;
								console.log($scope.formData.question_arr, "heree")

								$scope.questionTempList = $scope.formData.question_arr;

								console.log('I am here', $scope.questionTempList)
								// $scope.random_question = {}
								// $scope.random_question_no = {}
								// for (var key in $scope.formData.questionList) {
								// 	console.log('qid here', key, $scope.assessment_question)
								// 	angular.forEach($scope.assessment_question, function (val) {
								// 		if (val.id == key) {
								// 			$scope.random_question[val.id] = val.questiontext
								// 			$scope.quesionAdded = true
								// 		}
								// 	})
								// }
								// console.log('+++++', $scope.random_question)

								// $scope.random_question = [];
								// for (var h in $scope.formData.question_arr) { 
								// 	$scope.random_question.push({ qid: $scope.formData.question_arr[h].qid, is_mandatory: $scope.formData.question_arr[h].is_mandatory });
								// }
								// $scope.random_question = JSON.stringify($scope.question_json);
								// console.log('Im here', $scope.question_json)
								// $scope.formData.question_json = JSON.stringify($scope.question_json);
								// console.log('$scope.formData.question_json',$scope.formData.question_json)
							}
						} else {

						}
					})
					console.log($scope.formData.filter_zone, "zoneee")

				} else if ($scope.sideselect.id == 2) {
					$scope.formtitle = "Edit Participant";
				}
			} else {
				$rootScope.modalShown = true;
				$rootScope.message = $scope.pageData.errorMsg[0];
			}
		}
		$scope.questypeList = function () {
			var qobj = {};
			qobj.funName = "questiontype";
			qobj.type = $scope.formData.type;
			qobj.lang = $rootScope.selLanguage;
			AppData.sendData($scope.commonurl, qobj, function (res) {
				if (res.cont) {
					$scope.questype = res.item;
				} else {
					$rootScope.modalShown = true;
					$rootScope.message = (res.mess);
				}
			})
		}
		$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 () {
			if ($scope.sideselect.id == 1) {
				$scope.formData.funName = "deleteassessment";
			} else {
				$scope.formData.funName = "deleteassessmentuser";
			}
			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 = 0;
			fobj.is_survey = 0;
			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)
						// angular.forEach($scope.assessment_question,function(element){
						// 	element.selected = false;
						// })
						
					}
				} 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) {
			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;

			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;
							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 });
						} else {
							$scope.regionList = [];
							$scope.dealerList = [];
							$scope.stateVisible = false;
							$scope.dealerVisible = false;
						}
					}
				});
			} else {

				//angular.forEach($scope.dealerList,function(item,index){
				if ($scope.dealerList.length != undefined) {
					for (i = 0; i < $scope.dealerList.length; i++) {
						for (var k in $scope.formData.regionList) {
							if ($scope.dealerList.length != undefined && $scope.dealerList.length > 0) {
								if (k == $scope.dealerList[i].title) {
									if ($scope.formData.regionList[k] == false) {
										angular.forEach($scope.dealerList[i].child, function (dealer, ind) {
											for (var j in $scope.formData.dealerList) {
												if (j == dealer.dealer_code) {
													$scope.formData.dealerList[j] = false;

												}
											}
											$scope.dealerList.splice(i, 1);
										})

									}
								}
							}
						}
					}
				}

			}


		}
		$scope.regionChanged = function (item, i) {

			var dobj = {};
			dobj.cont = true;
			if ($scope.dealerList != undefined) {
				angular.forEach($scope.dealerList, function (dealername, index) {
					if (item == dealername.title) {
						angular.forEach(dealername.child, function (dealer, ind) {
							for (var k in $scope.formData.dealerList) {
								if (dealer.dealer_code == k) {
									$scope.formData.dealerList[k] = false;
								}
							}
						})
						$scope.dealerList.splice(index, 1);
						// dobj.cont = false;
					}
				})
			}

			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);
							}
							$scope.dealerList.push({ "title": dealer_name, "child": arr });
						} else {
							$scope.dealerVisible = false;
							$scope.dealerList = [];
						}
					}
				});
			} else {
			}
		}
		$scope.getTag = function () {
			var obj = {};
			obj.funName = "questiontag";
			obj.lang = $rootScope.selLanguage;
			obj.certified = "0";
			obj.is_survey = "0";
			obj.questiontypeid = "";
			AppData.sendData($scope.commonurl, obj, function (res) {
				$rootScope.pageLoadComp = true;
				if (res.cont) {
					if (res.item != undefined) {
						console.log(res.item)
						$scope.tagList = res.item;
					} else {

					}
				}
			});
		}

		$scope.getUserType = function () {
			var obj = {};
			obj.funName = "accessmentusertype";
			obj.lang = $rootScope.selLanguage;
			obj.certified = "0";
			obj.is_survey = "0";
			obj.questiontypeid = "";
			AppData.sendData($scope.commonurl, obj, function (res) {
				$rootScope.pageLoadComp = true;
				if (res.cont) {
					if (res.item != undefined) {
						console.log(res.item)
						$scope.UserType = res.item;
					} else {

					}
				}
			});
		}
		$scope.changeTag = function () {
			var obj = {};
			obj.funName = "questions";
			obj.lang = $rootScope.selLanguage;
			obj.certified = "0";
			obj.is_survey = "0";
			obj.questiontypeid = "";
			obj.tag = $scope.formData.tag;
			AppData.sendData($scope.commonurl, obj, function (res) {
				$rootScope.pageLoadComp = true;
				if (res.cont) {
					if (res.item != undefined) {
						console.log(res.item)
						$scope.assessment_question = res.item;
						// $scope.questionList = res.item;
					} else {

					}
				}
			});
		}
		
		$scope.scoreListFun = function(){
			for(var i=1;i<=100;i++){
				// console.log(i)
				$scope.scoreList.push(i);
			}
			$scope.formData.pass_score = '80';
		}
		$scope.addFun = function () {
			$scope.formData = {};
			$scope.assessment_question = [];
			$scope.scoreList=[];
			$scope.formData.assessmenttime = 30;
			$scope.formData.attemptslimit = 1;
			$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;
			$scope.questionTempList = []
			$("input#myCheck").attr("disabled", true);
			$(".clsbuttoncont .clsbutton").addClass("disable_btn");
			$scope.formAction = 'Add';
			$scope.formLoad = true;
			$scope.formData.funName = "addassessment";
			$scope.formtitle = "Add Assessment";
			console.log($scope.formtitle)
			$scope.question_json = [];
			$scope.random_question = {};
			$scope.formData.is_mandatory = {};
			$scope.questionList();
			$scope.getRole();
			$scope.getZone();
			$scope.getTag();
			$scope.getUserType();
			$scope.scoreListFun();
		}
		$scope.quesChange = function (item, i) {
			console.log('+++++', $scope.assessment_question)
			if (item != undefined) {
				for (var i = 0; i < $scope.assessment_question.length; i++) {
					if ($scope.assessment_question[i].id == item.id) {
						// console.log($scope.assessment_question[i].selected)
						if ($scope.assessment_question[i].selected) {
							$scope.formData.is_mandatory[item.id] = '0';
							$scope.formData.questionList[item.id] = true;
						} else {
							$scope.formData.questionList[item.id] = false;
						}
					}

				}

				// if(item.id == val.id){
				// 	console.log($scope.formData.questionList,val)
				// 	
				// console.log($scope.formData.questionList[val.id])
				// var index = $scope.questionTempList.indexOf(item);
				// console.log(index)
				// if($scope.formData.questionList[val.id]){
				// 	$scope.questionTempList.push(item)
				// }else{
				// 	$scope.questionTempList.splice(index,1)
				// }


				// 	}

			}


			// console.log(item)
			// if (item != undefined) {
			// 	angular.forEach($scope.assessment_question, function (val) {
			// 		if (item.id == val.id) {
			// 			console.log('SCOPE', $scope.formData)
			// 			$scope.formData.is_mandatory[val.id] = '0';
			// 			// $scope.formData.questionId[val.id]=val.id;
			// 		}
			// 	})
			// }
		}


		$scope.closeForm = function () {
			$scope.formLoad = false;
			$scope.formData = {};
			$scope.selectedIndex = null;
			$scope.search_box = true;
			$scope.listFound = true;
			$scope.assessmentList();
			$scope.clearErrorMsg()
		}

		$scope.onremoveQuestion = function (val) {
			$scope.random_question = {}
			var index = val
			angular.forEach(this.assessment_question, function (val) {
				if (val.id == index) {
					console.log($scope.random_question[index])

					$scope.random_question[index].splice(val.id, 1);

				}
			})
			//  $scope.this.assessment_question.splice(index,1);  	
		}


		$scope.addQuestions = function () {
		
			for (var i = 0; i < $scope.assessment_question.length; i++) {
				//console.log($scope.assessment_question[i])
				var index = $scope.questionTempList.indexOf($scope.assessment_question[i]);
				if ($scope.assessment_question[i].selected) {
					if (index == -1) {
						$scope.questionTempList.push($scope.assessment_question[i])
						$scope.assessment_question[i].selected= false;
					} else {
						// $rootScope.modalShown = true;
						// $rootScope.message = "Same question already added in the question list";
					}
				}
			}
			console.log('addQuestions', $scope.questionTempList.length)

	$scope.question_tag_error= ($scope.questionTempList.length == undefined || $scope.questionTempList.length == '0' ) ? 'Please select atleast one question': '';

			// $scope.random_question = {}
			// for (var key in $scope.formData.questionList) {
			// 	angular.forEach(this.assessment_question, function (val) {
			// 		if (val.id == key) {
			// 			$scope.random_question[val.id] = val.questiontext
			// 			$scope.quesionAdded = true
			// 		}
			// 	})
			// }
			// angular.forEach($scope.formData.questionList, function (val) {
			// 	$scope.formData.random_question[val.id]="" 
			// }) 
		}
		$scope.fieldValidation = function () {
			$scope.assessment_error = ($scope.formData.assessmentname == undefined || $scope.formData.assessmentname == '') ? 'Please enter assessment name' : '';
			$scope.type_error = ($scope.formData.assessmenttimetype == undefined || $scope.formData.assessmenttimetype == '') ? 'Please select assessment type' : '';

			$scope.publisheddate_error = ($scope.formData.publisheddate == undefined || $scope.formData.publisheddate == '') ? 'Please select published date' : '';
			$scope.enddate_error = ($scope.formData.enddate == undefined || $scope.formData.enddate == '') ? 'Please select end date' : '';
			$scope.starttime_error = ($scope.formData.publishedtime == undefined || $scope.formData.publishedtime == '') ? 'Please select start time' : '';
			$scope.endtime_error = ($scope.formData.endtime == undefined || $scope.formData.endtime == '') ? 'Please select end time' : '';
			$scope.ques_error = ($scope.formData.randomcond == undefined || $scope.formData.randomcond == '') ? 'Please select question type' : '';
			$scope.role_error = ($scope.formData.roleList == undefined || $scope.formData.roleList == '') ? 'Please select user type' : '';
            $scope.segment_error = ($scope.formData.segment == undefined || $scope.formData.segment == '') ? 'Please select any one' : '';
			// $scope.zone_error = ($scope.formData.zoneList == undefined || $scope.formData.zoneList == '') ? 'Please select zone' : '';


			$scope.addquestion_error = ($scope.questionTempList.length == 0) ? 'Please add a question ' : '';
			// console.log("9999", $scope.questionTempList)
			//    $scope.limit_error = ($scope.formData.attemptslimit == undefined || $scope.formData.attemptslimit == '') ?   'Please enter no. of attempts limit' : '';
			//    $scope.assessmenttime_error = ($scope.formData.assessmenttime == undefined || $scope.formData.assessmenttime == '') ?   'Please enter assessment time' : '';
			// console.log('+-+-+-',$scope.assessment_error,$scope.type_error , $scope.publisheddate_error,$scope.enddate_error,$scope.starttime_error , $scope.endtime_error, $scope.ques_error,$scope.role_error ,$scope.zone_error&&$scope.addquestion_error)
			return ($scope.assessment_error == '' && $scope.type_error == '' && $scope.publisheddate_error == '' && $scope.enddate_error == '' && $scope.starttime_error == '' && $scope.endtime_error == '' && $scope.ques_error == '' && $scope.role_error == ''  && $scope.addquestion_error == '' &&  $scope.segment_error=='') ? true : false;
		}

		$scope.clearErrorMsg = function () {
			$scope.assessment_error = '';
			$scope.type_error = '';
			$scope.publisheddate_error = '';
			$scope.enddate_error = '';
			$scope.starttime_error = '';
			$scope.endtime_error = '';
			$scope.ques_error = '';
			$scope.role_error = '';
			$scope.zone_error = '';
			$scope.addquestion_error = ''
            $scope.segment_error='';
			// $scope.questiontype_error = '';

			//    $scope.limit_error = '';
			//    $scope.assessmenttime_error = '';
		}

		$scope.assessmentSubmit = function () {
			$scope.clearErrorMsg();
			if ($scope.formAction == "Add") {
				$scope.formData.funName = "addassessment";
			} else {
				$scope.formData.funName = "editassessment";
				$scope.formData.assessmentid = $scope.formData.assessmentid;
			}
			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 = [];
				console.log($scope.formData.regionList, "$scope.formData.regionList")
				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 = [];
				console.log($scope.formData.dealerList, "$scope.formData.dealerList")
				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 = [];
				console.log($scope.formData.zoneList, "$scope.formData.zoneList")
				for (k in $scope.formData.zoneList) {
					if ($scope.formData.zoneList[k] == true) {
						zonearr.push(k)
						console.log(zonearr, "zonearr")
					}
				}
				if (zonearr.length != 0) {
					$scope.formData.filter_zone = zonearr.toString();
				}
			}


			var arr = [];
			$scope.question_json = [];
			console.log($scope.questionTempList)
			
			for (var h = 0; h < $scope.questionTempList.length; h++) {
				$scope.question_json.push({ qid: ($scope.questionTempList[h].id!=undefined)?$scope.questionTempList[h].id:$scope.questionTempList[h].qid, is_mandatory: $scope.formData.is_mandatory[($scope.questionTempList[h].id!=undefined)?$scope.questionTempList[h].id:$scope.questionTempList[h].qid] });
			}
			
			
			console.log($scope.question_json)
			$scope.formData.question_json = JSON.stringify($scope.question_json);
			console.log($scope.formData.question_json)
			$scope.formData.assessmentnotification = "1";
			$scope.formData.total_question = $scope.questionTempList.length;
			$scope.formData.user_type = $scope.formData.roleList;
            $scope.formData.segment = $scope.formData.segment
			// 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();
			// }
			// $scope.formData.filter_role = []
			// $scope.formData.total_question = Object.keys($scope.random_question).length;
			// console.log(Object.keys($scope.random_question).length)
			// $scope.formData.atype = '0'
			// $scope.formData.user_type = $scope.formData.roleList;
			// var arr = [];
			// $scope.question_json = [];
			// for (var h in $scope.random_question) {
			// 	if ($scope.random_question[h]) {
			// 		$scope.question_json.push({ qid: h, is_mandatory: $scope.formData.is_mandatory[h] });
			// 	}
			// }
			// $scope.formData.question_json = JSON.stringify($scope.question_json);
			// console.log($scope.formData.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);
			console.log('$scope.fieldValidation())', $scope.fieldValidation())
			if ($scope.fieldValidation()) {
				AppData.sendData($scope.pageurl, $scope.formData, function (res) {
					if (res.cont) {
						$rootScope.modalShown = true;
						$rootScope.message = (res.mess);
						$scope.assessmentList();

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

		$scope.deleteQuestion = function (item) {
			var index = $scope.questionTempList.indexOf(item);
			if (index != -1) {
				$scope.questionTempList.splice(index, 1);
			}
			// console.log($scope.questionTempList)
		}


	});

}); 
