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

$coupons = array();
if(empty($shopid) || $shopid < 0) redirect('对不起，无效的商铺ID。');

if(!function_exists('getItem')) {
    require_once(MUDDER_ROOT.'./'.$_modules['shop']['directory'].'/include/fun_item.php');
}
$shop = getItem($shopid);
empty($shop) && redirect('对不起，商铺不存在或已被删除。');

$where = "shopid='$shopid' AND flag=1 AND endtime>UNIX_TIMESTAMP()";
$total = $db->get_value("SELECT COUNT(*) FROM {$dbpre}coupons WHERE $where");
// 更正商铺优惠券数量
if($total != $shop['coupon']) {
    $db->update("UPDATE {$dbpre}shops SET coupons='$total' WHERE sid='$shopid'");
}
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;
    $orderby = $order_arr[$order];
    $offset = 15;
    $limit = get_limit($page, $offset);
    $multipage = multi($total, $offset, $page, 'coupon.php?ac=shop&shopid='.$shopid);
    $query = $db->query("SELECT * FROM {$dbpre}coupons WHERE $where ORDER BY $orderby LIMIT $limit");
    while($row = $db->fetch_array($query)) {
        $coupons[] = $row;
    }
};

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

$tpl_name = 'coupon_shop';
$subtitle = $shop['fullname'].'_优惠券';
require_once MUDDER_ROOT.'header.php';
require_once template($tpl_name);
footer();
?>