define(['app'], function (app) {
	
    app.controller('DealerCtrl', function ($scope,$rootScope,AppData,$location) {
		//Every module Set Same Variable//
	
		$rootScope.menucont = true;
		$rootScope.pageLoadComp = false;
		$scope.pageData = dealerData[$rootScope.selLanguage];
		$scope.formLoad = false;
		$scope.search_box = true;
		$scope.filerequired = true;
		$scope.url = "../senddata/log/";
		$scope.pageurl = "../senddata/dealer/";
		$scope.commonurl = "../senddata/common/";
		$scope.formAction = "Add";
		$rootScope.sideselect = $scope.pageData.sidemenu[0];
		$scope.submenu = [];
		$scope.selectedIndex = null;
		$rootScope.cofirmWindow = false;
		$scope.tblheading = $rootScope.sideselect.headingText;
		$rootScope.imageFormat = ["image/jpeg","image/png"];
		//add form
		$scope.dealercode_error = ''                                 
		$scope.dealername_error = ''                                 
		$scope.mobile_error = ''                                 
		$scope.zone_error = ''                                  
		$scope.state_error = ''                                 
		$scope.city_error = ''                                 
		$scope.zmname_error = ''                                 
		$scope.rmname_error = ''                                 
		$scope.amname_error = ''                                 
		$scope.zmemail_error = ''                                 
		$scope.rmemail_error = ''                                 
		$scope.amemail_error = ''                                 
		$scope.zmmobile_error = ''                                 
		$scope.rmmobile_error = ''                                 
		$scope.ammobile_error = ''

		//Check Session//
		$scope.formData = {}
		$scope.formData.ismenuload = "Yes";
		if($rootScope.items == undefined || $rootScope.items == null){
			$scope.formData.ismenuload = "No";
		}
		$scope.viewby = '10';
		$scope.itemsPerPage = $scope.viewby;
		$scope.maxSize = 5; //Number of pager buttons to show
		$scope.currentPage=1;
		
		$scope.formData.funName = "checksession";
		$scope.formData.lang = $rootScope.selLanguage;
		$scope.formData.start = 0;
		$scope.formData.limit = 2;
		$rootScope.log = true;
		
		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.issa = res.item.role
				if($scope.issa != 1){
					for(var id = 0; id < $scope.pageData.sidemenu.length-3;id++){
						$scope.submenu.push($scope.pageData.sidemenu[id]);
					}
				}else{
					$scope.submenu = $scope.pageData.sidemenu;
				}
				$scope.dealerList = function(){
					$scope.searchvalue = ($scope.formData.mymodel!='')?$scope.formData.mymodel : "";
					var fobj = {};
					fobj.funName = "dealerlist";
					fobj.lang = $rootScope.selLanguage;
					fobj.pn = $scope.currentPage;
					fobj.nr = $scope.itemsPerPage;
				    fobj.key = $scope.searchvalue
					
					AppData.sendData($scope.pageurl,fobj,function(res){
						$rootScope.pageLoadComp = true;
						if(res.cont){
							$scope.tblheading = $scope.pageData.sidemenu[0].headingText;
							$scope.itemdealerdetail = res.item.users;
							$scope.totalItems = res.item.total_records;
							$scope.listFound = true;
							$scope.search_box = true;
							// $scope.formData = {}
						}else{
							$scope.listFound = false;
							$scope.errorMsg = res.mess;
						}
					})
				}
				
				$scope.dealerList();
				
				$scope.setPage = function (pageNo) {
					$scope.currentPage = pageNo;
				  };
				
				  $scope.pageChanged = function() {
					console.log('Page changed to: ' + $scope.currentPage);
					$scope.dealerList();
				  };
				
				$scope.setItemsPerPage = function(num) {
				  $scope.itemsPerPage = num;
				  $scope.currentPage = 1; //reset to first page
				  $scope.dealerList();
				}
				$scope.filterData = function() {
					console.log($scope.searchtext)
					$scope.dealerList();
				}
			}else{
				$location.path("/login");
			}
		})
		
		
		$scope.onSearchChange = function(){
		  console.log($scope.formData.mymodel)
		  $scope.dealerList();
		}
		$scope.zoneList = function(){
			var zobj = {};
			zobj.funName = "zone";
			zobj.lang = $rootScope.selLanguage;
			AppData.sendData($scope.commonurl,zobj,function(res){
				$rootScope.pageLoadComp = true;
				if(res.cont){
					$scope.zonedata = res.item;
				}else{
					$scope.listFound = false;
					$scope.errorMsg = res.mess;
				}
			})
		}
		$scope.stateList = function(){
			var sobj = {};
			sobj.funName = "state";
			sobj.lang = $rootScope.selLanguage;
			sobj.zone = $scope.formData.zone;
			AppData.sendData($scope.commonurl,sobj,function(res){
				$rootScope.pageLoadComp = true;
				if(res.cont){
					$scope.statedata = res.item;
				}else{
					$scope.listFound = false;
					$scope.errorMsg = res.mess;
				}
			})
		}
		$scope.changeZone = function(){
			$scope.statedata=[];
			$scope.citydata=[];
			$scope.formData.state='';
            $scope.formData.city='';
			$scope.stateList();
		}
		$scope.cityList = function(){
			var cobj = {};
			cobj.funName = "city";
			cobj.lang = $rootScope.selLanguage;
			cobj.zone = $scope.formData.zone;
			cobj.state = $scope.formData.state;
			AppData.sendData($scope.commonurl,cobj,function(res){
				$rootScope.pageLoadComp = true;
				if(res.cont){
					$scope.citydata = res.item;
				}else{
					$scope.listFound = false;
					$scope.errorMsg = res.mess;
				}
			})
		}
		$scope.changeState = function(){
			$scope.citydata=[];
			$scope.cityList();
			$scope.formData.city='';
		}
		$scope.addFun = function(){
			console.log($scope.formLoad,$scope.listFound)
			$scope.formLoad = true;
			$scope.listFound = false;
			$scope.formAction = "Add";
			$scope.formTitle = "ADD DEALER";
			$scope.formData.funName = "adddealer";
			$scope.selectedIndex = null;
			$scope.zoneList();
		}
		
		$scope.closeForm = function(){
			$scope.formLoad = false;
			$scope.listFound = true;
			$scope.formData = {};
			$scope.selectedIndex = null
			$scope.clearErrorMsg();
			$scope.dealerList();
		}
		
		$scope.editFun = function(obj,i){
			$scope.formData = obj;
			$scope.formTitle = "EDIT DEALER";
			console.log($scope.formData)
			$scope.selectedIndex = i;
			$scope.listFound = false;
			$scope.formLoad = true;
			$scope.formAction = "Edit";
			$scope.formData.funName = "editdealer";
			$scope.zoneList();
			$scope.stateList();
			$scope.cityList();
			$scope.formData.zone = obj.zone;
			$scope.formData.state = obj.state;
			$scope.formData.city = obj.city;
		}
		
		$scope.deleteFun = function(){
			console.log($scope.selectedIndex)
			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 = "deletedealer";
				AppData.sendData($scope.pageurl,$scope.formData,function(res){
					if(res.cont){
						var index = $scope.itemdealerdetail.indexOf($scope.formData);
						if(index != -1){
							$scope.itemdealerdetail.splice(index,1);
						}
						$rootScope.modalShown =true;
						$rootScope.cofirmWindow = false;
						$rootScope.message=(res.mess);
						$scope.dealerList();
						
					}else{
						$rootScope.modalShown =true;
						$rootScope.message=(res.mess);
					}
				})
		})
		
		$scope.deleteRow = function(obj, i){
			console.log(obj)
			$scope.formData = obj;
			$scope.selectedIndex = i;
			$scope.deleteFun();
			
		}
		
		$scope.fieldValidation = function(){
			$scope.dealercode_error = ($scope.formData.dealer_code == undefined || $scope.formData.dealer_code == '') ?  $scope.pageData.errorMsg[2] : '';
			$scope.dealername_error = ($scope.formData.dealer_name == undefined || $scope.formData.dealer_name == '') ?  $scope.pageData.errorMsg[3] : '';
			$scope.mobile_error = ($scope.formData.phone == undefined || $scope.formData.phone == '') ?  $scope.pageData.errorMsg[4] : '';
			$scope.zone_error = ($scope.formData.zone == undefined || $scope.formData.zone == '') ?  $scope.pageData.errorMsg[5] : '';
			$scope.state_error = ($scope.formData.state == undefined || $scope.formData.state == '') ?  $scope.pageData.errorMsg[6] : '';
			$scope.city_error = ($scope.formData.city == undefined || $scope.formData.city == '') ?  $scope.pageData.errorMsg[7] : '';
			$scope.zmname_error = ($scope.formData.zm_name == undefined || $scope.formData.zm_name == '') ?  $scope.pageData.errorMsg[8] : '';
			$scope.rmname_error = ($scope.formData.rm_name == undefined || $scope.formData.rm_name == '') ?  $scope.pageData.errorMsg[9] : '';
			$scope.amname_error = ($scope.formData.am_name == undefined || $scope.formData.am_name == '') ?  $scope.pageData.errorMsg[10] : '';
			$scope.zmemail_error = ($scope.formData.zm_email == undefined || $scope.formData.zm_email == '') ?  $scope.pageData.errorMsg[11] : '';
			$scope.rmemail_error = ($scope.formData.rm_email == undefined || $scope.formData.rm_email == '') ?  $scope.pageData.errorMsg[12] : '';
			$scope.amemail_error = ($scope.formData.am_email == undefined || $scope.formData.am_email == '') ?  $scope.pageData.errorMsg[13] : '';
			$scope.zmmobile_error = ($scope.formData.zm_mobile == undefined || $scope.formData.zm_mobile == '') ?  $scope.pageData.errorMsg[14] : '';
			$scope.rmmobile_error = ($scope.formData.rm_mobile == undefined || $scope.formData.rm_mobile == '') ?  $scope.pageData.errorMsg[15] : '';
			$scope.ammobile_error = ($scope.formData.am_mobile == undefined || $scope.formData.am_mobile == '') ?  $scope.pageData.errorMsg[16] : '';
			return ($scope.dealercode_error == '' && $scope.dealername_error == '' && $scope.mobile_error == '' && $scope.zone_error == ''  && $scope.state_error == '' && $scope.city_error == '' && $scope.zmname_error == '' && $scope.rmname_error == '' && $scope.amname_error == '' && $scope.zmemail_error == '' && $scope.rmemail_error == '' && $scope.amemail_error == '' && $scope.zmmobile_error == '' && $scope.rmmobile_error == '' && $scope.ammobile_error == '') ? true : false;
		}
		$scope.clearErrorMsg = function(){
			$scope.dealercode_error = ''                                 
			$scope.dealername_error = ''                                 
			$scope.mobile_error = ''                                 
			$scope.zone_error = ''                                  
			$scope.state_error = ''                                 
			$scope.city_error = ''                                 
			$scope.zmname_error = ''                                 
			$scope.rmname_error = ''                                 
			$scope.amname_error = ''                                 
			$scope.zmemail_error = ''                                 
			$scope.rmemail_error = ''                                 
			$scope.amemail_error = ''                                 
			$scope.zmmobile_error = ''                                 
			$scope.rmmobile_error = ''                                 
			$scope.ammobile_error = ''
		}
		///////// User Submit //////////////
		$scope.userSubmit = function(){
			$scope.clearErrorMsg();
			console.log($scope.fieldValidation());
			if($scope.fieldValidation()){
				AppData.sendData($scope.pageurl,$scope.formData,function(res){
					if(res.cont){
						$rootScope.modalShown =true;
						$rootScope.message=(res.mess);
						$scope.dealerList();
						$scope.selectedIndex = null;
						$scope.formLoad = false;
						$scope.listFound = true;
					}else{
						$rootScope.modalShown =true;
						$rootScope.message=(res.mess);
					}
				})
			}
		}
		
		
      	
    });
}); 

