<?php
/**
 * 商品内容
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaoq <570035208@qq.com>
 */

namespace Home\Controller;
use Think\Controller;
class PurGoodsController extends PurBaseController {
    public function index(){
        $goods_id=I('id');

        if(C('IS_WAREHOUSE')==1){// 判断该商品是否在配送范围
            $shops_info=session('shops_info');
            $area = idtoname($shops_info['shop_id'],'Shops','province,city_id,district');
            $area_id='';
            if($area['district']){
                $area_id = $area['district'];
            }
            if(!$area_id){
                $area_id = $area['city_id'];
            }
            if(!$area_id){
                $area_id = $area['province'];
            }
            $ids = library_goods($area_id);
			$goods_stock = '';
            if(count($ids)>0 && in_array($goods_id,$ids)){
				$goods_stock = idtoname($goods_id,'StorageStock','stock','goods_id');
				$this->assign('goods_stock', $goods_stock);//仓库库存
            }else{
                $this->error('该商品不在配送范围');exit;
            }
        }
        //获取商品基本信息
        $goods_info=D("Common/PurGoods")->get_goods_item($goods_id);
        if($goods_info['status']){
            $historyids=session('phistoryids');
            $historyids=empty($historyids)?array():$historyids;
            $historyids[]=$goods_id;
            $historyids=array_unique($historyids);
            session('phistoryids',$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);

            $price_info=D("Common/Cart")->getminprice($goods_id);//print_r($goods_info['data']);
            // print_r($price_info);
            // 起订量
            $moq = getmoq($goods_id);
            $goods_info['data']['moq'] = $moq['moq'];
            $goods_info['data']['unit_name'] = $moq['unit_name'];
            $goods_info['data']['shop_price'] = $moq['shop_price'];
            // 供货商信息
            $supp_info = M("Suppliers")->where("id=".$goods_info['data']['suppliers_id'])->field('name,phone')->find();
            $this->assign($data);
            $this->assign('price_info',$price_info);
            $this->assign('attr_list', $data['newarr']);//属性
            $this->assign('goods_info', $goods_info['data']);//商品信息
            $this->assign('goods_imgs', $goods_imgs);//商品相册
            $this->assign('supp_info', $supp_info);//供货商信息

        }else{
            $this->error($goods_info['msg'],U($goods_info['url']));
        }
        $seodata=D('Common/PurGoods')->format_contseotitle($goods_info['data']);
        $this->assign($seodata);
        $this->display();
    }
    /**
     * 活动
     */
    public function ajax_active(){
        $goods_id=I('goods_id');
        $shop_price=I('shop_price');
        $activelist=D("Common/Promot")->goods_to_promot($goods_id,1);
        //判断是否有限时促销
        $active=$promotion=array();
        foreach($activelist['list'] as $key=>$val){
            if($val['style']==5){
                $active[]=$val;
                unset($activelist['list'][$key]);
            }
        }
        if($active){
            foreach($active as $k=>$v){
                if($k==count($active)-1){
                    $remarks=unserialize($v['remarks']);
                    $v['remarks']=format_decimal($remarks['promot_price'][$goods_id]);
                    $promotion=$v;
                }
            }
        }
        //优惠券
        $ecv_list=D("Common/Promot")->goods_to_ticket($goods_id,idtoname($goods_id,'Goods','shop_id'));
        //会员红包
        $member=session("member_info");
        if($member){
            $bonus_map['user_id']=$member['id'];
            $memberbonus=M("MemberBonus")->where($bonus_map)->getField('bonus_id',true);
        }

        $promotion_remarks=$promotion['remarks'];
        $return['promotion_end_time']=$promotion['end_time'];//限时促销
        $return['promotion_remarks'] = empty($promotion_remarks)?$promotion_remarks=0:$promotion_remarks;//限时促销
        $price_info=D("Common/Cart")->getminprice($goods_id);
        $return['price_type']=$price_info['type'];
        $View= new \Common\Vendor\TplView();
        $View->assign('activelist',$activelist);//活动
        $View->assign('ecv_list',$ecv_list);//优惠券
        $View->assign('memberbonus',$memberbonus);//会员红包
        $View->assign('promotion',$promotion);//限时促销
        $View->assign('price_type',$price_info['type']);//限时促销
        $return['html']=$View->fetch('pro_act');
        $this->ajaxReturn($return);
    }

    /**
     * 刚加载的时候需要ajax请求的数据（普通商品和积分商品都要用）
     */
    public function ajax_data(){
        $collect['goods_id']=I('goods_id');
        $collect['type']=I('type');
        $return['collect_info']=$this->ajax_is_collect($collect);
        $shop['shop_id']=I('shop_id');
        $shop['goods_shop_catid']=I('goods_shop_catid');
        $return['shop_info']=$this->ajax_shop_info($shop);
        $this->ajaxReturn($return);
    }

    /**
     * 判断是否收藏
     */
    public function ajax_is_collect($collect){
        $goods_id=$collect['goods_id'];
        $type=$collect['type'];
        $is_collect = D('Common/PurGoods')->is_collect($goods_id,$type);
        $data['is_collect'] = $is_collect;
        return $data;
    }

    /**
     * 店铺信息
     */
    public function ajax_shop_info($data){
        $return=array();$return['status']=0;
        $shop_id=$data['shop_id'];$shop_cat_id=$data['goods_shop_catid'];
        $member=session('member_info');
        $shop=D("Common/Shops")->get_shop($shop_id,$member['id']);
        if($shop['status']==0)return $return;
        $return['status']=1;
        $this->assign('data',$shop['data']);
        $shop_info_html = $this->fetch("ajax_shop_info");
        $return['shop_info_html'] = $shop_info_html;
        if(!empty($shop['data']['cates'])) {
            $this->assign('cate', $shop['data']['cates']);
            $this->assign('shop_cat_id',$shop_cat_id);
            $shop_cat_html = $this->fetch("ajax_shop_cat");
            $return['shop_cat_html'] = $shop_cat_html;
        }
        return $return;
    }

    /**
     * 会员领取优惠券
     */
    public function get_ecv(){
        $bouns_id=I('id');
        $data=D("Common/Promot")->member_get_bonus($bouns_id);
        $this->ajaxReturn($data);
    }

    /**
     * 加入购物车
     */
    public function addcart(){
        $goods_id=I('goods_id');
        $goods_attr=I('goods_attr');
        $number=I('number');
        $name=I('name');
        $pack_id=I('pack_id');
        $type=I('type');
        if($type>0){
            $shops_info = session('shops_info');
            if(empty($shops_info)){
                $ret['msg'] = '请先登录';
                $ret['status']=0;
                $ret['url'] = getjumpurl('JUMP_8');
                $this->ajaxReturn($ret);
            }
        }
        $model=D("Common/Cart");
        $return=$model->puraddcart($goods_id,$goods_attr,$number,$name,'',$pack_id);
        $this->ajaxReturn($return);
    }

    /**
     * 列表页属性弹窗
     */
    public function goods_attrs(){
        $goods_id=I('goods_id');
        $number=I('number');
        if(!$goods_id){
            $result['status']=1;
            $result['msg']='参数错误';
        }
        //获取商品属性和规格
        $data = D("Common/GoodsAttr")->getattr($goods_id);
        $goods_info=D("Common/PurGoods")->get_goods_item($goods_id);
        //判断是否有限时促销
        $active=array();
        foreach($activelist['list'] as $k=>$val){
            if($val['style']==5){
                $active[]=$val;
            }
        }
        if($active){
            foreach($active as $k=>$v){
                if($k==count($active)-1){
                    if(!empty($v['remarks']))$remarksarr=unserialize($v['remarks']);
                    $v['remarks']=empty($v['remarks'])?$v['remarks']=0:format_decimal($remarksarr['promot_price'][$goods_id],false,' ');
                    $promotion=$v;
                }
            }
        }
       if(empty($promotion)){$promotion=0;}
        $promotion_remarks=$promotion['remarks'];
        $shops_info=session("shops_info");
        if($shops_info){
            $m_map['id']=$shops_info['shop_id'];
            $rank_id=M("Member")->where($m_map)->getField('rank_id');
            $r_map['id']=$rank_id;
            $rank=M("MemberRank")->where($r_map)->getField("goods_price");
            $member_price=format_price($goods_info['data']['shop_price']*$rank,false,' ');
        }
        $this->assign('combinationnum', $data['combinationnum']);
        $this->assign('combinationprice', $data['combinationprice']);
        $this->assign('combinationpricenum', $data['combinationpricenum']);
        $this->assign('combination', $data['combination']);
        $this->assign('attr_list', $data['newarr']);//属性
        $this->assign('activelist', $activelist);//活动
        $this->assign('promotion', $promotion);//活动
        $this->assign('promotion_remarks', empty($promotion_remarks)?$promotion_remarks=0:$promotion_remarks);//活动
        $this->assign('number', $number);//数量
        $this->assign('goods_id', $goods_id);//商品id
        $this->assign('goods_info', $goods_info['data']);//商品信息
        $this->assign('rank',empty($rank)?$rank=0:$rank);//会员价
        $this->display();
    }

    /**
     * 列表页加入购物车
     */
    public function add_cartlist(){
        $goods_id=I('goods_id');
        $number=I('number');
        $pack_id=I('pack_id');

        // print_r(I('post.'));exit;
        $model=D("Common/Cart");
        $return=$model->padd_cartlist($goods_id,$number,$pack_id);
        $this->ajaxReturn($return);
    }

    /**
     * 立即购买
     */
    public function buy_now(){
        $return=I("get.");
        $goods_id=I('goods_id');
        $goods_attr=I('goods_attr');
        $number=I('number');
        $name=I('name');
        $model=D("Common/Cart");
        $pack_id =I('pack_id');
        if(C('IS_WAREHOUSE')==1){
            $return_link=$model->check_carts($goods_id,$goods_attr,$number,$name,0,$pack_id);
        }else{
            $return_link=$model->check_cart($goods_id,$goods_attr,$number,$name,0,2,$pack_id);
        }

        $return=array_merge($return,$return_link);
        $this->ajaxReturn($return);
    }
}
?>