<?php
define('IN_PHPQUAN', true);
require dirname(__FILE__) . '/include/common.php';
require PHPQUAN_ROOT . 'include/json.class.php';
$durl = 'member';
$act = $_REQUEST['act'] ? trim($_REQUEST['act']) : 'index';
$not_login = array('login','act_login','register','act_register','logout',  'ajax');
$must_login = array('index','modify','act_modify', 'edit_pass', 'act_edit_pass', 'com_list', 'editcom', 'updatecom', 'juan_view','juan_list','my_post','my_juan_view','act_my_pay','act_my_juan','user_juanlist','user','comnav');
if(empty($_userid)) {
    if (!in_array($act, $not_login)) {
        if (in_array($act, $must_login)) {
				pheader('member.php?act=login');
        } else {
			showmsg('请不要提交非法请求！');
		}
    }
}
switch($act)
{
	case 'index':
		if($_gid>0){
		//商家
			$comuid = com_info('comid',2);
		}else{
		//会员
			$myjuan_num = $db->getOne("SELECT count(*) FROM {$table}juan_user WHERE userid='$_userid'");
		}
		$userinfo = member_info($_userid);
		extract($userinfo);
		$registertime = date('Y年m月d日', $registertime);
		if($_gid>0){
			$seo['title'] = "商家中心_".$CFG['webname'];
			include template('member_com_index');
		}else{
			
			$seo['title'] = "会员中心_".$CFG['webname'];
			include template('member');
		}
	break;
	case 'login':
		$seo['title'] = "用户登录".$CFG['webname'];
		include template('login');
	break;
	case 'act_login':
		if(!submitcheck('submit')) showmsg('提交错误');
		$username = $_POST['username'] ? htmlspecialchars_deep(trim($_POST['username'])) : '';
		$password = $_POST['password'] ? trim($_POST['password']) : '';
		$md5_password = MD5($password);
		if(empty($username))showmsg("用户名不能为空");
		if(empty($password))showmsg("密码不能为空");
		$username = $db->getOne("SELECT username FROM {$table}member WHERE (username='".$username."' or phone='".$username."')");
		$userid = $db->getOne("SELECT userid FROM {$table}member WHERE username='".$username."'");
		if(empty($username) && empty($userid))showmsg("用户名不存在");
		if(login($username,$md5_password)) {
			pheader('member.php');
		} else {
			showmsg("登录失败",'member.php?act=login');
		}
	break;
	
	case 'logout':
		logout();
		pheader("index.php");
	break;
	case 'register':
		if($CFG['close_register'] == '1') showmsg('本站已关闭用户注册。');
		if(!empty($_userid)) {
			$link = "member.php?act=index";
			showmsg("请退出后再进行该操作", $link);
		}
		$seo['title'] = "会员注册";
		include template('register');
	break;
	case 'act_register':
		$ip = get_ip();
		if(!submitcheck('submit')) showmsg('提交错误');
		$username   = $_POST['username'] ? htmlspecialchars_deep(trim($_POST['username'])) : '';
		$password   = $_POST['password'] ? trim($_POST['password']) : '';
		$repassword = $_POST['repassword'] ? trim($_POST['repassword']) : '';
		$md5_password = MD5($password);
		$code   = $_POST['code'] ? htmlspecialchars_deep(trim($_POST['code'])) : '';
		if(empty($username))showmsg("用户名不能为空");
		if(!check_name($username))showmsg("用户名仅可使用汉字、字母、数字、中划线或下划线。2-10个字符");
		if(empty($password))showmsg("密码不能为空");
		if(empty($repassword))showmsg("重复密码不能为空");
		if($code!=$_SESSION['lede_my_code'])showmsg("验证码错误，请核对后输入！");
		if($password != $repassword)showmsg("两次输入的密码不相同");
			if(register($username,$md5_password)) {
				showmsg("注册成功","member.php");
			} else {
				$link = "member.php?act=register";
				showmsg("注册失败",$link);
			}
		login($username, $md5_password);
		showmsg('注册成功', 'member.php');
	break;
	case 'modify':
		$userinfo = member_info($_userid);
		$userinfo['shengri'] = $userinfo['shengri']>0 ? date("Y-m-d",$userinfo['shengri']) : '';
		$here = array('title'=>'修改会员资料','url'=>'javascript:history.back()');
		$seo['title'] = "修改会员资料_".$CFG['webname'];
		include template('member_edit_user');
	break;
	case 'act_modify':
		$phone    = $_POST['phone'] ? htmlspecialchars_deep($_POST['phone']) : '';
		$shengri  = $_POST['shengri']>0 ? strtotime($_POST['shengri']) : '';
		$payid    = intval($_POST['payid']);
		$email    = $_POST['email'] ? htmlspecialchars_deep($_POST['email']) : '';
		if($email){
			if(!is_email($email))showmsg('Email 格式有误');
		}
		$sql = "update {$table}member set 
				phone = '$phone',
				shengri = '$shengri',
				email = '$email'
				where userid='$_userid'";
		$res = $db->query($sql);
		showmsg('修改资料成功', 'member.php?act=modify');
	break;
	case 'edit_pass':
		$userinfo = member_info($_userid);
		$seo['title'] = '修改密码_'.$CFG['webname'];
		$here = array('title'=>'修改密码','url'=>'javascript:history.back()');
		include template('member_edit_pass');
	break;
	case 'act_edit_pass':
		$oldpassword = $_POST['oldpassword'] ? trim($_POST['oldpassword']) : '';
		$password = $_POST['password'] ? trim($_POST['password']) : '';
		$repassword = $_POST['repassword'] ? trim($_POST['repassword']) : '';
		
		if(empty($oldpassword) && !empty($password))showmsg('请输入旧密码！');
		if($password && $repassword && $password!=$repassword)showmsg('两次输入的密码不一致！');
		$sql = "SELECT password FROM {$table}member WHERE userid='$_userid' LIMIT 1";
		if(MD5($oldpassword) != $db->getOne($sql))showmsg('旧密码输入错误');
		$password = MD5($password);
		$query = $db->query("UPDATE {$table}member SET password='$password' WHERE userid='$_userid' ");
		showmsg('密码修改成功!','member.php?act=logout');
	break;
	case 'ajax':
		$json = new Services_JSON;
		switch($_REQUEST['type'])
		{
			case 'username':
				$username = trim($_REQUEST['username']);
				$sql = "SELECT count(*) FROM {$table}member WHERE username='$username'";
				$count = $db->getOne($sql);
				if($count>0) {
					echo $json->encode(false);
					exit;
				} else {
					echo $json->encode(true);
					exit;
				}
			break;
			case 'email':
				$count = $uc_count = 0;
				$email = trim($_REQUEST['email']);
				$sql = "SELECT userid FROM {$table}member WHERE email='$email'";
				$count = $db->getOne($sql);
				if($count>0) {
					echo $json->encode(false);
					exit;
				} else {
					echo $json->encode(true);
					exit;
				}
			break;
		}
	break;

	case 'editcom':
		check_gid();
		$sql = "SELECT c.*,cc.catname FROM {$table}com as c left join {$table}com_cat as cc on c.catid=cc.catid WHERE userid = '$_userid'";
		$com = $db->getRow($sql);
		if(empty($com))showmsg('商家不存在','index.php');
		extract($com);
		if($userid!=$_userid)showmsg('店铺不是您的，不能修改');
		$postdate = date('Y-m-d', $postdate);
		$seo['title'] = '修改商家信息';
		include template('member_edit_com','com');
	break;
	case 'updatecom':
		check_gid();
		$comid    = intval($_POST['id']);
		$phone    = $_POST['phone'] ? trim($_POST['phone']) : '';
		$address  = $_POST['address'] ? trim($_POST['address']) : '';
		$sql = "SELECT userid FROM {$table}com WHERE comid = '$comid'";
		$comuid = $db->getOne($sql);
		if(empty($comuid))showmsg('商家不存在','index.php');
		if($comuid!=$_userid)showmsg('店铺不是您发布的，不能修改');
		$sql = "UPDATE {$table}com SET
				phone='$phone',
				address='$address'
				where comid = '$comid' ";
		$res = $db->query($sql);	
		showmsg("修改成功！", "member.php");
	break;

	case 'juan_view':
		$cat_array = get_cat_array();	
		$id = intval($_REQUEST['id']);
		$sql = "select * from {$table}juan where userid='$_userid' and id='$id'";
		$juan = $db->getRow($sql);
		extract($juan);
		$userjuan = $db->getOne("select count(*) from {$table}juan_user where juanid='$id'");
		$useroff = $db->getOne("select count(*) from {$table}juan_user where juanid='$id' and offdate>0");
		if(empty($juan)) showmsg('券不存在','member.php');
		$postdate = date('y/m/d', $postdate);
		$enddates = date('y/m/d', $enddate);
		$yunum = round($nums-$userjuan);
		$lastdate = enddate($enddate);
		$forusers = $foruser==1 ? '仅在本店核销过会员可领' : '';
		$jinfo = nl2br($jinfo);
		$seo['title'] = '券内容_'.$CFG['webname'];	
		include template('member_juan_view');
	break;
	case 'juan_list':
		check_gid();
		$id = intval($_REQUEST['id']);
		$r = $db->getOne("SELECT count(*) FROM {$table}juan_user WHERE juanid='$id' and offdate>0");
		$page = empty($_REQUEST['page'])? 1 : intval($_REQUEST['page']);
		$page = isset($page) ? intval($page) : 1;
		$pagesize = 20;
		$pager['search'] = array('act' => 'juan_list','id'=>$id);
		$pager = get_pager('member.php', $pager['search'], $r, $page, $pagesize);
		$custom = get_juan_custom($id);
		$juanlist = array();
		$result = $db->query("SELECT * FROM {$table}juan_user WHERE juanid='$id' and offdate>0 ORDER BY juanid DESC LIMIT $pager[start],$pager[size]");
		while($r = $db->fetchrow($result)) {
			$r['offdate'] = date("Y-m-d H:i",$r['offdate']);
			$juanlist[] = $r;
		}
		$tt = !empty($custom) ? cut_str($custom[0][value],4).'...'.$custom[0][unit] : '核销记录';
		$here = array('title'=>$tt,'url'=>'javascript:history.back()');
		$seo['title'] = '券核销记录_'.$CFG['webname'];
		include template('member_juan_list');
	break;
	case 'my_post':
		check_gid();
		$cat_array = get_cat_array();
		$typeid = empty($_REQUEST['typeid']) ? '1' : intval($_REQUEST['typeid']);
		$times = time();
		if($typeid=='1'){
			$sqls = "AND enddate>='$times'";
		}elseif($typeid=='2'){
			$sqls = "AND enddate<'$times'";
		}
		$r1 = $db->getOne("SELECT count(*) FROM {$table}juan WHERE userid='$_userid' AND enddate>='$times'");
		$r2 = $db->getOne("SELECT count(*) FROM {$table}juan WHERE userid='$_userid' AND enddate<'$times'");
		$r = $db->getOne("SELECT count(*) FROM {$table}juan WHERE userid='$_userid' $sqls");
		$page = empty($_REQUEST['page'])? 1 : intval($_REQUEST['page']);
		$page = isset($page) ? intval($page) : 1;
		$pagesize = 10;
		$pager['search'] = array('act' => 'my_post','typeid'=>$typeid);
		$pager = get_pager('member.php', $pager['search'], $r, $page, $pagesize);
		$sql = "select id,title,nums,postdate,enddate,catid, (SELECT count(*) FROM {$table}juan_user where juanid={$table}juan.id and offdate>0) as useroff,(SELECT count(*) as userjuan FROM {$table}juan_user where juanid={$table}juan.id) as userjuan from {$table}juan where userid='$_userid' $sqls order by postdate desc,enddate desc LIMIT $pager[start],$pager[size]";
		$res = $db->query($sql);
		$juan = array();
		while($row=$db->fetchRow($res)) {
			$row['postdate'] = date('Y-m-d', $row['postdate']);
			$row['enddates'] = date('Y-m-d', $row['enddate']);
			$juan[$row['id']] = $row;
		}
		$seo['title'] = '我发布的券_'.$CFG['webname'];
		include template('member_mypost_list');
	break;
	case 'act_my_juan':
		$id = intval($_REQUEST['id']);
		$payname = htmlspecialchars(trim($_REQUEST['pay']));
		$orderid = htmlspecialchars(trim($_REQUEST['orderid']));
		if(empty($id)) showmsg('缺少id参数！');
		if($_gid>0)showmsg('你所在用户组无法领券!');
		if($_userid>0){
		$time = time();
		$cip = get_ip();
		$sql = "SELECT a.* FROM {$table}juan as a left join {$table}juan_cat as c on a.catid=c.catid WHERE id='$id' and enddate>='$time' and is_check=1";
		$juan = $db->getRow($sql);
		if(empty($juan)) showmsg('券不存在','list.php');
		extract($juan);
		
		$n = $db->getOne("select count(*) from {$table}juan_user where juanid='$id'");
		if($n >= $nums) showmsg("优惠券已经抢光了！");
		if($foruser=='1'){
			$sql = "select count(*) from {$table}juan_user where comid='$comid' and offdate>0 and userid='$_userid'";
			if($db->getOne($sql) < 1) showmsg("未在商家核销过优惠券，无法领取！");
		}	
			$userdates = time() + 3600 * 24 * $userdate;
			cd:
			$code = date("ymd",time()).random(6,1);
			$codeid = $db->getOne("SELECT count(*) FROM {$table}juan_user WHERE codeid='$code'");
			if(empty($codeid) && strlen($code)==12){
				$sql = "insert into {$table}juan_user (codeid,userid,juanid,comid,adddate,enddate,ip) values ('$code','$_userid','$id','$comid','$time','$userdates','$cip')";
				$db->query($sql);
				showmsg("领取成功","member.php?act=my_juan_view&codeid=".$code);
			}else{
				goto cd;
			}
		}else{
			showmsg('每人限领'.$usernum.'次，你已经达到限制次数！');
		}
	break;

	case 'my_juan_view':
		$codeids = trim($_REQUEST['codeid']);
		$jid = intval($_REQUEST['id']);
		if($_gid>0)showmsg('你所在用户组无法领券!','index.php');
		$urlcode = htmlspecialchars_deep($_REQUEST['code']);
		if(!empty($codeids)){
			$sqls = "a.codeid='$codeids'";
		}
		if(!empty($jid)){
			$sqls = "a.id='$jid'";
		}
		if(empty($codeids) && empty($jid)) showmsg('缺少参数！');
		$sql = "SELECT a.*,a.enddate as aenddate,b.*,c.comid as cid,c.comname,c.phone,c.address FROM {$table}juan_user AS a LEFT JOIN {$table}juan AS b ON a.juanid=b.id left join {$table}com as c on a.comid=c.comid WHERE $sqls and b.is_check=1 and a.userid='$_userid'";
		$juan = $db->getRow($sql);
		if(empty($juan)) showmsg('券不存在','list.php');
		extract($juan);
		$cat_array = get_cat_array();
		$comurl   = url_rewrite('com',array('act'=>'view','comid'=>$cid));
		$catname = $cat_array[$catid][0];
		$postdate = date('m/d', $postdate);
		$enddates = date('Y/m/d', $aenddate);
		$lastdate = enddate($aenddate);
		$upusername = member_info('username','4',$upid);
		$jinfo = nl2br($jinfo);
		
		if($aenddate>=time() && empty($offdate)){
			$zt = '有效';
			$zts = 'my_juan_view_qr';
		}elseif($aenddate<time() && empty($offdate)){
			$zt = '过期';
		}else{
			$zt = '已核销';
			$zts = 'my_juan_view';
		}
		$seo['title'] = '我的券_'.$CFG['webname'];
		include template('member_myjuan_view');
	break;
	case 'user_juanlist':
		$cat_array = get_cat_array();
		$typeid = empty($_REQUEST['typeid']) ? '1' : intval($_REQUEST['typeid']);
		$times = time();
		if($typeid=='1'){
			$sqls = "a.userid='$_userid' and a.enddate>='$times' and a.offdate<=0";
		}elseif($typeid=='2'){
			$sqls = "a.userid='$_userid' and (a.offdate>0)";
		}elseif($typeid=='3'){
			$sqls = "a.userid='$_userid' and a.enddate<$times and (a.offdate<=0 or a.offdate is null)";
		}
		$r1 = $db->getOne("SELECT count(*) FROM {$table}juan_user as a WHERE a.userid='$_userid' AND a.enddate>='$times' and a.offdate<=0");
		$r2 = $db->getOne("SELECT count(*) FROM {$table}juan_user as a WHERE a.userid='$_userid' AND (a.offdate>0)");
		$r3 = $db->getOne("SELECT count(*) FROM {$table}juan_user as a WHERE a.userid='$_userid' AND a.enddate<$times and (a.offdate<=0 or a.offdate is null)");
		$r = $db->getOne("SELECT count(*) FROM {$table}juan_user as a WHERE $sqls");
		$page = isset($_REQUEST['page']) ? intval($_REQUEST['page']) : 1;
		$pagesize = 10;
		$pager['search'] = array('act' => 'user_juanlist','typeid'=>$typeid);
		$pager = get_pager('member.php', $pager['search'], $r, $page, $pagesize);
		$juan = array();
		$sql = "select a.*,a.id as aid,b.id as bid,c.comname,b.catid as bbid,b.postdate from {$table}juan_user as a left join {$table}juan as b on a.juanid=b.id left join {$table}com as c on a.comid=c.comid where $sqls order by offdate desc,adddate desc,enddate desc LIMIT $pager[start],$pager[size]";
		$res = $db->query($sql);
		while($row=$db->fetchRow($res)){
			$row['postdate'] = date("m/d", $row['postdate']);
			$row['enddates'] = date("m/d", $row['enddate']);
			$row['adddate']  = date("Y-m-d", $row['adddate']);
			$row['catcolor'] = $cat_array[$row['bbid']][1];
			$row['red'] = $cat_array[$row['bbid']][2];
			$row['comurl']   = url_rewrite('com',array('act'=>'view','comid'=>$row['comid']));
			$juan[] = $row;
		}
		$seo['title'] = '我领取的券_'.$CFG['webname'];
		include template('member_user_juanlist');
	break;

	case 'user':
		$here = array('title'=>'个人信息设置','url'=>'member.php');
		$seo['title'] = '个人设置_'.$CFG['webname'];
		include template('member_user');
	break;

	case 'comnav':
		check_gid();
		$coms = $db->getRow("SELECT comid FROM {$table}com WHERE userid='$_userid'");
		$comid = $coms['comid'];
		$r = $db->getOne("SELECT count(*) FROM {$table}comment WHERE comid='$comid'");
		$usernew = $db->getOne("SELECT count(*) FROM {$table}juan_user WHERE comid='$comid' and adddate>".mktime(0,0,0));
		$userhx = $db->getOne("SELECT count(*) FROM {$table}juan_user WHERE comid='$comid' and offdate>".mktime(0,0,0));
		$u = member_info($_userid);
		$seo['title'] = '我的店铺_'.$CFG['webname'];
		include template('member_com');
	break;
}
?>