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

namespace Mobile\Controller;
use Think\Controller;
class PurOrderInfoController extends ShopBaseController {
	public function _initialize(){
		parent::_initialize ();
        if (!checksiteauth('DingHuo,MenDian|GongHuoShang|CangKu')) {
            exit;
        }
		if(!MID){
			cookie('forward',U('Mobile/'.CONTROLLER_NAME.'/'.ACTION_NAME));
			$this->redirect('Mobile/Login/shop_index');
		}
		if($this->m_info['is_tmp']==1&&!$this->m_info['mobile']){
			$this->redirect('Mobile/Login/perfect');
		}elseif($this->m_info['is_tmp']==1&&$this->m_info['mobile']){
			$this->error('您已经领取过会员卡了！');exit;
		}
        $this->assign('uid',MID);
		$this->assign('tplseo_title','我的订单'.'_'.C('WEB_SITE_TITLE'));
		$this->assign('page_title','我的订单');
   	}
	//全部订单
    public function index($is_ajax = 0){
		$postdata=I('param.');
		if($postdata['no_ship']){$type=3;}
		$order_list=D('Common/OrderInfo')->all_order($postdata,$type,'',10,true,10);
		if ($is_ajax == 1) return $order_list;
		$this->assign($order_list);
		$this->assign($postdata);
		$list_html=$this->fetch('index_list');
        $this->assign('list_html',$list_html);
	    $this->display();
    }
	public function get_index_list(){
        $return['status'] = 0;
        $data = $this->index(1);
        if (empty($data['list'])) $this->ajaxReturn($return);

        $return['status'] = 1;
        $this->assign($data);
        $list_html = $this->fetch('index_list');
        $return['data'] = $list_html;
        $this->ajaxReturn($return);
    }
	//待付款订单
	public function no_pay($is_ajax = 0){
		$postdata=I('param.');
		$order_list=D('Common/OrderInfo')->all_order($postdata,2,'',10,true,10);
		if ($is_ajax == 1) return $order_list;
		$this->assign($order_list);
		$list_html=$this->fetch('index_list');
        $this->assign('list_html',$list_html);
        $this->assign('tplseo_title','待付款订单'.'_'.C('WEB_SITE_TITLE'));
        $this->assign('page_title','待付款订单');
	    $this->display();
    }
    //待发货订单
    public function shipment_pending($is_ajax = 0){
        $postdata=I('param.');
        $order_list=D('Common/OrderInfo')->all_order($postdata,3,'',10,true,10);
        if ($is_ajax == 1) return $order_list;
        $this->assign($order_list);
        $list_html=$this->fetch('index_list');
        $this->assign('list_html',$list_html);
        $this->assign('tplseo_title','待发货订单'.'_'.C('WEB_SITE_TITLE'));
        $this->assign('page_title','待发货订单');
        $this->display();
    }
	public function get_no_pay_list(){
        $return['status'] = 0;
        $data = $this->no_pay(1);
        if (empty($data['list'])) $this->ajaxReturn($return);

        $return['status'] = 1;
        $this->assign($data);
        $list_html = $this->fetch('index_list');
        $return['data'] = $list_html;
        $this->ajaxReturn($return);
    }
    public function shipment_pending_list(){
        $return['status'] = 0;
        $data = $this->shipment_pending(1);
        if (empty($data['list'])) $this->ajaxReturn($return);
        $return['status'] = 1;
        $this->assign($data);
        $list_html = $this->fetch('index_list');
        $return['data'] = $list_html;
        $this->ajaxReturn($return);
    }
	//待收货订单
	public function yes_ship($is_ajax = 0){
		$postdata=I('param.');
		$order_list=D('Common/OrderInfo')->all_order($postdata,4,'',10,true,10);
		if ($is_ajax == 1) return $order_list;
		$this->assign($order_list);
		$list_html=$this->fetch('index_list');
        $this->assign('list_html',$list_html);
        $this->assign('tplseo_title','待收货订单'.'_'.C('WEB_SITE_TITLE'));
        $this->assign('page_title','待收货订单');
        $this->display();
    }
	public function get_yes_ship_list(){
        $return['status'] = 0;
        $data = $this->yes_ship(1);
        if (empty($data['list'])) $this->ajaxReturn($return);

        $return['status'] = 1;
        $this->assign($data);
        $list_html = $this->fetch('index_list');
        $return['data'] = $list_html;
        $this->ajaxReturn($return);
    }
	//待评价订单
	public function no_comment($is_ajax = 0){
		$postdata=I('param.');
		$order_list=D('Common/OrderInfo')->all_order($postdata,5,'',10);
		if ($is_ajax == 1) return $order_list;
		$this->assign($order_list);
		$list_html=$this->fetch('index_list');
        $this->assign('list_html',$list_html);
        $this->assign('tplseo_title','待评价订单'.'_'.C('WEB_SITE_TITLE'));
        $this->assign('page_title','待评价订单');
        $this->display();
    }
	public function get_no_comment_list(){
        $return['status'] = 0;
        $data = $this->no_comment(1);
        if (empty($data['list'])) $this->ajaxReturn($return);

        $return['status'] = 1;
        $this->assign($data);
        $list_html = $this->fetch('index_list');
        $return['data'] = $list_html;
        $this->ajaxReturn($return);
    }
	//订单详情
	public function order_info(){
		$id=I('param.id');
		$order_info=M('OrderInfo')->where('id='.$id)->field(true)->find();
		if($order_info['is_delete']==1)$this->redirect('OrderInfo/index');
		$order_goods=M('OrderGoods')->where('order_id='.$id)->field(true)->select();
		foreach($order_goods as $k=>$v){
			$attr=unserialize($v['goods_attr']);
			$order_goods[$k]['img']=idtoname($v['goods_id'],'Goods','img');
			$order_goods[$k]['attr_name']=$attr['name'];
            $order_goods[$k]['goods_number']=intval($order_goods[$k]['goods_number']);
		}
		$this->assign($order_info);
		$this->assign('goods_info',$order_goods);
		$this->assign('tplseo_title','订单详情'.'_'.C('WEB_SITE_TITLE'));
		$this->assign('page_title','订单详情');
		$this->display();
    }
	/**
     * 删除订单
     */
    public function del_order(){
		$postdata['order_id']=I('order_id');
		$postdata['admin_id']=MID;//操作日志用
		$postdata['type']='Shops';//操作日志用
        $return=D('Common/OrderInfo')->del_order($postdata);
        $this->ajaxReturn($return);
    }
	/**
     * 取消订单
     */
    public function cancel_order(){
		$postdata['order_id']=I('order_id');
		$postdata['admin_id']=MID;//操作日志用
		$postdata['type']='Member';//操作日志用
        $return=D('Common/OrderInfo')->cancel_order($postdata);
        $this->ajaxReturn($return);
    }
	/**
     * 确认收货
     */
	public function confirm_order(){
		$postdata['order_id']=I('order_id');
		$postdata['admin_id']=MID;//操作日志用
		$postdata['type']='Shops';//操作日志用
        $postdata['og_id']=I('og_id');
        $postdata['number']=I('number');
		$result=D('Common/OrderInfo')->pconfirm_order($postdata);
		$this->ajaxReturn($result);
    }
	/**
     * 退款审核页面
     */
    public function refund_content(){
        $order_id=I("order_id");
        $order_goods=I("order_goods");
        $score=I("score");
        $this->assign("order_id",$order_id);
        $this->assign("order_goods",$order_goods);
        $this->assign("score",$score);
		if($score=='th'){
			$this->assign('types','退货');
			$this->assign('tplseo_title','退货审核'.'_'.C('WEB_SITE_TITLE'));
			$this->assign('page_title','退货审核');
		}elseif($score=='tk'){
			$this->assign('types','退款');
			$this->assign('tplseo_title','退款审核'.'_'.C('WEB_SITE_TITLE'));
			$this->assign('page_title','退款审核');
		}
        $this->display();
    }
	/**
     * 退款审核操作
     */
    public function sub_refund(){
        $data=I("post.");
        $order_info=$refund_data=array();
        $order_info[$data['data']['order_id']]=$data['data']['order_goods'];
        $refund_data['order_info']=$order_info;
        $refund_data['content']=$data['content'];
        $refund_data['type']=$data['type'];
        $refund_data['order_type']=10;
        $return=D('Common/OrderInfo')->check_refund_money($refund_data);
        $this->ajaxReturn($return);
    }
	/**
     * 退款
     */
    public function refund_money(){
        $data=I("post.");
        $return=D('Common/OrderInfo')->refund_audit($data);
        $this->ajaxReturn($return);
    }
	/**
     * 退货
     */
    public function refund_goods(){
        $data=I("post.");
        $return=D('Common/OrderInfo')->apply_return($data);
        $this->ajaxReturn($return);
    }
	/**
     * 评论页面
     */
	public function comment(){
		$order_id=I('param.order_id');
		$status=M('OrderInfo')->where('id='.$order_id)->getField('is_comment');
		if($status)$this->error('订单已评论',U('OrderInfo/no_comment'));
		
		$field='og.id,og.order_id,og.goods_id,og.goods_name,og.goods_number,og.goods_price,og.goods_attr,g.img,g.dp_count,g.avg_score,g.avg_point';
		$goods_info=M('OrderGoods')->alias('og')->where('order_id='.$order_id)->field($field)->join('LEFT JOIN __GOODS__ g on og.goods_id=g.id')->select();
		foreach($goods_info as $k=>$v){
			$goods_attr=unserialize($v['goods_attr']);
			$goods_info[$k]['goods_attr']=$goods_attr['name'];
			$goods_info[$k]['avg_point']=$v['avg_point']*100;
		}
		$this->assign('order_id',$order_id);
		$this->assign('list',$goods_info);
        $this->assign('tplseo_title','评论'.'_'.C('WEB_SITE_TITLE'));
        $this->assign('page_title','评论');
        $this->display();
    }
	/**
     * 添加评论
     */
	public function add_comment(){
		$postdata=I('param.');
		$return=D('Common/OrderInfo')->add_comment($postdata);
		if($return['status']==1)$return['url']=U('OrderInfo/no_comment');
		$this->ajaxReturn($return);
	}
}
?>