<?php
CURSCRIPT != 'wap' && exit('FORBIDDEN');
$id = isset($id) ? intval($id) : '';
$catid = isset($catid) ? intval($catid) : '';
$page = isset($page) ? intval($page) : 1;

require MYMPS_ROOT.'/plugin/goods/include/functions.php';

$seo	 	= get_seoset();
$rewrite	= $seo['seo_force_goods'];

if($Catid && $rewrite == 'rewrite'){
	$detail=explode("-",$Catid);
	if($detail[0] && in_array($detail[0],array('catid','orderby','page'))){
		$detail[1] = mhtmlspecialchars($detail[1]);
		for($i=0;$i<count($detail) ;$i++ ){
			$_GET[$detail[$i]]=${$detail[$i]}=str_replace(array('#@#','#!#'),array('-','/'),$detail[++$i]);	
		}
		extract($_GET);
	}
	$Catid = $detail = NULL;
}

if($id){
	if(!$goods = $db ->getRow("SELECT a.*,b.id as uid,b.tname FROM {$db_mymps}goods AS a LEFT JOIN `{$db_mymps}member` AS b ON a.userid = b.userid WHERE goodsid = '$id' AND onsale = '1'")) redirectmsg('该商品正在审核中，审核通过后正常显示！','?');
	if($goods['catid']) $cat = $db->getRow("SELECT * FROM `{$db_mymps}goods_category` WHERE catid = '$goods[catid]'");
	$db->query("UPDATE `{$db_mymps}goods` SET hit = hit +1 WHERE goodsid = '$id';");
	$goods['picture'] = $goods['picture']?$goods['picture']:'/images/nophoto.gif';
	$goods['picture'] = strstr($goods['picture'],'http')?$goods['picture']:$mymps_global['SiteUrl'].$goods['picture'];
	$goods[tname_uri]=mRewrite('store',array('uid'=>$goods[uid]));
	
	$loc = get_location_m('goods',$goods['catid'],$goods['goodsname']);
	$page_title = $loc['page_title'];
	$cityid = $goods['cityid'];
	$city=get_city_caches($cityid,$areaid);
	if(!defined('WAP')) $city['mdomain']=$city['domain'];
	$page_title = str_replace('{city}',$city['cityname'],$loc['page_title']);
	globalassign();
	include mayi_tpl('goods');
}else{
	$where = " WHERE onsale = '1' ";
	if($catid){
		$catid = intval($catid);
		$cat = $db -> getRow("SELECT * FROM `{$db_mymps}goods_category` WHERE catid = '$catid'");
		if(!$cat){
			$where = NULL;
			errormsg('该商品分类不存在或者已删除！');
			exit;
		}
		$goods_children = mymps_get_goods_children($catid);
		$where .= " AND catid IN (".$goods_children.")";
	}
	
	$goods_cat = goods_category_tree(0);
	
	$perpage = $mayimobile['mobiletopicperpage'] ? $mayimobile['mobiletopicperpage'] : 10;
	$param		= setparams(array('mod','catid'));
	$rows_num = $db -> getOne("SELECT COUNT(goodsid) FROM `{$db_mymps}goods` $where");
	$totalpage = ceil($rows_num/$perpage);
	$num = intval($page-1)*$perpage;
	
	if($orderby == 'tuijian'){
		$orderby2 = " ORDER BY tuijian DESC ";
	}elseif($orderby == 'cuxiao'){
		$orderby2 = " ORDER BY cuxiao DESC ";
	}elseif($orderby == 'hit' ){
		$orderby2 = " ORDER BY hit DESC ";
	}else{
		$orderby2 = " ORDER BY dateline DESC ";
	}
	
	$orderbyarr = array('tuijian'=>'按推荐排序','cuxiao'=>'按促销排序','hit'=>'按人气排序');
	
	$page1 = page1("SELECT * FROM {$db_mymps}goods {$where} {$city_limit} {$orderby2}",$perpage);
	foreach($page1 as $k => $v){
		$list[$v['goodsid']]['goodsid'] = $v['goodsid'];
		$list[$v['goodsid']]['goodsname'] = $v['goodsname'];
		$list[$v['goodsid']]['goodsnamej']=$v['goodsname'];
		$list[$v['goodsid']]['uri_m'] = mRewrite('goods',array('id'=>$v['goodsid'],'cityid'=>$cityid));
		$list[$v['goodsid']]['nowprice'] = $v['nowprice']>0?$v['nowprice']:'面议';
		$list[$v['goodsid']]['oldprice'] = $v['oldprice'];
		$list[$v['goodsid']]['cuxiao'] = $v['cuxiao'];
		$list[$v['goodsid']]['tuijian'] = $v['tuijian'];
		$v['pre_picture'] = $v['pre_picture']?$v['pre_picture']:'/images/nophoto.gif';
		$v['picture'] = $v['picture']?$v['picture']:'/images/nophoto.gif';
		$list[$v['goodsid']]['pre_picture'] = strstr($v['pre_picture'],'http')?$v['pre_picture']:$mymps_global['SiteUrl'].$v['pre_picture'];
		$list[$v['goodsid']]['picture'] = strstr($v['picture'],'http')?$v['picture']:$mymps_global['SiteUrl'].$v['picture'];
	}
	
	if($mayi){
		$list = is_array($list) ? array_values($list) : NULL;
		$list = $list ? $list : NULL;
		$list = json_encode($list,JSON_UNESCAPED_SLASHES);
		echo $list;
		exit;
	}

	if($catid){
		$loc = get_location_m('goods',$cat['catid']);
		$page_title = str_replace('{city}',$city['cityname'],$loc['page_title']);
		$keywords 	= $city['cityname'].$cat['catname'].'产品,'.$city['cityname'].$cat['catname'].'品牌,'.$city['cityname'].$cat['catname'].'商品,'.$city['cityname'].$cat['catname'].'商城,'.$city['cityname'].$cat['catname'].'批发,'.$city['cityname'].$cat['catname'].'团购';
		$description = $city['cityname'].$cat['catname'].'产品商城网提供'.$city['cityname'].$cat['catname'].'产品、'.$city['cityname'].$cat['catname'].'商品，'.$city['cityname'].'品牌'.$cat['catname'].'正品行货购买、批发、团购，全国联保、全网更低价，供您挑选。';
	}else{
		$loc = get_location_m('goods');
		$page_title = str_replace('{city}',$city['cityname'],$loc['page_title']);
		$keywords 	= str_replace('{city}',$city['cityname'],$pluginsettings['goods']['seokeywords']);
		$description = str_replace('{city}',$city['cityname'],$pluginsettings['goods']['seodescription']);
	}
	globalassign();
	include mayi_tpl('goods_list');
}
?>