<?php
define('IN_PHPQUAN', true);
require dirname(__FILE__) . '/include/common.php';
require PHPQUAN_ROOT . 'include/json.class.php';
$act = $_REQUEST['act'] ? trim($_REQUEST['act']) : 'select' ;
check_gid();
$ip = get_ip();
$durl = 'member';
if(empty($_userid)) {
	showmsg('请登陆后发布','member.php?act=login&refer='.$PHP_URL);
}
$u = com_info('userid');
if(empty($u))showmsg('还未登记店铺！','postcom.php');
if($_userid){
	$member = member_info($_userid);
	if($member['status']!=1) showmsg('您尚未通过审核或被锁定！');
}
if($act == 'select')
{
	$cats = get_cat_list();
	$here = array('name'=>'选择分类', 'url'=>'post.php');
	$com = $db->getOne("select count(*) from {$table}com where userid='$_userid' and is_check=1");
	if(empty($com)) showmsg('商家未审核，请审核后发布！');
	$seo['title'] = '选择券类型_'.$CFG['webname'];
	$seo['keywords'] = $CFG['keywords'];
	$seo['description'] = $CFG['description'];
	include template('select');
}
elseif($act == 'post')
{
	$catid = intval($_REQUEST['id']);
	if(empty($catid)) {
		showmsg('没有选择分类');
	}
	$catinfo = get_cat_info($catid);
	if(empty($catinfo)) showmsg('不存在此分类');
	$member = member_info($_userid);
	$seo['title'] = "发布".$catinfo['catname']."_".$CFG['webname'];
	$template = $catinfo['posttplname'] ? $catinfo['posttplname'] : 'post';
	include template($template);
}
elseif($act == 'postok')
{
	$title     = $_POST['title'] ? htmlspecialchars_deep(safe_replace($_POST['title'])) : '';
	$catid     = $_POST['catid'] ? intval($_POST['catid']) : '';
	$foruser   = $_POST['foruser'] ? intval($_POST['foruser']) : '';
	$postdate  = time();
	$enddate   = $_POST['enddate'] ? strtotime($_POST['enddate']) : '';
	$userdate  = $_POST['userdate']>0 ? intval($_POST['userdate']) : $CFG['myjuan_day'];
	$nums      = $_POST['nums'] ? intval(numzh($_POST['nums'])) : '';
	$usernum   = $_POST['usernum'] ? intval(numzh($_POST['usernum'])) : '';
	$jinfo     = $_POST['jinfo'] ? htmlspecialchars_deep(safe_replace($_POST['jinfo'])) : '';
	$is_check  = $CFG['post_check'] == '1' ?  '0' : '1';
	if($enddate<=0) showmsg('有效期不能为空！');
	if($CFG[postcom_maxdate]>0){
		if($enddate>(time() + 3600*24*$CFG[postcom_maxdate])) showmsg('有效期不能超过系统限制的'.$CFG[postcom_maxdate].'天！');
	}
	if($nums<=0) showmsg('数量不能为空！');
	if($usernum>$nums) showmsg('可领取次数不能大于券数量！');
	if($usernum<=0 || $usernum>99) showmsg('可领取次数限制在1~99！');
	$catinfo = get_cat_info($catid);
	
	$comid = com_info('comid',2);
	$items = array(
		'title' => $title,
		'userid' => $_userid,
		'comid' => $comid,
		'catid'  => $catid,
		'postdate' => $postdate,
		'enddate' => $enddate,
		'userdate' => $userdate,
		'is_check' => $is_check,
		'nums' => $nums,
		'jinfo' => $jinfo,
		'usernum' => $usernum
	);
	$id = addInfo($items, $_POST['cus_value']);
	showmsg("发布券成功！","member.php");
}
?>