<?php
namespace App\Controller;
use Common\Controller\HanchaoController;
class OrderInfoController extends BaseController {
    public function _initialize(){
        parent::_initialize ();
    }
    public function index(){//线下订单
        $return=array('status'=>0,'msg'=>'');
        $paramdata = I('param.');
        $auth_code='orderinfo_list';
        $return=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$paramdata);
        if($return['status']==1) {
          $list['status']=1;
          $list=D('OrderInfo')->lists($paramdata);
          if($list){$return['status']=1; $return['data'] = $list;}
          else{$return=array('status'=>2,'msg'=>'暂无数据');}
          $this->ajaxReturn($return);
        }
        
    }
    public function get_order_status(){
        $return=array('status'=>3,'msg'=>'');
        $postdata=I('request.');
        $auth_code='weborder_list';
        $return=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$paramdata);
        if($return['status']==1) {
          $data=D('OrderInfo')->order_list($postdata);
        }
        if($data['status']==0){
            $return['msg']=$data['msg'];
            $this->ajaxReturn($return);
        }
        if($data['status']==2)$this->ajaxReturn($data);
        foreach($data['data']['list'] as $k => $v){
            $list[$k]=$v;
            $list[$k]['id']=$v['id'];
            $list[$k]['order_sn']=$v['order_sn'];
            $list[$k]['addtime']=time_format($v['addtime']);
            $list[$k]['goods_allmoney']=$v['goods_allmoney'];
            $list[$k]['order_amount']=$v['order_amount'];
            $list[$k]['send_type']=$v['send_type'];
        }
        $return['status']=1;
        $return['data']['list']=$list;
        $this->ajaxReturn($return);
    }
    public function order_info(){
        $return=array('status'=>3,'msg'=>'');
        $postdata=I('param.');
        $data=D('Pad/Cashier')->order_info($postdata);
        if($data['status']==0){
            $return['msg']=$data['msg'];
            $this->ajaxReturn($return);
        }
        if($postdata['order_id']) $operable_list=D('Pad/Cashier')->operable_list($postdata['order_id']);//获取订单可操作列表
        $return['status']=1;
        $return['data']['goods_list']=$data['data']['order_goods'];
        $return['data']['infos']=$data['data']['infos'];
        $return['data']['distribute_code']=C("DIS_TRIBUTE");
        if($operable_list['status']==1) $return['data']['operable_list']=$operable_list['data'];
        $this->ajaxReturn($return);
    }
   /**
    * 订单操作按钮列表
    */
   public function order_manage(){
        $return=array('status'=>3,'msg'=>'');
        $postdata=I('post.');
        switch($postdata['auth_code']){
            //线上
            case 'weborder_nopay':$auth_code='weborder_nopay';
               break;
            case 'weborder_confirm':$auth_code='weborder_confirm';
               break;
            case 'weborder_pay':$auth_code='weborder_pay';
               break;
            case 'weborder_ordertake':$auth_code='weborder_ordertake';
               break;
            case 'weborder_send':$auth_code='weborder_send';
               break;
            case 'weborder_prepare':$auth_code='weborder_prepare';
               break;
            case 'weborder_unship':$auth_code='weborder_unship';
               break;
            case 'weborder_receive':$auth_code='weborder_receive';
               break;
            case 'weborder_unship':$auth_code='weborder_unship';
               break;
            case 'weborder_receive':$auth_code='weborder_receive';
               break;
            case 'weborder_cancel':$auth_code='weborder_cancel';
               break;
            case 'weborder_invalid':$auth_code='weborder_invalid';
               break;
            //线下
            case 'orderinfo_nopay':$auth_code='orderinfo_nopay';
               break;
            case 'orderinfo_confirm':$auth_code='orderinfo_confirm';
               break;
            case 'orderinfo_pay':$auth_code='orderinfo_pay';
               break;
            case 'orderinfo_ordertake':$auth_code='orderinfo_ordertake';
               break;
            case 'orderinfo_send':$auth_code='orderinfo_send';
               break;
            case 'orderinfo_prepare':$auth_code='orderinfo_prepare';
               break;
            case 'orderinfo_unship':$auth_code='orderinfo_unship';
               break;
            case 'orderinfo_receive':$auth_code='orderinfo_receive';
               break;
            case 'orderinfo_unship':$auth_code='orderinfo_unship';
               break;
            case 'orderinfo_receive':$auth_code='orderinfo_receive';
               break;
            case 'orderinfo_cancel':$auth_code='orderinfo_cancel';
               break;
            case 'orderinfo_invalid':$auth_code='orderinfo_invalid';
               break;
        }
        $return=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$postdata);
        if($return['status']==1) {
           $result=D('Pad/Cashier')->order_manage($postdata);
        }
        if($result['status']==0 || $result['status']!=1){$return['msg']=$result['msg']; $this->ajaxReturn($return);}
        $map=array();
        $map['type']=array('in','6,12,14');
        $map['pay_status']=3;
        $map['shipping_status']=1;
        $map['shop_id']=$postdata['shop_id'];
        $dingdan_count=M('OrderInfo')->where($map)->count();
        $return['status']=1;
        $return['msg']=$result['msg'];
        $return['count']=$dingdan_count;
        $this->ajaxReturn($return);
   }
    /**
    * 获取快递
    */
   public function order_take(){
       $return=array('status'=>3,'msg'=>'');
       if(I('is_kuaidi')){
           $auth_code='weborder_send';
       }else{
           $auth_code='weborder_prepare';
       }
       $paramdata=I('post.');
       $return=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$paramdata);
       if($return['status']==1) {
           if (C("DIS_TRIBUTE") == 0) {
               $return['msg'] = '';
               $this->ajaxReturn($return);
           }
           $map = array();
           $map['status'] = 1;
           if (I('is_kuaidi')) {
               $shipping_list = M('Shipping')->where('status=1')->field('code,name')->select();
               foreach ($shipping_list as $k => $v) {
                   $shipping_info[$v['code']] = $v['name'];
               }
               $return['status'] = 1;
               $return['data'] = $shipping_info;
               $this->ajaxReturn($return);
           } else {
               $distribute = M('Distribute')->field("name,code")->where($map)->cache(true)->select();
               if (empty($distribute)) {
                   $return['msg'] = '';
                   $this->ajaxReturn($return);
               }
               $return['status'] = 1;
               $return['data'] = $distribute;
               $this->ajaxReturn($return);
           }
       }
   }
    /**
     * 发货
     */
    public function deliver_goods(){
        $return=array('status'=>3,'msg'=>'');
        $postdata=I('post.');
        if(empty($postdata['order_id'])){$return['msg']='参数错误'; $this->ajaxReturn($return);}
        $data['order_id']=$postdata['order_id'];
        $data['notice_sn']=$postdata['notice_sn'];
        $data['send_type']=$postdata['send_type'];
        $data['order_type']=idtoname($data['order_id'],'OrderInfo','type','id');
        $data['type']='Shop';
        $data['admin_id']=$postdata['sid'];
        $result=D("Common/OrderInfo")->ship_goods($data);
        if($result['status']){
            $return['status']=$result['status'];
            $return['msg']=$result['info'];
            $this->ajaxReturn($return);
        }else{
            $return['msg']=$result['info'];
            $this->ajaxReturn($return);
        }
    }
    /**
     * 退货
     */
    public function tui_goods(){
        $return=array('status'=>3,'msg'=>'');
        $postdata=I('post.');
        $auth_code='orderinfo_tui';
        $return=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$postdata);
        if($return['status']==1) {
            if (empty($postdata['order_id'])) {
                $return['msg'] = '参数错误';
                $this->ajaxReturn($return);
            }
            $result = D("Pad/Cashier")->tui_goods($postdata);
            if ($result['status']) {
                $return['status'] = $result['status'];
                $return['msg'] = $result['msg'];
            } else {
                $return['msg'] = $result['msg'];
            }
            $this->ajaxReturn($return);
        }
    }
    //提交投诉与咨询
    public function submit_service(){
        $return=array('status'=>3,'msg'=>'');
        $postdata=I('post.');
        $postdata['content']=$postdata['remarks'];
        if(empty($postdata['order_id']) && $postdata['type']!=1){$return['msg']='参数错误'; $this->ajaxReturn($return);}
        if(empty($postdata['content'])){$return['msg']='请输入内容，以便我们及时解决您的问题'; $this->ajaxReturn($return);}
        $data = D('Common/Member')->submit_service($postdata);
        if($data['status']){
            $return['status']=$data['status'];
            $return['msg']=$data['msg'];
        }else{
            $return['msg']=$data['msg'];
        }
        $this->ajaxReturn($return);
    }
    /**
    * 线上退货
    */
    public function xstui_goods(){
        $return=array('status'=>3,'msg'=>'');
        $postdata=I('post.');
        $auth_code='weborder_tui';
        $return=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$postdata);
        if($return['status']==1) {
            if (empty($postdata['order_id'])) {
                $return['msg'] = '参数错误';
                $this->ajaxReturn($return);
            }
            $result = D('Common/OrderInfo')->check_tui($postdata);
            if ($result['status']) {
                $return['status'] = $result['status'];
                $return['msg'] = $result['msg'];
            } else {
                $return['msg'] = $result['msg'];
            }
            $this->ajaxReturn($return);
        }
    }
}
?>