<?php
namespace wstmart\mobile\controller;
use wstmart\common\model\GoodsCats;
/**
 * ============================================================================
 * 商品控制器
 */
class Goods extends Base{
	/**
	 * 商品主页
	 */
	public function detail(){
		$m = model('goods');
        $goods = $m->getBySale(input('goodsId/d'));
        $key=input('key');
        hook('mobileControllerGoodsIndex',['getParams'=>input()]);
       
        
        // 找不到商品记录
        if(empty($goods))return $this->fetch('error_lost'); 
         //判断是否药品
        $goods_cat=strpos($goods['goodsCatIdPath'],'389');
        if($goods_cat!==false  && $key==''){
            return $this->fetch("error_lost");
        }
       //记录用户浏览商品
        $userId = (int)session('WST_USER.userId');
        if($userId){
            $data['userId']=$userId;
            $data['goodsId']=$goods['goodsId'];
            $data['path']='2';
            $data['create_time']=time();
            $result=db('page_view')->insert($data);
        }
        $goods['goodsDesc']=htmlspecialchars_decode($goods['goodsDesc']);
        $rule = '/<img src=".*?\/(upload.*?)"/';
        preg_match_all($rule, $goods['goodsDesc'], $images);
        foreach($images[0] as $k=>$v){

            $goods['goodsDesc'] = str_replace('/'.$images[1][$k], '__IMGURL__/'.WSTConf("CONF.goodsLogo") . "\"  data-echo=\"__IMGURL__/".WSTImg($images[1][$k],2), $goods['goodsDesc']);
        }
        if(!empty($goods)){
            $history = cookie("wx_history_goods");
            $history = is_array($history)?$history:[];
            array_unshift($history, (string)$goods['goodsId']);
            $history = array_values(array_unique($history));
            if(!empty($history)){
                cookie("wx_history_goods",$history,25920000);
            }
        }
        $goods['consult'] = model('GoodsConsult')->firstQuery($goods['goodsId']);
        $goods['appraises'] = model('GoodsAppraises')->getGoodsEachApprNum($goods['goodsId']);
         hook('afterGetGoods',['params'=>&$goods]);
        $goods['is_seckilling']=isset($goods['is_seckilling'])?$goods['is_seckilling']:0;
        //hook('afterGetGoods',['params'=>&$goods]);
		$this->assign("info", $goods);
		return $this->fetch('goods_detail');
	}
	/**
     * 商品列表
     */
    public function lists(){
    	$this->assign("keyword", input('keyword'));
    	$this->assign("catId", input('catId/d'));
    	$this->assign("brandId", input('brandId/d'));
    	return $this->fetch('goods_list');
    }
    /**
     * 获取列表
     */
    public function pageQuery(){
    	$m = model('goods');
    	$gc = new GoodsCats();
    	$catId = (int)input('catId');
    	if($catId>0){
    		$goodsCatIds = $gc->getParentIs($catId);
    	}else{
    		$goodsCatIds = [];
    	}
    	$rs = $m->pageQuery($goodsCatIds);
    	foreach ($rs['Rows'] as $key =>$v){
    		$rs['Rows'][$key]['goodsImg'] = WSTImg($v['goodsImg'],2);
    		$rs['Rows'][$key]['praiseRate'] = ($v['totalScore']>0)?(sprintf("%.2f",$v['totalScore']/($v['totalUsers']*15))*100).'%':'100%';
    	}
        // `券`标签
        hook('afterQueryGoods',['page'=>&$rs]);
    	return $rs;
    }

    /**
    * 浏览历史页面
    */
    public function history(){
        return $this->fetch('users/history/list');
    }
    /**
    * 获取浏览历史
    */
    public function historyQuery(){
        $rs = model('goods')->historyQuery();
        if(!empty($rs)){
	        foreach($rs['Rows'] as $k=>$v){
	            $rs['Rows'][$k]['goodsImg'] = WSTImg($v['goodsImg'],3);
	        }
        }
        return $rs;
    }
}