<?php
namespace app\index\controller;
use think\Db;
use think\Controller;
use think\View;
use app\index\model\Item as ItemModel;
use app\index\model\Alipaysubmit as SubmitModel;
use app\index\model\Notify as NotifyModel;
use app\index\model\Search as SearchModel;
use app\index\model\Km as KmModel;
use app\index\model\Order as OrderModel;
use app\index\model\Option as OptionModel;
use app\index\model\Category as CategoryModel;
use app\index\model\Link as LinkModel;
use app\admin\model\Article as ArticleModel;
class Index extends Controller
{
	function _initialize()
	{
		$res = (new OptionModel())->getinfo();
		$this->assign('siteinfo', $res);
	}
	public function index()
	{
		$res2 = (new CategoryModel())->getcat();
		$res = (new ItemModel())->itemall();
		$link = (new LinkModel())->getlink();
                
		$this->assign('res', $res);
		$this->assign('res2', $res2);
		$this->assign('link', $link);
		return view();
	}
	public function item($abridge)
	{
		$abridge = xss_clean($abridge);
		$res = (new ItemModel())->item($abridge);
		$this->assign('res', $res);
		$catname = (new CategoryModel())->getcatname($abridge);
		$this->assign('title', $catname['name']);
		return view();
	}
	public function trade($id)
	{
		$goodinfo = (new ItemModel())->getgoodinfo($id);
		if ($goodinfo['status'] == 1) {
			$this->error('此商品已下架');
			exit;
		}
		$kucun = (new KmModel())->kucun_km($id);
		$this->assign('goodinfo', $goodinfo);
		$this->assign('kucun', count($kucun));
		return view();
	}
	
	
	public function pay()
	{
		$siteinfo = (new OptionModel())->getinfo();
		$ddid = date("YmdHis") . mt_rand(10000, 99999);
		$tkcount = xss_clean($_POST['count']);
		$email = xss_clean($_POST['email']);
		if (preg_match('/[\\x{4e00}-\\x{9fa5}]/u', $email) > 0 || !$email) {
			$this->error('联系方式不得有中文或空格');
			exit;
		}
		if ($siteinfo['mailon'] == 1) {
			if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)) {
				$this->error('无效的email邮箱');
				exit;
			}
		}
		if ($siteinfo['maxsl'] != 0) {
			if ($tkcount > $siteinfo['maxsl']) {
				$this->error('超过单笔限制最大购买数量' . $siteinfo['maxsl'] . '件');
				exit;
			}
		}
		$pay_kmkucun = (new KmModel())->paykucun_km($_POST['goodid']);
		if ($tkcount > $pay_kmkucun) {
			$this->error('库存不足');
			exit;
		}
		$out_trade_no = $ddid;
		$res = (new ItemModel())->getgoodinfo($_POST['goodid']);
		if ($tkcount >= $res['mansl'] && $res['mansl'] && $res['yhprice']) {
			$res['price'] = $res['yhprice'];
		}
		$name = $res['name'];
		$money = $res['price'] * $tkcount;
		$type = $_POST['type'];
		$param = "自动发卡";
		$res = (new OrderModel())->insertorder(['ddid' => $out_trade_no, 'name' => $name, 'price' => $money, 'type' => $type, 'status' => 0, 'time' => date("Y-m-d H:i:s"), 'goodid' => $_POST['goodid'], 'email' => $email, 'count' => $_POST['count']]);
		if ($siteinfo['paytype'] == 2) {
			$bbspay_id = $siteinfo['bbspayid'];
			$bbspay_key = $siteinfo['bbspaykey'];
			$ip = $_SERVER["REMOTE_ADDR"];
			switch ($type) {
				case 'alipay':
					$paytype = 2;
					break;
				case 'wxpay':
					$paytype = 1;
					break;
				default:
					$paytype = 2;
					break;
			}
			$sign = md5($bbspay_id.$out_trade_no.$param.$paytype.$money.$bbspay_key);
			$jxpayurl = "mid=".$bbspay_id."&payId=".$out_trade_no.'&param='.$param.'&type='.$paytype."&price=".$money.'&sign='.$sign.'&notifyUrl='. $siteinfo['siteurl'] ."index/notify".'&returnUrl='. $siteinfo['siteurl'] ."index/returninfo".'&isHtml=1';
			header("Location:http://www.3mpay.com/createOrder?{$jxpayurl}");
			exit;
		} else {
			$notify_url = $siteinfo['siteurl'] . 'index/notify';
			$return_url = $siteinfo['siteurl'] . 'index/returninfo';
			$parameter = array("pid" => trim($siteinfo['partner']), "type" => $type, "notify_url" => $notify_url, "return_url" => $return_url, "out_trade_no" => $out_trade_no, "name" => $name, "money" => $money, "sitename" => $siteinfo['sitename']);
			$alipayinfo = array('notify_url' => $notify_url, 'return_url' => $return_url, 'partner' => $siteinfo['partner'], 'key' => $siteinfo['key'], 'sign_type' => strtoupper('MD5'), 'input_charset' => strtolower('utf-8'), 'transport' => 'http', 'apiurl' => $siteinfo['payapi']);
			$alipaySubmit = new SubmitModel($alipayinfo);
			$html_text = $alipaySubmit->buildRequestForm($parameter);
			echo $html_text;
		}
	}
	
	
	
	public function notify()
	{
		$siteinfo = (new OptionModel())->getinfo();
		$bbspay_id = $siteinfo['bbspayid'];
		$bbspay_key = $siteinfo['bbspaykey'];
		
		if ($siteinfo['paytype'] == 2) {
			$payId = $_GET['payId'];
			$param = $_GET['param'];
			$type = $_GET['type'];		
			$price = $_GET['price'];
			$reallyPrice = $_GET['reallyPrice'];
			$sign = $_GET['sign'];
			$_sign =  md5($bbspay_id.$payId.$param.$type.$price.$reallyPrice.$bbspay_key);
			
			if($_sign == $sign) {
				$out_trade_no = $_GET['payId'];
				$pay_no = $_GET['payId'];
				$res = (new OrderModel())->updateorder($out_trade_no, 'status', 1);
				$res2 = (new OrderModel())->orderinfo($out_trade_no);
				$yscount = (new KmModel())->yishoukami($out_trade_no);
				$res2['count'] = $res2['count'] - $yscount;
				if ($res2['yeskm'] == 0) {
					$res3 = (new KmModel())->kucun_km($res2['goodid']);
					for ($i = 0; $i < $res2['count']; $i++) {
						$kamiid = $res3[$i]['id'];
						$kahao = $res3[$i]['kahao'];
						$mima = $res3[$i]['mima'];
						$updata_km = (new KmModel())->update_km($kamiid, ['status' => 1, 'ddid' => $out_trade_no]);
					}
					$kmstatus = (new OrderModel())->updateorder($out_trade_no, 'yeskm', 1);
					$putpayno = (new OrderModel())->updatepayno($out_trade_no, $pay_no);
					$kmsales = (new ItemModel())->update_sales($res2['goodid'], $res2['count']);
					$kmdd = (new KmModel())->Kminfo($out_trade_no);
					
				}
			echo "success";
			}else{
				echo "fail";
			}
		}
		
		
		
		
		
	}
	public function returninfo()
	{
		$siteinfo = (new OptionModel())->getinfo();
		$bbspay_id = $siteinfo['bbspayid'];
		$bbspay_key = $siteinfo['bbspaykey'];
		if ($siteinfo['paytype'] == 2) {
			$payId = $_GET['payId'];
			$param = $_GET['param'];
			$type = $_GET['type'];
			$price = $_GET['price'];
			$reallyPrice = $_GET['reallyPrice'];
			$sign = $_GET['sign'];
			$_sign =  md5($bbspay_id . $payId . $param . $type . $price . $reallyPrice . $bbspay_key);
			if ($_sign != $sign) {
			  exit('验证失败');
			}else {
				$out_trade_no = $_GET['payId'];
				$this->success('付款成功,正在跳转订单页面', "/search.html?ddid={$out_trade_no}&page=1");
			}
		} 
		
	}
	public function search($ddid, $page = 1)
	{
		$ddid = xss_clean($ddid);
		$page = xss_clean($page);
		if (!$ddid) {
			$this->error('请输入订单号或联系方式');
			exit;
		}
		$orderinfo = (new SearchModel())->search($ddid, $page);
		$count = (new SearchModel())->countorder($ddid);
		$kminfo = (new KmModel())->Kminfo($ddid);
		if (!$count) {
			$this->error('没有该订单号的购买记录');
			exit;
		}
		$this->assign('orderinfo', $orderinfo);
		$this->assign('count', count($count));
		return view();
	}
	public function fenge()
	{
		if (request()->ispost()) {
			$char = implode("", $_POST);
			echo '<pre>';
			$arr = explode("\r\n", $char);
			for ($i = 0; $i < count($arr); $i++) {
				$arr[$i] = explode(" ", $arr[$i]);
			}
			print_r($arr);
		}
		return view();
	}
	public function kminfo($ddid)
	{
		$kminfo = (new KmModel())->Kminfo($ddid);
		return json($kminfo);
	}
	public function article($id)
	{
		$res = (new ArticleModel())->getart($id);
		$this->assign('res', $res);
		return view();
	}
}
?>