<?php
/**
* 会员卡后台操作
* @author moufer<moufer@163.com>
* @copyright www.modoer.com
* @version $Id membercard.inc.php 1 2008-2-23 21:10 $
*/
(!defined('IN_ADMIN') || !defined('IN_MUDDER')) && exit('Access Denied');

if(!$dosubmit) {

        $pcd = isset($pcd) ? (preg_match("/^[0-9]{2}$/",$pcd) ? $pcd : '') : '';
        $where .= " a.available=1 AND b.status=1 ".($pcd ? " AND b.classcode like '{$pcd}__' " : '');

        $result = $db->get_one("SELECT COUNT(*) as total FROM {$dbpre}carddiscounts a LEFT JOIN {$dbpre}shops b ON(a.shopid=b.sid) WHERE $where");
        $total = $result['total'];

        if($total > 0) {

            $offset = 20;
            $limit = get_limit($page, $offset);
            $query = $db->query("SELECT a.*,b.classcode,b.areacode,b.shopname,b.subname FROM {$dbpre}carddiscounts a LEFT JOIN {$dbpre}shops b ON(a.shopid=b.sid) WHERE $where ORDER BY b.reviews DESC LIMIT $limit");
            while($shop = $db->fetch_array($query)) {
                $shop['shopname'] .= $shop['subname'] ? "({$shop['subname']})" : '';
                $shop['membercard'] = $_membercard['enable'] && $shop['membercard_available'];
                if($shop['cardsort'] == 'both') {
                    $shop['membercard_msg'] = $shop['discount'].'折, '.$shop['largess'];
                } elseif($shop['cardsort'] == 'discount') {
                    $shop['membercard_msg'] = $shop['discount'].'折';
                } else {
                    $shop['membercard_msg'] = $shop['largess'];
                }
                $shops[] = $shop;
            }

            $multipage = multi($total, $offset, $page, 'admincp.php?action='.$action.'&file='.$file);
        }

    include cptpl('discount', MOD_ROOT.'./admin/templates/');

} else {

    if($shops) foreach($shops as $id => $val) {
        if($shopids && is_array($shopids) && in_array($id, $shopids)) {
            $db->query("DELETE FROM {$dbpre}carddiscounts WHERE shopid='$id'");
        } else {
            $finer = intval($val['finer']) > 0 ? 1 : 0;
            $db->query("UPDATE {$dbpre}carddiscounts SET finer='$finer' WHERE shopid='$id'");
        }
    }

    //$cacheclass->Read_Shops('card_commend_shops','',TRUE);
    cpmsg('更新完毕。','admincp.php?action='.$action.'&file='.$file);
}
?>
