<?php
define('IN_PHPQUAN', true);
require dirname(__FILE__) . '/include/common.php';
require dirname(__FILE__) . '/include/com.fun.php';
$durl = 'list';
$act  = $_REQUEST['act'] ? trim($_REQUEST['act']) : '';
$cat_array = get_cat_array();
$com_cat_array = get_com_cat_array();
$comid   = $_REQUEST['comid'] ? intval($_REQUEST['comid']) : '';
$catid   = $_REQUEST['catid'] ? intval($_REQUEST['catid']) : '';
$order   = $_REQUEST['order'] ? trim($_REQUEST['order']) : 'new';
$keyword = $_REQUEST['keyword'] ? keyword_replace($_REQUEST['keyword']) : '';
$ordername = array('new'=>'最新发布','hot'=>'人气最高','nearby'=>'离我最近');
if($order=='new')$orders='a.postdate DESC';
if($order=='hot')$orders='a.click DESC';
if(!empty($comid)){
	$comids = 'AND b.comid = '.$comid;
}
$cats = get_com_cat_list();
$com_cat_name = empty($catid) ? '优惠券分类' : $com_cat_array[$catid];
if(!empty($catid)){
	$catids = 'AND b.catid = '.$catid;
}
$r = $db->getOne("SELECT COUNT(*) FROM {$table}juan as a left join {$table}com as b on a.comid=b.comid where a.enddate>=".time()." and a.is_check=1 $comids $catids ");
$page = empty($_REQUEST['page'])? 1 : intval($_REQUEST['page']);
$page = isset($page) ? intval($page) : 1;
$pagesize = 20;
$pager['search'] = array('comid'=>$comid,'catid'=>$catid,'order'=>$order,'keyword'=>urlencode($keyword));
$pager = get_page('list.php', $pager['search'], $r, $page, $pagesize, '?act=json&');
$sql = "select a.*,a.catid as aaid,b.comid,b.catid as bid,b.comname,count(c.juanid) as ujid from {$table}juan as a left join {$table}com as b on a.comid=b.comid left join {$table}juan_user as c on a.id=c.juanid where a.enddate>=".time()." and a.is_check=1 $comids $catids $qcids  group by a.id order by $orders LIMIT $pager[start],$pager[size]";
$res = $db->query($sql);
$juan = array();
	while($row=$db->fetchRow($res)) {
		//$row['postdate'] = date("m/d", $row['postdate']);
		$row['enddate']  = date("Y/m/d", $row['enddate']);
		$row['url']      = url_rewrite('view',array('vid'=>$row['id']));
		$row['uton']	 = get_stat($row['ujid'],$row['nums']);
		$row['utons']    = $row['uton']>=100 ? '已抢光了' : $row['ujid'].'/'.$row['nums'];
		$row['mystat']	 = round($row['ujid']/$row['nums'] * 100 , 0);
		$juan[] = $row;
	}
if($act=="json"){
	echo json_encode(array('data'=>$juan,'pages'=>$pager['page_count']));
}else{
	$seo['title'] = $area_info['areaname'] . $cat_info['catname'] . '优惠券列表_'.$CFG['webname'];
	$seo['keywords'] = '优惠券,'.$area_info['areaname'].$cat_info['keywords'].','.$CFG['webname'];
	$seo['description'] = $cat_info['description'];
	include template('list');
}
?>