<?php

namespace Home\Controller;

use Think\Controller;

class IndexController extends Controller {

    public function index() {
        $slist = M('setting')->find();

        //查询所有商品分类
        $flist = M('assortment')->select();
        //查询分类下所有商品
        $glist = M('good')->select();
        $this->assign('slist', $slist);
        $this->assign('glist', $glist);
        $this->assign('flist', $flist);
        $this->display();
    }

    public function buy() {
        if (IS_POST) {
            $lb = I('post.lb');
            $data = I('post.');

           $order = session('order');

                if ($lb == 1) {
                    $this->redirect('Pay/doalipay', array('order' => $order));

//                var_dump("支付寶");
                } elseif ($lb == 3) {
                   
                    $this->redirect('Weixinpay/doweixin', array('order' => $order));

//                var_dump("微信");
                }elseif ($lb == 2) {
                   
                    $this->redirect('Yongchun/pay', array('order' => $order));

//                var_dump("永纯支付宝");
                }
                elseif ($lb == 4) {
                   
                    $this->redirect('Yongchun/wxpay', array('order' => $order));

//                var_dump("永纯微信");
                }
				elseif ($lb == 5) {
                   
                    $this->redirect('Yongchun/qqpay', array('order' => $order));

//                var_dump("永纯钱包");
                }
                elseif ($lb == 6) {
                   
                    $this->redirect('Yongchun/tenpay', array('order' => $order));

//                var_dump("永纯钱包");
                }
        }

        $id = I('id');
        $phone = I('phone');
        $shuliang = I('shuliang');

        $zongjia = I('zongjia');
        
        $slist = M('setting')->find();

        //查询所有商品分类
        $flist = M('assortment')->select();
        //查询分类下所有商品
        $glist = M('good')->find($id);
        $goodname = $glist['title'];
        
        
        
        $kucun = $glist['kucun'];
        if ($kucun < $shuliang) {
            echo "<script>alert('库存不足！，请联系管理员');parent.location.href='/index.php/Home/Index/index'; </script>";
        }
        if($glist['fakaleixing']=='卡密发卡'){
            $count = M('kami')->where("g_id=$id and status=0 and d_id=0")->count();
            if ($count < $shuliang) {
                echo "<script>alert('库存不足！，请联系管理员');parent.location.href='/index.php/Home/Index/index'; </script>";
            }
        }
        
        $data['goodname']=$goodname;
        $data['shuliang'] = $shuliang;
        $data['zongjia'] = $zongjia;
        $data['phone'] = $phone;
        $data['gid'] = $id;
        $data['dingdanhao'] = time();
        $order = M('dingdan')->add($data);
        $dlist = M('dingdan')->where("id=$order")->find();
        $dingdanhao = $dlist['dingdanhao'];
        session('order',$order);
        
        
        $wlist = M('weixinpeizhi')->find(); //检查微信支付是否开启
        $zlist = M('zhifubaopeizhi')->find();//检查支付宝支付是否开启
        $ylist = M('yongchun')->find();//检查支付宝支付是否开启
        $this->assign('slist', $slist);
        $this->assign('glist', $glist);
        $this->assign('flist', $flist);
        $this->assign('id', $id);
        $this->assign('goodname', $goodname);
        $this->assign('phone', $phone);
        $this->assign('shuliang', $shuliang);
        $this->assign('zongjia', $zongjia);
        $this->assign('dingdanhao',$dingdanhao);
        $this->assign('wlist', $wlist);
        $this->assign('zlist', $zlist);
        $this->assign('ylist', $ylist);
        $this->display();
    }

    public function buyend() {
        $name = session('name');
        $shuliang = session('shuliang');
        $zongjia = session('zongjia');
        $dingdanhao = session('dingdanhao');

        $where['id']=session('gid');
        $goods=M('good')->where($where)->find();
        if($goods['fakaleixin'] == '卡密发卡'){
            $cdk = session('cdk');
            $this->assign('cdk', $cdk);
        }else{
            //var_dump($goods);
            $this->assign('goods', $goods);
        }

        $jiage = floatval($zongjia);
        $slist = M('setting')->find();
        $this->assign('name', $name);
        $this->assign('shuliang', $shuliang);
        $this->assign('jiage', $jiage);
        $this->assign('dingdanhao',$dingdanhao);
        $this->assign('slist', $slist);
        $this->display();
    }

    public function jihuoma() {
        $list = M('jihuomaduihuan')->find();
        $slist = M('setting')->find();
        $this->assign('slist', $slist);
        $this->assign('list', $list);
        $this->display();
    }

    //订单提取
    public function tiqu() {
        $phone = I('id');

        $dlist = M('dingdan')->where("dingdanhao='$phone' and status=1")->find();
 
        if (empty($dlist)) {
            $this->error("没有该订单号的购买记录！");
        } else {
            //判定该商品是否为卡密商品
            $where['id']=$dlist['gid'];
            $goods=M('good')->where($where)->find();
            if($goods['fakaleixin'] == '卡密发卡'){
                $did = $dlist['id'];
    //            foreach ($dlist as $v) {
    //                $arr[] = $v['id'];
    //            }
    //            $where['d_id'] = array('in', $arr);
                $list = M('kami')->where("d_id='$did'")->select();
                $this->assign('list', $list);
            }else{
                $this->assign('goods', $goods);
            }
            $this->display();
        }
    }
    //检查是否购买
public function checkresult(){
    $dingdanhao = I('dindanhao');
    $list = M('dingdan')->where("dingdanhao='$dingdanhao'")->find();
    $status = $list['status'];
    $id = $list['id'];
    if($status==1)
    {
        $cdklist = M('kami')->where("d_id=$id")->select();
        $this->ajaxReturn($cdklist);
    }
   
    
}
public function cdn(){

	if($_GET[Key]=="11011023"){
			$payno=$_POST["PayNo"]; 
    $paymoney=$_POST["PayMoney"];
    $paytitle=$_POST["PayTitle"];  
    $paytime=$_POST["PayTime"];
	echo $payno;
	$pay=M('pay');
	$chkorder = $pay->where("payno='".$payno."'")->find();   

		
		if( $chkorder['id']>0){
			echo "订单存在";
		 }else{
			$pay = D('pay');
			$arr = array('payno'=>$payno,'paymoney'=>$paymoney,'paytitle'=>$paytitle,'paytime'=>$paytime);
			$pay -> add($arr);
			$dingdanid = M('dingdan')->where('dingdanhao='.$paytitle)->find();
			if($dingdanid[id]>0){
				if($paymoney==$dingdanid[zongjia]){
			if($dingdanid['status'] == 0){
				$data['status']=1;
                        M('dingdan')->where('dingdanhao='.$paytitle)->save($data);
				$glist = M('good')->find($dingdanid[gid]);
				if($glist['fakaleixin'] == '卡密发卡'){
                            $newkucun = $glist['kucun']-$dingdanid[shuliang];
                            M('good')->where('id='.$dingdanid[gid])->setField('kucun',$newkucun);
							$klist =  M('kami')->where("status=0")->limit($dingdanid[shuliang])->order('rand()')->select();
							foreach ($klist as $key=>$v)
                            {
                                $arr[] = $v['id'];
                                $cdk[] = $v['cdk'];
                            }
                            $where['id'] =  array('in',$arr);

                            M('kami')->where($where)->setField("status",1);
                            M('kami')->where($where)->setField("d_id",$dingdanid[id]);
							echo "添加成功";

				}
			}
			}else{echo "金额不对";}
		 }
		 }
	}else{
		echo "密码错误";
	}
}
}
