<?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"],"C")===false&&strpos($r["Ar"],"D")===false)die('{"msg":"无此权限！"}');
	$nid=$m["nid"];$uid=$m["uid"];$rid=$r["Ar"];$unm=$r["An"];
}
switch($m["act"]){
	case "cfg"://设置
	$r=Qra(Qry("Select Sn,Sc From ".$pre."shop Where Si=".$nid));
	$p=["msg"=>"ok","Sn"=>$r["Sn"],"Sc"=>Dec($r["Sc"]),"An"=>$unm,"Ar"=>$rid,"Mt"=>[]];
	$q=Qry("Select ID,Tn From ".$pre."table Where Th=0 And Si=".$nid);
	while($r=Qra($q))$p["Mt"][]=$r;
	echo Enc($p);
	break;

	case "lst"://列表
	$p=["msg"=>"ok","Md"=>[],"Rt"=>date("Y-m-d H:i:s")];
	$q=Qry("Select A.ID,A.Fn,A.Fc,A.Fs,A.Ft,A.F2,A.F3,A.Fr,A.Fb,A.Fo,B.Ox From ".$pre."ofood As A,".$pre."order As B Where B.ID=A.Fo And A.Si=".$nid." And A.Fs In (6,7,8,9) Order By A.ID Asc");
	while($r=Qra($q))$p["Md"][]=$r;
	echo Enc($p);
	break;

	case "cfo"://详单
	$a="";$q=Qry("Select Fn,Fc,Fs From ".$pre."ofood Where Si=".$nid." And Fo=".$m["oid"]);
	$b=['空桌待客','顾客入座','申请开台','顾客下单','顾客退单','厨房退单','提交厨房','厨房接收','制作完成','上菜送餐','顾客用餐','申请结账','结账完成','打扫清台','退单完成','顾客呼叫'];
	while($r=Qra($q))$a.="<br>　".$r["Fn"]."(×".$r["Fc"].")[".$b[$r["Fs"]]."]";
	echo '{"msg":"ok","Mf":"'.$a.'"}';
	break;

	case "cfd"://操作
	$a=$m["Ci"];$b=$m["Ct"];
	$r=Qra(Qry("Select Ft,Fo,F2,F3 From ".$pre."ofood Where Si=".$nid." And ID=".$a));
	if($r){
		if($b==6&&$r["F2"]*1!=$uid)die('{"msg":"撤单失败！"}');
		if($b==7&&$r["F2"]*1!=0)die('{"msg":"收单失败，可能已被其他员工抢单。"}');
		if($b==9&&$r["F3"]*1!=0)die('{"msg":"收单失败，可能已被其他员工抢单。"}');
		$f=$b==6?",F2=0":($b==7||$b==8?(",F2=".$uid):(",F3=".$uid));
		Qry("Update ".$pre."ofood Set Fs=".$b.$f." Where Si=".$nid." And ID=".$a);
		Qry("Update ".$pre."table Set Ts=".$b." Where Si=".$nid." And ID=".$r["Ft"]." And Ts In (6,7,8,9)");
		Qry("Update ".$pre."order Set Os=".$b." Where Si=".$nid." And ID=".$r["Fo"]." And Os In (6,7,8,9)");
		echo '{"msg":"ok"}';
	}else echo '{"msg":"收单失败。"}';
	break;

	case "cft"://退单
	$a=$m["Ci"];$b=$m["Ct"];$e="";
	$r=Qra(Qry("Select Ft,Fo From ".$pre."ofood Where Si=".$nid." And ID=".$a));
	if($b==6||$b==7)$e=",F2=".$uid;if($b==8||$b==9)$e=",F3=".$uid;
	Qry("Update ".$pre."ofood Set Fr=1".$e.",Fb='厨房退单' Where Si=".$nid." And ID=".$a);
	Qry("Update ".$pre."table Set Ts=5 Where Si=".$nid." And ID=".$r["Ft"]." And Ts In (6,7,8,9)");
	if($r["Ft"]==0)Qry("Update ".$pre."order Set Os=5 Where Si=".$nid." And ID=".$r["Fo"]);
	echo '{"msg":"ok"}';
	break;

	case "lgo"://退出
	Qry("Update ".$pre."admin Set Ak='' Where Si=".$nid." And ID=".$uid);
	echo '{"msg":"ok"}';
	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;}
.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:318px;left:20px;background-color:#FFF;box-shadow:2px 2px 2px #666;display:none;}
.flt {line-height:32px;background-color:#F90;color:#FFF;padding:0px 10px;}
.flb {padding:10px 20px;line-height:30px;}
.flk {height:40px;text-align:center;}
.dn0 {float:left;width:88px;line-height:36px;height:36px;text-align:center;border:1px solid #F90;border-bottom:0px;}
.dn1 {float:left;width:88px;line-height:36px;height:36px;text-align:center;border:1px solid #F90;background-color:#F90;color:#FFF;}
.dl1 {height:40px;clear:both;border:1px dashed #F90; background-color:#FFF9F3;margin:5px;}
.dl2 {float:left;line-height:40px;width:80px;text-align:center;}
.dl3 {float:left;line-height:40px;width:220px;text-align:center;}
.dl4 {float:left;line-height:40px;width:40px;text-align:center;}
</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='cooker.php';
var nid='<?php echo Req("nid");?>';
var uid='<?php echo Req("uid");?>';
var sid='<?php echo Req("sid");?>';
var pw,ph,Ar,Sc,Mt,Md,oid,cid,mid,tid,kid;
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';dt.style.width=pw-2+'px';dt.style.height=ph-92+'px';setTimeout('rsz()',500);}
function jsn(x){return {act:x,uid:uid,nid:nid,sid:sid,rnd:rnd(6)};}
function cfg(){xml(uri,jsn('cfg'),function(r){Ar=r.Ar;Sc=r.Sc;Mt=r.Mt;An.innerText=r.An;document.title=r.Sn+' - 厨房管理';Sn.innerText=r.Sn;kid=0;lst(6);});}
function lst(n){if(Ar.indexOf('C')==-1&&(n==6||n==7)){alt('无此权限！');return;}if(Ar.indexOf('D')==-1&&(n==8||n==9)){alt('无此权限！');return;}clearTimeout(tid);ds6.className='dn1';ds7.className='dn1';ds8.className='dn1';ds9.className='dn1';$('ds'+n).className='dn0';mid=n;var c=false;xml(uri,jsn('lst'),function(r){Md=r.Md;var s='';for(var i=0;i<Md.length;i++){if(Md[i].Fr*1!=2&&Md[i].Fs*1==n&&(((n==7||n==9)&&Md[i].F2==uid)||n==6||n==8))s+='<div class="dl1" onclick="tcd=Md['+i+'];cfk('+n+',Md['+i+']);"><div class="dl2">'+cfi(Md[i].Ft,Md[i].Ox)+'</div><div class="dl3">'+Md[i].Fn+'</div><div class="dl4">'+Md[i].Fc+['','[T]','[T]'][Md[i].Fr]+'</div></div>';if(Md[i].Fs*1==6)c=[Md[i].ID,Md[i].Fn,Md[i].Fc];}dt.innerHTML=(s==''?'当前没有点单！':s)+'<div style="text-align:center;">更新于 '+r.Rt+' [<a href="javascr'+'ipt:void(0);" onclick="clearTimeout(tid);lst('+n+');">刷新</a>]</div>';tid=setTimeout('lst('+n+')',10000);if(c&&kid!=c[0]){kid=c[0];ply(Sc.i[13].replace('{{i1}}',c[1]).replace('{{i2}}',c[2]));}});}
function cfi(n,m){var s=['正餐','快餐','外卖'][m];for(var i=0;i<Mt.length;i++){if(Mt[i].ID*1==n*1)s=Mt[i].Tn;}return s;}
function cfk(n,m){$('dh'+n).innerHTML='单号：<a href="javascript:void(0);" onclick="cfo('+n+','+m.Fo+')">'+m.Fo+'</a><span id="dx'+n+'"></span><br>桌号：'+cfi(m.Ft,m.Ox)+'<br>菜品：'+m.Fn+'<br>数量：'+m.Fc+' '+['','[已申请退单]','[已同意退单]'][m.Fr]+'<br>要求：'+m.Fb;cid=m.ID;oid=m.Fo;fio('dc'+n,1,1);}
function cfo(n,m){if($('dx'+n).innerHTML!=''){$('dx'+n).innerHTML='';return;}var p=jsn('cfo');p.oid=m;xml(uri,p,function(r){$('dx'+n).innerHTML=r.Mf;});}
function cfd(m){var p=jsn('cfd');p.Ci=cid;p.Ct=m;xml(uri,p,function(r){fio('dc'+mid,-1,1);lst(mid);});}
function cft(m){cfm('确认退单？',function(){var p=jsn('cft');p.Ci=cid;p.Ct=m;xml(uri,p,function(r){fio('dc'+mid,-1,1);lst(mid);});});}
function lgo(){cfm('确认退出？',function(){xml(uri,jsn('lgo'),function(r){location.reload();});});}
function ply(n){var p=jsn('snd');p.snd=n;xml(uri,p,function(r){dsy.pause();dsy.src=r.mp3;dsy.play();});}
</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;width:60px;line-height:30px;text-align:center;font-size:16px;margin:10px;background-color:#F90;color:#FFF;" onClick="lgo();">退出</div>
</div>
<div style="clear:both;">
  <div id="ds6" class="dn0" onClick="lst(6);">待接收</div>
  <div id="ds7" class="dn1" onClick="lst(7);">已接收</div>
  <div id="ds8" class="dn1" onClick="lst(8);">待送餐</div>
  <div id="ds9" class="dn1" onClick="lst(9);">已送餐</div>
</div>
<div id="dt" style="clear:both;border:1px solid #F90;overflow:auto;"></div>
<!--处理层-->
<div id="dc6" class="fld" style="z-index:16;">
  <div class="flt"><div class="bnc" onClick="fio('dc6',-1,1);">×</div>点单处理</div>
  <div id="dh6" class="flb"></div>
  <div class="flk"><span class="btn" onClick="cft(6);">退单</span>　　<span class="btn" onClick="cfd(8);">完成</span>　　<span class="btn" onClick="cfd(7);">接收</span></div>
</div>
<div id="dc7" class="fld" style="z-index:17;">
  <div class="flt"><div class="bnc" onClick="fio('dc7',-1,1);">×</div>点单处理</div>
  <div id="dh7" class="flb"></div>
  <div class="flk"><span class="btn" onClick="cft(7);">退单</span>　　<span class="btn" onClick="cfd(6);">撤单</span>　　<span class="btn" onClick="cfd(8);">完成</span></div>
</div>
<div id="dc8" class="fld" style="z-index:18;">
  <div class="flt"><div class="bnc" onClick="fio('dc8',-1,1);">×</div>点单处理</div>
  <div id="dh8" class="flb"></div>
  <div class="flk"><span class="btn" onClick="cft(8);">退单</span>　　<span class="btn" onClick="cfd(10);">完成</span>　　<span class="btn" onClick="cfd(9);">送餐</span></div>
</div>
<div id="dc9" class="fld" style="z-index:19;">
  <div class="flt"><div class="bnc" onClick="fio('dc9',-1,1);">×</div>点单处理</div>
  <div id="dh9" class="flb"></div>
  <div class="flk"><span class="btn" onClick="cft(9);">退单</span>　　　　<span class="btn" onClick="cfd(10);">完成</span></div>
</div>
</body>
</html>
<?php
}
?>