<?php
/**
* @author moufer<moufer@163.com>
* @copyright www.modoer.com
* @version $Id 3 2008-09-04 18:29 $
*/
!defined('IN_MUDDER') && exit('Access Denied');

// 提交会员卡信息
function postCard($post) {
    global $db,$dbpre;

    // 参数过滤
    $post['enable'] = intval($post['enable']) > 0 ? 1: 0;
    $post['cardsort'] = in_array($post['cardsort'], array('discount','largess','both')) ? $post['cardsort'] : '';
    $post['discount'] = is_numeric($post['discount']) && $post['discount'] > 0 && $post['discount'] < 10 ? $post['discount'] : 0;
    $post['largess'] = html_special_chars_ex($post['largess']);
    $post['exception'] = html_special_chars_ex($post['exception']);

    // 判断
    if ($post['enable']) {
        if(!$post['cardsort']) {
            cpmsg("未选择优惠形式，请返回修改。");
        }
        if($post['cardsort'] == 'discount' || $post['cardsort'] == 'both') {
            if(!$post['discount']) {
                cpmsg("未填折扣度或折扣度无效，请返回修改。");
            }
            $set[]="discount='{$post['discount']}'";
        }
        if($post['cardsort'] == 'largess' || $post['cardsort'] == 'both') {
            if(!$post['largess']) {
                cpmsg("未填写赠送内容，请返回修改。");
            }
            $set[]="largess='{$post['largess']}'";
        }
        $post['enable'] && $set[]="exception='{$post['exception']}'";
        $set[] = "cardsort='{$post['cardsort']}'";
    }

    $membercard_available = intval($post['enable']);
    $set[] = "available='$membercard_available'";
    $sqlset = implode(',',$set);
    $recode = $db->get_one("SELECT available FROM {$dbpre}carddiscounts WHERE shopid='{$post['shopid']}'");
    if($recode) {
        $db->query("UPDATE {$dbpre}carddiscounts SET $sqlset WHERE shopid='{$post['shopid']}'");
    } elseif($post['enable']) {
        unset($post['enable']);
        $post['addtime'] = $timestamp;
        $post['available'] = 1;
        $db->insert_new($dbpre.'carddiscounts', $post);
    }
}
?>