<?php

require_once "ourphpapi.php";

/*
 * OURPHP 逻辑模块
 * 验证开始
*/

if(isset($_POST['id'])){
	$shopid = implode(',',$_POST["id"]);
}else{
	$shopid = 0;
}

if($_POST['tuanset'] == 1){
    $tuanset = 'usertuanlist';
}else{
    $tuanset = 'usershopping';
}

$idmd5 = $_POST['idmd5']; 
$coupon = $_POST['coupon']; 
$state = $shopid.'_'.$idmd5."_".$coupon;

$rs = $db -> select("`id`,`OP_Add`,`OP_Addtel`,`OP_Addname`","`ourphp_usershopadd`","where OP_Addindex = 1 && `OP_Adduser` = '".$_SESSION['username']."'");
if(!$rs){
	exit("<script language=javascript> alert('先创建物流信息!~');history.go(-1);</script>");
}


$getinfo = explode('_',$state);

if ($getinfo[0] == 0){
	exit("请先选购商品!");
}

if(preg_match('/[a-zA-Z]/',$getinfo[0])){
	exit("no2!"); 
}else{
	$shopid = $getinfo[0];
}

$idsafe = str_replace(",","",$shopid);
$idsafe = md5($idsafe.$ourphp['safecode']);
if($getinfo[1] != $idsafe){
	exit("参数出错!");
}

$stocknumno = $ourphp_adminfont['stocknumno'];
$query = $db -> listgo("`OP_Ordersnum`,`OP_Ordersusermarket`,`OP_Ordersfreight`,`OP_Ordersid`,`OP_Ordersproductatt`,`OP_Orderswebmarket`","`ourphp_orders`","where `id` in (".$shopid.") && OP_Ordersemail = '".$_SESSION['username']."' && OP_Orderspay = 1");
$zj = '';
while($ourphp_rs = $db -> whilego($query)){
	$zj += ($ourphp_rs[0] * $ourphp_rs[1]) + $ourphp_rs[2];	
	
		$prinfo = $db -> select("OP_Stock,`OP_Specifications`","ourphp_product","where id = ".intval($ourphp_rs[3]));
		if($ourphp_rs[4] == '')
		{
			if($prinfo[0] < $ourphp_rs[0])
			{
				exit("<script language=javascript> alert('".$stocknumno."');history.go(-1);</script>");
			}			
		}else{
			
			$newkucun = str_replace("、", ",", $ourphp_rs[4]).','.$ourphp_rs[5].','.$ourphp_rs[1];
			$kucun = explode("|", $prinfo[1]);
			foreach($kucun as $op)
			{
				if(strpos($op,$newkucun) !== false){ 
					$kucun2 = explode(",",$op);
					if(end($kucun2) < $ourphp_rs[0])
					{
						exit("<script language=javascript> alert('".$stocknumno."');history.go(-1);</script>");
					}
				}
			}
			
		}
}

if($zj < 0.01){
	exit("<script language=javascript> alert('金额不能低于0.01元');history.go(-1);</script>");
}

$zje = $zj * 100;

//优惠券
if($getinfo[2] == 'ourphp'){ 
	
	$zje = $zje;
	$coupon = explode('|','0|0|0');
	
}else{
	
	$coupon = explode('|',$getinfo[2]);
	if($zje < $coupon[2]){
		
		exit("<script language=javascript> alert('优惠券已过期,无法使用!~');history.go(-1);</script>");
		
	}else{
		
		$c = $db -> select("`id`,`OP_Timewin`","`ourphp_couponlist`","where `id` = ".intval($coupon[0])." && `OP_Type` = 0 && `OP_Md` = '".dowith_sql($coupon[1])."' && `OP_Username` = '".$_SESSION['username']."'");
		if($c){
			
			$date = date("Y-m-d H:i:s");
			if($c[1] != '0000-00-00 00:00:00'){
				if(strtotime($date) > strtotime($c[1])){
					exit("<script language=javascript> alert('优惠券已过期,无法使用!~');history.go(-1);</script>");
				}
			}
			
			$zje = $zje - $coupon[2];
			
		}else{
			
			exit("优惠券出错!");
			
		}
		
	}
	
}

$pay = array(
				"safeinfo",
				$shopid,
				$safecode['id'],
				$safecode['md5code'],
				$coupon[0],
				$coupon[2],
				$rs[0],
				$zje,
);

?>
<script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.3.2.js"></script>
<script type="text/javascript">
        wx.miniProgram.getEnv(function (res) {
            if (res.miniprogram) {
                wx.miniProgram.navigateTo({
                    url: '/pages/pay/pay?safeinfo=<?php echo $pay[0];?>|<?php echo $pay[1];?>|<?php echo $pay[2];?>|<?php echo $pay[3];?>|<?php echo $pay[4];?>|<?php echo $pay[5];?>|<?php echo $pay[6];?>|<?php echo $pay[7];?>|<?php echo $_SESSION['username'];?>|<?php echo $tuanset;?>'
                })
            }
        })
</script>