<?php
/** App登录控制器
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaoq <xiaoq@570035208.com>
 */

namespace Wxsmall\Controller;
class OrderController extends BaseController {
	public function _initialize(){
        	parent::_initialize ();
    	}
	/**
	 * 订单页
	 */
	public function index(){
		$param = I('param.');
		$user_info=$this->checkuserauth();
		$param['uid']=$user_info['id'];
		$param['HCshop']=$param['HCshopid'];
        if(empty($param['delivery_id']))$param['delivery_id']=0;
		$result = D('OrderInfo')->index($param);
        $result['HCshop'] = $user_info['HCshop'];//当前会员匹配的门店存储到客户端
		$this->ajaxReturn($result);
	}

    /**
     * 计算价格
     */
    public function count_total(){
        $param=I('post.');
        $user_info=$this->checkuserauth();
        $param['bonu']['id']=$param['bonus_id'];
        $param['bonu']['money']=$param['bonus_money'];
        $param['uid']=$user_info['id'];
        $data=array();
        $data['goods_id']=$param['goods_id'];
        $data['type']=$param['type'];
        $data['number']=$param['number'];
        $data['goods_attr']=$param['goods_attr'];
        $param['data']=$data;
        $info=D('Common/OrderInfo')->count_total($param);
        $this->ajaxReturn($info);
    }
    public function sub_order(){
        $return=array('status'=>0);
        $param=I('post.');
        $param['addr_id']=$param['delivery_id'];
        $param['boun']['id']=$param['bonus_id'];
        $param['boun']['money']=$param['bonus_money'];
        $param['ids']=$param['cart_ids'];
        $param['source']=3;
        if($param['best_time_day'] || $param['best_time_hour']){
            $bast_day = date('Y-m-d',$param['best_time_day']);
            $bast_hour = date('H:i',$param['best_time_hour']);

            $param['best_time'] = strtotime($bast_day.' '.$bast_hour);
            unset($param['best_time_day'],$param['best_time_hour']);
        }
        if(empty($param['send_type']) || $param['send_type']==2){
            if($param['addres_status']==1){
                $param['send_type']=2;
            }elseif($param['addres_status']==2){
                $param['send_type']=3;
            }
        }
        $user_info=$this->checkuserauth();
        $param['uid']=$user_info['id'];
        $param['shop_id']=$param['HCshopid'];
        $data=array();
        $data['goods_id']=$param['goods_id'];
        $data['type']=$param['type'];
        $data['number']=$param['number'];
        $data['goods_attr']=$param['goods_attr'];
        $param['order_info']=$data;
        $info=D('Common/OrderInfo')->sub_order($param);
        if($info['status']==1){
            $return['status']=1;
            $return['data']['order_id']=$info['data'];
            $return['data']['pay_id']=$info['pay'];
            $this->ajaxReturn($return);
        }else{
            if($info['app_paydata']=='app_notpay'){
                $return['data']['app_pay']=1;
            }else{
                $return['data']['app_pay']=0;
            }
            if($info['wxsmall']==3){
                $return['data']['url']=3;
            }else{
                $return['data']['url']=0;
            }
            $return['msg']=$info['msg'];
            $this->ajaxReturn($return);
        }
        $this->ajaxReturn($return);
    }
    /**
     * 支付成功
     */
    public function order_success(){
        $return=array('status'=>1);
        $order_id=I('order_id');
        if(empty($order_id)){
            $return['status']=3;
            $return['msg']='参数错误';
        }
        $order_id_true=array_filter(explode(',',$order_id));
        $o_where=array();
        $o_where['is_delete'] = 0;
        $o_where['order_id'] = $order_id;
        $pay_info = M("PayLog")->where($o_where)->field(true)->find();
        if($pay_info){
            $map=array();
            $map['id']=$pay_info['payment_id'];
            $payment_info=M("Payment")->where($map)->field(true)->find();
            if($pay_info['is_paid']==1 && $pay_info['pay_code']!='Hanchaoline'){
                $pay_info['msg']='支付成功';
            }else{
                $pay_info['msg']=$payment_info['name'].'付款'.format_price($pay_info['pay_amount']);
            }
            $add_points = look_give_double_points($order_id_true[1]);
            $return['data']=$pay_info;
            $return['data']['give_points']=$add_points;
            $return['data']['payment_info']=$payment_info;
        }else{
            $return['status']=3;
            $return['msg']='支付信息错误';
        }
        $this->ajaxReturn($return);
    }
    /**
     * 取消订单
     */
    public function cancel_order(){
        $return=array('status'=>1);
        $postdata['order_id']=I('order_id');
        $postdata['admin_id']=MID;//操作日志用
        $postdata['type']='Member';//操作日志用
        $result=D('Common/OrderInfo')->cancel_order($postdata);
        if($result['status']==0){
            $return['status']=3;
            $return['msg']=$result['msg'];
        }
        $this->ajaxReturn($return);
    }

    /**
     * 继续付款
     */
    public function continu_pay(){
        $return=array('status'=>1);
        $order_id=I("order_id");
        $user_info=$this->checkuserauth();
        $param['uid']=$user_info['id'];
        $param['order_id']=$order_id;
        $res=D("Common/OrderInfo")->continu_pay($order_id);
        if($res['status']==1){
            $return['data']=$res['order_id'];
        }else{
            $return['status']=3;
            $return['msg']=$res['msg'];
        }
        $this->ajaxReturn($return);
    }

    /**
     * 继续付款页面
     */
    public function continue_payhtml(){
        $return=array('status'=>1);
        $order_id=I('order_id');
        $result=D("Common/OrderInfo")->get_continue($order_id);
        if($result['status']==0){
            $return['status']=3;
            $return['msg']=$result['msg'];
        }else{
            if($result['data']){
                foreach($result['data'] as $key=>$val){
                    $result['data'][$key]['shop_name']=idtoname($val['shop_id'],"Shops",'name');
                    $result['data'][$key]['best_time']=time_format($val['best_time']);
                    $result['data'][$key]['order_amount']=format_price($val['order_amount']);
                    $result['data'][$key]['goods_allmoney']=format_price($val['goods_allmoney']);
                    foreach($result['data'][$key]['goods_list'] as $k=>$va){
                        $result['data'][$key]['goods_list'][$k]['img']=thumb($va['img']);
                    }
                }
            }
            $user_info=$this->checkuserauth();
            $return['data']=$result['data'][0];
            $return['data']['memberinfo']=$user_info;
        }
        $this->ajaxReturn($return);
    }

    /**
     * 继续付款计算价格
     */
    public function continue_count_total(){
        $param=I('post.');
        $order_id=$param['order_id'];
        $type=$param['type'];
        $score=$param['score'];
        $use_account=$param['use_account'];
        $payment_id=$param['payment_id'];
        $result = D('Common/OrderInfo')->count_order_total($order_id,$type,$score,$use_account,'',$payment_id);
        if($result['total_info']){
            $result['total_info']['total_price']=format_price($result['total_info']['total_price']);
            $result['total_info']['pay_price']=format_price($result['total_info']['pay_price']);
            $result['total_info']['account_money']=format_price($result['total_info']['account_money']);
        }
        $this->ajaxReturn($result);
    }
	public function sub_continue_order(){
        $return=array();
        $param=I('post.');
        $param['ids']=$param['order_id'];
        $res=D("Common/OrderInfo")->sub_continue_order($param);
        if($res['status']==1){
            $return['status']=1;
            $return['data']['order_id']=$res['data'];
            $return['data']['pay_id']=$res['pay'];
        }else{
            if($res['app_paydata']=='app_notpay'){
                $return['data']['app_pay']=1;
            }else{
                $return['data']['app_pay']=0;
            }
            if($res['wxsmall']==3){
                $return['data']['url']=3;
            }else{
                $return['data']['url']=0;
            }
            $return['status']=0;
            $return['msg']=$res['msg'];
        }
        $this->ajaxReturn($return);
    }

    /**
     * 确认收货
     */
    public function confirm_order(){
        $param=I('param.');
        $postdata['order_id']=I('order_id');
        $postdata['admin_id']=MID;//操作日志用
        $postdata['type']='Member';//操作日志用
        $result=D('Common/OrderInfo')->confirm_order($postdata);
        $this->ajaxReturn($result);
    }

    /**
     * 发票
     */
    public function order_invoice(){
        $return=array('status'=>3);
        $ids = I('cart_ids');
        $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'] = format_url($goods_info['img']);
                }
                $shop_goodsinfo[$key] = $goods;
            }
        }
        $user_info=$this->checkuserauth();
        $invoce_map['uid']=$user_info['id'];
        $invoce_info=M("MemberInvoice")->where($invoce_map)->field(true)->find();
        $return['status']=1;
        $return['data']['shop_goodsinfo']=$shop_goodsinfo;
        $return['data']['invoce_info']=unserialize($invoce_info['invoice_info']);
        $this->ajaxReturn($return);
    }

    /**
     * 保存发票
     */
    public function sub_invoice(){
        $root=array('status'=>0,'msg'=>'');
        $post=I("param.");
        $user_info=$this->checkuserauth();
        $invoce_map['uid']=$user_info['id'];
        $data['invoice_info']=serialize($post);
        $invoce_info=M("MemberInvoice")->where($invoce_map)->field(true)->find();
        if($post['email']){
            $regular=new \Common\Vendor\Regular;
            if(!$regular->isEmail($post['email'])){
                $root['msg']="电子邮箱格式错误";
                $this->ajaxReturn($root);
            }
        }
        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);
    }
}