define(['app'], function (app) {
    app.controller('TrainingCtrl', function ($scope,$rootScope,AppData,$location) {
		$rootScope.menucont = true;
		$rootScope.pageLoadComp = false;
		$scope.formLoad = false;
		$scope.listFound = false;
		$scope.pageData = trainingData[$rootScope.selLanguage];
		console.log($scope.pageData.assessment_publish_time)
		$scope.itemdetail = [];
		//$scope.modelData = [];
		// $scope.zoneList = [];
		$scope.stateList = [];
		$scope.cityList = [];
		$scope.dealerList = [];
		$scope.stateVisible = false;
		$scope.cityVisible = false;
		$scope.dealerVisible = false;
		
		$scope.url = "../senddata/log/";
		$scope.pageurl = "../senddata/training/";
		$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 = {}
		$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;
		// delete the below
		$scope.cateitem = null;
		$scope.questionitem = null;
		$scope.assignquestionitem = [];
		$scope.assignquestionnum = [];
		// delete end

		$scope.selectedLevel = 0;
		$scope.traininglevels = [];
		$scope.trainerlists = [];
		$scope.filterAvailable = false;
		$scope.dealerAvailable = false;
		$scope.participantAvailable = false;
		$scope.selectedtrainercity = [];
		$scope.filter_regions = [];
		$scope.selectedCityList = [];
		$scope.selectedCityDealer = [];
		$scope.filter_dealers = [];
		// $scope.trainerParticipantList = [];
		$scope.participant = [];
		$scope.selectedDealerParticipants = [];
		$scope.finalDealerParticipant = [];
		
		$scope.date =new Date();
		$scope.fullyear = $scope.date.getFullYear();
		$scope.curyear = $scope.date.getFullYear();
		$scope.curnmonth = $scope.date.getMonth();
		$scope.curnmonth = $scope.curnmonth + 1;
		$scope.minDate = $scope.date + 4;
		console.log($scope.minDate);
		$scope.trainingduration = [{"day":1, "dur":"1 day"}, {"day":2, "dur":"2 day"}, {"day":3, "dur":"3 day"}, {"day":4, "dur":"4 day"}, {"day":5, "dur":"5 day"}];
		$scope.tstart_time = [{"val":10, "time":"10:00"},{"val":11, "time":"11:00"},{"val":12, "time":"12:00"},{"val":13, "time":"13:00"},{"val":14, "time":"14:00"},{"val":15, "time":"15:00"}];
		$scope.trend_time = 16;
		$scope.notificationdate = [5, 4, 3, 2, 1];
		$scope.bufferdate = [5, 4, 3, 2, 1];
		$scope.ass_duration = [15, 30, 45, 60, 75, 90, 105, 120];
		$scope.ass_type = [{"val":0, "txt":"Normal"}, {"val":1, "txt":"Random"}];
		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;
				//$rootScope.role = res.item.role;
				
				$scope.trainingList();
				//$scope.moduleData();
			}else{
				$location.path("/login");
			}
		});
		$scope.trainingList = function(){
			$scope.search_show = true;
		    var fobj = {};
				fobj.funName = "traininglist";
				if($rootScope.sideselect.id == 1){
					fobj.pn = $scope.currentPage;
					fobj.nr = $scope.itemsPerPage;
				}else{
					fobj.pn = '';
					fobj.nr = '';
				}
				fobj.month = $scope.curnmonth;
				fobj.year = $scope.curyear;
				fobj.lang = $rootScope.selLanguage;
				fobj.key = $scope.searchtext;
				$rootScope.pageLoadComp = true;
				AppData.sendData($scope.pageurl,fobj,function(res){
					$rootScope.pageLoadComp = true;
					console.log("total records::::"+res.item.total_records)
					// if(res.cont && res.item.total_records>0){
					if(res.cont){
						if(res.item!= undefined){
							$scope.itemdetail = res.item.training;
							$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.trainingList();
				};
			
			$scope.setItemsPerPage = function(num) {
				$scope.itemsPerPage = num;
				$scope.currentPage = 1; //reset to first page
				$scope.trainingList();
			}
			$scope.filterData = function() {
				$scope.trainingList();
			}
			$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.trainingList();
			}
			
		$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.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.geteditQues = function(item,i){
			$scope.supplierlist[i].question=[];
			$scope.selectIndex = i;
			var arr = [];
			var fobj = {};
			fobj.lang = $rootScope.selLanguage;
			fobj.funName="questionlist";
			for(var k in $scope.formData.supplierList){
				if(!$scope.formData.supplierList[k]){
					$scope.totalques[item.supplierid] = false;
					$scope.supplier_json=[];
				}else{
					
					$scope.totalques[item.supplierid] = true;
				}
			}
			AppData.sendData($scope.pageurl,fobj,function(res){
				$rootScope.pageLoadComp = true;
				if(res.cont){
					if(res.item != undefined){
						$scope.topicVisible = true;
						if(res.item.length>0)
						{
							$scope.supplierlist[i].question = res.item;
							$('#collapse'+(i+1)).addClass("in")
							$scope.formData.random_question[item.supplierid]=item.random_question;
							$scope.formData.total_question[item.supplierid] = item.total_question;
							if($scope.supplierlist[i].question!=undefined){
								angular.forEach($scope.question_data,function(val) {
									
									$scope.formData.questionList[val.qid] = true;
									$scope.formData.is_mandatory[val.qid]=val.is_mandatory;
									$scope.formData.questionsupplierList[val.qid]=val.supplierid;
								})
							}
						}
					}else{
						$scope.topicVisible = false;
					}
					
				}
			});
		}
		$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("cofirmDelete",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);
						}
				  }else if($scope.sideselect.id==2){
					var index = $scope.participantdetail.indexOf($scope.formData);
					if(index != -1){
						$scope.participantdetail.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;
			AppData.sendData($scope.pageurl,fobj,function(res){
				$rootScope.pageLoadComp = true;
				if(res.cont){
					if(res.item!= undefined){
						$scope.user_assessment = res.item.questions;
						
					}
				}else{
					//$scope.listFound = false;
					$scope.errorMsg = res.mess;
				}
			})
		}
		
		$scope.getAssessmentList = function(){
			var fobj = {};
			fobj.funName = "assessmentlist";
			fobj.trainingid = $scope.formData.trainingid;
			fobj.lang = $rootScope.selLanguage;
			AppData.sendData($scope.pageurl,fobj,function(res){
				$rootScope.pageLoadComp = true;
				if(res.cont){
					if(res.item!= undefined){
						$scope.user_assessment = res.item.assessment;
						
					}
				}else{
					//$scope.listFound = false;
					$scope.errorMsg = res.mess;
				}
			})
		}
		
		$scope.changeAssessment = function(){
			$scope.getParticipant();
		}
		$scope.editrow = function(obj, i){
			console.log(obj);
			console.log(i)
			$scope.formData = obj;
			$scope.selectedIndex = i;
			$scope.search_box = false;
			$scope.formLoad = true;
			$scope.listFound = false;
			$scope.editFun();
		}
		$scope.editFun = function(){
			$(".clsbuttoncont .clsbutton").addClass("disable_btn");
			if($scope.selectedIndex != null){
				$scope.selectedLevel = $scope.formData.lvl_id;
				$scope.formLoad = true;
				$scope.formAction = 'Edit';
				$scope.formtitle = "Edit Training";
				$scope.tstart_time = [{"val":10, "time":"10:00"},{"val":11, "time":"11:00"},{"val":12, "time":"12:00"},{"val":13, "time":"13:00"},{"val":14, "time":"14:00"},{"val":15, "time":"15:00"}];
				// $scope.questionList();
				$scope.getTrainingLevel();
				$scope.getTrainerList();
				$scope.loadtrainerregion();
				var region_arr = JSON.parse($scope.formData.filter_region);
				var dealer_arr = JSON.parse($scope.formData.filter_dealer);
				$scope.formData.filter_regions = {};
				$scope.formData.filter_dealers = {};
				angular.forEach(region_arr,function(item, i) {
					$scope.formData.filter_regions[item] = true;
					var tmp = {};
					tmp.city = item;

					$scope.trainerCityChange(tmp, i);
				})
				angular.forEach(dealer_arr,function(item, i) {
					$scope.formData.filter_dealers[item] = true;
					// var tmp = {};
					// tmp.city = item;

					// $scope.trainerCityChange(tmp, i);
				})
				console.log($scope.formData.filter_regions)
			}else{
				$rootScope.modalShown =true;
				$rootScope.message=$scope.pageData.errorMsg[0];
			}
		}
		$scope.addFun = function(){
			$scope.formData = {}
			$scope.search_box = false;
			$scope.formLoad = true;
			$scope.listFound = false;
			$scope.filterAvailable = false;
			$scope.dealerAvailable = false;
			$scope.participantAvailable = false;
			$scope.selectedtrainercity=[];
			$scope.filter_regions = [];
			$scope.selectedCityList = [];
			$scope.selectedCityDealer = [];
			$scope.filter_dealers = [];
			// $scope.trainerParticipantList = [];
			$scope.formData.participant = [];
			$("input#myCheck").attr("disabled", true);
			$(".clsbuttoncont .clsbutton").addClass("disable_btn");
			$scope.formAction = 'Add';
			$scope.formData.funName = "addtraining";
			$scope.formtitle = "Add Training";
			$scope.tstart_time = [{"val":10, "time":"10:00"},{"val":11, "time":"11:00"},{"val":12, "time":"12:00"},{"val":13, "time":"13:00"},{"val":14, "time":"14:00"},{"val":15, "time":"15:00"}];
			// $scope.questionList();
			$scope.getTrainingLevel();
			$scope.getTrainerList();
			$scope.formData.assessment_type="0";
			$(".datepick").datepicker({dateFormat:'yy-mm-dd',minDate:'2018-06-10' ,maxDate:'2018-07-10'});
		}

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

		// Trainer List
		$scope.getTrainerList = function(){
			var fobj = {};
			fobj.lang = $rootScope.selLanguage;
			fobj.funName="trainerlist";
			AppData.sendData($scope.pageurl,fobj,function(res){
				if(res.cont){
					if(res.item != undefined){
						$scope.topicVisible = true;
						
						if(res.item.length>0)
						{
							$scope.trainerlists = res.item;
						}
					}else{
					}
				}
			});
		}

		$scope.selectedTrainingLevel = function(){
			$scope.selectedLevel = $scope.formData.lvl_id;
		}
		
		$scope.selectedstartTime = function(){
			console.log($scope.formData.starttime);
			$scope.tend_time = [];
			// $('.endtime').prop('selectedIndex',0);
			for(var i=$scope.formData.starttime; i<=$scope.trend_time; i++){
				var sval = Number(i)+1;
				var obj = {};
				obj.val = sval;
				obj.time = sval+":00";
				$scope.tend_time.push(obj);
			}
			$(".endtime").prop( "disabled", false );
		}
		$scope.loadtrainerregion = function(){
			console.log($scope.formData.trainer_id)
			$scope.selectedtrainercity = [];
			$scope.selectedCityDealer = [];
			$scope.dealerParticipant = [];
			$scope.selectedCityList = [];
			$scope.finalDealerParticipant = [];
			var fobj = {};
			fobj.lang = $rootScope.selLanguage;
			fobj.funName="city";
			fobj.trainer_id=$scope.formData.trainer_id;
			AppData.sendData($scope.commonurl,fobj,function(res){
				if(res.cont){
					$scope.filterAvailable = true;
					if(res.item != undefined){
						$scope.topicVisible = true;
						
						if(res.item.length>0)
						{
							$scope.selectedtrainercity = res.item;
						}
					}else{
					}
				}
			});
		}

		$scope.trainerCityChange = function(item, i){
			console.log(item);
			var fobj = {};
			fobj.lang = $rootScope.selLanguage;
			fobj.funName="dealer";
			var index = $scope.selectedCityList.indexOf(item.city);
			console.log($scope.selectedCityList);

			console.log(index);
			if(index != -1){
				$scope.selectedCityList.splice(index,1);
			}else{
				$scope.selectedCityList.push(item.city);
			}
			console.log($scope.selectedCityList);
			var citylist = $scope.selectedCityList.toString();
			console.log(citylist)
			fobj.city=citylist;
			AppData.sendData($scope.commonurl,fobj,function(res){
				console.log(res);
				console.log(res.item);
				if(res.cont){
					if(res.item != undefined){
						$scope.dealerAvailable = true;
						
						if(res.item.length>0)
						{
							$scope.selectedCityDealer = res.item;
						}
					}else{
					}
				}
			});
		}
		$scope.trainerDealerChange = function(item, i){
			console.log(item,i);
			console.log($scope.formData.filter_dealers);
			if($scope.selectedLevel==0){
				alert($scope.pageData.errorMsg[0]);
				$scope.formData.filter_dealers[item.dealer_code] = false;
				return;
			}
			console.log(item.dealer_name);
			var fobj = {};
			fobj.lang = $rootScope.selLanguage;
			fobj.funName="participantlist";
			fobj.lvl_id = $scope.selectedLevel;
			var index = $scope.filter_dealers.indexOf(item.dealer_code);
			// console.log($scope.formData.filter_dealer);

			// console.log(index);
			
			
			angular.forEach($scope.dealerParticipant,function(sdlpart,i) {
				console.log(sdlpart);
				console.log(i)
				// if(sdlpart.dealer_code)
				var sindex = item.dealer_code.indexOf(sdlpart.dealer_code);
				console.log(sindex+"--- sindex")
				if(sindex==0){
					$scope.selectedDealerParticipants.splice(i,1);
					$scope.dealerParticipant.splice(i,1);
				}
				// $scope.formData.filter_dealer.push(i);
			})
			if(index != -1){
				$scope.filter_dealers.splice(index,1);
			}else{
				$scope.filter_dealers.push(item.dealer_code);
				// fobj.dealer=dealerlist;
				fobj.dealer=item.dealer_code;
					AppData.sendData($scope.pageurl,fobj,function(res){
						console.log(res);
						console.log(res.item);
						if(res.cont){
							if(res.item != undefined){
								$scope.participantAvailable = true;
								if(res.item.length>0)
								{
									$scope.dealerParticipant.push(res.item[0]);
									// $scope.dealerParticipant = res.item;
									console.log("dealerParticipant")
									console.log($scope.dealerParticipant);
								}
							}else{
							}
						}
					});
				
			}
			// console.log($scope.filter_dealer);
			
			var dealerlist = $scope.filter_dealers.toString();
			console.log(dealerlist);
			if(dealerlist==""){
				console.log("i m here")
				$scope.participantAvailable = false;
				$scope.selectedDealerParticipants = [];
				$scope.dealerParticipant = [];
			}
			// console.log($scope.trainerParticipantList);
			console.log($scope.selectedDealerParticipants);
			// var sindex = $scope.selectedDealerParticipants.indexOf(item.dealer_code)

			
			
		}
		$scope.readytoAddParticipant = function(dcode, dname, puserid, pname, i){
			// [{"day":1, "dur":"1 day"}, {"day":2, "dur":"2 day"}, {"day":3, "dur":"3 day"}, {"day":4, "dur":"4 day"}, {"day":5, "dur":"5 day"}];
			// console.log($scope.selectedDealerParticipant);
			// var slist = $scope.selectedDealerParticipant;
			// if(slist.length>0){
			// 	for(var i=0; i<slist.length; i++){
			// 		console.log(slist[i].dealer_code);
			// 		for(var j=0; j<slist[i].user.length; j++){
			// 			// console.log(slist[i].user[j].userid.index(puserid))
			// 			console.log(slist[i].user[j].userid.filter(function (person){
			// 				return person.userid==puserid;
			// 			}))
			// 			// if(slist[i].user[j].userid.indexOf)
			// 			var pid = slist[i].user[j].userid.indexOf(puserid);
			// 			if(pid != -1){
			// 				// $scope.selectedDealerParticipant.user[j].userid.splice(pid,1);
			// 				// delete $scope.selectedDealerParticipant.user[j].
			// 			}else{
			// 				// $scope.filter_dealer.push(item.dealer_code);
			// 				$scope.selectedDealerParticipant.user[j].push({"userid":puserid, "name":pname});
			// 			}
			// 		}
			// 	}
			// }else{
			// 	$scope.selectedDealerParticipant.push({"dealer_code":dcode, "dealer_name":dname, "user":[{"userid":puserid, "name":pname}]});
			// }
			// console.log($scope.selectedDealerParticipant);
		}

		$scope.readytoAddParticipants = function(dealer_data, user_data, i){
			console.log($scope.selectedDealerParticipants);
			var dindex = -1;
			var pindex = -1;
			var indx = -1;
			var dindx = -1;
			if($scope.selectedDealerParticipants.length>0){
				for(var i=0; i<$scope.selectedDealerParticipants.length; i++){
					dindex = $scope.selectedDealerParticipants[i].dealer_code.indexOf(dealer_data.dealer_code);
					if(dindex==0){
						dindx = i;
					}
					if($scope.selectedDealerParticipants[i].dealer_code == dealer_data.dealer_code){
						if($scope.selectedDealerParticipants[i].user.length>0){
							indx=-1;
							for(var j=0; j<$scope.selectedDealerParticipants[i].user.length; j++){
								pindex = $scope.selectedDealerParticipants[i].user[j].userid.indexOf(user_data.userid);
								console.log("pindex:"+pindex)
								if(pindex==0){
									indx = j;
								}
							}
							console.log("pindex::::"+pindex+", indx:::"+indx)
							if(indx != -1){
								$scope.selectedDealerParticipants[i].user.splice(indx,1);
							}else{
								var udata = {};
								udata = user_data;
								$scope.selectedDealerParticipants[i].user.push(user_data);
							}
						}else{
							$scope.selectedDealerParticipants[i].user[j].push(user_data);
						}
					}
				}
				console.log("dindex:::"+dindex+", dindx:::"+dindx)
				if(dindex != -1){
					if(dindx != -1){
						for(var k=0; k<$scope.selectedDealerParticipants.length; k++){
							console.log("len::"+$scope.selectedDealerParticipants[k].user.length)
							if($scope.selectedDealerParticipants[k].user.length==0){
								$scope.selectedDealerParticipants.splice(dindx,1);
							}
						}
					}
				}else{
					var ddata = {};
					ddata.dealer_code = dealer_data.dealer_code;
					ddata.dealer_name = dealer_data.dealer_name;
					ddata.user = [];
					var udata = {};
					udata = user_data;
					ddata.user[0] = udata;
					$scope.selectedDealerParticipants.push(ddata);
				}
			}else{
				var ddata = {};
				ddata.dealer_code = dealer_data.dealer_code;
				ddata.dealer_name = dealer_data.dealer_name;
				ddata.user = [];
				var udata = {};
				udata = user_data;
				ddata.user[0] = udata;
				$scope.selectedDealerParticipants.push(ddata);
			}
			console.log($scope.selectedDealerParticipants);
			console.log($scope.finalDealerParticipant)
		}

		$scope.addParticipant = function(){
			console.log("add participant click")
			// console.log($scope.formData.trainerParticipantList);
			$scope.finalDealerParticipant = $scope.selectedDealerParticipants;
			// $scope.formData.participant = $scope.selectedDealerParticipants;
			// $("#spart")
			console.log($scope.formData);
		}
		
		$scope.closeForm = function(){
			$scope.formLoad = false;
			$scope.formData = {};
			$scope.selectedIndex = null;
			$scope.search_box = true;
			$scope.listFound = true;
			$(".clsbuttoncont .clsbutton").removeClass("disable_btn");
		}
		$scope.assessmentSubmit = function(){
			if($scope.formAction=="Add"){
				$scope.formData.funName = "addassessment";
			}else{
				$scope.formData.funName = "editassessment";
				$scope.formData.assessmentid = $scope.formData.assessmentid;
			}
			var arr=[];
			$scope.supplier_json=[];
			$scope.question_json=[];
			for(var k in $scope.formData.supplierList){
				
				if($scope.formData.supplierList[k]){
					arr.push(k)
					
					$scope.supplier_json.push({supplierid:k,total_question:$scope.formData.total_question[k],random_question:$scope.formData.random_question[k]});
				}else{
					$scope.supplier_json=[];
				}
				
				
			}
			for(var h in $scope.formData.questionList){
				
				if($scope.formData.questionList[h]){
					$scope.question_json.push({supplierid:$scope.formData.questionsupplierList[h],qid:h,is_mandatory:$scope.formData.is_mandatory[h]});
				}
			}
			$scope.formData.supplier_json =JSON.stringify($scope.supplier_json);
			$scope.formData.question_json = JSON.stringify($scope.question_json);
		   
			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.trainingSubmit = function(){
			sdate = new Date($scope.formData.start_date);
			edate = new Date($scope.formData.enddate);
			var diff_date =  edate - sdate;
 
			var years = Math.floor(diff_date/31536000000);
			var months = Math.floor((diff_date % 31536000000)/2628000000);
			var days = Math.floor(((diff_date % 31536000000) % 2628000000)/86400000);
			console.log("datediffffff"+days);
			if(days<0){
				$rootScope.modalShown =true;
				$rootScope.message=("Please select the date is greater than start date..");
				return;
			}
			
			console.log($scope.formData.courseduration+"haiiiiiiii");
			
			if($scope.formData.courseduration!=(days+1))
			{
				$rootScope.modalShown =true;
				$rootScope.message=("Please select the correct date...");
				return;
			}
			if($scope.formAction=="Add"){
				$scope.formData.funName = "addtraining";
			}else{
				$scope.formData.funName = "edittraining";
				$scope.formData.schedule_id = $scope.formData.schedule_id;
			}
			$scope.formData.lang = $rootScope.selLanguage;
			$scope.formData.participant = [];
			var cnt=0;
			for(var i=0; i<$scope.selectedDealerParticipants.length;i++){
				for(var j=0; j<$scope.selectedDealerParticipants[i].user.length;j++){
					$scope.formData.participant[cnt]=$scope.selectedDealerParticipants[i].user[j].userid;
					cnt++;
				}
			}
			$scope.formData.filter_dealer = [];
			angular.forEach($scope.formData.filter_dealers,function(item,i) {
				console.log(item);
				console.log(i)
				$scope.formData.filter_dealer.push(i);
			})
			$scope.formData.filter_region = [];
			angular.forEach($scope.formData.filter_regions,function(item,j) {
				console.log(item);
				console.log(j)
				$scope.formData.filter_region.push(j);
			})
			$scope.formData.filter_dealer = JSON.stringify($scope.formData.filter_dealer);
			$scope.formData.filter_region = JSON.stringify($scope.formData.filter_region);
			$scope.formData.participant = JSON.stringify($scope.formData.participant);
			console.log($scope.formData);
			$rootScope.modalShown =true;
			$rootScope.message=("Training ready to submit");
			// AppData.sendData($scope.pageurl,$scope.formData,function(res){
			// 	if(res.cont){
			// 		$scope.trainingList();
			// 	}else{
			// 		$rootScope.modalShown =true;
			// 		$rootScope.message=(res.mess);
			// 	}
			// })
			console.log($scope.formData);
		}
		
	
 });
	
}); 
