<?php
require "common.php";
$m=file_get_contents("php://input");if($m=="")$m='{"act":"index"}';$m=Dec($m,true);
if($m["act"]!="index"){$nid=$m["nid"];if(!is_numeric($nid)||$nid*1==0)die('{"msg":"Error Shop ID."}');}
switch($m["act"]){
	case "cfg"://设置
	$p=["msg"=>"ok","Sb"=>[],"Sd"=>[]];
	$q=Qry("Select ID,Bn,Bp From ".$pre."class Where Si=".$nid." Order By Br Asc");
	while($r=Qra($q))$p["Sb"][]=$r;
	$q=Qry("Select ID,Dn,Dc,Ds,Di,Dm,Dk,Dh From ".$pre."foods Where Dh=0 And Si=".$nid." Order By Dr Asc");
	while($r=Qra($q))$p["Sd"][]=$r;
	$r=Qra(Qry("Select Sn,Sc,Sh From ".$pre."shop Where Si=".$nid));
	if($r["Sh"]>0)die('{"msg":"该店已暂停歇业！"}');
	$p["Sc"]=Dec($r["Sc"]);$p["Sn"]=$r["Sn"];
	echo Enc($p);
	break;

	case "fcz"://餐桌
	$q=Qry("Select ID,Tn,Ts,Ti,Tu From ".$pre."table Where Si=".$nid." And Th=0 Order By ID Asc");$c=0;
	while($r=Qra($q)){if($m["Tk"]==substr(md5($nid.$r["ID"]),0,8)){$c=1;$r["msg"]="ok";echo Enc($r);break;}}
	if($r["Ts"]*1==0&&$m["Ta"]*1==1)Qry("Update ".$pre."table Set Ts=1,Tu=2 Where Si=".$nid." And ID=".$r["ID"]);
	if($c==0)echo '{"msg":"参数错误！"}';
	break;

	case "fds"://已点
	$p=["msg"=>"ok","Mb"=>["Pm"=>0,"Pd"=>0,"Pc"=>0,"Ps"=>0,"Pt"=>0],"Md"=>[]];
	$q=Qry("Select Pm,Pd,Pc,Ps,Pt,Ob From ".$pre."order Where Si=".$nid." And ID=".$m["oid"]);
	if(Qrc($q)>0)$p["Mb"]=Qra($q);
	$q=Qry("Select A.ID,A.Fi,A.Fn,A.Fm,A.Fc,A.Fs,A.Fr,A.Fb,B.Dm From ".$pre."ofood As A,".$pre."foods As B Where B.ID=A.Fi And A.Fs<14 And A.Si=".$nid." And A.Fo=".$m["oid"]." Order By A.ID Asc");
	while($r=Qra($q))$p["Md"][]=$r;
	echo Enc($p);
	break;
	
	case "kxd"://下单
	$c=Qrs(Qry("Select Ti From ".$pre."table Where Si=".$nid." And ID=".$m["tid"]));
	if($c!=0&&$c!=$m["oid"]*1)die('{"msg":"请勿重复下单！"}');
	$q=Qry("Select Ot From ".$pre."order Where Si=".$nid." And ID=".$c);
	if(Qrc($q)>0){
		Qry("Update ".$pre."order Set Ot=".$m["tid"].",O1=0,Os=3,Ob='".$m["Ob"]."' Where Si=".$nid." And ID=".$c);
	}else{
		Qry("Insert Into ".$pre."order (Si,Ot,O1,O4,Ox,Pm,Pd,Pc,Ps,Pt,Os,Od,Oe,Ob,Ou,Oo) Values (".$nid.",".$m["tid"].",0,0,0,0,0,0,0,0,3,Now(),Now(),'".$m["Ob"]."','','')");
		$c=Qri();
	}
	for($i=0;$i<count($m["Mc"]);$i++){
		$a=$m["Mc"][$i];
		$r=Qra(Qry("Select Dn,Dm,Dt From ".$pre."foods Where Si=".$nid." And ID=".$a[0]));
		if($r["Dt"]=="0"){
			Qry("Insert Into ".$pre."ofood (Si,Fo,Ft,Fi,Fn,Fm,Fc,Fs,F1,F2,F3,Fd,Fr,Fb) Values (".$nid.",".$c.",".$m["tid"].",".$a[0].",'".$r["Dn"]."',".$r["Dm"].",".$a[1].",3,0,0,0,Now(),0,'".$a[2]."')");
		}else{
			Qry("Insert Into ".$pre."ofood (Si,Fo,Ft,Fi,Fn,Fm,Fc,Fs,F1,F2,F3,Fd,Fr,Fb) Values (".$nid.",".$c.",".$m["tid"].",".$a[0].",'".$r["Dn"]."',0,".$a[1].",3,0,0,0,Now(),0,'".$a[2]."')");
			$e=explode(",",$r["Dt"]);$b=0;
			$f=$r["Dm"]/Qrs(Qry("Select Sum(Dm) From ".$pre."foods Where Si=".$nid." And ID In (".$r["Dt"].")"));
			for($j=1;$j<count($e);$j++){
				$k=Qra(Qry("Select Dn,Dm,ID From ".$pre."foods Where Si=".$nid." And ID=".$e[$j]));
				$b+=round($k["Dm"]*$f);
				Qry("Insert Into ".$pre."ofood (Si,Fo,Ft,Fi,Fn,Fm,Fc,Fs,F1,F2,F3,Fd,Fr,Fb) Values (".$nid.",".$c.",".$m["tid"].",".$k["ID"].",'".$k["Dn"]."[T]',".round($k["Dm"]*$f).",".$a[1].",3,0,0,0,Now(),0,'".$a[2]."')");
			}
			Qry("Update ".$pre."ofood Set Fm=Fm+".($r["Dm"]-$b)." Where Si=".$nid." And ID=".Qri());
		}
	}
	$p=0;
	$q=Qry("Select Fm,Fc From ".$pre."ofood Where Si=".$nid." And Fr<2 And Fo=".$c);
	while($r=Qra($q))$p+=$r["Fm"]*$r["Fc"];
	Qry("Update ".$pre."order Set Pm=".$p." Where Si=".$nid." And ID=".$c);
	Qry("Update ".$pre."table Set Ts=3,Ti=".$c.",Tm=".$p." Where Si=".$nid." And ID=".$m["tid"]);
	echo '{"msg":"ok","oid":'.$c.'}';
	break;

	case "ktd"://退单
	Qry("Update ".$pre."ofood Set Fr=1 Where Si=".$nid." And Fs<12 And ID=".$m["ID"]);
	Qry("Update ".$pre."order Set Os=4 Where Si=".$nid." And Os<12 And ID=".$m["oid"]);
	Qry("Update ".$pre."table Set Ts=4 Where Si=".$nid." And Ts<12 And Ts>0 And ID=".$m["tid"]);
	echo '{"msg":"ok"}';
	break;

	case "kjz"://结账
	Qry("Update ".$pre."table Set Ts=11 Where Si=".$nid." And Ts>0 And Ts<12 And ID=".$m["tid"]);
	if($m["Pt"]*1<2)die('{"msg":"ok"}');
	$o=$m["nid"]."-".$m["oid"]."-".Rnd(6);
	Qry("Insert Into ".$pre."pay (Mc,Mp,Md,Mt,Nc,Np,Nd,Nt) Values ('".$o."',".$m["Pn"].",Now(),".(3-$m["Pt"]).",'',0,Now(),0)");
	Qry("Update ".$pre."order Set Pd=".$m["Pd"].($m["Ps"]==0?(",Pt=".$m["Pt"]):"").",Oo='".$o."' Where ID=".$m["oid"]);
	echo '{"msg":"ok","odr":"'.$o.'"}';
	break;

	case "kjm"://卡付
	$r=Qra(Qry("Select Us,Ue From ".$pre."user Where Uc='".$m["Uc"]."' And Up='".md5($m["Up"])."'"));
	if(!$r)die('{"msg":"会员卡号或消费密码错误！"}');
	$e=Qra(Qry("Select Cm,Cn,Cd From ".$pre."coupon Where Co=0 And Cu='".$m["Uc"]."' ID=".$m["Ci"]));
	if($e){if($e["Cn"]<=$m["Ps"]&&$e["Cd"]>=date("Y-m-d")){$w=$e["Cm"];}else $w=0;}else $w=0;
	$m["Ps"]=$m["Ps"]-$w;if($m["Ps"]<0)$m["Ps"]=0;
	if(($r["Us"]+$r["Ue"])<$m["Ps"])die('{"msg":"会员卡当前余额'.($r["Us"]/100).'元，可用信用额度'.($r["Ue"]/100).'元，不足以支付，请先充值！"}');
	Qry("Update ".$pre."user Set Us=Us-".$m["Ps"].",Ux=Ux+".$m["Ps"].",Ut=Now() Where Uc='".$m["Uc"]."'");
	Qry("Update ".$pre."coupon Set Co=".$m["oid"]." Where ID=".$m["Ci"]);
	Qry("Insert Into ".$pre."expend (Si,Gc,Go,Gu,Gm,Gn,Gd,Ge) Values (".$nid.",'".$m["Uc"]."',".$m["oid"].",'".$nid."-0-".Rnd(6)."',0,-".$m["Ps"].",Now(),0)");
	$c=$cfg["Ac"];
	for($i=count($c)-1;$i>0;$i--){
		if($m["Ps"]<$c[$i][0]*100)continue;
		$d=$c[$i][4]=="0"?(strtotime($c[$i][5])?$c[$i][5]:"2099-12-31"):date("Y-m-d",strtotime("+ ".$c[$i][4]."days"));
		for($j=0;$j<$c[$i][3]*1;$j++)Qry("Insert Into ".$pre."coupon (Cu,Cm,Cn,Cd,Co) Values ('".$m["Uc"]."',".($c[$i][1]*100).",".($c[$i][2]*100).",'".$d."',0);");
		break;
	}
	Qry("Update ".$pre."order Set Ps=Ps+".$m["Ps"].",Pc=Pc+".$w.",Pd=Pm-Pc-Ps,Pt=4,Oe=Now(),O4=0,Os=12 Where Si=".$nid." And ID=".$m["oid"]);
	Qry("Update ".$pre."table Set Ts=12 Where Si=".$nid." And ID=".$m["tid"]);
	echo '{"msg":"ok"}';
	break;

	case "kjc"://完成
	$r=Qra(Qry("Select Os,Oo From ".$pre."order Where Si=".$nid." And ID=".$m["oid"]));
	$e=Qra(Qry("Select Mt,Np,Nt From ".$pre."pay Where Mc='".$r["Oo"]."'"));
	if($e&&$e["Nt"]==1&&$r["Os"]<12){
		Qry("Update ".$pre."order Set Ps=Ps+".$e["Np"].",Pd=Pm-Ps-Pc,Pt='".(3-$e["Mt"])."',Oe=Now(),O4=0,Os=12,Oo='' Where Si=".$nid." And ID=".$m["oid"]);
		Qry("Update ".$pre."table Set Ts=12 Where Si=".$nid." And ID=".$m["tid"]);
	}
	echo '{"msg":"ok","pay":'.($r["Os"]==12?1:$e["Nt"]).'}';
	break;

	case "kju"://会员
	$p=["msg"=>"ok","Um"=>0,"Us"=>[]];
	$q=Qry("Select Us,Ue From ".$pre."user Where Uc='".$m["Uc"]."' And Up='".md5($m["Up"])."'");
	if(Qrc($q)==0)die('{"msg":"会员卡号或消费密码错误！"}');
	$r=Qra($q);$p["Um"]=$r["Us"]+$r["Ue"];
	$q=Qry("Select ID,Cm,Cn,Cd From ".$pre."coupon Where  Co=0 And Cu='".$m["Uc"]."' And Cd>='".date("Y-m-d")."'");
	while($r=Qra($q))$p["Us"][]=$r;
	echo Enc($p);
	break;

	case "khj"://呼叫
	Qry("Update ".$pre."table Set Ts=".$m["Ts"]." Where ID=".$m["tid"]);
	echo '{"msg":"ok"}';
	break;

	default:
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=360, initial-scale=1, user-scalable=no">
<script type="text/javascript">document.write('<meta name="viewport" content="width=360, initial-scale='+parseInt(document.documentElement.clientWidth)/360+', user-scalable=no">');</script>
<title>手机点单</title>
<style type="text/css">
html,body {margin:0px;padding:0px;font-size:16px;}
a:link,a:visited {color:#09F;text-decoration:none;}
a:hover,a:active {color:#F00;text_decoration:underline;}
input[type=text],input[type=date],input[type=number],input[type=password],textarea,select {padding:3px;font-size:16px;resize:none;}
#msk {position:fixed;width:360px;left:0px;top:0px;background-color:#000;opacity:0.2;z-index:9;display:none;}
#tip {position:fixed;max-width:300px;background-color:#000;color:#FFF;padding:5px 10px;z-index:99;display:none;}
#cfn {position:fixed;background-color:#FFF;padding:20px;width:280px;border:1px solid #CCC;line-height:24px;box-shadow:2px 2px 2px #666;display:none;z-index:98;}
.bn0 {display:inline-block;line-height:24px;padding:0px 6px;border:1px solid #CCC;text-align:center;cursor:pointer;}
.bn1 {display:inline-block;line-height:24px;padding:0px 6px;border:1px solid #CCC;background-color:#09F;color:#FFF;text-align:center;cursor:pointer;}
.btn {display:inline-block;line-height:28px;text-align:center;font-size:16px;padding:0px 8px;margin:0px 5px;background-color:#F90;color:#FFF;border-radius:5px;box-shadow:2px 2px 2px #CCC;}
.bnc {float:right;line-height:22px;text-align:center;width:22px;border:1px solid #FFF;color:#FFF;margin:4px 0px;}
.fld {position:absolute;width:320px;left:0px;top:0px;display:none;background-color:#FFF;box-shadow:2px 2px 2px #666;}
.flt {line-height:32px;background-color:#F90;color:#FFF;padding:0px 10px;}
.flb {padding:10px;line-height:32px;font-size:16px;}
.flk {line-height:32px;background-color:#FEF7E7;text-align:right;padding:0px 10px;}
.bl0 {margin:2px 0px 2px 2px;line-height:25px;border:1px solid #F90;text-align:center;background-color:#F90;color:#FFF;border-right:0px;border-top-left-radius:5px;border-bottom-left-radius:5px;}
.bl1 {margin:2px 0px 2px 2px;line-height:25px;border:1px solid #F90;text-align:center;border-right:0px;border-top-left-radius:5px;border-bottom-left-radius:5px;}
.dl0 {margin:4px;padding:4px;background-color:#FFE;border:1px dashed #FDA;line-height:28px;font-size:14px;}
.dl1 {width:256px;height:144px;overflow:hidden;background-image:url(images/none.jpg);}
.dl2 {float:right;font-size:28px;width:28px;height:28px;line-height:28px;text-align:center;background-color:#F90;color:#FFF;}
.dl3 {font-size:24px;color:#F00;}
.sl1 {margin:2px 5px;border-bottom:1px dashed #CCC;line-height:36px;font-size:14px;}
.sl2 {display:inline-block;width:234px;}
.sl3 {display:inline-block;width:40px;height:25px;text-align:right;font-size:12px;overflow:hidden;}
.sl4 {display:inline-block;width:16px;line-height:16px;border-radius:3px;background-color:#F90;color:#FFF;text-align:center;margin-left:4px;}
.sl5 {display:inline-block;width:16px;height:25px;text-align:center;margin-left:4px;font-size:12px;overflow:hidden;}
.sl6 {color:#09F;font-size:11px;}
.sl7 {display:inline-block;width:202px;}
.sl8 {display:inline-block;width:56px;font-size:11px;text-align:center;}
.sl9 {display:inline-block;width:16px;line-height:16px;text-align:center;background-color:#09F;font-size:12px;color:#FFF;}
.kw0 {display:inline-block;line-height:24px;border:1px dashed #F90;background-color:#F90;color:#FFF;margin:3px;padding:0px 5px;text-align:center;}
.kw1 {display:inline-block;line-height:24px;border:1px dashed #F90;background-color:#FFFCF9;margin:3px;padding:0px 5px;text-align:center;}
.zf1 {float:left;width:120px;margin:15px 18px 0px 0px;background-color:#F90;color:#FFF;font-size:18px;text-align:center;line-height:40px;}
.zf2 {float:left;width:120px;margin-top:15px;padding:4px 0px;background-color:#F90;color:#FFF;font-size:12px;text-align:center;line-height:16px;}
.dje {display:inline-block;width:56px;text-align:right;font-size:16px;color:#F00;}
marquee a:link,marquee a:visited {color:#FFF;text-decoration:none;}
</style>
<script type="text/javascript">
function $(o){return document.getElementById(o);}function $$(o){return document.getElementsByName(o);}
function xml(u,p,f){var x;try{x=new ActiveXObject('Msxml2.XMLHTTP');}catch(e){try{x=new ActiveXObject('Microsoft.XMLHTTP');}catch(f){x=false;}}if(!x&&typeof XMLHttpRequest!='undefined'){x=new XMLHttpRequest();}if(!x){alt('创建XHR对象失败！');return;}x.open(p?'Post':'Get',u,true);x.setRequestHeader('Content-Type','application/json;');x.onreadystatechange=function(){if(x.readyState==4&&x.status==200){try{var s=JSON.parse(x.responseText.replace(/\&\#32;/gi,' ').replace(/\&\#34;/gi,'&quot;').replace(/\&\#37;/gi,'%').replace(/\&\#39;/gi,'\''));if(s.msg!='ok'){alt(s.msg);return;}}catch(e){alt('响应JSON解析错误！\n'+x.responseText+'\n'+e);return;}f(s);}};x.send(p?JSON.stringify(p):null);}
function rnd(n,c){var s='';for(var i=0;i<(n||16);i++)s+='1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM'.substr(Math.floor((c?10:62)*Math.random()),1);return s;}
function num(n){var o=event.srcElement;if(o.tagName=='INPUT'&&(o.type=='text'||o.type=='number')){if(!n)n=0;var v=parseFloat(o.value);if(!v)v=0;v=Math.round(v*Math.pow(10,n))/Math.pow(10,n);o.value=v;}}
function sck(n,v){var d=new Date();d.setTime(d.getTime()+1080000000);document.cookie=n+'='+encodeURI(v)+'; expires='+d.toGMTString();}
function gck(n){var c=document.cookie.split("; ");for(var i=0;i<c.length;i++){var t=c[i].split("=");if(t[0]==n)return decodeURI(t[1]);}return '';}
function css(n){for(var i=0;i<document.styleSheets.length;i++){var r=document.styleSheets[i].cssRules?document.styleSheets[i].cssRules:document.styleSheets[i].rules;for(var j=0;j<r.length;j++){if(r[j].selectorText==n)return r[j].style;}}}
function psz(o,s){var w=o.width;var h=o.height;if(w<h){h=h*s/w;w=s;}else{w=s*w/h;h=s;}o.width=w;o.height=h;o.style.margin=(s-h)/2+'px '+(s-w)/2+'px';}
function bsz(o,w,h){w=parseInt(w);h=parseInt(h)-4;var s=w>h?w:h;psz(o,s);}
function fio(m,n,o){if(o&&o!=1){o=o.split(',');for(var i=0;i<o.length;i++)$(o[i]).style.display='none';}o=o?1:0;$(m).style.display='block';if(o)msk.style.display='block';var c=Math.round(parseFloat($(m).style.opacity||(n>0?0:1))*100)/100;c+=0.1*n;if(c<=1&&c>0){$(m).style.opacity=c;if(o)msk.style.opacity=c/5;$(m).style.left=(pw-parseInt($(m).offsetWidth))/2+'px';$(m).style.top=(ph-parseInt($(m).offsetHeight))/2+20-c*20+'px';setTimeout('fio(\''+m+'\','+n+','+o+')',20);}if(c==0){$(m).style.display='none';if(o)msk.style.display='none';}}
function alt(n){tip.innerText=n;if(tip.style.display=='block')return;fio('tip',1);setTimeout(function(){fio('tip',-1);},2000);}
function cfm(n,s,c){var m=msk.style.display=='block'?false:1;msk.style.zIndex=97;cfn.innerHTML=n.replace(/\n/gi,'<br>')+'<div style="text-align:right;"><br><span id="cfc" class="bn0">取消</span>　<span id="cfs" class="bn1">确定</span></div>';cfc.addEventListener('click',function(){fio('cfn',-1,m);msk.style.zIndex=9;if(c)c();});cfs.addEventListener('click',function(){fio('cfn',-1,m);msk.style.zIndex=9;s();});fio('cfn',1,m);}
//----------------------------------------------------------------
var uri='guest1.php';
var nid='<?php echo Req("i");?>';
var tik='<?php echo Req("t");?>';
var Ct=['空桌待客','顾客入座','申请开台','顾客下单','顾客退单','厨房退单','提交厨房','厨房接收','制作完成','上菜送餐','顾客用餐','申请结账','结账完成','打扫清台','退单完成','顾客呼叫'];
var Sc,Sb,Sd,Mc,Mb,Md,Ci,Cm,oid,tid,kid;
var pw,ph;
function rsz(){pw=parseInt(document.documentElement.clientWidth);ph=parseInt(document.documentElement.clientHeight);if(pw>360)pw=360;msk.style.height=ph+'px';Di.style.height=ph+'px';Fi.style.height=ph-20+'px';Fd_l.style.height=ph-172+'px';AD.style.top=(ph-480)/2+'px';setTimeout('rsz()',500);}
function jsn(x){return {act:x,nid:nid,rnd:rnd(6)};}
function cfg(){xml(uri,jsn('cfg'),function(r){Sc=r.Sc;Sb=r.Sb;Sd=r.Sd;document.title=r.Sn+' - 手机点单';Sm.innerText=r.Sn;Sn.innerText=r.Sn;if(gck('Jir_PayChk')==''){Ab.innerHTML=Sc.i[15].replace(/\&quot;/gi,'"');Aa.style.display=Sc.i[15]==''?'none':'block';Fs_u.value=gck('Jir_MbrUsr');Fs_p.value=gck('Jir_MbrUpw');Fs_a.checked=gck('Jir_MbrAtl')=='1';AD.innerHTML=Sc.i[16].replace(/\&quot;/gi,'"')+'<div style="font-size:12px;text-align:center;" onclick="AD.style.display=\'none\';">关闭</div>';AD.style.display=Sc.i[16].indexOf(' on ')<0?'none':'block';}Lb_0.style.height=ph-(Sc.i[15]==''?100:124)+'px';Ld.style.height=ph-(Sc.i[15]==''?100:124)+'px';fcz(tik,1);});}
//选桌
function fcz(n,m){var p=jsn('fcz');p.Tk=n;p.Ta=Sc.i[2];xml(uri,p,function(r){tid=r.ID*1;oid=r.Ti*1;Tn.innerText=r.Tn;if(r.Ts*1==0){oid=0;Mb=[];Md=[];Mc=[];Fs.style.display='none';Fd.style.display='none';}if(r.Ts*1==0||r.Ts*1==2){Di.style.display='block';if(m==1&&Sc.i[2]*1==0){cfm('本桌尚未开台，是否呼叫服务员开台？',function(){khj(2-Sc.i[2]*1);});}setTimeout('fcz(\''+n+'\',2)',Sc.i[2]*1==0?5000:500);}else{if(m){var d=gck('Jir_PreOdr');if(d!=''){Mc=JSON.parse(d);sck('Jir_PreOdr','');alt('您已在等待期间预点了餐品，请确认后下单！');fct(1);}else Mc=[];fct();fcb(0);}Di.style.display='none';setTimeout('fcz(\''+n+'\')',5000);}});}
//已点
function fct(n){if(oid==0){Md=[];fds();return;}var p=jsn('fds');p.oid=oid;xml(uri,p,function(r){Mb=r.Mb;Md=r.Md;var c=0;var s=0;for(var j=0;j<Md.length;j++){c+=Md[j].Fc*(Md[j].Fr*1==2?0:1);s+=Md[j].Fm*Md[j].Fc*(Md[j].Fr*1==2?0:1);}Dm.innerText=s/100;Dn.innerText=c;if(n)fds(n);kjc(0);if(document.URL.indexOf('#ewm')>0){setTimeout('kjs()',300);}});}
//菜单
function fcb(n){var s='';for(var i=0;i<Sb.length;i++){if(Sb[i].Bp*1==n)s+='<div id="Db_'+Sb[i].ID+'" class="bl0" onClick="fcb('+Sb[i].ID+');">'+Sb[i].Bn+'</div><div id="Lb_'+Sb[i].ID+'" style="margin-left:6px;"></div>';}$('Lb_'+n).innerHTML=$('Lb_'+n).innerHTML==''?s:'';n?fcd(n):fcb(Sb[0].ID);}
//菜类
function fcd(n,m){for(var i=0;i<Sb.length;i++){if($('Db_'+Sb[i].ID))$('Db_'+Sb[i].ID).className='bl0';}$('Db_'+n).className='bl1';var s='';for(var i=0;i<Sd.length;i++){if(Sd[i].Dc.indexOf(','+n+',')>-1)s+='<div class="dl0"><div class="dl1" onclick="fci('+i+');"><img src="images/'+(Sd[i].Di*1==1?(nid+'/c'+Sd[i].ID+'.jpg'):'none.jpg')+'" onload="psz(this,256);"></div>'+Sd[i].Dn+'<br>￥ <span class="dl3">'+Sd[i].Dm/100+'</span><div class="dl2" onclick="fca('+i+');">+</div></div>';}Ld.innerHTML=s;}
//大图
function fci(n){if(Sd[n].Di*1==0)return;fio('Fi',1,1);Fi_t.style.display='block';Fi_t.innerHTML='<span style="font-size:20px;font-weight:bold;">'+Sd[n].Dn+'</span>'+(Sd[n].Ds==''?'':('<br>　　'+Sd[n].Ds));Fi.innerHTML='<img src="images/'+nid+'/c'+Sd[n].ID+'.jpg" onload="bsz(this,300,Di.style.height);">';}
//点单
function fca(n){if(Sd[n].Dk.split(',').length>1){fws(Sd[n].Dn,-n-1,Sd[n].Dk);}else fce(n,'');}
function fce(n,k){var c=0;for(var i=0;i<Mc.length;i++){if(Mc[i][0]==Sd[n].ID&&Mc[i][5]==k){Mc[i][3]++;c=1;}}if(!c)Mc.push([Sd[n].ID,Sd[n].Dn,Sd[n].Dm,1,Sd[n].Di,k]);var c=0;var s=0;for(var i=0;i<Mc.length;i++){s+=Mc[i][2]*Mc[i][3];c+=Mc[i][3]*1;}fds();}
//点单口味
function fws(o,n,w){var k;if(n>-1){var m=Mc[n][0];for(var i=0;i<Sd.length;i++){if(m*1==Sd[i].ID*1)k=Sd[i].Dk;}if(!k)return;}else k=w;var s='';var t=k.split(',');for(var j=0;j<t.length;j++)s+='<span class="kw'+((','+Mc[n]).indexOf(','+t[j]+',')>-1?0:1)+'" onclick="fwc(this);">'+t[j]+'</span>';Fk_s.innerHTML=s;Fk_i.value=n;Fk_w.value=n<0?'':Mc[n][5];Fk_n.innerText=o;fio('Fk',1,1);}
//口味选择
function fwc(o){if(o.className=='kw1'){o.className='kw0';Fk_w.value+=(o.innerText+',');}else{o.className='kw1';Fk_w.value=(','+Fk_w.value).replace(','+o.innerText+',',',').substr(1);}}
//已点
function fds(n){if(Mc.length==0&&Md.length==0&&Fd.style.display=='block'){fio('Fd',-1);}var s='';var c=0;var m=0;for(var i=0;i<Mc.length;i++){s+='<div class="sl1"><span class="sl2">'+Mc[i][1]+'<span class="sl6" onclick="fws(\''+Mc[i][1]+'\','+i+');">['+(Mc[i][5]==''?'口味':Mc[i][5].substr(0,Mc[i][5].length-1))+']</span></span><span class="sl3">'+Mc[i][2]/100+'元</span><span class="sl4" onclick="fdc('+i+',-1,'+Mc[i][0]+',\''+Mc[i][5]+'\');">-</span><span id="Fk_c'+i+'" class="sl5">'+Mc[i][3]+'</span><span class="sl4" onclick="fdc('+i+',1,'+Mc[i][0]+',\''+Mc[i][5]+'\');">+</span></div>';m+=Mc[i][2]*Mc[i][3];c+=Mc[i][3]*1;}for(var i=0;i<Md.length;i++){if(Md[i].Fr!='2'){s+='<div class="sl1"><span class="sl7">'+Md[i].Fn+'<span class="sl6">'+(Md[i].Fb==''?'':('['+Md[i].Fb.substr(0,Md[i].Fb.length-1)+']'))+'</span></span><span class="sl8">'+Ct[Md[i].Fs]+'</span><span class="sl3">'+Md[i].Fm/100+'元</span><span class="sl5">×'+Md[i].Fc+'</span><span class="sl9" onclick="ktd('+Md[i].ID+');">退</span></div>';m+=Md[i].Fm*Md[i].Fc;c+=Md[i].Fc*1;}}Fd_l.innerHTML=s;if(n&&(Mc.length>0||Md.length>0)){fio('Fd',1);}Dm.innerText=m/100;Dn.innerText=c;}
//数量
function fdc(o,n,r,k){var m=$('Fk_c'+o).innerText*1;m+=n;$('Fk_c'+o).innerText=m;Mc[o][3]=m;if(m==0){for(var i=0;i<Mc.length;i++){if(Mc[i][0]*1==r*1&&Mc[i][5]==k)Mc.splice(i,1);}}fds();}

//下单
function kxd(){cfm('请核对菜品及数量无误后下单！\n确认要下单吗？',function(){var t=[];for(var i=0;i<Mc.length;i++){t.push([Mc[i][0],Mc[i][3],Mc[i][5]]);}var p=jsn('kxd');p.oid=oid;p.tid=tid;p.Mc=t;p.Ob=Fd_b.value;xml(uri,p,function(r){oid=r.oid;Mc=[];sck('Jir_PreOdr','');alt('下单已成功！');fct(1);});});}
//退单
function ktd(n){cfm('确认要退单吗？',function(){var p=jsn('ktd');p.tid=tid;p.oid=oid;p.ID=n;xml(uri,p,function(r){alt('退单已提交，请等待服务员处理！');fds();});});}
//结账
function kjs(n){Fs_h.style.display='none';Fs_b.style.display='none';if(n||Mb.Ps>0)Fs_t.options[Mb.Pt].selected=true;Fs_t.disabled=Mb.Ps>0;Fs_m.innerText=Mb.Pm/100;Fs_s.innerText=Mb.Ps/100;var m=Mb.Pm*1-Mb.Pc*1;var d=Math.round(m-m*Sc.c[0][Fs_t.value]);Fs_d.innerText=(d+Mb.Pc*1)/100;m=m-Mb.Ps-d;if(m<0)m=0;Fs_n.innerText=m/100;if(Fs_t.value*1==4){Fs_b.style.display='block';kju();}fio('Fs',1,1);}
//会员
function kju(n){Fs_h.style.display='none';Fs_b.style.display='block';Ci=0;Cm=0;var p=jsn('kju');p.Uc=Fs_u.value;p.Up=Fs_p.value;if(p.Up=='')return;xml(uri,p,function(r){if(Fs_a.checked){sck('Jir_MbrUsr',Fs_u.value);sck('Jir_MbrUpw',Fs_p.value);sck('Jir_MbrAtl','1');}var s='';for(var i=0;i<r.Us.length;i++)s+='<br><span><input type="radio" name="Fs_c"'+((Fs_m.innerText*100>=r.Us[i].Cn)?'':' disabled')+' onclick="Ci='+r.Us[i].ID+';Cm='+r.Us[i].Cm+';"> '+(r.Us[i].Cm/100)+'元 [满'+(r.Us[i].Cn/100)+'元可用，有效期至：'+r.Us[i].Cd+']</span>';Fs_b.style.display='none';Fs_h.style.display='block';Fs_h.innerHTML='卡号：'+Fs_u.value+'　额度：'+(r.Um/100)+'元　[<a href="javascript:void(0);" onclick="sck(\'Jir_MbrUpw\',\'\');Fs_p.value=\'\';Fs_h.style.display=\'none\';Fs_b.style.display=\'block\';Ci=0;Cm=0;">退出</a>]'+s;});}
//结账
function kjz(m,n){if(Fs_n.innerText*1<=0){alt('款已结清，无需支付！');return;}var b=navigator.userAgent.toLowerCase();if(b.indexOf('micromessenger')>0&&n==3){alt('您正在使用微信，无法调用支付宝。');return;}if((b.indexOf('aliapp')>0||b.indexOf('alipay')>0)&&n==2){alt('您正在使用支付宝，无法调用微信。');return;}var n=Fs_t.value*1;if(n==4){kjm();return;}var p=jsn('kjz');p.tid=tid;p.oid=oid;p.Ps=Mb.Ps*100;p.Pn=Fs_n.innerText*100;p.Pd=Fs_d.innerText*100;p.Pt=n;xml(uri,p,function(r){if(n>1){sck('Jir_PayChk',1);alert('支付订单已生成，请务必在支付完成后重新进入商家页面同步支付结果！');window.location='mobile.php?o='+r.odr+'&r=guest1%2Ephp%3Fi%3D'+nid+'%26t%3D'+tik;}else alt('您的结账申请已发出，请稍候......');});}
//查询
function kjc(n){clearTimeout(kid);if(gck('Jir_PayChk')=='')return;fio('Fc',1,1);Fc_t.innerText='正在查询支付结果并同步数据，请稍候......';var p=jsn('kjc');p.tid=tid;p.oid=oid;xml(uri,p,function(r){if(r.pay==1){Fc_t.innerText='手机自助支付已成功！';sck('Jir_PayChk','');fct(1);}else{if(n<5){n++;kid=setTimeout('kjc('+n+')',5000);}else Fc_t.innerText='未查询到支付结果！如未支付，请重新支付！如已支付，请联系服务员！';}});}
//卡付
function kjm(){if(Fs_h.style.display=='none'){alt('请先登录会员！');return;}var p=jsn('kjm');p.oid=oid;p.tid=tid;p.Uc=Fs_u.value;p.Up=Fs_p.value;p.Ci=Ci;p.Ps=Fs_n.innerText*100;xml(uri,p,function(r){alt('自助支付结账成功！');fio('Fs',-1,1);fct(1);});}
//呼叫
function khj(n){var p=jsn('khj');p.tid=tid;p.Ts=n;xml(uri,p,function(r){if(n!=1)alt('您的服务请求已发出，请稍候......');});}
</script>
</head>
<body onLoad="rsz();cfg();">
<div id="msk"></div><div id="tip"></div><div id="cfn"></div>
<!--欢迎-->
<div id="Di" style="position:absolute;z-index:88;width:360px;height:560px;background-color:#EEE;background-image:url(images/back.jpg);background-position:center;">
  <div style="position:absolute;width:320px;top:40px;left:20px;font-size:24px;color:#FFF;text-shadow:#999 3px 3px 3px;text-align:center;"><img src="images/icon.png" width="96" height="96"><br>欢迎光临<br><span id="Sm"></span></div>
  <div style="position:absolute;left:80px;bottom:40px;width:198px;line-height:40px;border:1px solid #FFF;border-radius:7px;background-color:#09F;text-align:center;color:#FFF;font-size:20px;" onClick="fcz(tik,1);">进入点餐</div>  
</div>
<!--顶部-->
<div style="position:absolute;z-index:1;top:0px;left:0px;width:360px;height:50px;background-color:#666;">
  <div style="float:left;margin:3px;width:44px;height:44px;"><img src="images/icon.png" width="44" height="44"></div>
  <div style="float:left;color:#FFF;font-size:18px;line-height:20px;margin-top:6px;"><span id="Sn"></span><br><span id="Tn" style="font-size:14px;"></span></div>
  <div style="float:right;width:45px;line-height:30px;text-align:center;margin:10px 5px;background-color:#F90;color:#FFF;" onClick="if(Fd.style.display=='none'){fct(1);}if(Mc.length){alt('您有菜品未下单，请先下单或删除未下单的菜品！');return false;}if(Md.length>0)kjs(1);">结账</div>
  <div style="float:right;width:45px;line-height:30px;text-align:center;margin:10px 5px;background-color:#F90;color:#FFF;" onClick="window.location='weixin.php';">会员</div>
  <div style="float:right;width:45px;line-height:30px;text-align:center;margin:10px 5px;background-color:#F90;color:#FFF;" onClick="cfm('您是否要呼叫服务员？',function(){khj(15);});">呼叫</div>
</div>
<!--底部-->
<div style="position:absolute;z-index:2;left:0px;bottom:0px;line-height:50px;width:360px;height:50px;font-size:18px;color:#FFF;background-color:#333;clear:both;">
  <div style="float:left;width:50px;height:50px;background-image:url(images/cart.png);line-height:18px;margin-right:10px;" onClick="fds(1);"><span id="Dn" style="font-size:12px;display:inline-block;background-color:#F90;color:#FFF;height:18px;width:18px;text-align:center;margin:3px;border-radius:3px;">0</span></div>
  <div style="float:right;width:90px;background-color:#F90;color:#FFF;text-align:center;font-size:24px;" onClick="if(Mc.length>0||oid){if(Fd.style.display=='none'){fds(1);}else{if(Mc.length>0)kxd();}}">下 单</div><span id="Dm" style="font-size:32px;color:#F90;">0</span> 元
</div>
<!--菜单-->
<div style="position:absolute;z-index:3;top:50px;left:0px;width:360px;">
  <div id="Aa" style="width:360px;height:24px;background-color:#F90;color:#FFF;top:50px;border-radius:12px;display:none;"><div style="float:right;width:20px;text-align:center;" onClick="Aa.style.display='none';Lb_0.style.height=ph-100+'px';Ld.style.height=ph-102+'px';">×</div><marquee style="float:left;width:320px;margin:0px 10px;" id="Ab"></marquee></div>
  <div id="Lb_0" style="float:left;border-right:1px solid #F90;width:85px;overflow:auto;"></div>
  <div id="Ld" style="float:left;width:274px;overflow:auto;">正在加载，请稍候......</div>
</div>
<!--大图-->
<div id="Fi_t" style="position:absolute;width:320px;left:20px;bottom:20px;color:#FFF;z-index:12;display:none;font-size:14px;text-shadow:2px 2px 2px #666;"></div>
<div id="Fi" style="position:absolute;width:340px;left:10px;top:10px;background-color:#FFF;overflow:auto;z-index:11;display:none;" onClick="fio('Fi',-1,'Fi_t');"></div>
<!--口味-->
<div id="Fk" class="fld" style="width:320px;height:212px;z-index:13;">
  <div class="flt"><div class="bnc" onClick="fio('Fk',-1,1);">×</div>选择口味</div>
  <div class="flb" style="height:128px;">菜品： <span id="Fk_n"></span><br><span id="Fk_s"></span></div>
  <div class="flk"><input type="hidden" id="Fk_i"><input type="hidden" id="Fk_w"><span class="btn" onClick="if(Fk_i.value*1>-1){Mc[Fk_i.value][5]=Fk_w.value;fds();}else fce(-1*Fk_i.value-1,Fk_w.value);fio('Fk',-1,1);if(Fd.style.display!='none')fds();">确定</span></div>
</div>
<!--点单-->
<div id="Fd" style="position:absolute;z-index:4;top:55px;left:0px;width:348px;border:1px solid #CCC;background-color:#FFF;display:none;">
  <div style="margin:0px 5px;line-height:39px;border-bottom:1px solid #CCC;text-align:center;font-size:18px;"><div style="float:right;width:20px;line-height:20px;text-align:center;font-size:18px;border:1px solid #999;border-radius:6px;margin-top:8px;color:#999;" onClick="fio('Fd',-1);">×</div>已点餐品</div>
  <div id="Fd_l" style="overflow:auto;"></div>
  <div style="line-height:25px;text-align:center;font-size:12px;">备注：<input type="text" id="Fd_b" style="width:290px;font-size:12px;"></div>
</div>
<!--结账-->
<div id="Fs" class="fld" style="width:320px;z-index:31;">
  <div class="flt"><div class="bnc" onClick="fio('Fs',-1,1);">×</div>结账支付</div>
  <div class="flb" style="height:300px;overflow:auto;">
    餐费： <span id="Fs_m" class="dje"></span> 元　优惠： <span id="Fs_d" class="dje"></span> 元<br>已付： <span id="Fs_s" class="dje"></span> 元　应付： <span id="Fs_n" class="dje"></span> 元<br>支付方式： <select id="Fs_t" onChange="kjs();"><option value="0">现金支付</option><option value="1">刷卡支付</option><option value="2">微信支付</option><option value="3">支付宝支付</option><option value="4">会员卡支付</option></select>
    <div id="Fs_b" style="display:none;">卡号： <input type="text" id="Fs_u" style="width:120px;"> <input type="checkbox" id="Fs_a"> 自动<br>密码： <input type="password" id="Fs_p" style="width:120px;"> <span class="btn" onClick="kju(1);">登录</span></div>
    <div id="Fs_h" style="font-size:12px;display:none;"></div>
  </div>
  <div class="flk"><span class="btn" onClick="kjz();">确定支付</span></div>
</div>
<!--查询-->
<div id="Fc" class="fld" style="width:320px;z-index:32;">
  <div class="flb" id="Fc_t"></div>
  <div class="flk"><span class="btn" onClick="clearTimeout(kid);sck('Jir_PayChk','');fio('Fc',-1,1);">返回</span></div>
</div>
<!--广告-->
<div id="AD" style="position:absolute;width:320px;height:480px;z-index:33;left:20px;background-color:#FFF;display:none;" onClick="AD.style.display='none';"></div>
</body>
</html>
<?php
}

?>
