<?php
/**
 * 订单管理
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author zhaoh <1070309178@qq.com>
 */

namespace Mobile\Controller;
use Think\Controller;
class PurOrderController extends ShopBaseController {
	public function _initialize(){
		parent::_initialize ();
        if (!checksiteauth('DingHuo,MenDian|GongHuoShang|CangKu')) {
            exit;
        }
        if(!MID){
			$this->redirect('Mobile/Login/shop_index');
		}
		if($m_info['is_tmp']==1&&!$m_info['mobile']){
			$this->redirect('Mobile/Login/perfect');
		}elseif($m_info['is_tmp']==1&&$m_info['mobile']){
			$this->error('您已经领取过会员卡了！');exit;
		}
        $this->assign('tplseo_title','确认订单'.'_'.C('WEB_SITE_TITLE'));
        $this->assign('page_title','确认订单');
   	}
    public function index(){
        $map['id']=intval($this->shops_info['shop_id']);
        $shop_info=M("Shops")->where($map)->find();
        $ids = I('ids');
        $invoice_title=I("invoice_type");
        $this->assign("invoice_title",$invoice_title);
        //发票信息
        $invoce_map['uid']=$shop_info['id'];
        $invoce_info=M("MemberInvoice")->where($invoce_map)->field(true)->find();
        $this->assign("data",unserialize($invoce_info['invoice_info']));
        //立即购买信息
        $goods_id=I("goods_id");
        $data=I("get.");
        $addres_id=I('addres_id');
        if($addres_id)$this->assign('addres_id',$addres_id);
        $this->assign('order_datas',10);
        $this->assign("o_param",$data);

        $result = D('Common/OrderInfo')->porder_list($ids,$goods_id,$type,$addres_id);//获取订单信息
        if($result['data']['status']==0){
            $this->error($result['data']['msg'],get_forward());
        }
        $this->assign($result);
        $this->assign('address_list',$result['address_list']);
        $this->assign('ids',$ids);
        if(!empty($shop_info['paypassword'])){
            $this->assign('userpaypass',1);
        }
        $this->assign('shops',$shop_info);
        $this->display();
    }

    /**
     * 发票
     */
    public function order_invoice(){
        $this->assign('page_title','发票信息');
        $user_info=idtoname($this->member_info['id'],'Member','*');
        $ids = I('ids');
        $data=I('get.');
        $goods_id=I("goods_id");
        $type=I("type");
        $result = D('Common/OrderInfo')->order_list($ids,$goods_id,$type,'');//获取订单信息
        $shop_goods=$result['data']['shopgoods'];
        foreach($shop_goods as $key=>$val) {
            if ($key > 0) {
                foreach ($val['goods_ids'] as $kk => $v) {
                    $map['id'] = $v;
                    $goods_info = M("Goods")->where($map)->field('goods_name,img')->find();
                    $goods[$kk]['goods_id'] = $v;
                    $goods[$kk]['goods_name'] = $goods_info['goods_name'];
                    $goods[$kk]['goods_img'] = $goods_info['img'];
                }
                $shop_goodsinfo[$key] = $goods;
            }
        }
        $invoce_map['uid']=$user_info['id'];
        $invoce_info=M("MemberInvoice")->where($invoce_map)->field(true)->find();
        $this->assign('shop_goods',$shop_goodsinfo);
        $this->assign('ids',$ids);
        $this->assign("order_param",json_encode($data));
        $this->assign(unserialize($invoce_info['invoice_info']));
        $this->display();
    }
    public function sub_vat(){
        $root=array('status'=>0,'msg'=>'');
        $post=I("post.");
        $user_info=idtoname($this->member_info['id'],'Member','*');
        $invoce_map['uid']=$user_info['id'];
        $data['invoice_info']=serialize($post);
        $invoce_info=M("MemberInvoice")->where($invoce_map)->field(true)->find();
        if($invoce_info){
            $invoce_map['id']=$invoce_info['id'];$data['update_time']=NOW_TIME;
            $info=M("MemberInvoice")->where($invoce_map)->save($data);
        }else{
            $data['uid']=$user_info['id'];
            $info=M("MemberInvoice")->add($data);
        }
        if($info){
            $root['status']=1;
            $root['msg']="保存成功";
        }else{
            $root['msg']="保存失败";
        }
        $this->ajaxReturn($root);
    }
    //计算总价
    public function count_total(){
        $return=array();
        $param = I('param.');
        $cart_ids=I('ids')?I('ids'):0;
        $payment_id=I('payment_id');
        $delivery_id=I('addr_id');
        $score=I('score')?1:0;
        $bonus=I('bouns');
        $shop_id=I('shop_id');
        $bonu=array_combine($shop_id,$bonus);
        $use_account=I('use_account')?1:0;
        $account_money=I('account_money');
        $type=$param['type'];
        $data=I('order_info');
        $info=array();
        $info['cart_ids'] = $cart_ids;
        $info['data'] = $data;
        $info['type'] = intval($type);
        $info['bonu'] = $bonu;
        $info['score'] = $score;
        $info['use_account'] = $use_account;
        $info['account_money'] = $account_money;
        $info['payment_id'] = $payment_id;
        $info['delivery_id'] = '';
        $result = D('Common/OrderInfo')->count_total($info);
        $this->assign($result['total_info']);
        $html=$this->fetch('cart_total');
        $return['html']=$html;
        $return['total_info']=$result['total_info'];
        $this->ajaxReturn($return);
    }
    /**
     * 提交订单
     */
    public function sub_order(){
        $cart_ids=I('ids');
        $param = I('post.');
        $param['data']=I("order_info");
        $result= D("Common/OrderInfo")->sub_order($param);
        $this->ajaxReturn($result);
    }

    /**
     *支付成功
     */
    public function order_success(){
        $order_id=I("order_ids");
        $pay_id=I("id");
        if($order_id){
            $this->assign('page_title','支付结果');
            $o_where['is_delete'] = 0;
            $o_where['order_id'] = $order_id;
        }
        if($pay_id){
            $this->assign('page_title','立即支付');
            $o_where['id']=$pay_id;
            $this->assign('pay_id',$pay_id);
        }
        $pay_info = M("PayLog")->where($o_where)->field(true)->find();
        if($pay_info){
            $p_map['id']=$pay_info['payment_id'];
            $payment_info=M("Payment")->where($p_map)->find();
            $this->assign('payment_info',$payment_info);
        }else{
            $this->error("支付信息错误");
        }
        $this->assign('pay_info',$pay_info);

        $this->display();
    }
    /**
     * 继续付款
     */
    public function continu_pay(){
        $order_id=I("order_id");
        $res=D("Common/OrderInfo")->continu_pay($order_id);
        $this->ajaxReturn($res);
    }
    /**
     * 继续付款页面加载
     */
    public function continue_payhtml(){
        $map['id']=intval($this->shops_info['shop_id']);
        $shop_info=M("Shops")->where($map)->find();
        $order_id=I("order_id");
        $result=D("Common/OrderInfo")->get_continue($order_id);
        if($result['status']==0){
            $this->error("参数错误",$result['url']);
        }
        $order_info=$result['data'];
        $this->assign('order_info',$order_info);
        $this->assign('order_id',$order_id);
        $this->assign('is_addcart',$result['is_addcart']);
        $this->assign('shops',$shop_info);
        $this->display("continue_pay");
    }

    /**
     * 继续付款计算总价
     */
    public function count_order_total(){
        $order_id=I("ids");
        $payment_id=I('payment_id');
        $use_account=I('use_account')?1:0;
        $score=0;
        $type=I('type');
        $account_money=I('account_money');
        $result = D('Common/OrderInfo')->count_order_total($order_id,$type,$score,$use_account,$account_money,$payment_id,10);
        $this->assign($result['total_info']);
        $html=$this->fetch('cart_total');
        $return['html']=$html;
        $return['total_info']=$result['total_info'];
        //print_r($result);
        $this->ajaxReturn($return);
    }
    //继续付款提交
    public function sub_continue_order(){
        $param=I("post.");
        $result= D("Common/OrderInfo")->sub_continue_order($param);
        $this->ajaxReturn($result);
    }
}
?>