<?php
/**
* @author moufer<moufer@163.com>
* @copyright www.modoer.com
* @version $Id 2008-08-13 20:38 $
*/
!defined('IN_MUDDER') && exit('Access Denied');

$coupons = $where = array();

$from = "{$dbpre}coupons c LEFT JOIN {$dbpre}shops s ON(c.shopid=s.sid)";
$pcd = isset($pcd) ? (preg_match("/^[0-9]{2}$/",$pcd) ? $pcd : '') : '';
$pcd && $where[] = "s.classcode BETWEEN '{$pcd}00' AND '{$pcd}9999'";
$where[] = "c.endtime>UNIX_TIMESTAMP() AND flag=1";
$where = implode(" AND ", $where);

$total = $db->get_value("SELECT COUNT(*) FROM $from WHERE $where");

if($total > 0) {
    $order_arr = array(
        'time' => 'dateline DESC',
        'time_s' => 'dateline ASC',
        'end' => 'endtime DESC',
        'end_s' => 'endtime ASC',
        'start' => 'starttime DESC',
        'start_s' => 'starttime ASC',
        'view' => 'pageview DESC',
        'useful' => 'effect1 DESC',
    );
    $order = empty($order) || empty($order_arr[$order]) ? 'useful' : $order;
    $select = "c.*,s.shopname,s.subname,s.classcode,s.areacode,s.address";
    $offset = 5;
    $limit = get_limit($page, $offset);
    $orderby = $order_arr[$order];
    $query = $db->query("SELECT $select FROM $from WHERE $where ORDER BY $orderby LIMIT $limit");

    while($row = $db->fetch_array($query)) {
        $row['shopname'] .= $row['subname'] ? '('.$row['subname'].')' : '';
        $coupons[] = $row;
    }

    $multipage = multi($total, $offset, $page, 'index.php');
}

// 排序数组
$orderlist = array(
    'useful' => '最有用的',
    'view' => '浏览量',
    'time' => '最新发布',
    'start' => '开始时间',
    'end' => '结束时间',
);

$tpl_name = 'coupon_index';
$subtitle = '优惠券';
require_once MUDDER_ROOT.'header.php';
require_once template($tpl_name);
footer();
?>