<?php
/**
 * Email:b2bbuilder@qq.com
 * Auther:Brad
 * 2008,7,17
 */
include_once("includes/page_utf_class.php");
include_once("module/buy/includes/buy_class.php");
$buy = new buy();
//===================================================================
$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;
$totalRows=!empty($_GET['totalRows'])?$_GET['totalRows']: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.statu=2";
if(!empty($_GET['brand']))
	$sub_sql.= " and a.pp='$_GET[brand]'";
if(isset($_GET['type']))
	$sub_sql.=" and a.type='$_GET[type]'";
if(is_numeric($id))
{
	$cat=readCat($id,1);
	if(isset($cat['pcat']['catid']))
		$pcat="<a href='$config[weburl]/?m=buy&s=buy_list&id=".$cat['pcat']['catid']."'>".$cat['pcat']['cat']."</a> &raquo; ";
	$guide=$pcat.$cat['cat'];

	$cid=strlen($id);
	$sub_sql.=" and left(a.catid,$cid)='$id' ";
}
if(!empty($key))
{
	$guide.="('$key')";
	$sub_sql.=" and (a.keywords like '%$key%' or a.title like '%$key%')";
}

if($_GET['order']==1)
	$sub_sql.="  ORDER BY a.ifpay desc";
elseif($_GET['order']==2)
	$sub_sql.="  ORDER BY a.uptime DESC";
else
	$sub_sql.="  ORDER BY a.uptime DESC";

$sql="SELECT a.id,a.title,a.con,a.userid,a.user,a.uptime,a.pic,a.country,a.province,a.city,u.company,u.tel 
	FROM  ".BUY." a  left join ".COMPANY." u on u.company_id=a.userid WHERE a.statu>0 $sub_sql";
//-----------------------
$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);
$res=$db->getRows();
foreach($res as $pl)
{
	$pl['img'] =$buy->get_cover_img($pl['pic']);
	$pl['big_img'] = ''.substr($pl['pic'],0,strrpos($pl['pic'],'/')+1).'big_'.substr($pl['pic'],strrpos($pl['pic'],'/')+1);
	$buy_list[] = $pl;
}
$infoList['list']=$buy_list;
$infoList['page']=$page->prompt();
//-------------------------
$tpl->assign("info",$infoList);
if(!isset($cat['cat']))
	$cat['cat']=$key?$key:$guide;
$tpl->assign("current_cat",$cat['cat']);
$tpl->assign("guide",$guide);
$tpl->assign("subcat",readsubcat($id,'buycat','all'));
$tpl->assign("buyType",explode('|',$config['buyType']));	
//------------------------紧急求购
$sql="select title,id,valid_time from ".BUY." where 1 order by valid_time desc limit 0,10";
$db->query($sql);
$re=$db->getRows();
$tpl->assign("rec_buy",$re);
//----------------------------
$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","buy");
include_once("footer.php");
$out=tplfetch("buy_index.htm");
?>