<?php
/**
 * 积分商品列表
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author shixiaoli <1094186793@qq.com>
 */

namespace Home\Controller;
use Think\Controller;
class FightGroupController extends BaseController {
    public function index(){
        $map=array();
        $join='';
        $order='';
        $param=I('param.');
        if(empty($param['cat_id']))$param['cat_id']=0;
        if(empty($param['brand_id']))$param['brand_id']=0;
        $p=$param['p'];
        $catid=$param['cat_id'];
        /*排序开始*/
        $orders=D('Common/Goods')->cate_order($param);
        /*排序结束*/
        //价格分级开始
        $price_list = get_price_map($catid,$param['price']);
        if(!empty($price_list['shop_price']))$map['shop_price']=$price_list['shop_price'];
        //价格分级结束
        /*属性筛选开始*/
        $type_id=get_cate_type($catid);//类型ID
        $attr_list=D('Common/GoodsAttr')->get_attr($param,$type_id);
        if(!empty($attr_list)){
            $map['_string']=$attr_list['_string'];
        }
        //属性筛选结束
        //关键词筛选
        if(!empty($param['keyword']))$map['goods_name']=array('like','%'.$param['keyword'].'%');
        //分类筛选
        if(!empty($param['cat_id']))$map['cat_id']=$param['cat_id'];
        //品牌筛选
        if(!empty($param['brand_id']))$map['brand_id']=$param['brand_id'];
        //排序
        if($param[$param['order_name']]==1){$order_type='desc';
		}elseif($param[$param['order_name']]==2){$order_type='asc';}
        if(!empty($param['order_name']) && $order_type)$order=$param['order_name'].' '.$order_type;
        
        $goodslist=D('Common/PromotExt')->get_fight_list($map,$join,$order,$p);
        $seodata=D('Common/Goods')->format_listseotile($param['brand_id'],$catid,1);
        $catpos_title='拼团商品';
       
        //分类
        $where['pid']=0;
        $where['status']=1;
        $cat_list=M('GoodsCategory')->where($where)->field('id,name')->select();
        $this->assign('cat_list',$cat_list);
        $this->assign('catpos_title',$catpos_title);
        $this->assign('pricelist', empty($price_list['pricelist'])?array():$price_list['pricelist']);
        $this->assign($goodslist);
        $this->assign('orders',$orders);
        $this->assign('type_id',$type_id);
        $this->assign($seodata);
        $this->assign('param',$param);
        $this->assign($param);
        $this->display();
    }
    public function cont(){
        $goods_id=intval(I('get.id'));
        //获取商品基本信息
        $goods_info=D("Common/PromotExt")->get_fight($goods_id);
        if($goods_info['status']){
            //团长列表
            $group_id = $goods_info['data']['cid'];
            $map['pt_id'] = $group_id;   //活动id
            $map['pid'] = 0;             //团长
            $map['status'] = 2;          //已确认
            $map['shipping_status'] = 5; //待成团
            $map['pay_status'] = 3;      //已付款
            $group_list = M('OrderInfo')->where($map)->select();
            if(!empty($group_list)){
                foreach ($group_list as $key => $val) {
                    $url = U('Mobile/FightGroup/group_join',array('order_id'=>$val['id'],'eid'=>$val['uid']),true,true);
                    $group_list[$key]['img'] = phpgrcode(urlencode($url),'','4','img_'.$val['uid']);
                }
            }
            //二维码
            $memberinfo=session('member_info');
            $url = U('Mobile/FightGroup/cont',array('id'=>$goods_id,'eid'=>$uid),true,true);
            $img_url = phpgrcode(urlencode($url),'','4','wximg_'.$memberinfo['id']);

            //浏览历史
            $historyids=session('historyids');
            $historyids=empty($historyids)?array():$historyids;
            $historyids[]=$goods_id;
            $historyids=array_unique($historyids);
            session('historyids',$historyids);
            $goods_imgs = M("GoodsGallery")->where("goods_id=" . $goods_id)->select();
            foreach($goods_imgs as $key=>$val){
                $goods_imgs[$key]['goods_img']=$val['goods_img'];
            }
            if (empty($goods_imgs)) $goods_imgs[0]['goods_img'] = $goods_info['data']['img'];
            //获取商品属性和规格
            $data = D("Common/GoodsAttr")->getattr($goods_id);
             // print_r($data);
            //判断该地区是否有货
            if(cookie('dingwei_address')){
                $address_info= cookie('dingwei_address');
            }elseif(session('dingwei_address')){
                $address_info= session('dingwei_address');
            }
            if($address_info['district']){
                $area_id=$address_info['district'];
            }else{
                $area_id=$address_info['city'];
            }
            $root=D("Common/Cart")->get_area_fee($goods_info['data']['shop_id'],$area_id);
            $price_info=D("Common/Cart")->getminprice($goods_id);//print_r($price_info);//print_r($goods_info['data']);
            //print_r($price_info);
            $this->assign('group_list',$group_list);
            $this->assign('img_url',$img_url);
            $this->assign($data);
            $this->assign('price_info',$price_info);
            $this->assign("is_stock",$root['is_stock']);
            $this->assign('attr_list', $data['newarr']);//属性
            $this->assign('goods_info', $goods_info['data']);//商品信息
            $this->assign('goods_imgs', $goods_imgs);//商品相册

        }else{
            $this->error($goods_info['msg'],U($goods_info['url']));
        }
        $this->display();
    }
    /**
       *关注收藏
       */
    public function favorite(){
        $goods_id=I('id');
        $type=I('type');
        $is_on=I('is_on');
        $data=D('Common/Goods')->favorite($goods_id,$type,$is_on);
        $this->ajaxReturn($data);
    }
   
}