<?php
error_reporting(0);
$ver="0.8.0";
if(PHP_VERSION<'5.5.0')die('PHP版本过低，至少需要5.5版及以上');
$db=[//数据库结构
	"admin"=>["Si"=>"int(11)","An"=>"varchar(32)","Au"=>"varchar(32)","Ap"=>"varchar(32)","Aw"=>"varchar(64)","Am"=>"varchar(32)","Ar"=>"varchar(32)","Ad"=>"datetime","A0"=>"int(11)","A1"=>"int(11)","Ak"=>"varchar(32)","Ah"=>"tinyint(3)"],
	//店员=店铺，名称，用户，密码，微信，手机，权限，登录，未提，已提，链接，隐藏
	"class"=>["Si"=>"int(11)","Bn"=>"varchar(32)","Bp"=>"int(11)","Br"=>"int(11)","Bs"=>"int(11)","Bh"=>"int(11)"],
	//菜类=店铺，名称，父类，顺序，数量，隐藏
	"expend"=>["Si"=>"int(11)","Gc"=>"varchar(32)","Go"=>"int(11)","Gu"=>"varchar(32)","Gm"=>"int(11)","Gn"=>"int(11)","Gd"=>"datetime","Ge"=>"tinyint(3)"],
	//消费=店铺，卡号，单号，员工，实付，记账，日期，分账
	"coupon"=>["Cu"=>"varchar(32)","Cm"=>"int(11)","Cn"=>"int(11)","Cd"=>"date","Co"=>"tinyint(3)"],
	//卡券=卡号，金额，可用，期限，单号
	"foods"=>["Si"=>"int(11)","Dn"=>"varchar(64)","Dc"=>"varchar(64)","Ds"=>"varchar(255)","Di"=>"tinyint(3)","Dm"=>"int(11)","Dr"=>"int(11)","Dk"=>"varchar(64)","Dt"=>"varchar(255)","Dv"=>"varchar(64)","Da"=>"varchar(64)","Db"=>"varchar(255)","D1"=>"int(11)","D2"=>"int(11)","Dh"=>"tinyint(3)"],
	//菜品=店铺，名称，类别，说明，图片，价格，顺序，口味，套餐，打印，员工，库存，提服，提厨，隐藏
	"ofood"=>["Si"=>"int(11)","Fo"=>"int(11)","Ft"=>"int(11)","Fi"=>"int(11)","Fn"=>"varchar(64)","Fm"=>"int(11)","Fc"=>"int(11)","Fs"=>"tinyint(3)","F1"=>"int(11)","F2"=>"int(11)","F3"=>"int(11)","Fd"=>"datetime","Fr"=>"tinyint(3)","Fb"=>"varchar(255)"],
	//点菜=店铺，单号，桌号，菜ID，菜名，价格，数量，状态，下单，厨师，送餐，时间，退单，备注
	"order"=>["Si"=>"int(11)","Ot"=>"int(11)","O1"=>"int(11)","O4"=>"int(11)","Ox"=>"tinyint(3)","Pm"=>"int(11)","Pd"=>"int(11)","Pc"=>"int(11)","Ps"=>"varchar(64)","Pt"=>"tinyint(3)","Os"=>"tinyint(3)","Od"=>"datetime","Oe"=>"datetime","Ob"=>"text","Ou"=>"varchar(32)","Oo"=>"varchar(32)"],
	//点单=店铺，桌号，员工，收银，类型，金额，优惠，抵扣，实付，方式，状态，下单，支付，备注，会员，临时
	"osend"=>["Si"=>"int(11)","Hu"=>"int(11)","Ho"=>"int(11)","Hd"=>"text","Hm"=>"int(11)","Hc"=>"text","Hs"=>"tinyint(3)"],
	//外卖=店铺，员工，单号，信息，费用，跟踪，状态
	"pay"=>["Mc"=>"varchar(64)","Mp"=>"int(11)","Md"=>"datetime","Mt"=>"tinyint(3)","Nc"=>"varchar(64)","Np"=>"int(11)","Nd"=>"datetime","Nt"=>"tinyint(3)"],
	//支付=单号，金额，日期，方式，流水，实付，日期，状态
	"shop"=>["Si"=>"int(11)","Sn"=>"varchar(32)","Sw"=>"varchar(64)","Sa"=>"varchar(255)","Sb"=>"varchar(32)","Sx"=>"varchar(16)","Sy"=>"varchar(16)","Se"=>"int(11)","Sc"=>"text","Sh"=>"tinyint(3)"],
	//店铺=店ID，名称，微信，地址，电话，定位，定位，分账，设置，隐藏
	"stock"=>["Si"=>"int(11)","Kn"=>"varchar(32)","Kc"=>"varchar(32)","Ku"=>"varchar(32)","Ks"=>"int(11)","Kw"=>"int(11)","Kh"=>"tinyint(3)"],
	//库存=店铺，名称，类别，单位，数量，预警，隐藏
	"table"=>["Si"=>"int(11)","Tn"=>"varchar(32)","Tr"=>"int(11)","Tc"=>"tinyint(3)","Tu"=>"tinyint(3)","Ts"=>"tinyint(3)","Ti"=>"int(11)","Tm"=>"int(11)","Ta"=>"int(11)","Th"=>"tinyint(3)"],
	//餐桌=店铺，名称，顺序，座位，人数，状态，单号，金额，员工，隐藏
	"user"=>["Uc"=>"varchar(32)","Un"=>"varchar(32)","Up"=>"varchar(32)","Ud"=>"datetime","Us"=>"int(11)","Ux"=>"int(11)","Ut"=>"datetime","Ue"=>"int(11)","Uf"=>"varchar(255)"]
	//会员=卡号，名称，密码，日期，余额，消费，额度，信息
];
$m=file_get_contents("php://input");if($m=="")$m='{"act":"index"}';
$m=json_decode($m,true);if(json_last_error())die('{"msg":"Request JSON parse error."}');
switch($m["act"]){
	case "setup":
	if(function_exists("mysqli_connect")){
		$rc=mysqli_connect($m["Dh"],$m["Dn"],$m["Dp"],$m["Db"]) or die('{"msg":"数据库连接参数不正确！"}');
		foreach($db as $dk=>$dv){
			if($m["De"]==1)mysqli_query($rc,"Drop Table ".$m["Df"].$dk);
			mysqli_query($rc,"Create Table ".$m["Df"].$dk." (ID int NOT NULL AUTO_INCREMENT,PRIMARY KEY(ID)) ENGINE=MyISAM DEFAULT CHARSET=UTF8");
			foreach($dv as $dm=>$dn){
				mysqli_query($rc,"Alter Table ".$m["Df"].$dk." Add ".$dm." ".$dn);
				mysqli_query($rc,"Alter Table ".$n["Df"].$dk." Modify Column ".$dm." ".$dn);
			}
		}
		file_put_contents('config.php','<?php'.PHP_EOL.'$cfg=\'{"DB":{"host":"'.$m["Dh"].'","user":"'.$m["Dn"].'","pass":"'.$m["Dp"].'","name":"'.$m["Db"].'","pre":"'.$m["Df"].'"},"Ak":"'.md5($m["Ak"]).'","An":"'.$m["An"].'","Aw":"'.str_replace("/","\/",$m["Aw"]).'","Ai":0,"Ab":{"Bc":0,"Bs":[[100,10],[300,50],[500,100]]},"Ac":[],"Ag":{"Wa":"","Wk":"","W1":"","W2":"","We":0,"Wt":"","Bi":"","Bk":"","Be":0,"Bt":"","Ac":"","At":"您的手机号：{{1}}，验证码：{{2}}，请及时完成验证，如不是本人操作请忽略。【阿里云市场】"},"Ap":{"Fs":0,"Gi":"","Gk":"","Wc":"","Wi":"","Wk":"","Zp":"","Zk":"","Zi":"","Zm":"","Zn":""},"AL":""}\';');
		echo '{"msg":"ok"}';
		rename("install.php","install.bak");
	}else die('{"msg":"Mysql服务器不支持mysqli类！"}');
	break;

	default:
?>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>云端智能餐厅 - 安装</title>
<style type="text/css">
a:link,a:visited{color:#09F;text-decoration:none;}
a:hover,a:active{color:#F00;text-decoration:none;}
input[type=text],textarea{font-size:14px;padding:3px;width:250px;font-family:Consolas,微软雅黑;}
input[type=button]{font-size:14px;padding:6px 16px;font-family:Consolas,微软雅黑;}
textarea{height:72px;vertical-align:text-top;}
</style>
<script type="text/javascript">
var uri='install.php';var svr=document.URL.split('/');svr[svr.length-1]='';svr=svr.join('/');
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){alert('Create XHR object failed.');return;}x.open(p?'Post':'Get',u,true);x.setRequestHeader('Content-Type','application/x-www-form-urlencoded;');x.onreadystatechange=function(){if(x.readyState==4&&x.status==200){try{var s=JSON.parse(x.responseText);if(s.msg!='ok'){alert(s.msg);Ib.disabled=false;return;}}catch(e){alert('Response JSON parse error.\n'+x.responseText+'\n'+e);return;}f(s);}};x.send(p?JSON.stringify(p):null);}
function sdb(){Ib.disabled=true;var p={act:'setup',An:An.value,Ak:Ak.value,Aw:svr,Dh:Dh.value,Dn:Dn.value,Dp:Dp.value,Db:Db.value,Df:Df.value,De:De.checked?1:0,rnd:Math.random()};if(p.Dn==''||p.Dh==''||p.Dn==''||p.Dp==''||p.Db==''||p.Df==''){alert('安装参数不全！');Ib.disabled=false;return;}if(!pwd(p.Ak)){Ib.disabled=false;return;}xml(uri,p,function(r){Ib.style.display='none';Ia.style.display='inline-block';alert('安装已成功！');});}
function pwd(t){var a=/[0-9]+/.test(t);var b=/[a-z]+/.test(t);var c=/[A-Z]+/.test(t);var d=t.length>7;var e=a&&b&&c&&d;if(!e)alert('超管密码必需包含大小写英文字母及数字且不少于8位！');return e;}
</script>
</head>
<body style="margin:0px;padding:0px;text-align:center;font-size:14px;font-family:Consolas,微软雅黑;">
<div style="width:360px;margin:20px auto;padding:20px;border:1px solid #CCC;line-height:36px;text-align:left;">
<strong>云端智能餐厅 - 安装</strong> (Ver:<?php echo $ver;?>)<hr>
  环境检测： php_version=<span style="color:#6C3;"><?php echo PHP_VERSION;?></span>;<br>
  　　　　　 php_mysqli=<?php echo extension_loaded('mysqli')?'<span style="color:#6C3;">On</span>':'<span style="color:#F00;">Off</span>';?>;　php_curl=<?php echo extension_loaded('curl')?'<span style="color:#6C3;">On</span>':'<span style="color:#F00;">Off</span>';?>;<br>
  　　　　　 php_gd2=<?php echo extension_loaded('gd')?'<span style="color:#6C3;">On</span>':'<span style="color:#F00;">Off</span>';?>;　php_openssl=<?php echo extension_loaded('openssl')?'<span style="color:#6C3;">On</span>':'<span style="color:#F00;">Off</span>';?>;<br>
  系统名称： <input type="text" id="An" value="云端智能餐厅"><br>
  <strong>数据连接</strong>： <input type="checkbox" id="De"> 旧表删除<br>
  服务器名： <input type="text" id="Dh" value="127.0.0.1"><br>
  登录名称： <input type="text" id="Dn"><br>
  登录密码： <input type="text" id="Dp"><br>
  数据库名： <input type="text" id="Db"><br>
  表名前缀： <input type="text" id="Df" value="Jir_"><br>
  <strong>超管密码</strong>： <input type="text" id="Ak"><br>
  　　　　　 <input type="button" id="Ib" value="开始安装" onClick="sdb();"><input type="button" id="Ia" value="进入管理" onClick="window.open('admin.php');" style="display:none;">
</div>
</body>
</html>
<?php
}
?>