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

if($op == 'searchshop') {

    !$keyword && dialog('查找商铺', '没有填写商铺关键字。');

    if($charset != 'utf-8') {
        if(!class_exists('Chinese')) {
            require_once(MUDDER_ROOT."./include/chinese.class.php");
        }
        $chs = new Chinese('utf-8', $charset);
        $keyword = isset($keyword) ? $chs->Convert($keyword) : '';
    }

    $query = $db->query("SELECT sid,shopname,subname,address FROM {$dbpre}shops WHERE CONCAT(shopname,subname) like '%{$keyword}%'");
    if($db->num_rows($query) == 0) {
        dialog('查找商铺', '没有找到相关的商铺，请先添加商铺。');
    }
    $result = array();
    while($row = $db->fetch_array($query)) {
        $result[] = $row;
    }
    require_once template('coupon_searchshop');
    output();
}

if(!$user->isLogin) {
    $forward = $php_reuri ? $host_url.$php_reuri : $_config['siteurl'];
    location($_config['siteurl'].'member/login.php?forward='.rawurlencode($forward), false);
    exit;
}

if(!check_submit('couponsubmit')) {

    if($shopid) {
        if(!function_exists('getItem')) {
            require_once(MUDDER_ROOT.'./'.$_modules['shop']['directory'].'/include/fun_item.php');
        }
        $shopdetail = getItem($shopid);
        empty($shopdetail) && redirect('对不起，商铺不存在，无法发布优惠券，您可以先添加商铺。');
        $shopdetail['status'] != 1 && redirect("对不起，商铺非正常营业状态无法发布优惠券。");
    }

    $tpl_name = 'coupon_submit';

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

} else {

    $shopid = intval($shopid);
    if(empty($shopid) || $shopid < 0) redirect("对不起，无效的优惠商铺。");
    $query = $db->query("SELECT sid,shopname,subname FROM {$dbpre}shops WHERE sid='$shopid'");
    $detail = $db->fetch_array($query);
    if(empty($detail)) redirect("对不起，优惠商铺不存在或已删除，请先登记商铺。");

    $subject = html_special_chars_ex($subject);
    $describe = html_special_chars_ex($describe);
    $starttime = html_special_chars_ex($starttime);
    $endtime = html_special_chars_ex($endtime);
    $content = nl_textarea($content);

    if(empty($subject)) redirect("对不起，您未填写优惠券主题名称。");
    if(empty($describe)) redirect("对不起，您未填写优惠说明。");
    if(empty($starttime) || empty($endtime)) redirect("对不起，您未填写优惠券有效期。");
    foreach(array($starttime,$endtime) as $value) {
        if(!preg_match("/[0-9]{4}\-[0-9]{1,2}\-[0-9]{1,2}/", $value)) redirect("对不起，您未填写有效的优惠券有效期。");
    }
    $starttime = strtotime($starttime);
    $endtime = strtotime($endtime);
    if($starttime > $endtime) redirect("对不起，优惠券有效期开始时间不能大于结束时间。");

    $inlist = array();
    $inlist['shopid'] = $shopid;
    $inlist['uid'] = $user->uid;
    $inlist['username'] = $user->username;
    $inlist['subject'] = $subject;
    $inlist['des'] = $describe;
    $inlist['dateline'] = $timestamp;
    $inlist['starttime'] = $starttime;
    $inlist['endtime'] = $endtime;
    $inlist['content'] = $content;

    // 上传的图片处理
    require_once MUDDER_ROOT.'./include/upload.class.php';
    $uploadimg = new Upload_Image_calss($_FILES['pic']);
    if($uploadimg->error) redirect($uploadimg->error);
    if($_FILES['pic'] && !$uploadimg->error) {
        $uploadimg->folder = 'coupon';
        $uploadimg->userWatermark = $MOD['watermark'];
        $uploadimg->max_size = $_config['picture_upload_size'];
        $uploadimg->thumb = array ( 
            'thumb' => array ( 
                'prefix' => 'thumb_',  
                'width' => empty($MOD['thumb_width'])?'72':$MOD['thumb_width'],
                'height' => empty($MOD['thumb_height'])?'47':$MOD['thumb_height'],
            ),
        );
        $uploadimg->upload();
        if($uploadimg->error) redirect($uploadimg->error.'请返回修改。');

        $inlist['thumb'] = $uploadimg->upsubdir.'/'.$uploadimg->thumb_filenames['thumb']['filename'];
        $inlist['picture'] = $uploadimg->upsubdir.'/'.$uploadimg->filename;

    } else {
        redirect("对不起，您未上传图片。");
    }

    $inlist['flag'] = empty($MOD['check'])?'1':'2';
    $db->insert_new($dbpre.'coupons', $inlist);
    $couponid = $db->insert_id();

    if(empty($MOD['check'])) {
        // 更新数量
        $newtotal = $db->get_value("SELECT COUNT(*) FROM {$dbpre}coupons WHERE shopid='$shopid' AND flag=1 AND endtime>UNIX_TIMESTAMP()");
        $db->update("UPDATE {$dbpre}shops SET coupons='$newtotal' WHERE sid='$shopid'");
        redirect("祝贺您，优惠券发布成功！", "index.php?ac=detail&couponid=$couponid");
    } else {
        redirect("优惠券已上传，请等待管理员审核！", "index.php");
    }
}
?>