<?php
// +----------------------------------------------------------------------
// | 订货首页、列表页控制器
// +----------------------------------------------------------------------
// | ThinkOAO （Online And Offline）
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.hanchao9999.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: xiaogg <xiaogg@sina.cn>
// +----------------------------------------------------------------------
namespace Mobile\Controller;
class PurIndexController extends ShopBaseController
{
    public function _initialize()
    {
        parent::_initialize();
        if (!checksiteauth('DingHuo,MenDian|GongHuoShang|CangKu')) {
            exit;
        }
    }
    /**
    **订货首页
    **/
    public function index()
    {
        $where = array();
        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);
            if(count($ids)>0){
                $where['id']=array('in',$ids);
            }else{
                $where['id']='bcz';
            }
        }
        $where['goods_type'] = 5;
        $where['publish_wait'] = 1;
        $where['status'] = 1;
        $where['is_hot'] = 1;
        $goodslist = M('Goods')->where($where)->select();
        $shops_info = session('shops_info');
        $map['user_id'] = $shops_info['shop_id'];
        if($goodslist){
            foreach ($goodslist as $k => $v) {
                $map['goods_id'] = $v['id'];
                $goodslist[$k]['number'] = M('Cart')->where($map)->getField('goods_num');
                if(!$goodslist[$k]['number'])$goodslist[$k]['number']=0;
                $attr = D("Common/GoodsAttr")->getattr($v['id']);
                if($attr['newarr']){
                    $goodslist[$k]['attr'] = 1;
                }else{
                    $goodslist[$k]['attr'] = 0;
                }
            }
        }
        //导航
        $navlist = nav_list(4, 0, 'list');
        $this->assign('navlist', $navlist);
        $this->assign('goodslist', $goodslist);
        $this->get_index_area_cache();
        $this->display();
    }

    public function get_index_area_cache()
    {
        $tree = get_arearegion_cache(3);
        $city_tree = get_arearegion_cache(0);
        ksort($tree);
        $this->assign('json_city', json_encode($city_tree));
        $this->assign('json_provinces', json_encode($tree));
        if (cookie('dingwei_address')) {
            $address_info = cookie('dingwei_address');//获取定位信息
        } elseif (session('dingwei_address')) {
            $address_info = session('dingwei_address');
        }
        //$address_info = session('dingwei_address');//获取定位信息
        if ($address_info && $address_info['city']) {
            $cityname = idtoname($address_info['city'], 'Area', 'name');
            $this->assign('index_cityname', $cityname);
        }
    }
    /**
    **订货列表页
    **/
    public function goods_list($type=0)
    {
        $param=I('param.');
        if(!MID){$this->redirect('Mobile/Login/shop_index');}
        $shops = session('shops_info');
        if(!empty($param['keyword'])){//搜索历史
            $history_num=5;
            $value=session('pursearch_history');
            krsort($value);
            if(!in_array($param['keyword'],$value)){
                if(count($value)>=$history_num)array_shift($value);
                $value[]=$param['keyword'];
                $value=array_filter(array_unique($value));
                session("pursearch_history",$value,3600*6);
            }
            $title=$param['keyword'];
        }
        $paramm=array();
        if(empty($param['json_param'])) {
            $paramm=$param;
            $this->assign('json_param', json_encode($param));
        }
        else {
            $json_param=json_decode(htmlspecialchars_decode($param['json_param']),true);
            $paramm=array_merge($json_param,$param);
            $this->assign('json_param', $param['json_param']);
        }
        $data=D("PurGoods")->get_goods_index($paramm);
        if($data['goods_list']['list']){
            foreach ($data['goods_list']['list'] as $k => $v) {
                $where = array();
                $where['goods_id'] = $v['id'];
                $where['user_id'] = $shops['shop_id'];
                $data['goods_list']['list'][$k]['number'] = M('Cart')->where($where)->sum('goods_num');
                if(!$data['goods_list']['list'][$k]['number'])$data['goods_list']['list'][$k]['number']=0;
                $attr = D('PurGoods')->getmobile_goodscontent($v['id']);
                if($attr['attr']['newarr']){
                    $data['goods_list']['list'][$k]['attr'] = 1;
                }else{
                    $data['goods_list']['list'][$k]['attr'] = 0;
                }
                $moq = getmoq($v['id']);
                $data['goods_list']['list'][$k]['moq'] = $moq['moq'];
                $data['goods_list']['list'][$k]['shop_price'] = $moq['shop_price'];
                $data['goods_list']['list'][$k]['supply_price'] = $moq['supply_price'];
                $data['goods_list']['list'][$k]['unit_name'] = $moq['unit_name'];
                $data['goods_list']['list'][$k]['pack_id'] = $moq['pack_id'];
            }
        }
        if($type==1)return $data;
        if($param['cat_id'])$title=$data['cate_name'];
        $this->assign($param);
        $this->assign($data);
        $list_html=$this->fetch('goodslist');
        $this->assign('list_html',$list_html);
        $before_title=$title?$title:'商品';
        $this->assign('page_title',$before_title);
        /*seo开始*/
        $seodata=D('Common/PurGoods')->format_listseotile($param['brand_id'],$param['cat_id'],'','','','',5);
        /*seo结束*/
        $this->assign($seodata);
        $this->display();
    }
    public function get_goods_list(){
        $return['status']=0;
        $data=$this->goods_list(1);
        if(empty($data['goods_list']['list']))$this->ajaxReturn($return);
        $return['status']=1;
        $this->assign($data);
        $list_html=$this->fetch('goodslist');
        $return['data']=$list_html;
        $this->ajaxReturn($return);
    }
    public function attr(){
        $goodsid = I('post.goodsid');
        $goodsimg = thumb(I('post.img'),500,500,'noimg_500_500.png');
        $goodsname = I('post.name');
        $goodsprice = I('post.price');
        $return['status'] = 0;
        $shops_info = checkuserauthtype(2);
        if(empty($shops_info)){
            $return['msg'] = '请登录';
            $this->ajaxReturn($return);
        }
        if($goodsid){
            $data = D('PurGoods')->getmobile_goodscontent($goodsid);
            if($data['is_stock']==0){
                $supplier_name = empty($data['supplier_info']['name'])?'官方供货商':("供货商".$supplier_name);
                $return['msg'] = $supplier_name."下的商品在该地区无货,不能购买";
                $this->ajaxReturn($return);
            }
            $return['combination'] = json_decode($data['attr']['combination']);
            $return['combinationnum'] = $data['attr']['combinationnum'];
            $return['combinationprice'] = $data['attr']['combinationprice'];
            $return['combinationpricenum'] = $data['attr']['combinationpricenum'];
            if($data['attr']['newarr']){
                $return['status'] = 1;
                $html = "<div class='info'><div class='pic'><img src='".$goodsimg."'></div><div class='info-content'><h6>".$goodsname."</h6><div class='title'></div></div><div class='close-buy-popup'><span class='allclose' onClick=\"claseattr()\">×</span></div></div><div id='list-sub-products' class='detailInfo_tags'>";
                foreach ($data['attr']['newarr'] as $key => $value) {
                    $html.= "<dl><dt>".$value['name']."</dt><dd>";
                    foreach ($value['options'] as $k => $v) {
                        $html.= "<span attr='".$v['id']."' price='".$v['attr_price']."'";
                        // if($k==0){
                        //     $html.= "class='on' ";
                        // }else{
                            $html.= "onClick=\"init_choose(this)\" id=\"attr".$v['id']."\">".$v['optionsval']."</span>";
                        // }
                    }
                    $html.="</dd></dl>";
                }
                $html.="<div class='info-item amount'><span>数量：</span><div class='num-box'><span class='btn-reduce col s3' onClick=\"changenum(".$goodsid.",'-')\">-</span><input type='tel' class='col s6' value='1' id='product_amount'><span onClick=\"changenum(".$goodsid.",'+')\" class='btn-add col s3'>+</span></div></div></div><span id='popup-confirm' class='allclose'  onClick=\"addcart(".$goodsid.")\">确认</span>";  
                $return['html'] = $html; 

            }else{
                $return['status'] = 2;
                $return['msg'] = '无属性';
            }
            
        }else{
            $return['msg'] = '数据获取错误';
        }
        
        $this->ajaxReturn($return);
    }
    public function add_cartlist(){
        $goodsid=I('post.goodsid');
        $goods_attr=I('post.goods_attr');
        $number=I('post.number');
        $name=I('post.name');
        $pack_id=I('post.pack_id');
        $return=D('Common/Cart')->pur_addcart($goodsid,$goods_attr,$number,$name,'',$pack_id);
        $this->ajaxReturn($return);
    }

}