<?php
include("comm/tuaninc.php");
include('comm/page.class.php');

if(TGOPEN==0){
    header("location:index.php");
}

function subnum($num){
	$s=sprintf("%2d",$num);
    if(strlen($s)>2){
	    return round($num);
	}
	else{
	    return $num;
	}
}

function IP_GetIP(){
    if(!empty($_SERVER["HTTP_CLIENT_IP"]))
        $cip = $_SERVER["HTTP_CLIENT_IP"];
    else if(!empty($_SERVER["HTTP_X_FORWARDED_FOR"]))
        $cip = $_SERVER["HTTP_X_FORWARDED_FOR"];
    else if(!empty($_SERVER["REMOTE_ADDR"]))
        $cip = $_SERVER["REMOTE_ADDR"];
    else
        $cip = "127.0.0.1";
    return $cip;
}

function param($get='',$val=''){
	global $tuan_cat_id;
	global $city_id;
	global $mall_id;
    switch($get){
	    case 'catid':
		    if(WJT==1){
			    return "tg-".$city_id."-".$val."-".$mall_id.'.html';
			}else{
			    return "tg.php?city_id=".$city_id."&catid=".$val."&mallid=".$mall_id;
			}
		    
		break;
		case 'city_id':
		    if(WJT==1){
				return "tg-".$val."-".$tuan_cat_id."-".$mall_id.'.html';
			}else{
			    return "tg.php?city_id=".$val."&catid=".$tuan_cat_id."&mallid=".$mall_id;
			}
		    
		break;
		case 'mallid':
		    if(WJT==1){
				return "tg-".$city_id."-".$tuan_cat_id."-".$val.'.html';
			}
			else{
			    return "tg.php?city_id=".$city_id."&catid=".$tuan_cat_id."&mallid=".$val;
			}
		break;
		case '':
		    if(WJT==1){
			    return "tg-".$city_id."-".$tuan_cat_id."-".$mall_id.'.html';
			}
			else{
			    return "tg.php?city_id=".$city_id."&catid=".$tuan_cat_id."&mallid=".$mall_id;
			}
		break;
	}
}

$mall_id=intval($_GET['mallid']);
if($mall_id>0){
    $where_mall_id=" b.id='$mall_id' and ";
	$where_mall_id_2="mall_id='".$mall_id."' and";
}
else{
    $where_mall_id="";
	$where_mall_id_2="";
}

//全部团购商家
$sql="select id,name,fan from ".$duoduo->BIAOTOU."mall where catid = ".TUANCID." and api_url is not null and api_url<>'' order by sort desc";
$malls=$duoduo->sqltoarr($sql);

//团购导航
$tuan_cat_arr['99']='全部';
$sql="select id,catename from ".$duoduo->BIAOTOU."catelist order by sortorder desc";
$query=$duoduo->query($sql);
while($row=$duoduo->fetch_array($query)){
    $tuan_cat_arr[$row['id']]=$row['catename'];
	$cat_arr[$row['id']]=$row['catename'];
}
$tuan_cat_id=$_GET['catid']?intval($_GET['catid']):99;
$tuan_cat_name=$tuan_cat_arr[$tuan_cat_id];

//热门搜索
$hot_arr=explode('|',$hot_words);

//城市
$cur_city_title=1;
$city_id=intval($_GET['city_id']);
if($city_id==''){
    $cur_city_title='';
}
if(!$city_id){
    $cur_ip = IP_GetIP();//$_SERVER["REMOTE_ADDR"];
	$IP_str=file_get_contents('http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip='.$cur_ip);
	$IP_str=iconv("GBK", "UTF-8", $IP_str);
	$IP_tmp=preg_split('/[\n\r\t\s]+/i', $IP_str);
    $cur_city=$IP_tmp[5];
	$city_id=$duoduo->sel_sql('city','cityid',"cityname='$cur_city' and isshow=1");
	if($city_id<1){
	    $city_id=159;
		$cur_city='全国'; //IP无对应城市，调用全国团购商品
	}
	else{
	    $sql="select id from ".$duoduo->BIAOTOU."tuan_goods where city='".$cur_city."'";
		$query=$duoduo->query($sql);
		$goods_num=mysql_num_rows($query);
		if($goods_num<1){
		    $city_id=159;
		    $cur_city='全国'; //IP无对应城市，调用全国团购商
		}
	}
}
else{
    $cur_city=$duoduo->sel_sql('city','cityname',"cityid='$city_id'");
}

if($cur_city!='全国'){
    $where_city_id=" (a.city like '".$cur_city."' or a.city like '全国') and";
}
else{
    $where_city_id=" a.city like '全国' and";
}

$city_sort_arr=include(CACHE.'city/city_sort.php');
$city_word_arr=include(CACHE.'city/city_word.php');
//print_r($city_sort_arr);

$keyword=$_GET['keyword'];
$sort=$_GET['sort']?$_GET['sort']:'startTime';
$by=$_GET['by']?$_GET['by']:'desc';

if($sort && $by){
    $order='order by a.'.$sort.' '.$by; //$order='order by a.sort desc, a.salt desc, a.'.$sort.' '.$by;
}
switch($sort){
    case 'startTime':
	$sort_arr=array('startTime'=>'时间','bought'=>'销量','price'=>'价格');
	break;
	case 'bought':
	$sort_arr=array('bought'=>'销量','startTime'=>'时间','price'=>'价格');
	break;
	case 'price':
	$sort_arr=array('price'=>'价格','startTime'=>'时间','bought'=>'销量');
	break;
}

//当前城市的团购商品
if($tuan_cat_id<99 || $keyword!='' || $mall_id>0){
	$page = !($_GET['page'])?'1':intval($_GET['page']);
    $pagesize=LISTNUM;
	$frmnum=($page-1)*$pagesize;
	if($tuan_cat_name=='全部'){
	    $cat_name='%';
	}
	else{
	    $cat_name=$tuan_cat_name;
	}
	
	//$sql="select id from ".$duoduo->BIAOTOU."tuan_goods where endTime>'".time()."' and city like '".$cur_city."' and mall_id=";
	
    $sql="select a.id,a.local,a.city,a.title,a.catname,a.image,a.startTime,a.endTime,a.value,a.price,a.rebate,a.bought,b.name,b.id as mallid,b.fan from ".$duoduo->BIAOTOU."tuan_goods as a,".$duoduo->BIAOTOU."mall as b where a.endTime>'".time()."' and $where_city_id $where_mall_id a.catname like '".$cat_name."' and a.title like '%".$keyword."%' and a.mall_id=b.id";
	$query=$duoduo->query($sql);
	$goods_num = mysql_num_rows($query);
	
	$sql="select a.id,a.local,a.city,a.title,a.catname,a.image,a.startTime,a.endTime,a.value,a.price,a.rebate,a.bought,b.name,b.id as mallid,b.fan from ".$duoduo->BIAOTOU."tuan_goods as a,".$duoduo->BIAOTOU."mall as b where a.endTime>'".time()."' and $where_city_id $where_mall_id a.catname like '".$cat_name."' and a.title like '%".$keyword."%' and a.mall_id=b.id $order limit $frmnum,$pagesize";
	$query=$duoduo->query($sql);
	$m=0;
	while($row=$duoduo->fetch_array($query)){
	    $goods[$tuan_cat_id][$m]=$row;
		if($keyword){
		    $goods[$tuan_cat_id][$m]['htitle']=str_replace($keyword,"<span class='h'>$keyword</span>",$row['title']);
		}
		else{
		    $goods[$tuan_cat_id][$m]['htitle']=$row['title'];
		}
		$m++;
	}
	$goods_num_arr[$tuan_cat_id]=$goods_num;
	$total_num+=$goods_num;
	$total=$total_num;
	$cat_arr[99]='全部';
	$cat_arr[0]='全部';
	$show=1;
}
else{
    foreach($cat_arr as $k=>$v){
		$limit=SHOWNUM;
        $sql="select a.id,a.local,a.city,a.title,a.catname,a.image,a.startTime,a.endTime,a.value,a.price,a.rebate,a.bought,b.name,b.id as mallid,b.fan from ".$duoduo->BIAOTOU."tuan_goods as a,".$duoduo->BIAOTOU."mall as b where $where_mall_id a.endTime>'".time()."' and a.city like '".$cur_city."' and a.catname like '".$v."' and a.mall_id=b.id $order limit 0,".$limit."";
		$query=$duoduo->query($sql);
		$m=0;
	    while($row=$duoduo->fetch_array($query)){
	        $goods[$k][$m]=$row;
			$goods[$k][$m]['htitle']=$row['title'];
			$m++;
	    }
    }
	$sql="select a.id,a.local,a.city,a.title,a.catname,a.image,a.startTime,a.endTime,a.value,a.price,a.rebate,a.bought,b.name,b.id as mallid,b.fan from ".$duoduo->BIAOTOU."tuan_goods as a,".$duoduo->BIAOTOU."mall as b where $where_mall_id a.endTime>'".time()."' and a.city like '".$cur_city."' and a.mall_id=b.id";
	$query=$duoduo->query($sql);
    $total_num = mysql_num_rows($query);
}

include('comm/wjt.php');
if(WJT==0){
	$url_head_city_id='.php?city_id=';
    $page_tuan_list=param()."&keyword=".urlencode($keyword);
}
else{
	$url_head_city_id='-';
    $page_tuan_list="tg-".$city_id."-".$tuan_cat_id."-".$mall_id."-".urlencode($keyword);
}

$tag='tg';
include_once 'comm/seo.php';
include 'template/'.MOBAN.'/discount.tpl.php';

echo '<script type="text/javascript" src="http://'.URL.'/comm/auto.php"></script>';
?>