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

		$rootScope.menucont = true;
		$rootScope.pageLoadComp = false;
		$scope.formLoad = false;
		$scope.listFound = false;
		$scope.pageData = questionData[$rootScope.selLanguage];
		$scope.formAction = "Add";
		$rootScope.sideselect = $scope.pageData.sidemenu[0];
		console.log($rootScope.sideselect)
		$scope.itemdetail = [];
		$scope.selectedIndex = null;
		$rootScope.cofirmWindow = false;
		$scope.submenu = $scope.pageData.sidemenu;
		$scope.title = $rootScope.sideselect.name;
		$scope.tblheading = $rootScope.sideselect.headingText;
		$scope.url = "../senddata/log/";
		$scope.pageurl = "../senddata/question/";
		$scope.commonurl = "../senddata/common/";
		$rootScope.imageFormat = ["image/jpeg", "image/png", "video/mp4"];
		$rootScope.qimage = '';
		$scope.search_box = true;

		//pagination
		$scope.viewby = '10';
		$scope.currentPage = 1;
		$scope.itemsPerPage = $scope.viewby;
		$scope.maxSize = 5; //Number of pager buttons to show
		$scope.pageSize = 10; 

		//Check Session//
		$scope.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.catearray = [];
		$scope.typearray = [];
		$scope.optdetail = [];
		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.questionList = function () {
					$scope.searchvalue = ($scope.formData.mymodel != '') ? $scope.formData.mymodel : "";
					var fobj = {};
					fobj.funName = "questionlist";
					fobj.lang = $rootScope.selLanguage;
					fobj.pn = $scope.currentPage;
					fobj.nr = $scope.itemsPerPage;
					fobj.lang = $rootScope.selLanguage;
					fobj.key = $scope.searchvalue;
					fobj.tag = ($scope.formData.tag!=undefined || $scope.formData.tag!='')?$scope.formData.tag:'';
					// fobj.cateid = 0;

					$rootScope.pageLoadComp = true;
					AppData.sendData($scope.pageurl, fobj, function (res) {
						$rootScope.pageLoadComp = true;
						if (res.cont) {
							if (res.item != undefined) {
								$scope.question_data = res.item.questions;
								$scope.totalItems = res.item.total_records;
								$scope.listFound = true;
								$scope.search_box = true;
							}
						} else {
							$scope.listFound = false;
							$scope.errorMsg = res.mess;
						}
					})
				}

				$scope.getTag = function () {
					var obj = {};
					obj.funName = "allquestiontag";
					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.changeTag = function(){
					$scope.questionList();
					$scope.getTag()
				}
				$scope.getTag()
				$scope.questionList();
				$scope.setPage = function (pageNo) {
					$scope.currentPage = pageNo;
				};

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

				$scope.setItemsPerPage = function (num) {
					$scope.itemsPerPage = num;
					$scope.currentPage = 1; //reset to first page
					console.log('ItemPage : ' + $scope.itemsPerPage);
					$scope.questionList();
				}
				$scope.filterData = function () {
					console.log($scope.searchtext)
					$scope.questionList();
				}

			} else {
				$location.path("/login");
			}
		})
		$scope.onSearchChange = function () {
			console.log($scope.formData.mymodel)
			$scope.questionList();
		}
		$scope.cartiQuesList = function () {
			var fobj = {};
			fobj.funName = "questionlist";
			fobj.lang = $rootScope.selLanguage;
			fobj.pn = $scope.currentPage;
			fobj.nr = $scope.itemsPerPage;
			fobj.lang = $rootScope.selLanguage;
			fobj.key = $scope.searchtext;
			fobj.certified = 1;
			$rootScope.pageLoadComp = true;
			AppData.sendData($scope.pageurl, fobj, function (res) {
				$rootScope.pageLoadComp = true;
				if (res.cont) {
					if (res.item != undefined) {
						$scope.cert_ques_data = res.item.questions;
						$scope.totalItems = res.item.total_records;
						$scope.listFound = true;
						$scope.search_box = true;
					}
					$scope.formData = {}
				} else {
					$scope.listFound = false;
					$scope.errorMsg = res.mess;
				}
			})
		}

		$scope.listClick = function (obj) {
			$(".clsbuttoncont .clsbutton").removeClass("disable_btn");
			$scope.formLoad = false;
			$rootScope.sideselect = obj;
			$scope.itemdetail = [];
			$scope.selectedIndex = null;
			var fobj = {};
			fobj.lang = $rootScope.selLanguage;
			fobj.funName = "questionlist";
			$scope.title = obj.name;
			if (obj.id == 1) {
				$scope.tblheading = obj.headingText;
				$scope.questionList();
			} else if (obj.id == 2) {
				$scope.tblheading = obj.headingText;
				$scope.cartiQuesList();
			}

		}

		$scope.changeRemarks = function () {
			if ($scope.formData.rtype == 2 || $scope.formData.rtype == 3) {
				$scope.optdetail = [];
				$scope.optdetail.push({ optid: 1, iscorrect: 0, opttext: "", score: "" });
				$scope.optdetail.push({ optid: 2, iscorrect: 0, opttext: "", score: "" });
				console.log($scope.optdetail)

			}


		}
		/*$scope.curRowClick = function(obj,e){
			$scope.formData = obj
			$scope.selectedIndex = e;
		}*/
		$scope.deleterow = function (obj, i) {
			$scope.formData = obj;
			$scope.selectedIndex = i;
			$scope.deleteFun();
		}
		
		$scope.editrow = function (obj, i) {
			console.log(obj)
			$scope.formData = obj;
			$scope.selectedIndex = i;
			console.log($scope.formData);
			$scope.search_box = false;
			$scope.formLoad = true;
			$scope.listFound = false;
			$rootScope.qimage = '';
			if ($scope.formData.qtype == 3) {
				$scope.formData.rtype = $scope.formData.surveyqtype;
				$scope.category = $scope.surveycatearray;

			} else {
				$scope.category = $scope.catearray;
				$scope.formData.cattype = $scope.formData.cattype;
			}
			$scope.editFun();
		}
		$scope.viewTiprow = function (obj, i) {
			$scope.formData = obj;
			$scope.selectedIndex = i;
			$scope.viewTipFun();
		}
		$scope.viewrow = function (item, i) {
			var obj = {};
			obj.id = item.id;
			obj.funName = "questiondetails";
			AppData.sendData($scope.pageurl, obj, function (res) {
				if (res.cont) {
					$scope.view_question = res.item;
					//console.log($scope.view_question)
				//	console.log($scope.view_question[0].options)
					
					// $scope.optdetail = res.item;
				}
			})
			// angular.forEach($scope.question_data,function(element)  {
			// 	if(element.id==obj.id){
			// 		console.log(element)
			// 		$scope.view_question = element;
			// 	}
			// });
		}
		$scope.editFun = function () {
			$scope.questypeList();
			$scope.getTrainingLevel();
			if ($scope.optdetail != undefined && $scope.optdetail != '') {
				if ($scope.optdetail.length < 0) {
					$scope.formData.child = [];
				}
			} else {
				$scope.formData.child = [];
			}
			console.log($scope.optdetail)
			$scope.formtitle = "Edit Question";
			$scope.formData.funName = "editquestion";
			$(".clsbuttoncont .clsbutton").addClass("disable_btn");
			$scope.formData.lvl_id = $scope.formData.cateid
			if ($scope.selectedIndex != null) {
				if ($scope.formData.questiontypeid == '1' || $scope.formData.questiontypeid == '2' || $scope.formData.questiontypeid == '5' || $scope.formData.questiontypeid == '6') {
					console.log($scope.formData.qtype)
					var obj = {};
					obj.id = $scope.formData.id;
					obj.funName = "questionoptionlist";
					AppData.sendData($scope.pageurl, obj, function (res) {
						if (res.cont) {
							$scope.formData.child = res.item;
							console.log($scope.formData.child)
							$scope.optdetail = res.item;
							


						}
					})
				}
				$scope.formLoad = true;
				$scope.formAction = "Edit";
				// $scope.title = $rootScope.sideselect.title[1];
				
			} 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 = "deletequestion";

			AppData.sendData($scope.pageurl, $scope.formData, function (res) {
				if (res.cont) {
					if ($rootScope.sideselect.id == 1) {
						var index = $scope.question_data.indexOf($scope.formData);
						if (index != -1) {
							$scope.question_data.splice(index, 1);
						}
					} else if ($rootScope.sideselect.id == 2) {
						var index = $scope.cert_ques_data.indexOf($scope.formData);
						if (index != -1) {
							$scope.cert_ques_data.splice(index, 1);
						}
					}

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

		//$scope.optionshow = false;
		$scope.changeQuestype = function () {
			if ($scope.formData.questiontypeid == 1 || $scope.formData.questiontypeid == 2 || $scope.formData.questiontypeid == 5 || $scope.formData.questiontypeid == 6) {
				//$scope.optionshow = true;
				$scope.optdetail = [];
				$scope.optdetail.push({ optid: 1, iscorrect: 0, opttext: "", score: "" });
				$scope.optdetail.push({ optid: 2, iscorrect: 0, opttext: "", score: "" });

				console.log($scope.optdetail)
			} else {
				//$scope.optionshow = false;
				$scope.optdetail = [];
			}
		}
		$scope.changetype = function () {
			if ($scope.formData.type == 1) {
				$scope.questypeList();
				$scope.getTrainingLevel()
			} else {
				$scope.questypeList();
			}

		}
		// get the Training Level list Basic, Bronze, Silver, Gold
		$scope.getTrainingLevel = function () {
			var fobj = {};
			fobj.lang = $rootScope.selLanguage;
			fobj.type = '1';
			fobj.funName = "certificate_level";
			AppData.sendData($scope.commonurl, fobj, function (res) {
				if (res.cont) {
					if (res.item != undefined) {
						$scope.topicVisible = true;

						if (res.item.length > 0) {
							$scope.traininglevels = res.item;
						}
					} else {
					}
				}
			});
		}
		$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.addFun = function () {
			$scope.formLoad = true;
			$scope.listFound = false;
			$scope.search_box = false;
			$scope.formAction = "Add";
			$scope.formtitle = "Add Question";
			$scope.title = $rootScope.sideselect.title[0];
			$scope.selectedIndex = null;
			$(".supplieredit").prop('disabled', false);
			// $scope.questypeList();
			$(".clsbuttoncont .clsbutton").addClass("disable_btn");
			$scope.formData = {};

			$scope.formData.funName = "addquestion";
			$scope.formData.child = [];
			$scope.optdetail = []
			$scope.category = [];
			$scope.optdetail.push({ optid: 1, iscorrect: 0, opttext: "", score: "" });
			$scope.optdetail.push({ optid: 2, iscorrect: 0, opttext: "", score: "" });
			console.log($scope.optdetail)
		}
		$scope.addChoice = function () {
			var len = getMaxID($scope.optdetail);
			$scope.optdetail.push({ optid: len, iscorrect: 0, opttext: "", score: "" });
			console.log($scope.optdetail)
		}
		$scope.deleteChioce = function (index, item) {
			console.log(index, item)
			if ($scope.optdetail.length > 2) {
				if ($scope.optdetail != undefined) {
					var num = $scope.optdetail.indexOf(index);

					if (num != -1) {
						$scope.optdetail.splice(num, 1);
						console.log($scope.optdetail);
					}

				}

				if ($scope.formAction == "Add") {
					if ($scope.formData.child != undefined) {
						var num2 = $scope.formData.child.indexOf(item);
						if (num2 != -1) {
							$scope.formData.child.splice(num2, 1);
						}
					}
				}
				console.log($scope.optdetail, $scope.formData.child);
			}
		}
		$scope.closeForm = function () {
			$scope.formLoad = false;
			$scope.formData = {};
			$scope.selectedIndex = null;
			$scope.search_box = true;
			$scope.listFound = true;
			$(".clsbuttoncont .clsbutton").removeClass("disable_btn");
			$scope.questionList();
		}

		$scope.questionSubmit = function () {

			$scope.formData.lang = $rootScope.selLanguage;
			$scope.formData.cateid = $scope.formData.lvl_id;
			// $scope.formData.tag=$scope.formData.tag;
			console.log($scope.formData.tag)
			var obj = selectcheckboxStatus($scope.pageData.errorMsg, $scope.formData.qtype);
			var arr = checkboxStatus();
			//console.log(arr)
			for (id = 0; id < arr.length; id++) {
				$scope.formData.child[arr[id]].iscorrect = 0;
			}
			var str = JSON.stringify($scope.formData.child);
			$scope.formData.child = str;
			//  $scope.formData.tag=" ";
			console.log($scope.formData)

			AppData.sendData($scope.pageurl, $scope.formData, function (res) {
				if (res.cont) {
					$rootScope.modalShown = true;
					$rootScope.message = (res.mess);
					// $scope.formData.tag=''
					$scope.questionList();
					$scope.getTag();
					
					$scope.selectedIndex = null;
					$scope.formLoad = false;
					$scope.listFound = true;
				} else {
					$rootScope.modalShown = true;
					$rootScope.message = (res.mess);
				}
			})
		
		}
		// $scope.fieldValidation = function(){
		// 	$scope.type_error = ($scope.formData.type == undefined || $scope.formData.type == '') ? 'Please select the type' : '';
		// 	$scope.qtype_error = ($scope.formData.questiontypeid == undefined || $scope.formData.questiontypeid == '') ? 'Please select the Question type' : '';
		// 	$scope.ctype_error = ($scope.formData.lvl_id == undefined || $scope.formData.lvl_id == '') ? 'Please select the Category type' : '';
		// 	$scope.tag_error = ($scope.formData.tag == undefined || $scope.formData.tag == '') ? 'Please enter the tag' : '';
		
		// }

		$scope.certiQuestionSub = function () {

			$scope.formData.lang = $rootScope.selLanguage;
			var obj = selectcheckboxStatus($scope.pageData.errorMsg, $scope.formData.qtype);
			var arr = checkboxStatus();
			console.log(arr)
			for (id = 0; id < arr.length; id++) {
				$scope.formData.child[arr[id]].iscorrect = 0;
			}
			var str = JSON.stringify($scope.formData.child);
			$scope.formData.child = str;
			console.log(str)

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

		}

		$scope.RemoveImage = function (obj) {
			current_imageObject = $(obj).parent().parent().find('img');
			current_imageObject.attr('src', "");
			$rootScope.image = '';
		}



	});
});
function getMaxID(arr) {
	var m = [];
	for (var id = 0; id < arr.length; id++) {
		m.push(arr[id].optid);
	}
	var MaxNum = Math.max.apply(null, m);
	return (MaxNum + 1);
}
function checkboxStatus() {
	var arr = [];
	var obj = document.getElementsByClassName("clsiscorrectbox");
	for (var id = 0; id < obj.length; id++) {
		var ms = obj[id];
		if (!ms.checked) {
			var index = String(ms.id).split("checkbox_")[1]
			arr.push(index);
		}
	}
	return arr;
}
function selectcheckboxStatus(arr, mid) {
	var marr = []
	var obj = document.getElementsByClassName("clsiscorrectbox");
	for (var id = 0; id < obj.length; id++) {
		var ms = obj[id];
		if (ms.checked) {
			marr.push(id);
		}
	}
	var obj = {};
	obj.cont = false;
	if (marr.length != 0) {
		if (mid == 1) {
			if (marr.length == 1) {
				obj.cont = true;
			} else {
				obj.cont = false;
				obj.mess = arr[2];
			}
		} else if (mid == 2) {
			obj.cont = true;
		}
	} else {
		obj.cont = false;
		obj.mess = arr[3];
	}
	return obj;
}
function onRemoveImage(obj) {
	current_imageObject = $(obj).parent().parent().find('img');
	// var iEl = angular.element( document.querySelector( '#store_image_1' ) );
	// iEl.remove();

	current_imageObject.attr('src', "");
}
function onclickimg(num) {
	console.log(num);
	console.log('#imgclick' + num);
	//$('#imgclick'+num).click();
}
function onAddImage(obj) {
	//current_imageObject= $(obj).parent().parent().find('img');
	//var num = current_imageObject.attr('data-id');
	console.log('#imgclick' + obj);
	//var element_string="<div><input type='file' id='informationimage"+num+"' data-id='"+num+"' name='informationimage'   accept='image/*' onchange='readURL(this);' hidden /></div>";

	//$('body').append(element_string);
	//$('#imgclick'+obj).click();
}
function readURL(input) {
	var num = current_imageObject.attr('data-id');
	if (input.files && input.files[0]) {
		var reader = new FileReader();

		reader.onload = function (e) {
			console.log(num);
			$('#store_image_' + num)
				.attr('src', e.target.result)
				.width(119)
				.height(112);
		}
		reader.readAsDataURL(input.files[0]);
	}
}