getRow(['stat' => 0, 'pid' => 0], null, ['id' => 'asc']); // header("content-type: text/html; charset=utf8"); // dump(json_decode(json_encode($category), true));die; if (empty($category)) { return exception('数据有误,请检查后再操作'); } $value = [ 'category' => $category, ]; $value['seo_title'] = config('product_seo_title')? : config('website_seo_title'); $value['seo_keyword'] = config('product_seo_keyword')? : config('website_seo_keyword'); $value['seo_description'] = config('product_seo_description')? : config('website_seo_description'); $this->assign($value); return $this->fetch('catelists'); } public function catelists($id = 0) { $arg_where = ['a.siteid' => $this->siteid, 'country_code' => $this->country_code]; if ($id > 0) { $arg_where['cid'] = $id; $category = Loader::model('ProductCategory')->getRow($id); } //echo "
=="; print_r($category); exit;
        
        if (empty($category)) {
            return exception('数据有误,请检查后再操作');
        }
        switch ($category['classtype']) {
            case 2:
                $template = $category['templist'];
                break;
            case 3:
                header('location:' . $category['url']);
                exit;
                break;
            default:
                $template = $category['tempindex'];
                break;
        }
        // 手机端热门产品
        $hot_tags = 'hot_category_id_' . $category['id'];
        $hot_product_where  = ['typeid' => 6, 'stat' => 0, 'tags' => $hot_tags];
        $hot_product_order  = ['sort' => 'asc'];
        $hot_product        = Loader::model('ad')->where($hot_product_where)->order($hot_product_order)->limit(4)->select();

        $value = [
            'category' => $category,
        ];
     
        $value['hot_product']       = empty($hot_product) ? "" : $hot_product;

        $value['seo_title']         = $category['seo_title']? : config('website_seo_title');
        $value['seo_keyword']       = $category['seo_keyword']? : config('website_seo_keyword');
        $value['seo_description']   = $category['seo_description']? : config('website_seo_description');
        $this->assign($value);
//echo "--".$template."
=="; print_r($value);exit;
        return $this->fetch('catelists');
    }

    public function subcatelists($id = 0) {
        $arg_where = ['a.siteid' => $this->siteid];
        if ($id > 0) {
            $arg_where['cid'] = $id;
            $category = Loader::model('ProductCategory')->getRow($id);
        }

        if (empty($category)) {
            return exception('数据有误,请检查后再操作');
        }

        switch ($category['classtype']) {
            case 2:
                $template = $category['templist'];
                break;
            case 3:
                header('location:' . $category['url']);
                exit;
                break;
            default:
                $template = $category['tempindex'];
                break;
        }

        $child = model('product_category')->where(['stat' => 0, 'isshow' => 1, 'pid' => $id])->find();
        if (!empty($child)) {
            // 有下级分类
            $subproductCategory = $this->list_to_tree($this->categoryList, 'id', 'pid', 'child', $id);
            $cate_ids = array();
            foreach($subproductCategory as $kc => $subCategory){
                $cate_ids[] = $subCategory['id'];
            }
            $cate_id            = implode(",", $cate_ids);
        } else {
            // 如果是最低级分类
            $subproductCategory = $this->list_to_tree($this->categoryList, 'id', 'pid', 'child', $category['pid']);
            $cate_id            = $category['id'];
        }

        // 获取分类下的所有产品
        $where = ['stat' => 0,  'country_code' => $this->country_code];
        $where['cid'] = ['in', $cate_id];
        $where = ['stat' => 0, 'cid' => $cate_id];
        $order = ['sort' => 'asc', 'id' => 'desc'];
        $where['is_show'] = 0;
        $field = ['id, name, brand_id'];
        $product = Loader::model('product')->where($where)->field($field)->order($order)->select();
        foreach ($product as $k => $v) {
            $product[$k]['product_two_img'] = [];
            $product[$k]['product_two_img'] = Loader::model('product_two_img')->where(['product_id' => $v['id'], 'stat' => 0])->select();
        }
        
        $value['product'] = $product;
        $value['subproductCategory'] = $subproductCategory;
        $value['pid'] = $id;
        $value['category'] = $category;
        $value['seo_title'] = $category['seo_title']? : config('website_seo_title');
        $value['seo_keyword'] = $category['seo_keyword']? : config('website_seo_keyword');
        $value['seo_description'] = $category['seo_description']? : config('website_seo_description');
        $this->assign($value);
        return $this->fetch($template);
    }

    public function ajaxcatelists($id = 0) {
        $arg_where = ['p.stat' => 0, 'c.stat' => 0, 'p.siteid' => $this->siteid];
        $id = intval($id);
        if ($id > 0) {
            if (0) {
                $ids = Loader::model('ProductCategory')->getChildIDArray(4);
                $arg_where['cid'] = ['in', $ids];
            } else {
                $arg_where['cid'] = $id;
            }
            $category = Loader::model('ProductCategory')->getRow($id);
        }
        if (empty($category)) {
            return exception('数据有误,请检查后再操作');
        }
        $arg_field = ['p.id', 'p.cid', 'p.name', 'p.shortname', 'p.sort', 'p.ishot', 'p.isnew', 'p.recommend', 'p.viewcount', 'p.tags', 'p.description', 'p.picture', 'p.picture_back', 'p.createtime', 'p.createtime', 'c.name' => 'categoryname', 'c.id' => 'categoryid'];
        $arg_order['p.sort'] = 'asc';
        $arg_order['p.id'] = 'desc';
        $dataObject = Loader::model('Product')->getCateProductLists($arg_where, $arg_order, $arg_field, 8);
        if ($dataObject->isEmpty()) {
            $value = ['list' => null];
        } else {
            $this->assign(['list' => $dataObject->items(), 'category' => $category,]);
            $value = ['list' => $this->fetch()];
        }
        return $this->result($value, true, '分类列表');
    }

    public function detail($id = 0, $color = '') {
        if ($id > 0) {
            $detail = Loader::model('Product')->where(['stat' => 0, 'is_show' => 0, 'country_code' => $this->country_code, 'id' => $id])->find();

            /*if ($this->customer_id > 0)
            {
                //  如果登录成功判断用户是否收藏该产品
                $customer_info = json_decode(Cookie::get('c'), true);
                if (empty($customer_info))
                {
                    $detail['is_collection'] = 0;       
                }
                else
                {
                    $where = [
                        'customer_id'   => $customer_info['id'],
                        'type'          => 1,
                        'coll_id'       => $detail['id'],
                        'stat'          => 0
                    ];
                    $result = model('collection')->where($where)->find();
                    if ($result)
                    {
                        $detail['is_collection'] = 1;           
                    }
                    else
                    {
                        $detail['is_collection'] = 0;
                    }
                }
                
            }
            else
            {
                $detail['is_collection'] = 0;
            }*/
            
            if(empty($detail)){
                //return exception('该产品已下架,请检查后再操作');
                return $this->error('该产品已下架,请检查后再操作');
            }            
            
            $detail['is_collection'] = 0;
            
            if (empty($detail)) {
                return exception('数据有误,请检查后再操作');
            }
            $category = Loader::model('ProductCategory')->getRow($detail['cid']);
            $cid = Loader::model('ProductCategory')->getRow($category['pid']);
            $pid = Loader::model('ProductCategory')->getRow($cid['pid']);
            if (empty($category)) {
                return exception('数据有误,请检查后再操作');
            }
            $template = $category['tempdetail'];

            $detail['product_two_img'] = model('product_two_img')->where(['stat' => 0, 'product_id' => $id])->select();
            $value = [
                'detail' => $detail,
                'category' => $category,
                'pid' =>$pid,
                'cid'=>$cid
                    //'prev_detail' => $prev_detail,
                    //'next_detail' => $next_detail,
            ];
            $value['product_attr'] = is_null(json_decode($detail['product_attr'])) ?  $detail['product_attr'] : json_decode($detail['product_attr']);


            $tmp_product_images = Loader::model('ProductImage')->getList(array('product_id' => $detail['id'],'stat' => 0, 'country_code' => $this->country_code), ['image_sort' => 'asc', 'id' => 'asc'], ['id', 'product_id', 'image_url', 'image_sort', 'image_desc', 'image_color']);
            // $value['product_dls'] = Loader::model('ProductDl')->getList(array('product_id' => $detail['id']), ['dl_sort' => 'asc', 'id' => 'desc'], ['id', 'product_id', 'dl_url', 'dl_sort', 'dl_name', 'dl_type']);
            // $value['product_questions'] = Loader::model('Question')->getList(array('product' => $detail['id']), ['sort' => 'asc', 'id' => 'asc'], ['id', 'name', 'product', 'sort', 'headline', 'recommend', 'description', 'createtime']);
            $product_relateds = Loader::model('Product')->getRelatedProductList(array('pr.product_id' => $detail['id']), ['pr.related_sort' => 'asc', 'pr.id' => 'asc',], ['p.id', 'p.name', 'p.shortname', 'p.picture', 'p.sort', 'p.ishot', 'p.list_bk_img','p.isnew', 'p.recommend', 'p.description', 'p.createtime', 'p.brand_id','pr.id' => 'related_id', 'pr.related_product_id', 'pr.related_sort', 'pr.related_desc']);
            foreach ($product_relateds as $k => $v) {
                $res = Loader::model('product_two_img')->where(['product_id' => $v['id'], 'stat' => 0])->find();
                $product_relateds[$k]['product_two_img'] = $res;
            }

            $product_images = [];
            $attributes = [];
            
            foreach ($tmp_product_images as $k => $v) {
                
                //兼容新旧两种版附加图、相册属性及图片
                if (is_string($v['image_url']) && json_decode($v['image_url'])) {
                    $v['image_url'] = json_decode($v['image_url'], true);           //多图Json格式
                    $v['image_color'] = json_decode($v['image_color'], true);       //多属性规则: 颜色,尺码,排序等
                    
                    if(isset($value['product_attr']) && is_array($value['product_attr'])){
                        foreach($value['product_attr'] as $ka => $attr){
                            $attributes[$attr][] =  $v['image_color'][$ka][$attr];
                        }
                    }
                    $product_images[] = $v;
                    
                }
                //原版, 仅支持颜色属性
                else{
                    $image_color    = $v['image_color'];
                    $pos            = strrpos($image_color, '/');
                    $key            = substr($image_color, $pos + 1, strpos($image_color, '.') - $pos - 1);
                    $product_images[$key][] = $v;

                }                
                
            }
            foreach($attributes as $attrbute => $attrValue) {
                    $attributes[$attrbute] = array_unique($attrValue);
            }
            //echo "
++"; print_r($attributes);
            $value['attributes'] =  $attributes;
         
            
            /*foreach ($tmp_product_images as $k => $v) {
                $image_color    = $v['image_color'];
                $pos            = strrpos($image_color, '/');
                $key            = substr($image_color, $pos + 1, strpos($image_color, '.') - $pos - 1);
                $product_images[$key][] = $v;
            }*/

            $value['product_images'] = $product_images;
            $value['product_relateds'] = $product_relateds;
            $value['seo_title'] = $detail['seo_title']? : $detail['name'] . '-' . config('website_seo_title');
            $value['seo_keyword'] = $detail['seo_keyword']? : config('website_seo_keyword');
            $value['seo_description'] = $detail['seo_description']? : config('website_seo_description');
            $value['color'] = $color;
            //评论数据获取
            $where = [
                'product_id' => $detail['id']
            ];
            
            $count = db('shopselle')->where($where)->count();
            $list = db('shopselle')->where($where)->paginate(4,$count);
            foreach ($list as $k => $v){
                $v['pics'] = json_decode($v['pics']);
                $list[$k] = $v;
            }

            $page = $list->render();
            $this->assign('page',$page);
            $this->assign('list',$list);
            $this->assign('count',$count);
            $this->assign($value);
            $this->viewcount($id);
            
            return $this->fetch($template);
        } else {
            return exception('数据有误,请检查后再操作');
        }
    }

    protected function viewcount($id) {
        $view = Cookie::get('productview', 'history');
        if (empty($view) || $view != $id) {
            Loader::model('Product')->where(['id' => $id])->setInc('viewcount');
            Cookie::set('productview', $id, ['prefix' => 'history', 'expire' => 3600]);
        }
    }

    protected function historyproduct($id) {
        $product = Cookie::get('product', 'history'); //print_r($history);exit;
        if (isset($product) && !empty($product)) {
            $product_ids = explode(',', $product);
            if ($product_ids[0] != $id) {
                array_unshift($product_ids, $id);
                $product_ids = array_unique($product_ids);
                $num = Config::get('history_number') > 0 ? Config::get('history_number') : 10;
                //$product_ids = array_slice($product_ids, 0, $num);
                while (count($product_ids) > $num) {
                    array_pop($product_ids);
                }
                Cookie::set('product', implode(',', $product_ids), ['prefix' => 'history', 'setcookie' => true, 'expire' => 3600 * 24 * 30]);
            }
        } else {
            Cookie::set('product', $id, ['prefix' => 'history', 'setcookie' => true, 'expire' => 3600 * 24 * 30]);
        }
    }
    
    /********新品展示 *********/
    public function newarrival() {

        $value = Loader::model('Product')->getNewItemLists(array(),['t.sort' => 'asc', 't.id' => 'asc',], ['p.id', 'p.name', 'p.shortname', 'p.picture', 'p.sort', 'p.ishot', 'p.list_bk_img','p.isnew', 'p.recommend', 'p.description','p.brand_id', 'p.createtime', 't.id' => 'category_id', 't.name'=>'category_name', 't.sort']);
        
        //分类分组
        $newProduct = array();
        foreach($value as $key => $item) {
            $newProduct[$item['category_id']]['category_id'] = $item['category_id'];
            $newProduct[$item['category_id']]['category_name'] = $item['category_name'];
            $newProduct[$item['category_id']]['list'][] = $item;
        }
        //echo "
---"; print_r($productCategory);die;        //echo "
=="; print_r($productCategory);die;
        $this->assign('newProduct',$newProduct);

        $value['category'] = "New Product";
        $value['seo_title'] = $value['category']? : config('website_seo_title_us');
        $value['seo_keyword'] = $value['category']? : config('website_seo_keyword');
        $value['seo_description'] = $value['category']? : config('website_seo_description');
        $this->assign($value);

        return $this->fetch('new');
    }
}