var vueObject = null;
var tableID = null;
var tableNo = null;
var firstFlag = true;


$(document).ready(function(){
	honorpos.setCookie('home_page_url',location.href);
	vueObject = new Vue({
		el : '.wrap_vue',
		data : {
			searchinfo : '',
			registGroupID :0,
			orderGroupID :0,
			orderLowAmount :0,
			getGroupID :0,
			shareRegistGroupID : 0,
			shareOrderGroupID : 0,
			QROrderMustLogin : 0,
			optionscount : 1,

			showUserCenterFlag : true,
			minAmount :0,
			orderStatus : 0,
			status : 1,
			guestOrder : 0,
			mainname : '',
			phoneinfo : '',
			orgPhone2 : '',
			remark : '',
			addressinfo : '',
			optionsshowflag : false,
			orderlistshowflag : false,
			categoryshowflag : false,
			categoryGroupList : new Array(),
			categoryList : new Array(),
			menuList : new Array(),
			currentGroupIndex : null,
			currentMenu : {
				L1 : ''
			},
			shoppingcartnum : 0,
			shoppingcartamount : 0,
			shoppingcartlist : new Array(),
			samelinelist : new Array(),
			nextlinelist : new Array(),
			mulitiple1list : new Array(),
			mulitiple1num : -1,
			mulitiple2list : new Array(),
			mulitiple2num : -1,
			mulitiple3list : new Array(),
			mulitiple3num : -1,
			mulitiple4list : new Array(),
			mulitiple4num : -1,
			mulitiple5list : new Array(),
			mulitiple5num : -1,
			mulitiple6list : new Array(),
			mulitiple6num : -1,
			mulitiple7list : new Array(),
			mulitiple7num : -1,
			mulitiple8list : new Array(),
			mulitiple8num : -1,
			optionsprice :0,

			bigPicShowFlag : false,
			tableNo : '',
			couponshowflag : false,
		    discountAmount : 0,
		    bamount : 0,
		    endtime : '',
		    couponName : ''
		},
		filters:{
			phoneformat : function(phoneNo){
		        if(phoneNo){
		             var info = "";
		            if(phoneNo.length<=3){
		                info = phoneNo;
		            }
		            if(phoneNo.length<=6 && phoneNo.length>3){
		                info = phoneNo.substr(0,3)+"-"+phoneNo.substr(3);
		            }

		            if(phoneNo.length>6){
		                info = phoneNo.substr(0,3)+"-"+phoneNo.substr(3,3)+"-"+phoneNo.substr(6);
		            }
		            if(phoneNo.length>10){
		                info = phoneNo.substr(0,3)+"-"+phoneNo.substr(3,3)+"-"+phoneNo.substr(6,4)+"/"+phoneNo.substr(10);
		            }

		            return info;
		        }else{
		            return "";
		        }
		       
		    },
			showprice : function(value){
				try{
					return "$"+(value/100).toFixed(2);
				}catch(e){}
				return "$0.00";
			},
			showlang : function(value){
				try{
					return value.L1;
				}catch(e){
					return "";
				}
			},
			showlangwprice : function(value){
				try{
					var info = value.L1;
					if(value.Price>0){
						info += " ($"+(value.Price/100).toFixed(2)+")";
					}
					return info;
				}catch(e){
					return "";
				}
			},
			showchoiceinfo : function(value){
				if(value == 0){
					return "Option";
				}
				if(value == 999){
					return "All";
				}
				if(value > 500){
					return "Option (Must choose "+(value-500)+")";
				}
				
				if(value > 1){
					return "Option (Choose up to "+value+")";
				}

				if(value <0){
					return "Option (Choose at least "+(0-value)+")";
				}
				
			},
			showdate : function(value){
				return new Date(value).format('yyyy-MM-dd')
			}
		},
		computed : {
			
		},
		methods : {
			showRemarkFun : function(){
				honorpos_ui.alert(this.remark)
			},
			showShareRegistInfoFun : function(){
				honorpos_ui.confirm('Invite friends to get coupons',function(){
					window.location.href="userCenter.html";
				});
			},
			showShareOrderInfoFun : function(){
				honorpos_ui.alert('Share coupon after successful order');
			},
			showRegistCouponInfoFun : function(){
				honorpos_ui.alert('Sign up to receive a coupon');
			},
			showOrderCouponInfoFun : function(){
				var info = "$"+(this.orderLowAmount/100).toFixed(2);
				honorpos_ui.alert('Order over '+info+' will receive a Coupon');
			},
			adddescfun : function(item,index){
				var that = this;

				honorpos_ui.prompt("Add Special Instructions(additional charges may apply and not all changes are possible)",item.adddesc,function(value){
					if(value.length>200){
						honorpos_ui.alert("Too Large")
						return;
					}
					var item = that.shoppingcartlist[index]; 

					honorpos.api('sys/googleTranslate?info='+encodeURIComponent(value),'',function(data){
						try{
							item.adddescCn = data[0][0][0];
						}catch(e){}
						item.adddesc = value;
						that.$forceUpdate();  
						honorpos.saveshoppingcart(that.shoppingcartlist);
					})
					item.adddesc = value;
					that.$forceUpdate();  
					honorpos.saveshoppingcart(that.shoppingcartlist);
					
				});
			},
			usercenterfun : function(){
				window.location.href = 'userCenter.html';
			},
			clickoptionsfun : function(menu,category,count){
				var that = this;
				if(count){
					this.optionscount = count;
				}else{
					this.optionscount = 1;
				}
				
				that.currentMenu = menu;
				that.mulitiple1list = new Array();
				that.mulitiple1num = 0;
				that.mulitiple2list = new Array();
				that.mulitiple2num = 0;
				that.mulitiple3list = new Array();
				that.mulitiple3num = 0;
				that.mulitiple4list = new Array();
				that.mulitiple4num = 0;
				that.mulitiple5list = new Array();
				that.mulitiple5num = 0;
				that.mulitiple6list = new Array();
				that.mulitiple6num = 0;
				that.mulitiple7list = new Array();
				that.mulitiple7num = 0;
				that.mulitiple8list = new Array();
				that.mulitiple8num = 0;

				that.samelinelist = new Array();
				that.nextlinelist = new Array();

				//that.optionsprice = optionsprice();
				if(category && category.FollowItemGroupID){
					honorpos.api('menu/follow','followGroupID='+category.FollowItemGroupID,function(data){
						if(data.returnCode == 0){
							var samelinelist = new Array();
							var nextlinelist = new Array();
							for(var i=0;i<data.list.length;i++){
								var one = data.list[i];
								one.index = i+1;

								if(one.FollowItemType == 6){
									continue;
								}
								if(one.FollowItemType == 2){
									one.num = 1;
								}
								if(one.FollowItemType != 0 ){
									one.Price = getSinglePrice(menu,one,i+1);
									if(one.Price>0){
										samelinelist.push(one);
									}
									
								}else{
									nextlinelist.push(one);
								}
							}
							if(samelinelist.length>0){
								samelinelist[0].num=1;
							}
							that.samelinelist = samelinelist;
							that.nextlinelist = nextlinelist;
						}
						that.optionsshowflag = true;
						that.optionsprice = optionsprice() * that.optionscount;
					})
				}
				if(menu.FollowGroupID){
					honorpos.api('menu/follow','followGroupID='+menu.FollowGroupID,function(data){
						if(data.returnCode == 0){							
							that.mulitiple1list = data.list;
							that.mulitiple1num = menu.MutliSelect;
							try{
								if(that.mulitiple1num == 1){
									that.mulitiple1list[0].num = 1;
								}
								if(that.mulitiple1num == 999){
									for(var i=0;i<that.mulitiple1list.length;i++){
										that.mulitiple1list[i].num = 1;
									}
									
								}
							}catch(e){}
							
						}
						that.optionsshowflag = true;
						that.optionsprice = optionsprice() * that.optionscount;
					})
				}
				if(menu.FollowGroupID2){
					honorpos.api('menu/follow','followGroupID='+menu.FollowGroupID2,function(data){
						if(data.returnCode == 0){							
							that.mulitiple2list = data.list;
							that.mulitiple2num = menu.MutliSelect2;
							try{
								if(that.mulitiple2num == 1){
									that.mulitiple2list[0].num = 1;
								}
								if(that.mulitiple2num == 999){
									for(var i=0;i<that.mulitiple2list.length;i++){
										that.mulitiple2list[i].num = 1;
									}
									
								}
							}catch(e){}
						}
						that.optionsshowflag = true;
						that.optionsprice = optionsprice() * that.optionscount;
					})
				}
				if(menu.FollowGroupID3){
					honorpos.api('menu/follow','followGroupID='+menu.FollowGroupID3,function(data){
						if(data.returnCode == 0){							
							that.mulitiple3list = data.list;
							that.mulitiple3num = menu.MutliSelect3;
							try{
								if(that.mulitiple3num == 1){
									that.mulitiple3list[0].num = 1;
								}
								if(that.mulitiple3num == 999){
									for(var i=0;i<that.mulitiple3list.length;i++){
										that.mulitiple3list[i].num = 1;
									}
									
								}
							}catch(e){}
						}
						that.optionsshowflag = true;
						that.optionsprice = optionsprice() * that.optionscount;
					})
				}
				if(menu.FollowGroupID4){
					honorpos.api('menu/follow','followGroupID='+menu.FollowGroupID4,function(data){
						if(data.returnCode == 0){							
							that.mulitiple4list = data.list;
							that.mulitiple4num = menu.MutliSelect4;
							try{
								if(that.mulitiple4num == 1){
									that.mulitiple4list[0].num = 1;
								}
								if(that.mulitiple4num == 999){
									for(var i=0;i<that.mulitiple4list.length;i++){
										that.mulitiple4list[i].num = 1;
									}
									
								}
							}catch(e){}
						}
						that.optionsshowflag = true;
						that.optionsprice = optionsprice() * that.optionscount;
					})
				}
				if(menu.FollowGroupID5){
					honorpos.api('menu/follow','followGroupID='+menu.FollowGroupID5,function(data){
						if(data.returnCode == 0){							
							that.mulitiple5list = data.list;
							that.mulitiple5num = menu.MutliSelect5;
							try{
								if(that.mulitiple5num == 1){
									that.mulitiple5list[0].num = 1;
								}
								if(that.mulitiple5num == 999){
									for(var i=0;i<that.mulitiple5list.length;i++){
										that.mulitiple5list[i].num = 1;
									}
									
								}
							}catch(e){}
						}
						that.optionsshowflag = true;
						that.optionsprice = optionsprice() * that.optionscount;
					})
				}
				if(menu.FollowGroupID6){
					honorpos.api('menu/follow','followGroupID='+menu.FollowGroupID6,function(data){
						if(data.returnCode == 0){							
							that.mulitiple6list = data.list;
							that.mulitiple6num = menu.MutliSelect6;
							try{
								if(that.mulitiple6num == 1){
									that.mulitiple6list[0].num = 1;
								}
								if(that.mulitiple6num == 999){
									for(var i=0;i<that.mulitiple6list.length;i++){
										that.mulitiple6list[i].num = 1;
									}
									
								}
							}catch(e){}
						}
						that.optionsshowflag = true;
						that.optionsprice = optionsprice() * that.optionscount;
					})
				}
				if(menu.FollowGroupID7){
					honorpos.api('menu/follow','followGroupID='+menu.FollowGroupID7,function(data){
						if(data.returnCode == 0){							
							that.mulitiple7list = data.list;
							that.mulitiple7num = menu.MutliSelect7;
							try{
								if(that.mulitiple7num == 1){
									that.mulitiple7list[0].num = 1;
								}
								if(that.mulitiple7num == 999){
									for(var i=0;i<that.mulitiple7list.length;i++){
										that.mulitiple7list[i].num = 1;
									}
									
								}
							}catch(e){}
						}
						that.optionsshowflag = true;
						that.optionsprice = optionsprice() * that.optionscount;
					})
				}
				if(menu.FollowGroupID8){
					honorpos.api('menu/follow','followGroupID='+menu.FollowGroupID8,function(data){
						if(data.returnCode == 0){							
							that.mulitiple8list = data.list;
							that.mulitiple8num = menu.MutliSelect8;
							try{
								if(that.mulitiple8num == 1){
									that.mulitiple8list[0].num = 1;
								}
								if(that.mulitiple8num == 999){
									for(var i=0;i<that.mulitiple8list.length;i++){
										that.mulitiple8list[i].num = 1;
									}
									
								}
							}catch(e){}
						}
						that.optionsshowflag = true;
						that.optionsprice = optionsprice() * that.optionscount;
					})
				}

			},
			clicksingleoptions : function(one,index,list){
				for(var i=0;i<list.length;i++){
					list[i].num = 0;
				}
				one.num = 1;

				this.optionsprice = optionsprice() * this.optionscount;
				this.$forceUpdate();  
			},
			clearoptions : function(list,selectmode){
				for(var i=0;i<list.length;i++){
					list[i].num=0;
				}
				this.optionsprice = optionsprice() * this.optionscount;
				this.$forceUpdate();  
			},
			lessnumfun : function(one){
				one.num -- ;
				this.optionsprice = optionsprice()* this.optionscount;
				this.$forceUpdate();  
			},
			clickoptions : function(one,selectmode,list){
				console.log(this.currentMenu);
				if(one.num == undefined){
					one.num = 0;
				}
				if(one.num>0 && false){
					if(selectmode != 1){
						one.num = 0;
						this.optionsprice = optionsprice() * this.optionscount;
						this.$forceUpdate();  
					}
					
					return;
				}
				if(selectmode == 999){
					one.num = 1;
				}else{
					if(selectmode == 1){
						for(var i=0;i<list.length;i++){
							list[i].num = 0;
						}
						one.num = 1;
					}else{
						if(selectmode>0){
							if(selectmode >500){
								selectmode -= 500;
							}
							var count = 0;
							for(var i=0;i<list.length;i++){
								if(list[i].num){
									count += list[i].num;
								}
							}
							if(selectmode == 0 || selectmode>count){
								try{
									if(one.MaxSelect>0 && one.num>= one.MaxSelect){
										return;
									}
								}catch(e){}
								one.num ++;
								if(this.currentMenu.PriceMode == 6){
									one.num = 1;
								}
							}
						}else{
							try{
								if(one.MaxSelect>0 && one.num>= one.MaxSelect){
									return;
								}
							}catch(e){}
							one.num ++;
							if(this.currentMenu.PriceMode == 6){
								one.num = 1;
							}
						}
						
					}
					
				}
				this.optionsprice = optionsprice()* this.optionscount;
				this.$forceUpdate();  
			},
			closeoptionsfun : function(){
				this.optionsshowflag = false;
				this.optionscount = 1;
			},
			clickcartfun : function(){
				if(this.shoppingcartnum>0){
					this.orderlistshowflag = true;
				}
				
			},
			clickcloseorderlistfun : function(){
				this.orderlistshowflag = false;
			},
			clickgroupfun : function(group,index){
				initCategory(index);
			},
			clickcmdaddfun : function(menu){
				var that = this;
				if(!menu.count){
					menu.count = 0;
				}
				menu.count ++;
				this.shoppingcartnum ++;
				var shippingInfo = honorpos.clone(menu);
				if( !shippingInfo.Price ){
					shippingInfo.Price = shippingInfo.Price1;
				}
				
				var flag = true;
				for(var i=0;i<this.shoppingcartlist.length;i++){
					var one = this.shoppingcartlist[i];
					if(one.MenuID == menu.MenuID){
						one.count ++;
						shippingInfo = one;
						//this.shoppingcartlist.splice(i,1,shippingInfo);
						//one = shippingInfo;
						flag = false;
						break;
					}
				}
				if(flag){
					var followIDS ="'0'";
					if(menu.MutliSelect == 999 && menu.FollowGroupID){
						followIDS += ",'"+menu.FollowGroupID+"'";
					}
					if(menu.MutliSelect2 == 999 && menu.FollowGroupID2){
						followIDS += ",'"+menu.FollowGroupID2+"'";
					}
					if(menu.MutliSelect3 == 999 && menu.FollowGroupID3){
						followIDS += ",'"+menu.FollowGroupID3+"'";
					}
					if(menu.MutliSelect4 == 999 && menu.FollowGroupID4){
						followIDS += ",'"+menu.FollowGroupID4+"'";
					}
					if(menu.MutliSelect5 == 999 && menu.FollowGroupID5){
						followIDS += ",'"+menu.FollowGroupID5+"'";
					}
					if(menu.MutliSelect6 == 999 && menu.FollowGroupID6){
						followIDS += ",'"+menu.FollowGroupID6+"'";
					}
					if(menu.MutliSelect7 == 999 && menu.FollowGroupID7){
						followIDS += ",'"+menu.FollowGroupID7+"'";
					}
					if(menu.MutliSelect8 == 999 && menu.FollowGroupID8){
						followIDS += ",'"+menu.FollowGroupID8+"'";
					}
					if(followIDS == "'0'"){
						that.shoppingcartamount += shippingInfo.Price;

						that.shoppingcartlist.push(shippingInfo);
						honorpos.saveshoppingcart(that.shoppingcartlist)
						that.$forceUpdate();  
					}else{
						honorpos_ui.showLoading("Loading...")
						honorpos.api('menu/follow','followGroupID='+followIDS,function(data){
							honorpos_ui.hideAllPanel();
							var followList = new Array();
							if(data.returnCode == 0){
								var list = data.list;
								for(var i=0;i<list.length;i++){
									var one = list[i];
									shippingInfo.Price += one.Price;
									one.Price = 0;
									followList.push(one);
								}
							}
							menu.followList = followList;
							shippingInfo.followList = followList;
							that.shoppingcartamount += shippingInfo.Price;

							that.shoppingcartlist.push(shippingInfo);
							honorpos.saveshoppingcart(that.shoppingcartlist)
							that.$forceUpdate();  
						})
					}
					
					
				}else{

					that.shoppingcartamount += shippingInfo.Price;
					honorpos.saveshoppingcart(that.shoppingcartlist)
					that.$forceUpdate();  
				}
			},
			clickcmdlessfun : function(menu){
				if(!menu.count){
					menu.count = 0;
				}
				menu.count --;

				this.shoppingcartnum --;
				var shippingInfo = honorpos.clone(menu);
				if( !shippingInfo.Price ){
					shippingInfo.Price = shippingInfo.Price1;
				}
				var flag = true;
				for(var i=0;i<this.shoppingcartlist.length;i++){
					var one = this.shoppingcartlist[i];
					if(one.MenuID == menu.MenuID){

						this.shoppingcartamount -= one.Price;
						if(shippingInfo.count<=0){
							this.shoppingcartlist.splice(i,1);
						}else{
							one.count --;
						}
						break;
						
					}
				}
				if(this.shoppingcartlist.length<=0){
					this.orderlistshowflag = false;
				}
				honorpos.saveshoppingcart(this.shoppingcartlist)
				this.$forceUpdate();  
			},
			clickcategoryfun : function(){
				this.categoryshowflag = true;
			},
			clickselectcategoryfun : function(category){
				this.categoryshowflag = false;
				var categoryID = category.CategoryID;
				var top = $('.wrap_category_main[categoryID='+categoryID+']').offset().top
				$("body,html").animate({
					scrollTop: top
				}, 100);  

			},
			clicksearchfun : function(){
				honorpos_ui.prompt("Search Info",vueObject.searchinfo,function(data){
					vueObject.searchinfo = data;
				},function(){
					vueObject.searchinfo = "";
				})
			},
			clickgototopfun : function(){
				$("body,html").animate({
					scrollTop: 0
				}, 100);  
			},
			orderlistaddfun : function(item){
				if(item.isOptions){
					item.count++;
					this.shoppingcartnum ++;
					this.shoppingcartamount += item.Price;
					honorpos.saveshoppingcart(this.shoppingcartlist)
					this.$forceUpdate();  
					return;
				}
				for(var x=0;x<this.categoryGroupList.length;x++){
					var categoryList = this.categoryGroupList[x].categoryList;
					for(var i=0;i<categoryList.length;i++){
						var one = categoryList[i];
						if(one.ClientID == item.CategoryID){
							var menuList = one.menuList;
							for(var j=0;j<menuList.length;j++){
								var menu = menuList[j];
								if(item.MenuID == menu.MenuID){
									this.clickcmdaddfun(menu);
									break;
								}
							}
							break;
						}
					}
				}
				

				
			},
			orderlistlessfun : function(item,index){
				if(item.isOptions){
					item.count--;
					this.shoppingcartnum --;
					this.shoppingcartamount -= item.Price;
					if(item.count<=0){
						this.shoppingcartlist.splice(index,1);
						if(this.shoppingcartlist.length<=0){
							this.orderlistshowflag = false;
						}
					}
					honorpos.saveshoppingcart(this.shoppingcartlist)
					this.$forceUpdate();  
					return;
				}
				for(var x=0;x<this.categoryGroupList.length;x++){
					var categoryList = this.categoryGroupList[x].categoryList;
					for(var i=0;i<categoryList.length;i++){
						var one = categoryList[i];
						if(one.ClientID == item.CategoryID){
							var menuList = one.menuList;
							for(var j=0;j<menuList.length;j++){
								var menu = menuList[j];
								if(item.MenuID == menu.MenuID){
									this.clickcmdlessfun(menu);
									break;
								}
							}
							break;
						}
					}
				}
				
			},
			clearcartfun : function(){
				this.shoppingcartlist = new Array();
				this.shoppingcartnum = 0;
				this.shoppingcartamount = 0;
				honorpos.saveshoppingcart(this.shoppingcartlist);
				this.orderlistshowflag = false;
				var list = honorpos.getCache("sys_allmenu");
				if(list){
					list = JSON.parse(list);
					vueObject.categoryGroupList =list;
					initCategory(0);
					initShoppingCart();
				}
			},
			optionsaddcartfun : function(){
				var that = this;
				var desc = $('#txt_remark').val();
				var menu = optionspricetocard();
				if(menu == null){
					honorpos_ui.alert("Please correct your selection!")
					return;
				}
				if(menu == false){
					return;
				}
				var shippingInfo = honorpos.clone(menu);	
				shippingInfo.adddesc = desc;
				shippingInfo.count = that.optionscount;

				
				if(desc){
					honorpos_ui.showLoading();
					honorpos.api('sys/googleTranslate?info='+encodeURIComponent(desc),'',function(data){
						honorpos_ui.hideAllPanel();
						that.optionsshowflag = false;
						that.shoppingcartnum +=  that.optionscount;;
						that.shoppingcartamount += menu.Price *  that.optionscount;
						try{
							shippingInfo.adddescCn = data[0][0][0];
						}catch(e){}						
						that.shoppingcartlist.push(shippingInfo);
						honorpos.saveshoppingcart(that.shoppingcartlist);
					})
				}else{
					that.optionsshowflag = false;
					
					that.shoppingcartnum +=  that.optionscount;;
					that.shoppingcartamount += menu.Price *  that.optionscount;
					
					shippingInfo.count = that.optionscount;
					that.shoppingcartlist.push(shippingInfo);
					honorpos.saveshoppingcart(that.shoppingcartlist);
				}
				

			
				
			},
			neworderfun : function(){
				//var tableID = honorpos.getParams('tableID');
				//var tableNo = honorpos.getParams('tableNo');
				if(tableID){
					if(vueObject.QROrderMustLogin == 1){
						honorpos.api('user/getUserInfo','',function(data){
							if(data.returnCode == 0){
								window.location.href="newTableOrder.html?tableID="+tableID+"&tableNo="+tableNo;						
							}else{
								var url = "index.html?tableID="+tableID+"&tableNo="+tableNo;
								window.location.href = "login.html?targetUrl="+encodeURIComponent(url);
							}
							
						})
					}else{
						window.location.href="newTableOrder.html?tableID="+tableID+"&tableNo="+tableNo;
					}
					
				}else{
					honorpos.api('user/getUserInfo','',function(data){
						if(data.returnCode == 0){
							var url = "newOrder.html";
							if(tableNo){
								url += "?tableNo="+tableNo;
							}
							window.location.href=url;							
						}else{
							var url = "index.html";
							if(tableNo){
								url += "?tableNo="+tableNo;
							}
							if(vueObject.guestOrder == 0){
								window.location.href = "login.html?targetUrl="+encodeURIComponent(url);
							}else{
								honorpos_ui.showPannel('<h1>Create your account</h1><ul style="text-align:left;padding-left:0px;"><li>Keep your addresses, easy for reordering</li><li>View all your past orders</li><li>Get coupons on email and much more</li></ul>',['Checkout as a guest','Login & Register','Cancel'],function(index){
									if(index == 0){
										var url = "newOrder.html";
										if(tableNo){
											url += "?tableNo="+tableNo;
										}
										window.location.href=url;		
									}
									if(index == 1){
										var url = "index.html";
										if(tableNo){
											url += "?tableNo="+tableNo;
										}
										window.location.href = "login.html?targetUrl="+encodeURIComponent(url);
									}
								})

								/*
								honorpos_ui.confirm("Check out as a guest?",function(){
									var url = "newOrder.html";
									if(tableNo){
										url += "?tableNo="+tableNo;
									}
									window.location.href=url;		
								},function(){
									var url = "index.html";
									if(tableNo){
										url += "?tableNo="+tableNo;
									}
									window.location.href = "login.html?targetUrl="+encodeURIComponent(url);
								})*/

							}
							/*
							*/
							
						}
						
					})
					
				}
				
			},
			clickPicfun : function(one){
				$('.wrap_menu_bigpic').html("<img src='"+one.Picurl+"' />");
				$('.wrap_menu_bigpic_wrap').show();
			},
			optionsaddfun : function(){
				this.optionscount ++;
				this.optionsprice = optionsprice() * this.optionscount;
			},
			optionslessfun : function(){
				if(this.optionscount <= 1){
					return;
				}
				this.optionscount --;
				this.optionsprice = optionsprice() * this.optionscount;
			},
			closePicfun : function(){
				$('.wrap_menu_bigpic_wrap').hide();
			},
			closecouponfun : function(){
				this.couponshowflag = false;
			},
			closebtnfun:function(){
				var html = "<table style='border:none;'>";
				if(vueObject.timeInfo1){
					html += "<tr><td style='text-align:right'>Monday : </td><td style='text-align:left'>"+vueObject.timeInfo1+"</td></tr>";
				}
				if(vueObject.timeInfo2){
					html += "<tr><td style='text-align:right'>Tuesday : </td><td  style='text-align:left'>"+vueObject.timeInfo2+"</td></tr>";
				}
				if(vueObject.timeInfo3){
					html += "<tr><td style='text-align:right'>Wednesday : </td><td style='text-align:left'>"+vueObject.timeInfo3+"</td></tr>";
				}
				if(vueObject.timeInfo4){
					html += "<tr><td style='text-align:right'>Thursday : </td><td style='text-align:left'>"+vueObject.timeInfo4+"</td></tr>";
				}
				if(vueObject.timeInfo5){
					html += "<tr><td style='text-align:right'>Friday : </td><td style='text-align:left'>"+vueObject.timeInfo5+"</td></tr>";
				}
				if(vueObject.timeInfo6){
					html += "<tr><td style='text-align:right'>Saturday : </td><td style='text-align:left'>"+vueObject.timeInfo6+"</td></tr>";
				}
				if(vueObject.timeInfo7){
					html += "<tr><td style='text-align:right'>Sunday : </td><td style='text-align:left'>"+vueObject.timeInfo7+"</td></tr>";
				}

				html += "</table>";
				honorpos_ui.alert(html)
			}
		}
	})
	
	var list = honorpos.getCache("sys_allmenu");
	if(list){
		list = JSON.parse(list);
		vueObject.categoryGroupList =list;
		initCategory(0);
		initShoppingCart();
	}else{
		honorpos_ui.showLoading("Loading Menu...")
	}

	initCategoryGroup();


	honorpos.api('sys/getMyOrgInfo','',function(data){
		if(data.returnCode == 0 && data.orgInfo){
			honorpos.saveCache("sys_orgInfo",data.orgInfo);
			initOrgInfo(data.orgInfo)
			
		}
		
	})

	honorpos.api('sys/getBussnissTime','',function(data){
		if(data.returnCode == 0){
			var info = data.week1;
			vueObject.timeInfo1 = toHHMMTime(info);
			info = data.week2;
			vueObject.timeInfo2 = toHHMMTime(info);
			info = data.week3;
			vueObject.timeInfo3 = toHHMMTime(info);
			info = data.week4;
			vueObject.timeInfo4 = toHHMMTime(info);
			info = data.week5;
			vueObject.timeInfo5 = toHHMMTime(info);
			info = data.week6;
			vueObject.timeInfo6 = toHHMMTime(info);
			info = data.week7;
			vueObject.timeInfo7 = toHHMMTime(info);



		}
		
	})

	function toHHMMTime(info){
		var timeInfo = "";
		for(var i=0;i<info.length;i++){
			var one = info[i];
			if(one.startTime == one.endTime){
				return "";
			}
			timeInfo += new Date(one.startTime).format('HH:mm a') + "-" + new Date(one.endTime).format('HH:mm a')+"<br/>";
		}
		console.log(timeInfo);
		return timeInfo;

	}

	var orgInfo = honorpos.getCache('sys_orgInfo');
	if(orgInfo){
		orgInfo = JSON.parse(orgInfo);
		initOrgInfo(orgInfo)
	}

	var token = honorpos.getParams('token');
	var params = "";
	if(token){
		params = "token="+token;
	}
	tableID = honorpos.getParams('tableID');
	tableNo = honorpos.getParams('tableNo');

	if(tableID){
		vueObject.showUserCenterFlag = false;		
	}else{
		honorpos.setCookie('sys_order_code','');
	}
	if(tableNo){
		vueObject.tableNo = tableNo;
	}else{
		vueObject.tableNo = '';
	}

	if(!tableID){
		honorpos.api('sys/getWxUserInfo',params,function(data){
			if(honorpos.checkWeixin() && (!data.info || !data.info.openid)){
				//honorpos_ui.confirm("Auto Login?",function(){
					var url = "http://wx.honor.jxtcloud.cn/wxIntf/wx/thridSSO?redirectUrl=";
					var thisUrl = window.location.protocol +"//" + window.location.host;
					
					url += encodeURIComponent(thisUrl);
					//window.location.href=url;
				//})
				
			}
		})
	}

	honorpos.api('user/checkRegistCoupon','',function(data){
		if(data.returnCode == 0){
			vueObject.couponshowflag = true;
			vueObject.discountAmount = data.couponInfo.DiscountAmount;
			vueObject.bamount = data.couponInfo.BAmount;
			vueObject.endtime = data.couponInfo.EndTime;
			vueObject.couponName = data.couponInfo.GroupName;
		}
		
	})
	



	//vueObject.clearcartfun();
})


function initOrgInfo(orgInfo){
	vueObject.mainname = orgInfo.OrgName;
	vueObject.phoneinfo = orgInfo.OrgPhoneNo;
	vueObject.addressinfo = orgInfo.Street+","+orgInfo.City+" "+orgInfo.State+" "+orgInfo.ZipCode;
	vueObject.orderStatus = orgInfo.OrderStatus;
	vueObject.status = orgInfo.Status;
	vueObject.minAmount = orgInfo.MinAmount;
	vueObject.orgPhone2 = orgInfo.OrgPhone2;
	vueObject.remark = orgInfo.Remark;
	vueObject.QROrderMustLogin = orgInfo.QROrderMustLogin;
	vueObject.registGroupID = orgInfo.RegistGroupID;
	vueObject.orderGroupID = orgInfo.OrderGroupID;
	vueObject.orderLowAmount = orgInfo.OrderLowAmount;
	vueObject.getGroupID = orgInfo.GetGroupID;
	vueObject.shareRegistGroupID = orgInfo.ShareRegistGroupID;
	vueObject.shareOrderGroupID = orgInfo.ShareOrderGroupID;
	vueObject.guestOrder = orgInfo.GuestOrder;
	if(orgInfo.QROrderMustLogin == 1){
		vueObject.showUserCenterFlag = true;		
	}

}

function initCategoryGroup(){
	var tableID = honorpos.getParams('tableID');
	var params = '';
	if(tableID){
		params = "type=1";
	}
	honorpos.api('menu/categoryGroup',params,function(data){
		honorpos_ui.hideAllPanel();
		if( firstFlag && vueObject.remark && vueObject.remark.length>20){
			firstFlag = false;		
			honorpos_ui.alert(vueObject.remark)
		}
		if(data.returnCode == 0){
			honorpos.saveCache("sys_allmenu",data.list);
			vueObject.categoryGroupList = data.list;
			initCategory(0);
			initShoppingCart();

		}
		var tableID = honorpos.getParams('tableID');
		if(tableID){
			var orderCode = honorpos.getCookie('sys_order_code');
			if(orderCode){
				honorpos_ui.confirm("You have an order in progress. <br/>Do you need to add items?<br/><br/>Click \"YES\" to add items.<br/>Click \"No\" to create a new order. ",function(){
					tableNo = honorpos.getCookie('sys_order_tno');
					tableID = honorpos.getCookie('sys_order_tid');
					if(tableNo){
						vueObject.tableNo = tableNo;
					}
				},function(){
					honorpos.setCookie('sys_order_code','');
					honorpos.setCookie('sys_order_tno','');
					honorpos.setCookie('sys_order_tid','');
					window.location.href = window.location.href;
				})
			}
		}
	})
}

function initCategory(index){
	var group = vueObject.categoryGroupList[index];
	if(group){
		vueObject.currentGroupIndex = index;
		vueObject.categoryList = group.categoryList;
	}
}


function initShoppingCart(){
	var list = honorpos.getshoppingcart();
	var newList = new Array();
	vueObject.shoppingcartnum = 0;
	vueObject.shoppingcartamount = 0;
	for(var i=0;list && i<list.length;i++){
		var one = list[i];
		var menu = getMenuItem(one);
		if(menu){
			menu.Price = one.Price;
			menu.count = one.count;
			vueObject.shoppingcartnum += menu.count;
			vueObject.shoppingcartamount += menu.count*menu.Price;
			newList.push(one);
		}
	}
	//honorpos.saveshoppingcart(newList)
	vueObject.shoppingcartlist = newList;
}

function optionsprice(){
	var menu =  honorpos.clone(vueObject.currentMenu);

	var price = menu.Price1;
	for(var i=0;i<vueObject.samelinelist.length;i++){
		var one = vueObject.samelinelist[i];
		if(one.Price!=0 && one.num>0){
			price = one.Price*one.num;
		}
	}
	for(var i=0;i<vueObject.nextlinelist.length;i++){
		var one = vueObject.nextlinelist[i];
		if(one.Price!=0 && one.num>0){
			price += one.Price*one.num;
		}
	}
	for(var i=0;i<vueObject.mulitiple1list.length;i++){
		var one = vueObject.mulitiple1list[i];
		if(one.Price!=0 && one.num>0){
			price += one.Price*one.num;
		}
		if(one.num>0){
			if(one.PriceMode == 8){
				return 0;
			}
		}
	}
	for(var i=0;i<vueObject.mulitiple2list.length;i++){
		var one = vueObject.mulitiple2list[i];
		if(one.Price!=0 && one.num>0){
			price += one.Price*one.num;
		}
	}
	for(var i=0;i<vueObject.mulitiple3list.length;i++){
		var one = vueObject.mulitiple3list[i];
		if(one.Price!=0 && one.num>0){
			price += one.Price*one.num;
		}
	}
	for(var i=0;i<vueObject.mulitiple4list.length;i++){
		var one = vueObject.mulitiple4list[i];
		if(one.Price!=0 && one.num>0){
			price += one.Price*one.num;
		}
	}
	for(var i=0;i<vueObject.mulitiple5list.length;i++){
		var one = vueObject.mulitiple5list[i];
		if(one.Price!=0 && one.num>0){
			price += one.Price*one.num;
		}
	}
	for(var i=0;i<vueObject.mulitiple6list.length;i++){
		var one = vueObject.mulitiple6list[i];
		if(one.Price!=0 && one.num>0){
			price += one.Price*one.num;
		}
	}
	for(var i=0;i<vueObject.mulitiple7list.length;i++){
		var one = vueObject.mulitiple7list[i];
		if(one.Price!=0 && one.num>0){
			price += one.Price*one.num;
		}
	}
	for(var i=0;i<vueObject.mulitiple8list.length;i++){
		var one = vueObject.mulitiple8list[i];
		if(one.Price!=0 && one.num>0){
			price += one.Price*one.num;
		}
	}

	return price;
}


function optionspricetocard(){
	
	var followList = new Array();
	var menu =  honorpos.clone(vueObject.currentMenu);

	var price = menu.Price1;
	for(var i=0;i<vueObject.samelinelist.length;i++){
		var one = vueObject.samelinelist[i];
		if(one.Price!=0 && one.num>0){
			price = one.Price;
			if(menu.Price2 > 0){
				menu.followWord = one.L1;
				menu.followWord2 = one.L2;
			}
			try{
				if(one.PrintMode == 1){
					menu.Printer += ","+one.Printer;
				}

			}catch(e){}
			
			
		}
	}
	var followPrice = 0;
	for(var i=0;i<vueObject.nextlinelist.length;i++){
		var one = vueObject.nextlinelist[i];
		for(var x=0;one.num && x<one.num;x++){	
			price += one.Price;
			followPrice += one.Price;
			followList.push(one);
		}
	}

	var followCount = 0;
	for(var i=0;i<vueObject.mulitiple1list.length;i++){
		var one = vueObject.mulitiple1list[i];
		if(one.num && one.PriceMode == 8){
			honorpos.api('menu/menu','itemNo='+one.Remark,function(data){
				if(data.list.length>0){
					var changeMenu = data.list[0];
					var num = vueObject.optionscount;
					//changeMenu.count = menu.count;
					vueObject.clickoptionsfun(changeMenu,null,vueObject.optionscount);
					
				}
				console.log(data);
				
			})
			return false;
		}

		for(var x=0;one.num && x<one.num;x++){
			followCount += 1;//one.num;
			price += one.Price;
			followPrice += one.Price;
			followList.push(one);
		}
	}
	if(vueObject.mulitiple1num<0 && 0 - vueObject.mulitiple1num > followCount){
		return null;
	}
	if(vueObject.mulitiple1num >500  && vueObject.mulitiple1num !=999 && vueObject.mulitiple1num-500 > followCount){
		return null;
	}
	followCount = 0;
	for(var i=0;i<vueObject.mulitiple2list.length;i++){
		var one = vueObject.mulitiple2list[i];
		/*
		if(vueObject.mulitiple2num == 999){
			price += one.Price;
			followPrice += one.Price;
			followList.push(one);
			continue;
		}*/
		for(var x=0;one.num && x<one.num;x++){
			followCount += 1;//one.num;
			price += one.Price;
			followPrice += one.Price;
			followList.push(one);
		}
	}
	if(vueObject.mulitiple2num<0 && 0 - vueObject.mulitiple2num > followCount){
		return null;
	}
	if(vueObject.mulitiple2num >500  && vueObject.mulitiple2num !=999 && vueObject.mulitiple2num-500 > followCount){
		return null;
	}
	followCount = 0;
	for(var i=0;i<vueObject.mulitiple3list.length;i++){
		var one = vueObject.mulitiple3list[i];
		/*
		if(vueObject.mulitiple3num == 999){
			price += one.Price;
			followPrice += one.Price;
			followList.push(one);
			continue;
		}*/
		for(var x=0;one.num && x<one.num;x++){
			followCount += 1;//one.num;
			price += one.Price;
			followPrice += one.Price;
			followList.push(one);
		}
	}
	if(vueObject.mulitiple3num<0 && 0 - vueObject.mulitiple3num > followCount){
		return null;
	}
	if(vueObject.mulitiple3num >500  && vueObject.mulitiple3num !=999 && vueObject.mulitiple3num-500 > followCount){
		return null;
	}
	followCount = 0;
	for(var i=0;i<vueObject.mulitiple4list.length;i++){
		var one = vueObject.mulitiple4list[i];
		/*
		if(vueObject.mulitiple4num == 999){
			price += one.Price;
			followPrice += one.Price;
			followList.push(one);
			continue;
		}*/
		for(var x=0;one.num && x<one.num;x++){
			followCount += 1;//one.num;
			price += one.Price;
			followPrice += one.Price;
			followList.push(one);
		}
	}
	if(vueObject.mulitiple4num<0 && 0 - vueObject.mulitiple4num > followCount){
		return null;
	}
	if(vueObject.mulitiple4num >500  && vueObject.mulitiple4num !=999 && vueObject.mulitiple4num-500 > followCount){
		return null;
	}
	followCount = 0;
	for(var i=0;i<vueObject.mulitiple5list.length;i++){
		var one = vueObject.mulitiple5list[i];
		/*if(vueObject.mulitiple5num == 999){
			price += one.Price;
			followPrice += one.Price;
			followList.push(one);
			continue;
		}*/
		for(var x=0;one.num && x<one.num;x++){
			followCount += 1;//one.num;
			price += one.Price;
			followPrice += one.Price;
			followList.push(one);
		}
	}
	if(vueObject.mulitiple5num<0 && 0 - vueObject.mulitiple5num > followCount){
		return null;
	}
	if(vueObject.mulitiple5num >500  && vueObject.mulitiple5num !=999 && vueObject.mulitiple5num-500 > followCount){
		return null;
	}
	followCount = 0;
	for(var i=0;i<vueObject.mulitiple6list.length;i++){
		var one = vueObject.mulitiple6list[i];
		/*
		if(vueObject.mulitiple6num == 999){
			price += one.Price;
			followPrice += one.Price;
			followList.push(one);
			continue;
		}*/
		for(var x=0;one.num && x<one.num;x++){
			followCount += 1;//one.num;
			price += one.Price;
			followPrice += one.Price;
			followList.push(one);
		}
	}
	if(vueObject.mulitiple6num<0 && 0 - vueObject.mulitiple6num > followCount){
		return null;
	}
	if(vueObject.mulitiple6num >500  && vueObject.mulitiple6num !=999 && vueObject.mulitiple6num-500 > followCount){
		return null;
	}
	followCount = 0;
	for(var i=0;i<vueObject.mulitiple7list.length;i++){
		var one = vueObject.mulitiple7list[i];
		/*
		if(vueObject.mulitiple7num == 999){
			price += one.Price;
			followPrice += one.Price;
			followList.push(one);
			continue;
		}*/
		for(var x=0;one.num && x<one.num;x++){
			followCount += 1;//one.num;
			price += one.Price;
			followPrice += one.Price;
			followList.push(one);
		}
	}
	if(vueObject.mulitiple7num<0 && 0 - vueObject.mulitiple7num > followCount){
		return null;
	}
	if(vueObject.mulitiple7num >500  && vueObject.mulitiple7num !=999 && vueObject.mulitiple7num-500 > followCount){
		return null;
	}
	followCount = 0;
	for(var i=0;i<vueObject.mulitiple8list.length;i++){
		var one = vueObject.mulitiple8list[i];
		/*
		if(vueObject.mulitiple8num == 999){
			price += one.Price;
			followPrice += one.Price;
			followList.push(one);
			continue;
		}*/
		for(var x=0;one.num && x<one.num;x++){
			followCount += 1;//one.num;
			price += one.Price;
			followPrice += one.Price;
			followList.push(one);
		}
	}
	if(vueObject.mulitiple8num<0 && 0 - vueObject.mulitiple8num > followCount){
		return null;
	}
	if(vueObject.mulitiple8num >500  && vueObject.mulitiple8num !=999 && vueObject.mulitiple8num-500 > followCount){
		return null;
	}
	menu.count = 1;
	menu.isOptions = true;
	menu.Price = price;
	menu.followPrice = followPrice;
	menu.followList = followList;
	return menu;
}


function getSinglePrice(menu,follow,index){
	if(follow.FollowItemType == 1 || follow.FollowItemType ==2 || follow.FollowItemType ==6 || follow.FollowItemType ==7 || follow.FollowItemType ==8){
		eval("var price=menu.Price"+index);
		return price;
	}
	if(follow.FollowItemType == 3){
		return follow.Price;
	}
	if(follow.FollowItemType == 4){
		return menu.Price1 + follow.Price;
	}
	return 0;
}

function getMenuItem(item){
	try{
		for(var x=0;x<vueObject.categoryGroupList.length;x++){
			var categoryList = vueObject.categoryGroupList[x].categoryList;
			for(var i=0;i<categoryList.length;i++){
				var one = categoryList[i];
				if(one.ClientID == item.CategoryID){
					var menuList = one.menuList;
					for(var j=0;j<menuList.length;j++){
						var menu = menuList[j];
						if(item.ClientID == menu.ClientID){
							return menu;
						}
					}
					break;
				}
			}
		}

	}catch(e){

	}
	
	return null;
}