<?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"],"E")===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];
	echo Enc($p);
	break;

	case "lst"://列表
	$p=["msg"=>"ok","Ms"=>[],"Rt"=>date("Y-m-d H:i:s")];
	$q=Qry("Select ID,Hu,Ho,Hd,Hm,Hs From ".$pre."osend Where Si=".$nid." And Hs<4 And (Hu=0 Or Hu=".$uid.") Order By ID Asc");
	while($r=Qra($q)){$r["Hd"]=Dec($r["Hd"]);$p["Ms"][]=$r;}
	echo Enc($p);
	break;

	case "cfd"://操作
	$q=Qry("Select Hc From ".$pre."osend Where Si=".$nid." And Ho=".$m["oid"]." And (Hu=0 Or Hu=".$uid.")");
	$a=['','骑手已接单，前往取餐中。','骑手已取餐，正在派送中。','骑手已送达，点单已完成。',''];$b=$m["bid"]*1+1;
	if(Qrc($q)>0){
		Qry("Update ".$pre."osend Set Hs=".$b.",Hu=".$uid.",Hc='".Qrs($q)."<br>[".date("Y-m-d H:i:s")."] ".$a[$b]."' Where Si=".$nid." And Ho=".$m["oid"]." And Hs<3");
		if($b==3){Qry("Update ".$pre."ofood Set Fs=10 Where Si=".$nid." And Fo=".$m["oid"]);Qry("Update ".$pre."order Set Os=13 Where Si=".$nid." And ID=".$m["oid"]);}
		echo '{"msg":"ok"}';
	}else echo '{"msg":"收单失败，可能已被别的骑手抢单。"}';
	break;

	case "lgo"://退出
	Qry("Update ".$pre."admin Set Ak='' Where Si=".$nid." And ID=".$m["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;}
.flk {padding:3px 10px;line-height:30px;text-align:right;}
.dn1 {float:left;width:88px;line-height:36px;height:36px;text-align:center;border:1px solid #F90;background-color:#F90;color:#FFF;}
.dn2 {float:left;width:88px;line-height:36px;height:36px;text-align:center;border:1px solid #F90;border-bottom:0px;}
.dl1 {clear:both;border:1px dashed #F90; background-color:#FFF9F3;margin:5px;}
.dl2 {float:left;line-height:30px;width:103px;padding-left:10px;}
.dl3 {line-height:30px;width:320px;padding:0px 10px;overflow:hidden;}
</style>
<script type="text/javascript" src="https://api.map.baidu.com/api?v=2.0&ak=p7qaGmr37q4uCgiBShG0tOsI&s=1"></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='sender.php';
var nid='<?php echo Req("nid");?>';
var uid='<?php echo Req("uid");?>';
var sid='<?php echo Req("sid");?>';
var pw,ph,Av,Ar,Ms,bid,tid,kid;
function rsz(){pw=parseInt(document.documentElement.clientWidth);ph=parseInt(document.documentElement.clientHeight);if(pw>360)pw=360;msk.style.height=ph+'px';dv.style.width=pw+'px';dv.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;Av=r.Sc.i[14];An.innerText=r.An;document.title=r.Sn+' - 外卖送餐';Sn.innerText=r.Sn;kid=0;lst(0);});}
function lst(n){if(Ar.indexOf('E')==-1){alt('无此权限！');return;}clearTimeout(tid);ds0.className='dn1';ds1.className='dn1';ds2.className='dn1';ds3.className='dn1';$('ds'+n).className='dn2';bid=n;var c=false;xml(uri,jsn('lst'),function(r){Ms=r.Ms;var s='';for(var i=0;i<Ms.length;i++){if(Ms[i].Hs*1==n&&((n>0&&Ms[i].Hu==uid)||n==0))s+='<div class="dl1" onclick="cid='+i+';cfk('+n+','+i+');"><div class="dl2">单号： '+Ms[i].Ho+'</div><div class="dl2">运费： '+(Ms[i].Hm/100)+' 元</div><div class="dl3">从： '+Ms[i].Hd.Fd+'</div><div class="dl3">至： '+Ms[i].Hd.Td+'[<a href="tel:'+Ms[i].Hd.Tn+'">'+Ms[i].Hd.Tn+'</a>]</div></div>';if(Ms[i].Hs*1==0)c=[Ms[i].ID,Ms[i].Hd.Td,Ms[i].Hd.Hm];}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(Av.replace('{{i1}}',c[1]).replace('{{i2}}',c[2]/100));}});}
function cfk(n,m){dci.innerHTML='从：['+Ms[m].Hd.Fn+']'+Ms[m].Hd.Fd+'<br>至：[<a href="tel:'+Ms[m].Hd.Tn+'">'+Ms[m].Hd.Tn+'</a>]'+Ms[m].Hd.Td;fio('dc',1,1);dcp.innerText=Ms[m].Hm/100;dco.innerText=Ms[m].Ho;dcb.innerText=['接 单','送 餐','完 成','关 闭'][Ms[m].Hs];var map=new BMap.Map('dmp');map.enableScrollWheelZoom();map.clearOverlays();var wlk=new BMap.WalkingRoute(map,{renderOptions:{map:map,autoViewport:true},onSearchComplete:function(r){if(wlk.getStatus()!=BMAP_STATUS_SUCCESS){alt('无法获取位置信息，请确认确认您手机已打开定位功能！');return;}}});wlk.search(new BMap.Point(Ms[m].Hd.Fx,Ms[m].Hd.Fy),new BMap.Point(Ms[m].Hd.Tx,Ms[m].Hd.Ty));}
function cfd(m){var p=jsn('cfd');p.bid=bid;p.oid=dco.innerText;xml(uri,p,function(r){fio('dc',-1,1);lst(bid);});}
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 id="dv">
  <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 id="dn" style="clear:both;">
    <div id="ds0" class="dn2" onClick="lst(0);">待接收</div>
    <div id="ds1" class="dn1" onClick="lst(1);">待取餐</div>
    <div id="ds2" class="dn1" onClick="lst(2);">送餐中</div>
    <div id="ds3" class="dn1" onClick="lst(3);">已完成</div>
  </div>
  <div id="dt" style="clear:both;border:1px solid #F90;overflow:auto;"></div>
</div>
<!--处理层-->
<div id="dc" class="fld" style="z-index:11;">
  <div class="flt"><div class="bnc" onClick="fio('dc',-1,1);">×</div>点单处理　单号：<span id="dco"></span></div>
  <div id="dci" style="line-height:22px;padding:0px 5px;"></div>
  <div id="dmp" style="width:318px;height:318px;"></div>
  <div class="flk"><div style="float:left;line-height:30px;padding-left:5px;">运费： <span id="dcp"></span> 元</div><span id="dcb" class="btn" onClick="cfd();">接 单</span></div>
</div>
</body>
</html>
<?php
}
?>