<?php
/*
* powered by b2bbuilder
* copyright http://www.b2b-builder.com
* Auther:brad zhang;
*/
//======================================
useCahe();
$flag=md5($dpid.$dcid.$config["temp"].$_COOKIE["langtw"]);
if(!$tpl->is_cached("offer_index.htm",$flag))
{	
	include_once("includes/page_utf_class.php");
	//===================================================================
	$id=!empty($_GET['id'])?$_GET['id']:NULL;
	$ifpay=!empty($_GET['ifpay'])?$_GET['ifpay']:NULL;
	$key=!empty($_GET['key'])?trim($_GET['key']):NULL;
	$catType=!empty($_GET['catType'])?trim($_GET['catType']):NULL;
	$firstRow=!empty($_GET['firstRow'])?$_GET['firstRow']:NULL;
	
	if(!empty($_GET['province']))
		$sub_sql=" and a.province='$_GET[province]'";
	if(!empty($dpid))
		$sub_sql=" and a.province='$dpid'";
	if(!empty($dcid))
		$sub_sql=" and a.city='$dcid'";
	if(!empty($ifpay))
		$sub_sql=" and a.ifpay>1";
	
	if(!empty($_GET['brand']))
		$sub_sql.= " and a.brand='$_GET[brand]'";
	if(isset($_GET['type']))
		$sub_sql.= " and a.type='$_GET[type]'";
		
	if(is_numeric($id))
	{
		$guide=GetCatName($id,true,OCAT);
		$tpl->assign("guide",$guide?$guide:"");
		$cat=readCat($id,1);
		//----是否有为分类自定义模板
		if($cat['template'])
		{
			if(!empty($cat['template'])&&file_exists($tpl->template_dir.'/'.$cat['template']))
			{
				include_once("footer.php");
				$out=tplfetch($cat['template']);
				die;
			}
		}
		$cid=strlen($id);
		$sub_sql.=" and left(a.catid,$cid)='$id' ";
		//-----------------------------获取自定义字段搜索项
		$sql="select fieldDisplay,fieldName,catItem from ".EXTENDFILE." where isSearch=1 and displayType in (3,4,5) and catid='$cat[ext_field_cat]'";
		$db->query($sql);
		$catfile=$db->getRows();
		foreach($catfile as $fkey=>$v)
		{
			$catField=explode(',',$v['catItem']);
			foreach($catField as $skey=>$sv)
			{
				$catField[$skey]=explode('|',$sv);
			}
			$catfile[$fkey]['catItem']=$catField;
			//------组合皖搜索
			if(!empty($_GET[$v['fieldName']]))
				$ext_sql.=' and b.'.$v['fieldName'].'=\''.$_GET[$v['fieldName']].'\'';
		}
		$tpl->assign("catfile",$catfile);
	}
	if(!empty($key))
	{
		$guide.="('$key')";
		$sub_sql.=" and (a.keywords like '%$key%' or a.title like '%$key%')";
	}
	if($_GET['order']==1)
		$order.="  order by a.ifpay desc";
	elseif($_GET['order']==2)
		$order.="  order by a.uptime desc";
	else
		$order.="  order by a.rank desc,a.ifpay desc,a.uptime desc";
	
	
	if(empty($cat['ext_field_cat']))
		$sql="select a.id,a.title,a.pic from ".INFO." a where a.statu>0 $ext_sql $sub_sql $order";
	else
		$sql="select a.id,a.title,a.pic from  ".INFO." a  left join ".$cat['ext_table']." b on a.id=b.infoid and b.infotype='offer' and a.type='1' where a.statu>0 $ext_sql $sub_sql $order";
	//-----------------------
	$page = new Page();
	$page->listRows=20;
	$page->url=$config['weburl'];
	if (!$page->__get('totalRows'))
	{
		$db->query($sql);
		$page->totalRows = $db->num_rows();
	}
	$sql .= "  limit ".$page->firstRow.",".$page->listRows;
	//------------------------
	$db->query($sql);
	$re['list']=$db->getRows();
	foreach($re['list'] as $k=>$pl)
	{
		$img= explode(',',$pl['pic']);
		$re['list'][$k]['pic'] =$img[0];	
	}
	$re['page']=$page->prompt();
	$tpl->assign("info",$re);
	$tpl->assign("subcat",readsubcat($id,'offercat','all'));
}
$config['title']=str_replace('[catname]',$cat['cat'],$config['title2']);
$config['keyword']=str_replace('[catname]',$cat['cat'],$config['keyword2']);
$config['description']=str_replace('[catname]',$cat['cat'],$config['description2']);
$tpl->assign("current","offer");
include_once("footer.php");
$out=tplfetch("offer_index.htm",$flag);
?>