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

namespace Uapp\Controller;
class TakeoutController extends BaseController {
	public function _initialize(){
    	parent::_initialize ();
	}
	public function index(){
		$return=array();
		$param = I('param.');
		if($param['uid'] && !empty($param['uid']) && $param['uid']!='false' && $param['uid']!=false){
            $user_info=$this->checkuserauth();
        }else{$user_info=session('member_info');}
		// 
        $map = array();
        $map['id'] = array('gt', 0);
        $map['status'] = 1;
        $map['pid'] = 0;
        $cate = M('PurchaseCategory')->where($map)->field('id,name')->order('sort asc,id asc')->select();
        if($cate){
            foreach ($cate as $key => $value) {
                $map = array();
                $map['id'] = array('gt', 0);
                $map['status'] = 1;
                $map['pid'] = $value['id'];
                $cate[$key]['cate_son_list']=M('PurchaseCategory')->where($map)->field('id,name')->order('sort asc,id asc')->select();
            }
        }
		$param['uid']=$user_info['id'];
		// $param['shop_id']=$user_info['HCshop']['bind_shop_id'];//追加当前会员匹配的门店
		// $param['shop_id']=1;//追加当前会员匹配的门店
        // 7-5 rank_id
		$result = D('Wxsmall/Takeout')->index($param,$user_info['rank_id']);
        $goods_list=array();
        if($result['data']['goods_list']){
            foreach ($result['data']['goods_list'] as $key => $value) {
                if($result['data']['init_list'][$value['id']]['num'] && $value['id']==$result['data']['init_list'][$value['id']]['id']){
                    $result['data']['goods_list'][$key]['cart_num']=$result['data']['init_list'][$value['id']]['num'];
                }
                $result['data']['goods_list'][$key]['s_price']=format_price(idtoname($value['id'],'Goods','shop_price'));
            }
        }
        // echo "<pre/>";print_r($result['data']['goods_list']);exit();
		$result['HCshop'] = $user_info['HCshop'];//当前会员匹配的门店存储到客户端
        $cart_info=D("Wxsmall/Takeout")->get_cart_total_info($user_info['id']);
        $result['cart_info']=$cart_info;
        $result['cate_list']=$cate;
        $result['list_tpl_app']=C('LIST_TPL_APP');
        // $result['list_tpl_app']='shops_index_blue';
		$this->ajaxReturn($result);
	}
    
	public function add_carts(){
        $param=I('param.');
        if($param['goods_attr']){
           $goods_attr= json_decode(htmlspecialchars_decode($param['goods_attr']),true);
        }
        if($goods_attr){
            $param['spec']=$goods_attr['key'];
            $param['name']=$goods_attr['name'];
        }
        $user_info=$this->checkuserauth();
        $param['uid']=$user_info['id'];
        $param['shop_id']=$user_info['HCshop']['bind_shop_id'];//追加当前会员匹配的门店
        session('main_shop_id',$param['shop_id']);
        $param['is_wxsmall']=1;
        $data=D("Common/Takeout")->add_cart_item($param);
        if($data['status']!=1){$data['status']=3;}else{$data['info']='加入购物车成功';}
        $cart_info=D("Wxsmall/Takeout")->get_cart_total_info($user_info['id']);
        $data['cart_info']=$cart_info;
        $this->ajaxReturn($data);
    }
    /**
     * 商品详情页
     */
    public function cont(){
        $param=I('param.');
        $user_info=array();
        if($param['uid']!='false' && !empty($param['uid']) && $param['uid'] && $param['uid']!=false){
            $user_info=$this->checkuserauth();
            // zjn 购物车商品
            $cart_num = D("Wxsmall/Takeout")->get_cart_total_info($param['uid']);
            $result['cart_num']=$cart_num['cart_num'];
        }
        // $param['shop_id']=$user_info['HCshop']['bind_shop_id'];//追加当前会员匹配的门店
        
        $result = D('Wxsmall/Takeout')->cont($param);
        if($result['status']==1){
            // $content= D('Wxsmall/Takeout')->get_goods_content($param);
            $map=array();
            $map['goods_id']=$param['id'];
            $content=M("GoodsContent")->where($map)->getField('content');
            $str_content=$this->format_content($content);
            $result['goods_info']['content']=$str_content;
            if($param['uid']!='false'){
                $price_info=D('Common/Takeout')->get_min_price($param['shop_id'],$param['id']);
                $result['goods_info']['min_price']=$price_info['goods_price'];
            }
            $result['goods_info']['now_time']=NOW_TIME;;
            $result['goods_attrs']['combination']=json_decode($result['goods_attrs']['combination'],true);
            $result['goods_attrs']['combinationprice']=json_decode($result['goods_attrs']['combinationprice'],true);
            $cart_info=D("Wxsmall/Takeout")->get_cart_total_info($param['uid']);
            $result['cart_info']=$cart_info;
            $result['goods_info']['shop_price']=idtoname($param['id'],'Goods','shop_price');
            $re_data=is_sanji($param['uid'],$result['goods_info']);
            $lshop_id=0;
            if($user_info['is_littleshop']==1){
                $lshop_id=intval(idtoname($param['uid'],'LittleShop','id','uid',false));
                $lmap=array();
                $lmap['shop_id']=$lshop_id;
                $lmap['goods_id']=$param['id'];
                $result['lmap']=$lmap;
                $lg_info=M('LittleShopGoods')->where($lmap)->find();
                $result['lg_info']=$lg_info;
            }
            $re_data['is_show']=intval($lshop_id);
            $result['re_data']=$re_data;
            $share_url=geturl('Mobile/Goods/goods_cont',array('id'=>$param['id'],'eid'=>$param['uid']),true,true);
            $result['share_url']=$share_url;
            //判断是否收藏
            $is_collect=D("Common/Goods")->is_collect($param['id'],0);
            $result['is_collect']=$is_collect;
            $result['common_list']=D('Mobile/Dp')->get_goods('',$param['id'],0);
            if($result['common_list']['count_dp']){
                foreach ($result['common_list']['dp_list'] as $key => $value) {
                    $result['common_list']['dp_list'][$key]['username']=idtoname($value['uid'],'Member','username;nickname;mobile;real_name');
                    $result['common_list']['dp_list'][$key]['xing']=$value['dp_point']/5*100;
                    $result['common_list']['dp_list'][$key]['addtime']=time_format($value['addtime']);
                    if($value['pl_img']){
                        foreach ($value['pl_img'] as $k => $value) {
                            $img=format_url($value);
                            $img=str_replace('https','http',$img);
                            $result['common_list']['dp_list'][$key]['imgjson'][$k]=$img;
                        }
                    }
                }
            }
            if($result['common_list']['dp_group']['preset_list']){
                foreach($result['common_list']['dp_group']['preset_list'] as $key=>$val){
                    $result['common_list']['dp_group']['preset_list'][$key]=$val."(".get_tag_num($val,1,$param['id']).")";
                }
            }
            // $result['dp_count']=$result['common_list']['count_dp'];
        }
        $this->ajaxReturn($result);
    }
    public function update_myshop(){
        $param=I('param.');
        $param['shop_id']=$param['lshop_id'];
        $result = D('Takeout')->update_goods($param);
        if($result['status']!=1)$result['status']=3;
        $this->ajaxReturn($result);
    }
    // 图文分享zjn
    public function cont_tgercode(){
        $return=array();
        $param=I('param.');
        $result = D('Takeout')->cont_tgercode($param);
        $this->ajaxReturn($result);
    }
}
?>