<?php
/**
 * 消息中心
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author lss <lss@qq.com>
 */

namespace Shop\Controller;
use Think\Controller;
class ThreeOrderController extends BaseController {
	public function _initialize()
    {
        parent::_initialize();
        $shops_info=session('shops_info');
        if(empty($shops_info['id']))$this->error('身份过期，请重新登录',U('Shop/Register/shop_login'));
		$this->mendian();
        $this->order_code=array('Jddj'=>'京东到家','ele'=>'饿了吗');$this->assign('order_code',$this->order_code);
    }
    public function order_list(){
        $postdata=I('param.');
        $tabid=$postdata['tabid'];
        if($tabid=='order_info'){
            $postdata['order_id']=I('id');
            $tabid='info';
            $order_info=D('Pad/ThreeOrder')->order_info($postdata);
            $order_info['data']['infos']=$this->common_check($order_info['data']['infos']);
            $this->assign($order_info['data']['infos']);           
        }else{
            if($tabid=='tabid_1')$postdata['order_status']=1;
            if($tabid=='tabid_2')$postdata['order_status']=2;
            if($tabid=='tabid_3')$postdata['order_status']=3;
            if($tabid=='tabid_4')$postdata['order_status']=4;

            if($tabid=='tabid_5')$postdata['order_status']=5;
            if($tabid=='tabid_7')$postdata['order_status']=7;
            if($tabid=='tabid_8')$postdata['order_status']=8;
            if($tabid=='tabid_1000')$postdata['order_status']=1000;
            $tabid='index'; 
            $all_order=D('Pad/ThreeOrder')->order_list($postdata);
            if($all_order['data']['list']){
                foreach ($all_order['data']['list'] as $key => $value) {
                    $all_order['data']['list'][$key]['goods']=$this->common_check($value);
                }
            }
            $this->assign($all_order['data']);
        } 
        $tabid = $tabid ? $tabid : 'index';        
        $this->assign($postdata);
        $this->display($tabid);
    }
    public function manage(){
        $postdata=I('param.');
        if(empty($postdata['order_id']))$this->error('参数不正确');
        $shops_info=session('shops_info');
        $postdata['operator']=$shops_info['login_name'];
        $postdata['content']='不同意取消订单';
        $result=D('Pad/ThreeOrder')->three_manage($postdata);
        if($result['status']){
            $this->success($result['info']);
        }else{
            $this->error($result['info']);
        }
    }
    //*格式化数据
    public function common_check($data){
        $order_info=$data;
        $group=unserialize($order_info['goods_info']);

        $order_info['list']=$group;
        foreach ($order_info['list'] as $key => $value) {
            if($value['act_good_ids']){
                $val=$value['act_good_ids'];
                $order_info['list'][$key]['act_good_ids']=$val;

                if(strpos($val,"-")){
                    $new_arr = $arr = array();
                    $new_arr = explode('-',$val);
                    $arr['attr_key'] = $new_arr[1];
                    $arr['goods_id'] = $new_arr[0];
                    $order_info['list'][$key]['img'] = idtoname($new_arr[0],'Goods','img');
                    $order_info['list'][$key]['goods_id'] = $new_arr[0];
                    $order_info['list'][$key]['goodsname'] = idtoname($new_arr[0],'Goods','goods_name');
                    $order_info['list'][$key]['attr']='('.attr_key2name($arr['attr_key'],$arr['goods_id']).')';
                    $order_info['list'][$key]['goods_sn']=idtoname($arr['attr_key'],'GoodsAttrStock','goods_attr_sn','attr_key',false);
                }else{
                    $order_info['list'][$key]['goods_id'] = $val;
                    $order_info['list'][$key]['img'] = idtoname($val,'Goods','img');
                    $order_info['list'][$key]['goodsname'] = idtoname($val,'Goods','goods_name');
                    $order_info['list'][$key]['goods_sn']=idtoname($val,'Goods','goods_sn','id',false);
                }
            }else{
                $whereor=array();
                $whereor['goods_name']=$value['goods_name'];
                $goods=M('Goods')->where($whereor)->field(true)->find();
                if(empty($goods)){$whereor=array();$whereor['goods_sn']=$value['goodssn'];$goods=M('Goods')->where($whereor)->field(true)->find();}
                if($goods){
                    $order_info['list'][$key]['goods_id'] = $goods['id'];
                    $order_info['list'][$key]['goodsname'] = $goods['goods_name'];
                    $order_info['list'][$key]['goods_sn']=$goods['goods_sn'];
                }else{
                    $order_info['list'][$key]['goodsname'] = '请匹配该商品';
                }                
            }
        }
        $order_info['three_status']=$order_info['order_status'];
        if($order_info['order_status']==8)$order_info['three_status']=3;
        if($order_info['order_status']==6)$order_info['three_status']=1;
        return $order_info;
    }
}
?>