<?php
//====== 点单管理 ======
require "common.php";
$m=file_get_contents("php://input");if($m==""||Req("act")=="index")$m='{"act":"index"}';$m=Dec($m,true);
if($m["act"]!="index"){
	$r=Qra(Qry("Select Ar,An From ".$pre."admin Where Ah=0 And ID=".$m["uid"]." And Si=".$m["nid"]." And Ak='".$m["sid"]."'"));
	if(!$r||$m["sid"]=="")die('{"msg":"连接超时，请重登录！"}');
	if(strpos($r["Ar"],"A")===false&&strpos($r["Ar"],"B")===false)die('{"msg":"无此权限！"}');
	$nid=$m["nid"];$uid=$m["uid"];$rid=$r["Ar"];$adm=$r["An"];
}
switch($m["act"]){
	case "cfg"://设置读取
	$p=["msg"=>"ok","Sb"=>[],"Sd"=>[],"Sc"=>[]];
	$q=Qry("Select ID,Bn,Bh,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 From ".$pre."shop Where Si=".$nid));
	$p["Sc"]=Dec($r["Sc"]);$p["Sn"]=$r["Sn"];$p["rid"]=$rid;$p["An"]=$adm;$p["vid"]=md5($m["sid"]);
	echo Enc($p);
	break;

	case "lst"://大厅列表
	$p=["msg"=>"ok","Ts"=>[],"Ks"=>[],"Rt"=>date("Y-m-d H:i:s")];
	$q=Qry("Select ID,Tn,Tc,Tu,Ts,Ti,Tm From ".$pre."table Where Si=".$nid." And Th=0 Order By Tr Asc");
	while($r=Qra($q))$p["Ts"][]=$r;
	$q=Qry("Select ID,Pm,Os,Od,Ox From ".$pre."order Where Si=".$nid." And Ot=0 And Os<13 Order By ID Desc");
	while($r=Qra($q))$p["Ks"][]=$r;
	echo Enc($p);
	break;

	case "lsi"://点单详情
	$p=["msg"=>"ok","Mb"=>[],"Md"=>[]];$c=1;$d=0;
	$r=Qra(Qry("Select ID,Ot,O1,O4,Ox,Pm,Pd,Pc,Ps,Pt,Os,Od,Ob From ".$pre."order Where Si=".$nid." And ID=".$m["oid"]));
	if($r){
		$p["Mb"]=$r;$c=$r["Os"];$d=$r["Ot"];
		$q=Qry("Select ID,Fo,Ft,Fi,Fn,Fm,Fc,Fs,F1,F2,F3,Fd,Fr,Fb From ".$pre."ofood Where Si=".$nid." And Fo=".$m["oid"]." Order By Fr Asc");
		while($r=Qra($q))$p["Md"][]=$r;
	}
	Qry("Update ".$pre."table Set Ts=0 Where Si=".$nid." And Ts=1 And ID=".$d);
	Qry("Update ".$pre."table Set Ts=".$c." Where Si=".$nid." And Ts>5 And ID=".$d);
	echo Enc($p);
	break;

	case "trz"://正餐入座
	Qry("Update ".$pre."table Set Ts=1,Tu=".$m["Tu"]." Where Si=".$nid." And Ts<3 And ID=".$m["Ot"]);
	Qry("Update ".$pre."table Set Ta=".$uid." Where Si=".$nid." And ID=".$m["Ot"]);
	echo '{"msg":"ok"}';
	break;

	case "txd"://正餐下单
	if($m["oid"]*1>0){
		Qry("Update ".$pre."order Set Ot=".$m["Ot"].",O1=".$m["uid"].",Os=3,Ob='".$m["Ob"]."' Where Si=".$nid." And ID=".$m["oid"]);
	}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["Ot"].",".$m["uid"].",0,0,0,0,0,0,0,3,Now(),Now(),'".$m["Ob"]."','','')");
		$m["oid"]=Qri();
	}
	for($i=0;$i<count($m["Mc"]);$i++){
		$c=$m["Mc"][$i];
		$r=Qra(Qry("Select Dn,Dm,Dt From ".$pre."foods Where Si=".$nid." And ID=".$c[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.",".$m["oid"].",".$m["Ot"].",".$c[0].",'".$r["Dn"]."',".$r["Dm"].",".$c[1].",3,".$uid.",0,0,Now(),0,'".$c[2]."')");
		}else{
			Qry("Insert Into ".$pre."ofood (Si,Fo,Ft,Fi,Fn,Fm,Fc,Fs,F1,F2,F3,Fd,Fr,Fb) Values (".$nid.",".$m["oid"].",".$m["Ot"].",".$c[0].",'".$r["Dn"]."',0,".$c[1].",3,".$uid.",0,0,Now(),0,'".$c[2]."')");
			$e=explode(",",$r["Dt"]);
			$f=$r["Dm"]/Qrs(Qry("Select Sum(Ns) From ".$pre."foods Where Si=".$nid." And ID In (".$r["Dt"].")"));
			$d=0;
			for($j=1;$j<count($e);$j++){
				$k=Qra(Qry("Select ID,Dn,Dm From ".$pre."foods Where Si=".$nid." And ID=".$e[$j]));
				$d+=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.",".$m["oid"].",".$m["Ot"].",".$k["ID"].",'".$k["Dn"]."[T]',".round($k["Dm"]*$f).",".$c[1].",3,".$m["uid"].",0,0,Now(),0,'".$c[2]."')");
			}
			Qry("Update ".$pre."ofood Set Fm=Fm+".($r["Dm"]-$d)." Where Si=".$nid." And ID=".Qri());
		}
	}
	$p=0;
	$q=Qry("Select Fm,Fc From ".$pre."ofood Where Si=".$nid." And Fr<2 And Fo=".$m["oid"]);
	while($r=Qra($q))$p+=$r["Fm"]*$r["Fc"];
	Qry("Update ".$pre."order Set Pm=".$p." Where Si=".$nid." And ID=".$m["oid"]);
	Qry("Update ".$pre."table Set Ts=3,Ti=".$m["oid"].",Tm=".$p." Where Si=".$nid." And ID=".$m["Ot"]);
	echo '{"msg":"ok"}';
	break;

	case "txc"://正餐改单
	Qry("Update ".$pre."ofood Set Fc=".$m["Fc"].",Fm=".$m["Fm"].",Fb='".$m["Fb"]."' Where Si=".$nid." And ID=".$m["ID"]);
	$q=Qry("Select Ft,Fm,Fc From ".$pre."ofood Where Si=".$nid." And Fr<2 And Fo=".$m["oid"]);
	$b=0;while($r=Qra($q)){$b+=$r["Fm"]*$r["Fc"];$c=$r["Ft"];}
	Qry("Update ".$pre."order Set Pm=".$b.",Os=6 Where Si=".$nid." And ID=".$m["oid"]);
	Qry("Update ".$pre."table Set Tm=".$b.",Ts=6 Where Si=".$nid." And ID=".$c);
	echo '{"msg":"ok"}';
	break;

	case "txr"://正餐退单
	Qry("Update ".$pre."ofood Set Fr=".$m["Fr"].",Fb='".$m["Fb"]."' Where Si=".$nid." And ID=".$m["ID"]);
	$q=Qry("Select Fm,Fc From ".$pre."ofood Where Si=".$nid." And Fr<2 And Fo=".$m["oid"]);
	$b=0;while($r=Qra($q))$b+=$r["Fm"]*$r["Fc"];
	Qry("Update ".$pre."order Set Os=6,Pm=".$b." Where Si=".$nid." And ID=".$m["oid"]);
	Qry("Update ".$pre."table Set Ts=6,Tm=".$b." Where Si=".$nid." And ID=".$m["Ot"]);
	echo '{"msg":"ok"}';
	break;

	case "thz"://正餐换桌
	if($m["Hz"]==1){
		if($m["Ot"]==$m["Nt"])die('{"msg":"ok"}');
		$o=Qra(Qry("Select Ti,Tu,Ts,Tm From ".$pre."table Where Si=".$nid." And ID=".$m["Ot"]));
		$n=Qra(Qry("Select Ti,Tu,Ts,Tm From ".$pre."table Where Si=".$nid." And ID=".$m["Nt"]));
		if($o["Ti"]==0){$r=["Pm"=>0,"Pd"=>0,"Pc"=>0,"Ps"=>0];}else $r=Qra(Qry("Select Pm,Pd,Pc,Ps From ".$pre."order Where Si=".$nid." And ID=".$o["Ti"]));
		if($n["Ti"]==0){Qry("Update ".$pre."order Set Ot=".$m["Nt"]." Where Si=".$nid." And ID=".$o["Ti"]);}else{
			Qry("Update ".$pre."order Set Ot=".$m["Nt"].",Pm=Pm+".$r["Pm"].",Pd=Pd+".$r["Pd"].",Pc=Pc+".$r["Pc"].",Ps=Ps+".$r["Ps"]." Where Si=".$nid." And ID=".$n["Ti"]);
			Qry("Delete From ".$pre."order Where Si=".$nid." And ID=".$o["Ti"]);
		}
		Qry("Update ".$pre."ofood Set Ft=".$m["Nt"].",Fo=".($n["Ti"]==0?$o["Ti"]:$n["Ti"])." Where Si=".$nid." And Ft=".$m["Ot"]." And Fo=".$o["Ti"]);
		Qry("Update ".$pre."table Set Ts=".($o["Ts"]<5?$n["Ts"]:($o["Ts"]<$n["Ts"]?$o["Ts"]:$n["Ts"])).",Tu=Tu+".$o["Tu"].",Ti=".($n["Ti"]==0?$o["Ti"]:$n["Ti"]).",Tm=Tm+".$o["Tm"]." Where Si=".$nid." And ID=".$m["Nt"]);
		Qry("Update ".$pre."table Set Ts=0,Tu=0,Ti=0,Tm=0 Where Si=".$nid." And ID=".$m["Ot"]);
		echo '{"msg":"ok"}';
	}else{
		if(Qrs(Qry("Select Ts From ".$pre."table Where Si=".$nid." And ID=".$m["Nt"]))==0){
			$r=Qra(Qry("Select Ts,Tu,Ti,Tm From ".$pre."table Where Si=".$nid." And ID=".$m["Ot"]));
			Qry("Update ".$pre."order Set Ot=".$m["Nt"]." Where Si=".$nid." And Ot=".$m["Ot"]." And ID=".$r["Ti"]);
			Qry("Update ".$pre."ofood Set Ot=".$m["Nt"]." Where Si=".$nid." And Ot=".$m["Ot"]." And Fo=".$r["Ti"]);
			Qry("Update ".$pre."table Set Ts=".$r["Ts"].",Tu=".$r["Tu"].",Ti=".$r["Ti"].",Tm=".$r["Tm"]." Where Si=".$nid." And ID=".$m["Nt"]);
			Qry("Update ".$pre."table Set Ts=0,Tu=0,Ti=0,Tm=0 Where Si=".$nid." And ID=".$m["Ot"]);
			echo '{"msg":"ok"}';
		}else echo '{"msg":"换桌失败，目标桌非空桌！"}';
	}
	break;

	case "tjs"://正餐结账
	if(Qrs(Qry("Select Ts From ".$pre."table Where Si=".$nid." And ID=".$m["Ot"]))==12)die('{"msg":"ok"}');
	Qry("Update ".$pre."order Set Ps=Ps+".$m["Ps"].",Pc=".$m["Pc"].",Pd=Pm-Pc-Ps,Pt='".$m["Pt"]."',Ob='".$m["Ob"]."',Oe=Now(),O4=".$uid.",Os=12,Oo='' Where Si=".$nid." And Ot=".$m["Ot"]." And ID=".$m["oid"]);
	Qry("Update ".$pre."table Set Ts=12 Where Si=".$nid." And ID=".$m["Ot"]);
	echo '{"msg":"ok"}';
	break;

	case "txu"://正餐清台
	Qry("Update ".$pre."table Set Ts=0,Tu=0,Ti=0,Tm=0,Ta=0 Where Si=".$nid." And Ts In (0,1,2,12) And ID=".$m["Ot"]);
	Qry("Update ".$pre."order Set St=13 Where Si=".$nid." And ID=".$m["oid"]);
	Qry("Update ".$pre."ofood Set St=10 Where Si=".$nid." And Fo=".$m["oid"]." And Ft=".$m["Ot"]);
	echo '{"msg":"ok"}';
	break;

	case "kxb"://快餐预单
	$d=Enc(["Bz"=>$m["Bz"],"Mc"=>$m["Mc"],"Pf"=>0]);
	$q=Qry("Select ID From ".$pre."order Where Si=".$nid." And Ou='".$m["Ou"]."'");
	if(Qrc($q)==0){
		Qry("Insert Into ".$pre."order (Si,Ot,O1,O4,Ox,Pm,Pd,Pc,Ps,Pt,Os,Od,Oe,Ob,Ou,Oo) Values (".$nid.",0,".$m["uid"].",0,1,".$m["Pm"].",".$m["Pd"].",".$m["Pc"].",0,".$m["Pt"].",3,Now(),Now(),'".$d."','".$m["Ou"]."','')");
		$c=Qri();
	}else{
		Qry("Update ".$pre."order Set Ot=0,O1=".$m["uid"].",Ob='".$d."',Pm=".$m["Pm"].",Pd=".$m["Pd"].",Pc=".$m["Pc"].",Ps=0,Pt=".$m["Pt"]." Where Si=".$nid." And Ou='".$m["Ou"]."'");
		$c=Qrs($q);
	}
	echo '{"msg":"ok","oid":"'.$c.'"}';
	break;

	case "kxt"://快餐下单
	$r=Qra(Qry("Select Os,Ob,Ox From ".$pre."order Where Si=".$nid." And ID=".$m["oid"]));
	if($r["Os"]==12)die('{"msg":"ok"}');
	$b=Dec($r["Ob"]);$h=$b["Bz"];$v=$r["Ox"]*1;
	for($i=0;$i<count($b["Mc"]);$i++){
		$c=$b["Mc"][$i];
		$r=Qra(Qry("Select Dn,Dm,Dt From ".$pre."foods Where Si=".$nid." And ID=".$c[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.",".$m["oid"].",0,".$c[0].",'".$r["Dn"]."',".$r["Dm"].",".$c[1].",3,".$uid.",0,0,Now(),0,'".$c[2]."')");
		}else{
			Qry("Insert Into ".$pre."ofood (Si,Fo,Ft,Fi,Fn,Fm,Fc,Fs,F1,F2,F3,Fd,Fr,Fb) Values (".$nid.",".$m["oid"].",0,".$c[0].",'".$r["Dn"]."',0,".$c[1].",3,".$uid.",0,0,Now(),0,'".$c[2]."')");
			$e=explode(",",$r["Dt"]);
			$f=$r["Dm"]/Qrs(Qry("Select Sum(Ns) From ".$pre."foods Where Si=".$nid." And ID In (".$r["Dt"].")"));
			$d=0;
			for($j=1;$j<count($e);$j++){
				$k=Qra(Qry("Select ID,Dn,Dm From ".$pre."foods Where Si=".$nid." And ID=".$e[$j]));
				$d+=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.",".$m["oid"].",0,".$k["ID"].",'".$k["Dn"]."[T]',".round($k["Dm"]*$f).",".$c[1].",3,".$m["uid"].",0,0,Now(),0,'".$c[2]."')");
			}
			Qry("Update ".$pre."ofood Set Fm=Fm+".($r["Dm"]-$d)." Where Si=".$nid." And ID=".Qri());
		}
	}
	if($b["Pf"]>0)Qry("Insert Into ".$pre."ofood (Si,Fo,Ft,Fi,Fn,Fm,Fc,Fs,F1,F2,F3,Fd,Fr,Fb) Values (".$nid.",".$m["oid"].",0,0,'送餐费',".$b["Pf"].",1,12,0,0,0,Now(),0,'')");
	Qry("Update ".$pre."order Set Ps=".$m["Ps"].",Pd=Pm-Ps-Pc,Os=12,O4=".$uid.",Oe=Now(),Pt=".$m["Pt"].",Ob='".($v==1?($h["bz"]." 店内用餐<br>餐桌： ".$h["cz"]):($h["bz"]." 外卖送餐<br>地址： ".$h["dz"]."<br>送时： ".$h["sj"]))."<br>电话： ".$h["dh"]."||',Ou='".$h["dh"]."' Where Si=".$nid." And ID=".$m["oid"]);
	if($v==2){
		$c=["Fn"=>$h["fn"],"Fd"=>$h["fd"],"Fx"=>$h["fx"],"Fy"=>$h["fy"],"Tn"=>$h["dh"],"Td"=>$h["dz"],"Tx"=>$h["gx"],"Ty"=>$h["gy"]];
		if(Qrc(Qry("Select ID From ".$pre."osend Where Si=".$nid." And Ho=".$m["oid"]))==0)Qry("Insert Into ".$pre."osend (Si,Hu,Ho,Hd,Hm,Hc,Hs) Values (".$nid.",0,".$m["oid"].",'".Enc($c)."',".$b["Pf"].",'[".date("Y-m-d H:i:s")."] 点单已发布，等待接单中。',0)");
		$q=Qry("Select Aw,Ar,Ak From ".$pre."admin Where Si=".$nid);$e=[];
		while($r=Qra($q)){
			if($r["Aw"]!=""&&strpos($r["Ar"],"E")>-1&&$r["Ak"]!=""){
				$s='{"touser":"'.$r["Aw"].'","template_id":"'.$cfg["Ag"]["W1"].'","url":"'.$cfg["Aw"].'","data":{"first":{"value":"小哥！来订单啦~~~~","color":"#FF0000"},"keyword1":{"value":"'.$m["oid"].'","color":"#0099FF"},"keyword2":{"value":"'.$n["Sn"].'","color":"#0099FF"},"keyword3":{"value":"'.$h["dh"].'","color":"#0099FF"},"keyword4":{"value":"'.$h["dz"].'","color":"#0099FF"},"keyword5":{"value":"'.($b["Pf"]/100).'元","color":"#0099FF"},"remark":{"value":"处理订单请点击详情按钮。","color":"#000000"}}}';
				if($cfg["Ag"]["W1"]!="")$e[]=Cul("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".Tkn(),$s);
			}
		}
	}else $e="";
	echo Enc(["msg"=>"ok","err"=>$e]);
	break;

	case "kxr"://快餐退单
	Qry("Update ".$pre."ofood Set Fr=".$m["Fr"].",Fm=".$m["Fm"].",Fb='".$m["Fb"]."' Where Si=".$nid." And ID=".$m["ID"]);
	Qry("Update ".$pre."order Set Os=6 Where Si=".$nid." And ID=".$m["Fo"]);
	echo '{"msg":"ok"}';
	break;

	case "kxe"://快餐完成
	Qry("Update ".$pre."order Set Os=13 Where Si=".$nid." And ID=".$m["oid"]);
	Qry("Update ".$pre."ofood Set Fs=10 Where Si=".$nid." And Fo=".$m["oid"]);
	echo '{"msg":"ok"}';
	break;

	case "kwm"://外卖状态
	$a=Qra(Qry("Select Hu,Hd,Hm,Hc,Hs From ".$pre."osend Where Si=".$nid." And Ho=".$m["oid"]));
	if($a["Hu"]*1>0){$b=Qra(Qry("Select An,Am From ".$pre."admin Where Si=".$nid." And ID=".$a["Hu"]));$c=[$a["Hu"],$b["An"],$b["Am"]];}else $c=[0,'',''];
	$p=["msg"=>"ok","Hu"=>$c[0],"An"=>$c[1],"Am"=>$c[2],"Hd"=>Dec($a["Hd"]),"Hm"=>$a["Hm"],"Hc"=>$a["Hc"],"Hs"=>$a["Hs"]];
	echo Enc($p);
	break;

	case "kwf"://外卖重发
	Qry("Update ".$pre."osend Set Hu=0,Hs=0,Hc='[".date("Y-m-d H:i:s")."] 点单已发布，等待接单中。' Where Si=".$nid." And Hs<3 And Ho=".$m["oid"]);
	echo '{"msg":"ok"}';
	break;

	case "ctf"://提交厨房
	$p=["msg"=>"ok","Pb"=>[],"Pd"=>[],"err"=>""];$h="";
	$q=Qry("Select A.Db,B.Fc,A.Da,B.Fd,B.Fn,B.Fb,B.ID,B.Fs,A.Dv From ".$pre."foods As A,".$pre."ofood As B Where B.Si=".$nid." And A.ID=B.Fi And B.Fs<6 And B.Fo In (".$m["oid"].")");
	while($r=Qra($q)){
		if($r["Fs"]==3)$p["Pd"][]=$r["Dv"]."|spd|".$r["ID"];
		$n=explode(",",$r["Db"]);
		for($i=1;$i<count($n);$i++){
			$d=explode("=",$n[$i]);
			Qry("Update ".$pre."stock Set Ks=Ks-".($d[1]*$r["Fc"])." Where Si=".$nid." And ID=".$d[0]);
		}
		$d=Qry("Select Ak,Aw From ".$pre."admin Where Si=".$nid." And ID In (".$r["Da"].")");
		while($e=Qra($d)){
			if($e["Ak"]!=""&&$e["Aw"]!=""&&$m["Wx"]*1==0&&$cfg["Ag"]["W2"]!=""){
				$s='{"touser":"'.$e["Aw"].'","template_id":"'.$cfg["Ag"]["W2"].'","url":"'.$cfg["Aw"].'","data":{"first":{"value":"Hello, New Order~~~~","color":"#FF0000"},"keyword1":{"value":"'.$r["Fd"].'","color":"#0099FF"},"keyword2":{"value":"['.$r["ID"].']'.$r["Fn"].' × '.$r["Fc"].'","color":"#0099FF"},"keyword3":{"value":"'.$r["Fb"].'","color":"#0099FF"},"remark":{"value":"To process an order, click the Details button.","color":"#000000"}}}';
				$p["err"].=Cul("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".Tkn(),$s);
			}
		}
	}
	$q=Qry("Select ID,Ox From ".$pre."order Where Si=".$nid." And Ot=0 And ID In (".$m["oid"].")");
	while($r=Qra($q))$p["Pb"][]=$r;
	Qry("Update ".$pre."order Set Os=6 Where Si=".$nid." And ID In (".$m["oid"].")");
	Qry("Update ".$pre."ofood Set Fs=6 Where Si=".$nid." And Fs<6 And Fo In (".$m["oid"].")");
	Qry("Update ".$pre."table Set Ts=6 Where Si=".$nid." And Ti In (".$m["oid"].")");
	echo Enc($p);
	break;

	case "ewm"://支付
	if($m["TK"]==0)Qry("Update ".$pre."order Set Pd=".$m["Pd"].",Pc=".$m["Pc"].",Pt=".(3-$m["Pt"]).",Ob='".$m["Ob"]."' Where ID=".$m["oid"]);
	$o=$m["nid"]."-".$m["oid"]."-".Rnd(6);
	Qry("Insert Into ".$pre."pay (Mc,Mp,Md,Mt,Nc,Np,Nd,Nt) Values ('".$o."',".$m["Ps"].",Now(),".$m["Pt"].",'',0,Now(),0)");
	Qry("Update ".$pre."order Set Oo='".$o."' Where ID=".$m["oid"]);
	echo '{"msg":"ok","err":0,"odr":"'.$o.'","qrcode":"'.$cfg["Aw"].'mobile.php?o='.$o.'","money":'.$m["Ps"].',"type":'.$m["Pt"].'}';
	break;

	case "syn"://查询
	$o=Qrs(Qry("Select Oo From ".$pre."order Where ID=".$m["oid"]));
	$r=Qra(Qry("Select Nt,Np,Mc,Mt From ".$pre."pay Where Mc='".$o."'"));
	echo $r?'{"msg":"ok","pay":'.$r["Nt"].',"mny":'.$r["Np"].',"odr":"'.$r["Mc"].'","api":'.$r["Mt"].'}':'{"msg":"ok","pay":0}';
	break;
	
	case "snd"://声音
	echo Snd($m["snd"]);
	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;}
.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;}
.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;}
.bnx {float:right;line-height:22px;text-align:center;width:22px;border:1px solid #F90;color:#F90;margin:4px 0px;}
.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;}
/*----------------------------------------------------------------*/
.tk0 {display:inline-block;line-height:30px;text-align:center;font-size:16px;padding:0px 10px;margin-top:9px;background-color:#F90;color:#FFF;border:1px solid #F90;}
.tk1 {display:inline-block;line-height:30px;text-align:center;font-size:16px;padding:0px 10px;margin-top:9px;background-color:#666;color:#F90;border:1px solid #F90;}
.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;}
.kw1 {display:inline-block;line-height:24px;border:1px dashed #F90;background-color:#FFFCF9;margin:3px;padding:0px 5px;text-align:center;}
.kw0 {display:inline-block;line-height:24px;border:1px dashed #F90;background-color:#F90;color:#FFF;margin:3px;padding:0px 5px;text-align:center;}
.sl1 {margin:2px 5px;border-bottom:1px dashed #CCC;line-height:36px;height:36px;font-size:14px;}
.sl2 {display:inline-block;width:234px;}
.sl3 {display:inline-block;width:40px;height:23px;font-size:12px;text-align:right;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:23px;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;}
.ksl {margin:5px;padding:5px 10px;background-color:#FFFCF9;border:1px dashed #F90;font-size:14px;line-height:28px;}
.ksl span{color:#09F;}
.tsl {margin:10px 5px;padding:0px 10px;border:1px dashed #CCC;line-height:40px;background-color:#EEF;color:#000;}
.tl1 {border-bottom:1px dashed #F90;line-height:32px;font-size:14px;}
.tl2 {float:right;width:40px;font-size:12px;text-align:center;}
.td0 {float:right;margin-top:8px;line-height:14px;text-align:center;border:1px solid #F90;background-color:#FFF;color:#F90;font-size:12px;}
.td1 {float:right;margin-top:8px;line-height:14px;text-align:center;border:1px solid #F90;background-color:#F00;color:#FFF;font-size:12px;}
.td2 {float:right;margin-top:8px;line-height:14px;text-align:center;border:1px solid #09F;background-color:#09F;color:#FFF;font-size:12px;}
</style>
<script src="images/spell.js" type="text/javascript"></script>
<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='waiter.php';
var nid='<?php echo Req("nid");?>';
var uid='<?php echo Req("uid");?>';
var sid='<?php echo Req("sid");?>';
var rid='';
var oid=0;
var tok=0;
var Sc,Sb,Sd,Ts,Ks;
var Mc=[];
var Mb=[];
var Md=[];
var Ct=['空桌待客','顾客入座','申请开台','顾客下单','顾客退单','厨房退单','提交厨房','厨房接收','制作完成','上菜送餐','顾客用餐','申请结账','结账完成','打扫清台','退单完成','顾客呼叫'];
var Ck=['空桌待客','顾客入座','申请开台','等待支付','等待打单','厨房退单','提交厨房','厨房接收','制作完成','上菜送餐','顾客用餐','申请结账','顾客下单','打扫清台','退单完成','顾客呼叫'];
var Cc=['F0FFFA','9CF','F30','F90','F90','F90','09F','09F','09F','09F','09F','F90','0C9','09F','09F','F30'];
var Cr=[,,,'F00',,,'F90','CC6','0C6','3CF','09F',,,'F00'];
var rou=gck('Jir_RndID');if(rou==''){rou=rnd(16);sck('Jir_RndID',rou);}
var pw,ph,tid,kid,vid;
function rsz(){pw=parseInt(document.documentElement.clientWidth);ph=parseInt(document.documentElement.clientHeight);if(pw>360)pw=360;msk.style.width=pw+'px';msk.style.height=ph+'px';Lb_0.style.height=ph-132+'px';Ld.style.height=ph-132+'px';Dt.style.height=ph-50+'px';Dk.style.height=ph-50+'px';Dk_l.style.height=ph-86+'px';Ds.style.height=ph-159+'px';Dm_l.style.height=ph-120+'px';Dn_l.style.height=ph-120+'px';   setTimeout('rsz()',500);}
function jsn(x){return {act:x,nid:nid,uid:uid,sid:sid,rnd:rnd(6)};}
function nav(o){Dc.style.display='none';Dt.style.display='none';Dk.style.display='none';Dm.style.display='none';Dn.style.display='none';$(o).style.display='block';}
//读取设置
function cfg(){xml(uri,jsn('cfg'),function(r){rid=r.rid;Sc=r.Sc;Sb=r.Sb;Sd=r.Sd;document.title=r.Sn+' - 点单管理';Sn.innerText=r.Sn;An.innerText=r.An;var o=document.createElement('script');o.src=Sc.p[0]+'CLodopfuncs.js';document.body.appendChild(o);cfb(0);nav('Dt');lst();});}
//菜单类别
function cfb(n){var s='';for(var i=0;i<Sb.length;i++){if(Sb[i].Bp*1==n)s+='<div id="Sb_'+Sb[i].ID+'" class="bl0" onClick="cfb('+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?cfd(n):cfb(Sb[0].ID);}
//菜单列表
function cfd(n){for(var i=0;i<Sb.length;i++){if($('Sb_'+Sb[i].ID))$('Sb_'+Sb[i].ID).className='bl0';}$('Sb_'+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="cfp('+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="cdw('+i+');">+</div></div>';}Ld.innerHTML=s;}
//选菜大图
function cfp(n){if(Sd[n].Di*1!=1)return;Fi.innerHTML='<img src="images/'+nid+'/c'+Sd[n].ID+'.jpg" onload="bsz(this,pw-20,ph-20);">';fio('Fi',1);}
//添加点单
function cdw(n){if(Sd[n].Dk.split(',').length>1){cdk(Sd[n].Dn,-n-1,Sd[n].Dk);}else cdx(n,'');}
function cdx(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]);cdj();}
//点单口味
function cdk(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="cdc(this);">'+t[j]+'</span>';Fk_b.innerHTML=s;Fk_i.value=n;Fk_s.value=n<0?'':Mc[n][5];Fk_c.innerText=o;fio('Fk',1,1);}
//口味选择
function cdc(o){if(o.className=='kw1'){o.className='kw0';Fk_s.value+=(o.innerText+',');}else{o.className='kw1';Fk_s.value=(','+Fk_s.value).replace(','+o.innerText+',',',').substr(1);}}
//订单金额
function cdj(){var m=0;var n=0;if(!tok){for(i=0;i<Md.length;i++){m+=Md[i].Fm*Md[i].Fc*(Md[i].Fr=='2'?0:1);n+=Md[i].Fc*1;}}for(i=0;i<Mc.length;i++){m+=Mc[i][2]*Mc[i][3];n+=Mc[i][3]*1;}Dc_m.innerText=m/100;Dc_c.innerText=n;}
//点单列表
function cdl(){var s='';for(var i=0;i<Mc.length;i++)s+='<div class="sl1"><span class="sl2">'+Mc[i][1]+'<span class="sl6" onclick="cdk(\''+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="cds('+i+',-1,'+Mc[i][0]+',\''+Mc[i][5]+'\');">-</span><span id="Ds_c'+i+'" class="sl5">'+Mc[i][3]+'</span><span class="sl4" onclick="cds('+i+',1,'+Mc[i][0]+',\''+Mc[i][5]+'\');">+</span></div>';if(!tok){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+']'))+'</span></span><span class="sl8">'+Ck[Md[i].Fs]+'</span><span class="sl3">'+Md[i].Fm/100+'元</span><span class="sl5">×'+Md[i].Fc+'</span></div>';}}Ds.innerHTML=s;if(Mc.length>0||Md.length>0){Dv.style.display='block';Dx.style.display='none';}cdj();return;}
//单品数量
function cds(o,n,r,k){var m=$('Ds_c'+o).innerText*1;m+=n;$('Ds_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);}}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;}cdl();}
//清理数据
function cls(){Mc=[];Dc_c.innerText=0;Dc_m.innerText=0;Dx.style.display='block';Dv.style.display='none';}

//读取点单
function lst(){clearTimeout(tid);xml(uri,jsn('lst'),function(r){Ts=r.Ts;Ks=r.Ks;var d=new Date();var t='<div style="text-align:center;">更新于 '+r.Rt+' [<a href="javascr'+'ipt:void(0);" onclick="lst();">刷新</a>]</div>';var y=0;var p=[];var s='';for(var i=0;i<Ts.length;i++){s+='<div class="tsl" style="background-color:#'+Cc[Ts[i].Ts]+';color:#'+(Ts[i].Ts>0?'FFF':'000')+';" onclick="tso('+i+');">'+Ts[i].Tn+'['+Ts[i].Tc+'/'+Ts[i].Tu+']<div style="float:right;width:80px;text-align:right;">'+(Ts[i].Tm/100)+' 元</div><div style="float:right;">'+Ct[Ts[i].Ts]+'</div></div>';if('.2.3.4.5.11.15.'.indexOf('.'+Ts[i].Ts+'.')>-1)y=1;if(Ts[i].Ts*1==3&&Sc.i[1]*1==0)p.push(Ts[i].Ti);}Dt.innerHTML=s+t;var s='';for(var i=0;i<Ks.length;i++){s+='<div class="ksl" onClick="kso('+Ks[i].ID+','+Ks[i].Os+');">单号：'+Ks[i].ID+'　金额：'+(Ks[i].Pm/100)+'　状态：<span style="color:#'+Cr[Ks[i].Os]+';">'+Ck[Ks[i].Os]+'</span><br>类型：'+['正餐','快餐','外卖'][Ks[i].Ox]+'　时间：'+Ks[i].Od.substr(11)+'</div>';if(Ks[i].Os*1==12||Ks[i].Os*1==5)y=1;if((Ks[i].Os*1==12&&Sc.i[1]*1==0)||Ks[i].Os*1==4)p.push(Ks[i].ID);}Dk_l.innerHTML=(s==''?'<div style="text-align:center;color:#CCC;line-height:36px;">-= 没有订单 =-</div>':s)+t;if(y){dsy.pause();dsy.src='images/warn.mp3';dsy.play();}if(p.length>0)ctf(p.join(','));tid=setTimeout('lst()',10000);});}
//正餐详情
function tso(n){var p=jsn('lsi');p['oid']=Ts[n].Ti;xml(uri,p,function(r){Mb=r.Mb.length==0?{ID:0,Ot:Ts[n].ID,O1:'',O4:'',Ox:0,Pm:0,Pd:0,Pc:0,Ps:0,Pt:0,Os:0,Od:'',Ob:''}:r.Mb;Mb.Oz=n;Dm_t.innerText=Ts[n].Tn+'('+Ts[n].Tu+'人)['+Ct[Ts[n].Ts]+']';Md=r.Md;var s='';for(var i=0;i<Md.length;i++)s+='<div class="tl1">'+Md[i].Fn+'<div class="td'+Md[i].Fr+'" onclick="txe('+i+');">改</div><div class="tl2" style="width:30px;">×'+Md[i].Fc+'</div><div class="tl2" style="width:50px;">'+(Md[i].Fm/100)+'</div><div class="tl2" style="color:#'+Cr[Md[i].Fs]+';text-decoration:'+['none','underline','line-through'][Md[i].Fr]+';width:50px;">'+Ct[Md[i].Fs]+'</div></div>';Dm_l.innerHTML=s+'单号：'+Mb.ID+'　下单：'+Mb.O1+'　收银：'+Mb.O4+'<br>时间：'+Mb.Od+'<br>餐费： '+Mb.Pm/100+'元　优惠： '+Mb.Pd/100+'元　抵扣： '+Mb.Pc/100+'元<br>已付： '+Mb.Ps/100+'元　方式： '+['现金','信用卡','微信','支付宝','会员'][Mb.Pt]+'<br>备注： '+Mb.Ob;Dv.style.display='none';Dx.style.display='block';nav('Dm');});}
//正餐入座
function trz(){var p=jsn('trz');p.Ot=Mb.Ot;p.Tu=Fr_u.value;xml(uri,p,function(r){fio('Fr',-1,1);lst();setTimeout('tso('+Mb.Oz+')');});}
//正餐提交
function txd(){if(rid.indexOf('A')==-1){alt('无此权限！');return;}if(Mc.length==0)return;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('txd');p.oid=Mb.ID;p.Ot=Mb.Ot;p.Mc=t;p.Ob=Dc_b.value;xml(uri,p,function(r){cls();lst();setTimeout('tso('+Mb.Oz+')',300);});});}
//正餐修改
function txe(n){Fb_i.value=Md[n].ID;Fb_n.innerText=Md[n].Fn;Fb_m.value=Md[n].Fm/100;Fb_b.value=Md[n].Fb;Fb_c.innerText=Md[n].Fc;Fb_w.value=Md[n].Fc;fio('Fb',1,1);}
function txc(){var p=jsn('txc');p.oid=Mb.ID;p.Fm=Math.round(Fb_m.value*100);p.Fb=Fb_b.value;p.Fc=Fb_c.innerText;p.ID=Fb_i.value;xml(uri,p,function(r){fio('Fb',-1,1);lst();setTimeout('tso('+Mb.Oz+')',300);});}
function txr(m){var p=jsn('txr');p.oid=Mb.ID;p.Ot=Mb.Ot;p.Fr=m;p.Fb=Fb_b.value;p.ID=Fb_i.value;xml(uri,p,function(r){fio('Fb',-1,1);lst();setTimeout('tso('+Mb.Oz+')',300);});}
//正餐换桌
function ths(){if(Mb.ID*1==0)return;Fh_o.innerText=Ts[Mb.Oz].Tn;var s1='';var s2='';for(var i=0;i<Ts.length;i++){if(Ts[i].Ts*1==0){s1+='<option value="'+Ts[i].ID+'">'+Ts[i].Tn+'</option>';}else s2+='<option value="'+Ts[i].ID+'">'+Ts[i].Tn+'</option>';}Fh_n.innerHTML=s1;Fh_m.innerHTML=s2;fio('Fh',1,1);}
function thz(c){if(c&&Ts[Mb.Oz].ID*1==Fh_m.value*1){fio('Fh',-1,1);return;}var p=jsn('thz');p.Ot=Mb.Ot;p.Nt=c?Fh_m.value:Fh_n.value;p.Hz=c?1:0;xml(uri,p,function(r){fio('Fh',-1,1);lst();nav('Dt');});}
//正餐清台
function txu(){if(Mb.ID*1>0&&Mb.Os*1!=12){alt('该桌已下单且尚未结账，不能清台！');return;}cfm('确认清台？',function(){var p=jsn('txu');p.Ot=Mb.Ot;p['oid']=Mb.ID;xml(uri,p,function(r){lst();nav('Dt');});});}
//正餐结算
function tjz(){if(Mb.ID*1==0)return;var s='';var t;for(var i=0;i<Sc.c[1].length;i++){s+='<span id="Fm_k'+i+'" class="kw'+(Mb.Ob.indexOf(Sc.c[1][i][0]+', ')>-1?0:1)+'" onclick="tjo(1);">'+Sc.c[1][i][0]+'</span>';}Fm_z.innerHTML=s;Fm_i.innerText='桌号：'+Ts[Mb.Oz].Tn+'　单号：'+Mb.ID;Fm_m.innerText=Mb.Pm/100;Fm_s.innerText=Mb.Ps/100;Fm_c.value=Mb.Pc/100;Fm_t.options[Mb.Pt].selected=true;Fm_t.disabled=Mb.Ps>0;Fm_b.value=Mb.Ob;Fm_a.style.display='none';Fm_e.style.display='none';tjo();fio('Fm',1,1);}
//正餐计算
function tjo(n){var o=event.srcElement;if(o.tagName=='SPAN'&&n)o.className=o.className=='kw1'?'kw0':'kw1';var m=Mb.Pm*1-Fm_c.value*100;if(m<0)m=0;var c=0;for(var i=0;i<Sc.c[1].length;i++){Fm_b.value=Fm_b.value.replace(Sc.c[1][i][0]+', ','');if($('Fm_k'+i).className=='kw0'){Fm_b.value+=Sc.c[1][i][0]+', ';if(Sc.c[1][i][2]*1<1){c+=Math.round(m-m*Sc.c[1][i][2]);}else{if(m>Sc.c[1][i][1]*100)c+=Sc.c[1][i][2]*100;}}}c=Math.round(c);var d=Math.round(m-m*Sc.c[0][Fm_t.value]);Fm_d.innerText=(c+d)/100;m=(Mb.Pm*1-c-d-Fm_c.value*100-Mb.Ps*1)/100;if(m<0)m=0;Fm_n.innerText=m;Fm_a.style.display=Fm_t.value*1>1&&Fm_t.value*1<4?'inline-block':'none';Fm_e.style.display=Fm_t.value*1<2?'inline-block':'none';}
//正餐结账
function tjs(m,c){if(rid.indexOf('B')==-1){alt('无此权限！');return;}var p=jsn('tjs');p.Ot=Mb.Ot;p.oid=Mb.ID;p.Pt=c;p.Pc=Fm_c.value*100;p.Ps=m;p.Ob=Fm_b.value;xml(uri,p,function(r){alt('收款成功！');fio('Fm',-1,'Fe');lst();setTimeout('tso('+Mb.Oz+')',300);});}

//快餐详情
function kso(n,m){if(m==3){cfm('该单尚无支付结果信息，请点击[确定]查询支付结果信息！',function(){cpx(n,1);});return;}var p=jsn('lsi');p['oid']=n;xml(uri,p,function(r){Mb=r.Mb;Dn_i.innerText='(单号：'+Mb.ID+')['+Ck[Mb.Os]+']';Md=r.Md;var s='';for(var i=0;i<Md.length;i++){s+='<div class="tl1">'+Md[i].Fn+(Md[i].Fb==''?'':'['+Md[i].Fb.substr(0,Md[i].Fb.length-1)+']')+'<div class="td'+Md[i].Fr+'" onclick="kxv(Md['+i+']);">退</div><div class="tl2">×'+Md[i].Fc+'</div><div class="tl2">'+(Md[i].Fm/100)+'元</div><div class="tl2" style="width:50px;color:#'+Cr[Md[i].Fs]+';text-decoration:'+['none','underline','line-through'][Md[i].Fr]+';">'+Ck[Md[i].Fs]+'</div></div>';}Dn_l.innerHTML=s+'时间： '+Mb.Od+'　员工： '+Mb.O1+'<br>餐费： '+(Mb.Pm/100)+'元　优惠： '+(Mb.Pd/100)+'元　抵扣： '+(Mb.Pc/100)+'元<br>实付： '+(Mb.Ps/100)+'元　方式： '+['现金','刷卡','微信','支付宝','会员卡'][Mb.Pt]+'<br>备注： '+Mb.Ob.split('|')[0];nav('Dn');});}
//快餐确认
function kxd(){fio('Fn',1,1);var s='<option>选择餐桌</option><option>打包外带</option>';for(var i=0;i<Ts.length;i++)s+='<option>'+Ts[i].Tn+'</option>';Fn_t.innerHTML=s;Fn_c.value=0;Fn_m.innerText=Dc_m.innerText;Fn_b.options[0].selected=true;var s='';for(var i=0;i<Sc.c[1].length;i++){s+='<span id="Fn_o'+i+'" class="kw1" onclick="kxn(1);">'+Sc.c[1][i][0]+'</span>';}Fn_e.innerHTML='折扣： '+s;kxn(0);}
//快餐计算
function kxn(n){var o=event.srcElement;if(o.tagName=='SPAN'&&n)o.className=o.className=='kw1'?'kw0':'kw1';var m=Dc_m.innerText*100;Fn_m.innerText=m/100;if(Fn_c.value*100>m)Fn_c.value=m/100;m-=Fn_c.value*100;var c=0;for(var i=0;i<Sc.c[1].length;i++){Dc_b.value=Dc_b.value.replace(Sc.c[1][i][0]+', ','');if($('Fn_o'+i).className=='kw0'){Dc_b.value+=Sc.c[1][i][0]+', ';if(Sc.c[1][i][2]*1<1){c+=Math.round(m-m*Sc.c[1][i][2]);}else{if(m>Sc.c[1][i][1]*100)c+=Sc.c[1][i][2]*100;}}}c=Math.round(c);var d=Math.round(m-m*Sc.c[0][Fn_b.value]);Fn_d.innerText=(c+d)/100;Fn_n.innerText=(Fn_m.innerText*100-c-d-Fn_c.value*100)/100;}
//快餐预单
function kxb(){if(rid.indexOf('A')==-1||rid.indexOf('B')==-1){alt('无此权限！');return;}if(Mc.length==0)return;if(Fn_t.options[0].selected==true){alt('请选择餐桌！');return;}var t=[];for(var i=0;i<Mc.length;i++)t[i]=[Mc[i][0],Mc[i][3],Mc[i][5]];var p=jsn('kxb');p.Mc=t;p.Pm=Math.round(Fn_m.innerText*100);p.Pc=Math.round(Fn_c.value*100);p.Pd=Math.round(Fn_d.innerText*100);p['Pt']=Fn_b.value*1;p.Bz={bz:Dc_b.value,cz:Fn_t.value,dh:'',dz:'',sj:'',gx:'',gy:''};p.Ou=rou;xml(uri,p,function(r){if(Fn_b.value*1<2){kxt(r.oid);}else cpe(r.oid);});}
//快餐下单
function kxt(o,m,c){if(rid.indexOf('A')==-1||rid.indexOf('B')==-1){alt('无此权限！');return;}var p=jsn('kxt');p.oid=o;p.Ps=m||Math.round(Fn_n.innerText*100);p.Pt=c||Fn_b.value;xml(uri,p,function(r){sck('Jir_RndID','');rou=rnd(16);cls();clearTimeout(kid);alt('下单成功！');fio('Fn',-1,'Fe');lst();nav('Dk');});}
//快餐退单
function kxv(n){Fv_i.value=n.ID;Fv_n.innerText=n.Fn;Fv_m.value=n.Fm/100;Fv_b.value=n.Fb;fio('Fv',1,1);}
//快餐退单
function kxr(n){cfm('确认退单？\n请退单后联系顾客退款！',function(){var p=jsn('kxr');p.ID=Fv_i.value;p.Fr=n;p.Fm=Fv_m.value*100;p.Fo=Mb.ID;p.Fb=Fv_b.value;xml(uri,p,function(r){fio('Fv',-1,1);lst();setTimeout('kso('+Mb.ID+')',300);});});}
//快餐完成
function kxe(){cfm('确认完成此单？\n外卖单请务必确认已送达！',function(){var p=jsn('kxe');p.oid=Mb.ID;xml(uri,p,function(r){lst();nav('Dk');});});}
//外卖跟踪
function kwm(n){var p=jsn('kwm');p.oid=n;xml(uri,p,function(r){Fw_l.innerHTML='单号：'+n+'　状态：'+['待接单','取餐中','派送中','已送达','已结算'][r.Hs]+'<br>骑手：'+r.An+'[<a href="tel:'+r.Am+'">'+r.Am+'</a>]<br>送至：['+r.Hd.Tn+']'+r.Hd.Td+'<br>'+r.Hc;fio('Fw',1,1);});}
//外卖重发
function kwf(n){cfm('确认重新发布？',function(){var p=jsn('kwf');p.oid=n;xml(uri,p,function(r){kwm(n);});});}

//提交厨房
function ctf(n,s){if(!n)return;if(Sc.i[0]*1==1){if(!prt()){alt('打印服务器设置错误！');return;}}var p=jsn('ctf');p.oid=n;p.Wx=Sc.i[0];xml(uri,p,function(r){for(var j=0;j<r.Pb.length;j++){if(r.Pb[j].Ox*1>0)pre(Sc.p[3]+'|spb|'+r.Pb[j].ID+'|'+Sc.p[5]);}if(Sc.i[0]=='1'){for(var i=0;i<r.Pd.length;i++)pre(r.Pd[i]+'|'+Sc.p[12]);}lst();if(s){alt('已提交厨房！');tok?setTimeout('kso('+n+')',300):setTimeout('tso('+Mb.Oz+')',300);}});}
//二维码
function cpe(o){var p=jsn('ewm');p.oid=o;p.TK=tok;p.Ps=$(tok?'Fn_n':'Fm_n').innerText*100;p.Pt=3-$(tok?'Fn_b':'Fm_t').value*1;if(!tok){p.Pd=Fm_d.innerText*100;p.Pc=Fm_c.value*100;p.Ob=Fm_b.value;}if(p.Ps<=0){alt('款已结清，无需结账！');return;}xml(uri,p,function(r){Fe_i.innerHTML='<div style="width:231px;height:231px;margin:10px auto;display:block;"><img src="qrcode.php?u='+escape(r.qrcode)+'&i='+['icoz','icow'][r.type]+'" width="231" height="231"></div>应付金额： '+(r.money/100)+' 元';fio('Fe',1);clearTimeout(kid);kid=setTimeout('cpx(\''+o+'\')',5000);});}
//查询
function cpx(o,n){n=n||0;var p=jsn('syn');p.oid=o;xml(uri,p,function(r){if(r.pay==1){clearTimeout(kid);alt('收款成功！');ply(Sc.i[12].replace('{{id}}',r.mny/100));if(tok){kxt(o,r.mny,3-r.api);}else{tjs(r.mny,3-r.api);}}else{if(!n)kid=setTimeout('cpx(\''+o+'\','+n+')',3000);if(n)alt('无支付信息！');}});}
//声音
function ply(n){var p=jsn('snd');p.snd=n;xml(uri,p,function(r){dsy.pause();dsy.src=r.mp3;dsy.play();});}
//打印
var pri=0;var prc=[];
function prt(){var o;try{o=getCLodop();}catch(e){return false;};o.SET_LICENSES(Sc.p[1],Sc.p[2],'','');return o;}
function pre(a){if((','+prc.join(',')+',').indexOf(','+a+',')==-1)prc.push(a);prn();}
function prn(){if(prc.length==0)return;if(pri){setTimeout('prn()',100);return;}pri=1;var o=prt();if(!o)return;var c=prc[0].split('|');o.PRINT_INIT('Print:'+prc[0]);o.SET_PRINTER_INDEX(c[0]);o.SET_PRINT_PAGESIZE(3,c[3]*10,48,'CreateCustomPage');o.ADD_PRINT_HTM(0,0,0,500,'URL:output.php?x='+c[1]+'&n='+nid+'&u='+uid+'&s='+vid+'&a='+c[2]);o.PRINT();prc.splice(0,1);pri=0;setTimeout('prn()',100);}
</script>
</head>
<body onLoad="rsz();cfg();">
<div id="msk"></div><div id="tip"></div><div id="cfn"></div><audio id="dsy" src=""></audio>
<!--顶部-->
<div style="width:360px;height:50px;top:0px;left:0px;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;line-height:20px;margin-top:6px;font-size:18px;"><span id="Sn"></span><br><span style="font-size:14px;" id="An"></span></div>
  <div style="float:right;padding-right:10px;"><span id="tk0" class="tk0" onClick="tok=0;tk0.className='tk0';tk1.className='tk1';cls();nav('Dt');">正餐</span><span id="tk1" class="tk1" onClick="tok=1;tk0.className='tk1';tk1.className='tk0';cls();nav('Dk');">快餐</span></div>
</div>
<!--菜品-->
<div id="Dc" style="width:360px;display:none;">
  <div style="line-height:32px;font-size:18px;margin:0px 5px;color:#f90;"><div class="bnx" onClick="if(Dv.style.display=='block'){Dv.style.display='none';Dx.style.display='block';}else nav(tok?'Dk':'Dm');">×</div><span id="Dc_t"></span></div>
  <div id="Dx" style="display:block">
    <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="Dv" style="display:none;overflow:auto;"><div id="Ds"></div><div style="line-height:24px;font-size:12px;text-align:center;">备注：<input type="text" id="Dc_b" style="width:280px;font-size:12px;"></div></div>
  <div style="line-height:50px;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="cdl();"><span id="Dc_c" 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(Dv.style.display=='none'){cdl();}else tok?kxd():txd();">下 单</div><span id="Dc_m" style="font-size:32px;color:#F90;">0</span> 元
  </div>
</div>
<!--大图-->
<div id="Fi" class="fld" style="width:340px;height:600px;z-index:11;overflow:auto;" onClick="fio('Fi',-1);"></div>
<!--口味-->
<div id="Fk" class="fld" style="width:320px;height:212px;z-index:12;">
  <div class="flt"><div class="bnc" onClick="fio('Fk',-1,1);">×</div>选择口味</div>
  <div class="flb" style="height:128px;">菜品： <span id="Fk_c"></span><br><span id="Fk_b"></span></div>
  <div class="flk"><input type="hidden" id="Fk_i"><input type="hidden" id="Fk_s"><span class="btn" onClick="if(Fk_i.value*1>-1){Mc[Fk_i.value][5]=Fk_s.value;cdl();}else cdx(-1*Fk_i.value-1,Fk_s.value);fio('Fk',-1,1);">确定</span></div>
</div>

<!--正餐列表-->
<div id="Dt" style="width:360px;display:none;overflow:auto;"></div>
<!--正餐详情-->
<div id="Dm" style="width:360px;display:none;">
  <div style="line-height:32px;font-size:18px;border-bottom:1px solid #F90;margin:0px 5px;color:#f90;"><div class="bnx" onClick="nav('Dt');">×</div><span id="Dm_t"></span></div>
  <div id="Dm_l" style="line-height:32px;padding:0px 5px;width:350px;overflow:auto;"></div>
  <div style="text-align:center;line-height:32px;"><span class="btn" onClick="Fr_t.innerText=Ts[Mb.ID].Tn;Fr_u.value=Ts[Mb.ID].Tu*1||2;fio('Fr',1,1);">入座</span><span class="btn" onClick="if(Ts[Mb.Oz].Ts*1>0){Dc_t.innerText=Ts[Mb.Oz].Tn;cls();nav('Dc');cdj();}">点单</span><span class="btn" onClick="ctf(Mb.ID,1);">交厨</span><span class="btn" onClick="ths();">换桌</span><span class="btn" onClick="tjz();">结账</span><span class="btn" onClick="txu();">清台</span></div>
</div>
<!--正餐入座-->
<div id="Fr" class="fld" style="width:320px;z-index:13;">
  <div class="flt"><div class="bnc" onClick="fio('Fr',-1,1);">×</div>入座</div>
  <div class="flb">桌号： <span id="Fr_t"></span><br>人数： <input type="number" id="Fr_u" style="width:30px;" value="2"> 人</div>
  <div class="flk"><span class="btn" onClick="trz();">确定</span></div>
</div>
<!--正餐退单-->
<div id="Fb" class="fld" style="width:320px;z-index:14;">
  <div class="flt"><div class="bnc" onClick="fio('Fb',-1,1);">×</div>退单</div>
  <div class="flb">菜品： <span id="Fb_n"></span><br>单价： <input type="number" id="Fb_m" style="width:50px;">　数量： <span style="font-size:12px;cursor:pointer;background-color:#F90;color:#FFF;width:16px;height:16px;line-height:16px;display:inline-block;text-align:center;" onClick="Fb_c.innerText=Fb_c.innerText*1-1;if(Fb_c.innerText*1<1)Fb_c.innerText=1;">-</span>　<span id="Fb_c"></span>　<span style="font-size:12px;cursor:pointer;background-color:#F90;color:#FFF;width:16px;height:16px;line-height:16px;display:inline-block;text-align:center;" onClick="Fb_c.innerText=Fb_c.innerText*1+1;if(Fb_c.innerText*1>Fb_w.value*1)Fb_c.innerText=Fb_w.value;">+</span><input type="hidden" id="Fb_w"><input type="hidden" id="Fb_i"><br>备注： <input type="text" id="Fb_b" size="4">　　<span class="btn" onClick="txc();">修改</span></div>
  <div class="flk"><span class="btn" onClick="txr(0);">拒退</span><span class="btn" onClick="txr(2);">退单</span></div>
</div>
<!--正餐换桌-->
<div id="Fh" class="fld" style="width:320px;z-index:15;">
  <div class="flt"><div class="bnc" onClick="fio('Fh',-1,1);">×</div>换桌</div>
  <div class="flb">原餐桌： <span id="Fh_o"></span><br>换桌至： <select id="Fh_n"></select><br>合并至： <select id="Fh_m"></select></div>
  <div class="flk"><span class="btn" onClick="thz(1);">合并</span><span class="btn" onClick="thz(0);">换桌</span></div>
</div>
<!--正餐结账-->
<div id="Fm" class="fld" style="width:320px;height:420px;z-index:16;">
  <div class="flt"><div class="bnc" onClick="fio('Fm',-1,1);">×</div>正餐支付</div>
  <div class="flb" style="height:336px;"><span id="Fm_i"></span><br>折扣：<span id="Fm_z"></span><br>合计：<span id="Fm_m"></span>元　已付：<span id="Fm_s"></span>元　优惠：<span id="Fm_d"></span>元<br>抵扣：<input type="number" id="Fm_c" style="width:64px;" onBlur="num(2);tjo();">　应付：<span id="Fm_n"></span>元<br>方式：<select id="Fm_t" onChange="tjo();"><option value="0">现金支付</option><option value="1">刷卡支付</option><option value="2">微信支付</option><option value="3">支付宝支付</option><option value="4">会员卡支付</option></select>　<br>备注：<input type="text" id="Fm_b" style="font-size:14px;text-align:left;width:200px;"></div>
  <div class="flk"><span id="Fm_e" style="display:none;">现金，刷卡请交由前台结账</span><span id="Fm_a" style="display:none;"><span class="btn" onClick="cpe(Mb.ID);">扫码支付</span><span class="btn" onClick="if(Fm_n.innerText*1==0){alt('收款成功！');tjs(0,Fm_t.value);}else cpx(Mb.ID,1);">查询结果</span></span></div>
</div>

<!--快餐列表-->
<div id="Dk" style="width:360px;display:none;overflow:auto;">
  <div style="line-height:32px;font-size:18px;color:#F90;margin:0px 5px;border-bottom:1px solid #f90;"><div style="float:right;"><span class="btn" style="line-height:24px;" onClick="Dc_t.innerText='快餐外卖';cls();nav('Dc');">点菜下单</span></div>快餐外卖</div>
  <div id="Dk_l" style="width:360px;overflow:auto;"></div>
</div>
<!--快餐详情-->
<div id="Dn" style="width:360px;display:none;overflow:auto;">
  <div style="line-height:32px;font-size:18px;border-bottom:1px solid #F90;margin:0px 5px;color:#f90;"><div class="bnx" onClick="nav('Dk');">×</div>快餐外卖<span id="Dn_i"></span></div>
  <div id="Dn_l" style="line-height:32px;padding:0px 5px;"></div>
  <div style="text-align:center;line-height:32px;"><span class="btn" onClick="ctf(Mb.ID,1);">收单</span><span class="btn" onClick="if(Mb.Ox*1==2)kwm(Mb.ID);">外卖跟踪</span><span class="btn" onClick="kxe();">完成</span></div>
</div>
<!--快餐支付-->
<div id="Fn" class="fld" style="width:320px;height:420px;z-index:17;">
  <div class="flt"><div class="bnc" onClick="clearTimeout(kid);fio('Fn',-1,1);">×</div>快餐支付</div>
  <div class="flb" style="height:342px;">选择餐桌： <select id="Fn_t"></select><br>支付方式： <select id="Fn_b" onChange="kxn(this.selectedIndex);"><option value="0">现金支付</option><option value="1">刷卡支付</option><option value="2">微信支付</option><option value="3">支付宝支付</option></select>
    <div id="Fn_e"></div>
    餐费： <span id="Fn_m">0</span> 元　优惠： <span id="Fn_d">0</span> 元<br>
    抵扣： <input type="number" id="Fn_c" onBlur="num(2);kxn();" style="width:50px;"> 元　应付： <span id="Fn_n">0</span> 元
  </div>
  <div class="flk"><span class="btn" onClick="kxb();">确定下单并支付</span></div>
</div>
<!--外卖跟踪层-->
<div id="Fw" class="fld" style="width:320px;z-index:18;">
  <div class="flt"><div class="bnc" onClick="fio('Fw',-1,1);">×</div>外卖送餐跟踪</div>
  <div class="flb" id="Fw_l" style="font-size:14px;"></div>
  <div class="flk" style="text-align:right;"><span class="btn" onClick="kwf(Mb.ID);">重发</span></div>
</div>
<!--快餐退单层-->
<div id="Fv" class="fld" style="width:320px;z-index:19;">
  <div class="flt"><div class="bnc" onClick="fio('Fv',-1,1);">×</div>快餐退单<input type="hidden" id="Fv_i"></div>
  <div class="flb">菜品： <span id="Fv_n"></span><br>金额： <input type="text" id="Fv_m" style="width:40px;" onBlur="num(2);"> 元　提示：顾客支付时可能使用了优惠，请注意核减优惠金额！<br>备注： <input type="text" id="Fv_b" style="width:160px;"></div>
  <div class="flk" style="text-align:right;"><span class="btn" onClick="kxr(0);">拒绝</span><span class="btn" onClick="kxr(2);">退单</span></div>
</div>
<!--扫码支付-->
<div id="Fe" class="fld" style="width:320px;height:420px;z-index:20;">
  <div class="flt"><div class="bnc" onClick="clearTimeout(kid);Fe_i.innerHTML='';fio('Fe',-1);">×</div>扫码支付</div>
  <div class="flb" id="Fe_i" style="height:296px;text-align:center;padding-top:56px;"></div>
  <div class="flk" style="text-align:center;">扫描二维码发起支付</div>
</div>
</body>
</html>
<?php
}
?>