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];
		$scope.itemdetail = [];
		$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 = [];
		$scope.mergecity = [];
		// 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.selectedcity_new=[];
		// $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;
		$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.tstart_time = $scope.pageData.training_time;
		$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_duration = [
			{"value":"10","title":"10"},
			{"value":"15","title":"15"},
			{"value":"20","title":"20"},
			{"value":"25","title":"25"},
			{"value":"30","title":"30"},
		]
		$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;
			$scope.searchvalue = ($scope.formData.mymodel!='')?$scope.formData.mymodel : "";
		    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.searchvalue;
				$rootScope.pageLoadComp = true;
				AppData.sendData($scope.pageurl,fobj,function(res){
					$rootScope.pageLoadComp = true;
					// 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;
						}
					}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.onSearchChange = 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.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 = "deletetraining";
			}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.editrow = function(obj, i){
			var fobj = {};
			fobj.lang = $rootScope.selLanguage;
			fobj.funName="trainingdetails";
			fobj.schedule_id = obj.schedule_id
			AppData.sendData($scope.pageurl,fobj,function(res){
				$scope.formData = {};
				if(res.cont){
					if(res.item != undefined){
						$scope.formData = res.item[0];
						$scope.selectedIndex = i;
						$scope.search_box = false;
						$scope.formLoad = true;
						$scope.listFound = false;
						$scope.formData.ques_count = Number($scope.formData.ques_count);
						$scope.formData.startdate = $scope.formData.start_date;
						$scope.formData.enddate = $scope.formData.end_date;
						// $scope.formData.endtime =
						$scope.editFun();
						// $scope.formData.randomcond = 
					}else{
					}
				}
			});
		}
		$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);
				var participant_arr = ($scope.formData.participant_id);
				$scope.formData.filter_regions = {};
				$scope.formData.filter_dealers = {};
				$scope.formData.trainerParticipantList = {};
				// $scope.selectedCityList=region_arr;
				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, j) {
					$scope.formData.filter_dealers[item] = true;
					$scope.filter_dealers.push(item);
					var dobj = {};
					dobj.dealer_code = item;
					$scope.trainerDealerChange(item, j);
				})
				angular.forEach(participant_arr,function(item, k) {
					$scope.formData.trainerParticipantList[item] = true;
				})
			}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.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){
				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.formData.trainer_id = '';
			$scope.selectedtrainercity = [];
			$scope.selectedCityDealer = [];
			$scope.dealerParticipant = [];
			$scope.selectedCityList = [];
			$scope.finalDealerParticipant = [];
			$scope.filterAvailable = false;
			$scope.formData.filter_regions = {};
			$scope.formData.filter_dealers = {};
			$scope.formData.trainerParticipantList = {};
		}
		
		$scope.selectedstartTime = function(){
			$scope.tend_time = [];
			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(){
			$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,event){
			if ($scope.selectedcity_new.indexOf(item) === -1) {
				$scope.selectedcity_new.push(item);
			}
			else {
				$scope.selectedcity_new.splice($scope.selectedcity_new.indexOf(item), 1);
			}
			var fobj = {};
			fobj.lang = $rootScope.selLanguage;
			// fobj.funName="dealer";
			fobj.funName="dealer_v2";
			var index = $scope.selectedCityList.indexOf(item.city);
			angular.forEach($scope.selectedCityDealer,function(scdealer,i) {
				var sindex = item.city.indexOf(scdealer.city);
				if(sindex==0){
					$scope.selectedCityDealer.splice(i,1);
					if($scope.formData.filter_dealers!=undefined){
						$scope.formData.filter_dealers[scdealer.dealer_code] = false;
					}
					var dealerindex = $scope.filter_dealers.indexOf(scdealer.dealer_code);
					if(dealerindex>=0){
						$scope.filter_dealers.splice(dealerindex,1);
						// $scope.selectedDealerParticipants.splice(dealerindex,1);
						angular.forEach($scope.dealerParticipant,function(sdlpart,i) {
							var sindex = scdealer.dealer_code.indexOf(sdlpart.dealer_code);
							if(sindex==0){
								angular.forEach(sdlpart.user, function(usr, j){
									$scope.formData.trainerParticipantList[usr.userid] = false;
								})
								$scope.selectedDealerParticipants.splice(i,1);
								$scope.dealerParticipant.splice(i,1);
							}
						});
						// $scope.dealerParticipant.splice(dealerindex,1);
					}
					
				}
			})
			if(index != -1){
				$scope.selectedCityList.splice(index,1);
				// $scope.selectedcity_new.splice(index,1)
			}else{
				$scope.selectedCityList.push(item.city);
				fobj.city=item.city;
				AppData.sendData($scope.commonurl,fobj,function(res){
					if(res.cont){
						console.log(res)
						if(res.item != undefined){
							$scope.dealerAvailable = true;
							for(var j=0;j<res.item.length;j++){
								res.item[j].city = item.city;
								$scope.selectedCityDealer.push(res.item[j]);
							}
							// if(res.item.length>0)
							// {
							// 	for(var j=0;j<res.item.length;j++){
							// 		res.item[j].city = item.city;
							// 		$scope.selectedCityDealer.push(res.item[j]);
							// 		// $scope.selectedcity_new.push(res.item[j])
							// 	}
							// 	console.log($scope.selectedCityDealer)
							// }
						}else{
						}
					}
				});
			}
			var citylist = $scope.selectedCityList.toString();
			if(citylist==""){
				$scope.dealerAvailable = false;
				$scope.selectedCityDealer = [];
				$scope.participantAvailable = false;
				$scope.selectedDealerParticipants = [];
				$scope.dealerParticipant = [];
				$scope.formData.filter_dealers = [];
				$scope.filter_dealers = [];
			}

			// setTimeout(()=>{
			// 	console.log($scope.selectedCityDealer);
			// 	console.log($scope.selectedcity_new);
			// 	var a=[];
			// 	for(let i=0;i<$scope.selectedcity_new.length;i++){
			// 		for(let j=0;j<$scope.selectedCityDealer.length;j++){
			// 			if($scope.selectedcity_new[i].city == $scope.selectedCityDealer[j].city){
			// 				$scope.mergecity.push(Object.assign($scope.selectedcity_new[i],[$scope.selectedCityDealer[j]]));
			// 			}else{
			// 				console.log('else')
			// 			}
			// 		}
			// 	}
			// 	console.log($scope.mergecity)
			// },1000)
			
			// $scope.mergecity = $scope.selectedCityDealer.concat($scope.selectedcity_new);
			// console.log($scope.selectedcity_new)
			
		}
		$scope.trainerDealerChange = function(item, i){
			
			if($scope.selectedLevel==0){
				alert($scope.pageData.errorMsg[0]);
				$scope.formData.filter_dealers[item.dealer_code] = false;
				return;
			}
			var fobj = {};
			fobj.lang = $rootScope.selLanguage;
			fobj.funName="participantlist";
			fobj.lvl_id = $scope.selectedLevel;
			if($scope.formAction == 'Edit'){
				fobj.schedule_id = $scope.formData.schedule_id;
				$scope.selectedDealerParticipants = [];
			}
			angular.forEach($scope.dealerParticipant,function(sdlpart,i) {
				var sindex = item.dealer_code.indexOf(sdlpart.dealer_code);
				if(sindex==0){
					angular.forEach(sdlpart.user, function(usr, j){
						$scope.formData.trainerParticipantList[usr.userid] = false;
					})
					$scope.selectedDealerParticipants.splice(i,1);
					$scope.dealerParticipant.splice(i,1);
				}
			});
			// $scope.formData.trainerParticipantList[item] = true;
			var index = $scope.filter_dealers.indexOf(item.dealer_code);
			if(index >= 0){
				$scope.filter_dealers.splice(index,1);
			}else{
				$scope.filter_dealers.push(item.dealer_code);
				fobj.dealer=item.dealer_code;
				AppData.sendData($scope.pageurl,fobj,function(res){
					if(res.cont){
						if(res.item != undefined){
							$scope.participantAvailable = true;
							$scope.participantAvailable1 = false;
							if(res.item.length>0)
							{
								for(var j=0;j<res.item.length;j++){
									$scope.dealerParticipant.push(res.item[j]);
									if($scope.formAction=="Edit"){
										// for(var k=0; k< $scope.dealerParticipant.length; k++){
										// }
										if(res.item[j].user.length>0){
											angular.forEach(res.item[j].user, function(userdetail,k){
												var indexpart = $scope.formData.participant_id.indexOf(userdetail.userid);
												if(indexpart>=0){
													$scope.readytoAddParticipants(res.item[j], userdetail,indexpart);
												}
											})
										}
									}
								}
							}
						}else{
							$scope.participantAvailable1 = true;
						}
					}
					console.log($scope.dealerParticipant)
				});	
			}
			var dealerlist = $scope.filter_dealers.toString();
			if(dealerlist==""){
				$scope.participantAvailable = false;
				if($scope.formAction != 'Edit'){
					$scope.selectedDealerParticipants = [];
				}
				$scope.dealerParticipant = [];
			}
		}
		$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"}];
			// var slist = $scope.selectedDealerParticipant;
			// if(slist.length>0){
			// 	for(var i=0; i<slist.length; i++){
			// 		for(var j=0; j<slist[i].user.length; j++){
			// 				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}]});
			// }
		}

		$scope.readytoAddParticipants = function(dealer_data, user_data, i){
			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++){
					
					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);
								if(pindex>=0){
									indx = j;
								}
							}
							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[0].push(user_data);
						}
					}
					
					dindex = $scope.selectedDealerParticipants[i].dealer_code.indexOf(dealer_data.dealer_code);
					if(dindex>=0){
						dindx = i;
					}
				}
				// if(dindex != -1){
					if(dindx != -1){
						for(var k=0; k<$scope.selectedDealerParticipants.length; k++){
							if($scope.selectedDealerParticipants[k].user.length==0){
								$scope.selectedDealerParticipants.splice(dindx,1);
							}
						}
					}else{
				// }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);
			}
		}

		$scope.addParticipant = function(){
			$scope.finalDealerParticipant = $scope.selectedDealerParticipants;
		}
		
		$scope.closeForm = function(){
			$scope.formLoad = false;
			$scope.formData = {};
			$scope.selectedIndex = null;
			$scope.search_box = true;
			$scope.listFound = true;
			$(".clsbuttoncont .clsbutton").removeClass("disable_btn");
			$scope.trainingList();
		}
       
		$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);
			if(days<0){
				$rootScope.modalShown =true;
				$rootScope.message=("Please select the date is greater than start date..");
				return;
			}
			// 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) {
				$scope.formData.filter_dealer.push(i);
			})
			$scope.formData.filter_region = [];
			angular.forEach($scope.formData.filter_regions,function(item,j) {
				$scope.formData.filter_region.push(j);
			})
			if($scope.formData.participant.length==0){
				$rootScope.modalShown =true;
				$rootScope.message=("Please select any participant to schedule the training");
				return;
			}
			$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);
			$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);
				}
			})
		}
		//==
	
 });
	
}); 
