<?php
	!defined('DIRCMS_MANAGE') && exit('Access Denied!');
	$r=cache_read('module.cache.php',DIRCMS_ROOT.'data/cache_module/');
	$r=$r['member']['roleid']; 
	if(!in_array($_roleid,$r) && $_userid!=ADMIN_FOUNDERS)showmsg_nourl('您没有该操作的权限!');

	$action=empty($action)?'manage':trim($action);
	require_once(DIRCMS_ROOT.'include/list.class.php');
	$l=new dircmslist();
	switch($action)
	{
		case 'manage':
			if(isset($do_submit))
			{
				if($do=='delete')
				{
					if(!is_array($id) && $id==intval(ADMIN_FOUNDERS))showmsg('网站创始人无法删除!',ADMIN_FILE.'?file=member&action=manage');
					$admin_founders=intval(ADMIN_FOUNDERS);
					if(is_array($id))
					{
						
						foreach($id as $vid)
						{
							$r=cache_read('cache_membermodel'.$modelid[$vid].'.cache.php',DIRCMS_ROOT.'data/cache_model/');
							if($vid!=$admin_founders)
							{
								$db->query("DELETE ".DB_PRE."member,".DB_PRE.$r['name']." FROM ".DB_PRE."member,".DB_PRE.$r['name']." WHERE ".DB_PRE."member.id=".DB_PRE.$r['name'].".userid AND ".DB_PRE."member.id=".intval($vid),true);
								$db->mysql_delete(DB_PRE."member_cache",intval($vid));
							}
						}
						showmsg($LANG['MANAGE_SUCEESS'],ADMIN_FILE.'?file=member&action=manage');
					}
					elseif(isset($id))
					{
						$r=cache_read('cache_membermodel'.$modelid.'.cache.php',DIRCMS_ROOT.'data/cache_model/');
						if($id!=$admin_founders)
						{
							$db->query("DELETE ".DB_PRE."member,".DB_PRE.$r['name']." FROM ".DB_PRE."member,".DB_PRE.$r['name']." WHERE ".DB_PRE."member.id=".DB_PRE.$r['name'].".userid AND ".DB_PRE."member.id=".intval($id),true);	
							$db->mysql_delete(DB_PRE."member_cache",intval($id));
						}
						showmsg($LANG['MANAGE_SUCEESS'],ADMIN_FILE.'?file=member&action=manage');
					}
					else
					{
						showmsg('请选择需要删除的用户!');
					}
				}
				elseif($do=='unpassed')
				{
					if(!$id)showmsg('请选择需要待审的用户!');
					foreach($id as $val)
					{
						if($val!=ADMIN_FOUNDERS)
						{
							$db->update(DB_PRE.'member',array('ispassed'=>0,'groupid'=>3),'id='.intval($val));
							$db->update(DB_PRE.'member_cache',array('ispassed'=>0,'groupid'=>3),'id='.intval($val));
						}
					}
					showmsg($LANG['MANAGE_SUCEESS'],ADMIN_FILE.'?file=member&action=manage');
				}
				elseif($do=='lock')
				{
					if(!$id)showmsg('请选择需要锁定的用户!');
					foreach($id as $val)
					{
						if($val!=ADMIN_FOUNDERS)
						{
							$db->update(DB_PRE.'member',array('islocked'=>1),'id='.intval($val));
							$db->update(DB_PRE.'member_cache',array('islocked'=>1),'id='.intval($val));
						}
					}
					showmsg($LANG['MANAGE_SUCEESS'],ADMIN_FILE.'?file=member&action=manage');
				}
				elseif($do=='unlock')
				{
					if(!$id)showmsg('请选择需要解锁的用户!');
					foreach($id as $val)
					{
						if($val!=ADMIN_FOUNDERS)
						{
							$db->update(DB_PRE.'member',array('islocked'=>0),'id='.intval($val));
							$db->update(DB_PRE.'member_cache',array('islocked'=>0),'id='.intval($val));
						}
					}
					showmsg($LANG['MANAGE_SUCEESS'],ADMIN_FILE.'?file=member&action=manage');
				}
				elseif($do=='upgrade')
				{
					if($do_submit==1)
					{
						if(!$id)showmsg('请选择需要升级的用户!');
						$group_select='';
						$r=cache_read('membergrade.cache.php',DIRCMS_ROOT.'data/cache/');
						if($r)foreach($r as $_r)
						{
							$group_select.='<label><input type="radio" name="gradeid" style="border:0px" value='.$_r['grade'].' />'.$_r['gradename'].'</label> ';
						}
						include admin_tlp('member_upgrade');
						break;
					}
					else if($do_submit==2)
					{
						if(!$gradeid)showmsg('请选择需要升级的会员等级!','?file=member&action=manage');
						$id=explode(',',$id);

						foreach($id as $val)
						{
							if(isset($delpoint) && $delpoint)
							{
								$r=$db->fetch_one("SELECT `".DB_PRE."member_cache`.`point`,`".DB_PRE."member_cache`.`amount` FROM `".DB_PRE."member_cache` WHERE `".DB_PRE."member_cache`.`id`=".intval($val));
								$p=cache_read('membergrade'.$gradeid.'.cache.php',DIRCMS_ROOT.'data/cache/');

								if(UPGRADEMETHOD=='amount')
								{
									$db->update(DB_PRE.'member',array('gradeid'=>intval($gradeid),'template'=>'default','isvip'=>1,'vipstart'=>TIME,'vipto'=>(TIME+intval($day)*24*3600),'amount'=>max(0,intval($r['amount']-$p['amount']))),'id='.intval($val));
									$db->update(DB_PRE.'member_cache',array('gradeid'=>intval($gradeid),'template'=>'default','isvip'=>1,'vipstart'=>TIME,'vipto'=>(TIME+intval($day)*24*3600),'amount'=>max(0,intval($r['amount']-$p['amount']))),'id='.intval($val));
								}
								else
								{
									$db->update(DB_PRE.'member',array('gradeid'=>intval($gradeid),'template'=>'default','isvip'=>1,'vipstart'=>TIME,'vipto'=>(TIME+intval($day)*24*3600),'point'=>max(0,intval($r['point']-$p['point']))),'id='.intval($val));
									$db->update(DB_PRE.'member_cache',array('gradeid'=>intval($gradeid),'template'=>'default','isvip'=>1,'vipstart'=>TIME,'vipto'=>(TIME+intval($day)*24*3600),'point'=>max(0,intval($r['point']-$p['point']))),'id='.intval($val));
								}
							}
							else
							{
								$db->update(DB_PRE.'member',array('gradeid'=>intval($gradeid),'template'=>'default','isvip'=>1,'vipstart'=>TIME,'vipto'=>(TIME+intval($day)*24*3600)),'id='.intval($val));
								$db->update(DB_PRE.'member_cache',array('gradeid'=>intval($gradeid),'template'=>'default','isvip'=>1,'vipstart'=>TIME,'vipto'=>(TIME+intval($day)*24*3600)),'id='.intval($val));
							}
						}
						showmsg($LANG['MANAGE_SUCEESS'],ADMIN_FILE.'?file=member&action=manage');
					}
				}
			}
			else
			{
				$where="ispassed=1";
				$where=isset($field)?$where." AND {$field}=".intval($fieldid):$where;
				if($stype=='username')
				{
					$k=trim($k);
					$where=!empty($k)?$where." AND username LIKE '%{$k}%'":$where;
				}
				else
				{
					$k=intval($k);
					$where=!empty($k)?$where." AND id = {$k}":$where;
				}
				$orderby=$orderby?$orderby:"id DESC";
				$page=max(isset($page)?intval($page):1,1);
				$result=$l->getlist(DB_PRE.'member_cache',$where,$orderby,$page,15);
				if(is_array($result))
				{
					foreach($result as $t_key => $t_r)
					{
						$m_t_r=cache_read('cache_membermodel'.$t_r['modelid'].'.cache.php',DIRCMS_ROOT.'data/cache_model/');
						$result[$t_key]['membermodel']=$m_t_r['modelname'];
						$g_t_r=cache_read('membergroup'.$t_r['groupid'].'.cache.php',DIRCMS_ROOT.'data/cache/');
						$result[$t_key]['membergroup']=$g_t_r['groupname'];
						$g_d_r=cache_read('membergrade'.$t_r['gradeid'].'.cache.php',DIRCMS_ROOT.'data/cache/');
						$result[$t_key]['membergrade']=$g_d_r['gradename'];
					}
				}
				$pages=$l->pages;
				include admin_tlp('member_manage');
			}
			break;
		case 'check':
			if(isset($do_submit))
			{
				if($do=='delete')
				{
					if(!is_array($id) && $id==intval(ADMIN_FOUNDERS))showmsg('网站创始人无法删除!',ADMIN_FILE.'?file=member&action=check');
					$admin_founders=intval(ADMIN_FOUNDERS);
					if(is_array($id))
					{
						
						foreach($id as $vid)
						{
							$r=cache_read('cache_membermodel'.$modelid[$vid].'.cache.php',DIRCMS_ROOT.'data/cache_model/');
							if($vid!=$admin_founders)
							{
								$db->query("DELETE ".DB_PRE."member,".DB_PRE.$r['name']." FROM ".DB_PRE."member,".DB_PRE.$r['name']." WHERE ".DB_PRE."member.id=".DB_PRE.$r['name'].".userid AND ".DB_PRE."member.id=".intval($vid),true);
								$db->mysql_delete(DB_PRE."member_cache",intval($vid));
							}
						}
						showmsg($LANG['MANAGE_SUCEESS'],ADMIN_FILE.'?file=member&action=check');
					}
					elseif(isset($id))
					{
						$r=cache_read('cache_membermodel'.$modelid.'.cache.php',DIRCMS_ROOT.'data/cache_model/');
						if($id!=$admin_founders)
						{
							$db->query("DELETE ".DB_PRE."member,".DB_PRE.$r['name']." FROM ".DB_PRE."member,".DB_PRE.$r['name']." WHERE ".DB_PRE."member.id=".DB_PRE.$r['name'].".userid AND ".DB_PRE."member.id=".intval($id),true);	
							$db->mysql_delete(DB_PRE."member_cache",intval($id));
						}
						showmsg($LANG['MANAGE_SUCEESS'],ADMIN_FILE.'?file=member&action=check');
					}
					else
					{
						showmsg('请选择需要删除的用户!');
					}
				}
				elseif($do=='passed')
				{
					if(!$id)showmsg('请选择需要操作的用户!');
					foreach($id as $val)
					{
						if($val!=ADMIN_FOUNDERS)
						{
							$db->update(DB_PRE.'member',array('ispassed'=>1,'groupid'=>4),'id='.intval($val));
							$db->update(DB_PRE.'member_cache',array('ispassed'=>1,'groupid'=>4),'id='.intval($val));
						}
					}
					showmsg($LANG['MANAGE_SUCEESS'],ADMIN_FILE.'?file=member&action=check');
				}
			}
			$where="ispassed=0 || ispassed=99";
			$where=isset($field)?$where." AND {$field}=".intval($fieldid):$where;
			if($stype=='username')
			{
				$k=trim($k);
				$where=!empty($k)?$where." AND username LIKE '%{$k}%'":$where;
			}
			else
			{
				$k=intval($k);
				$where=!empty($k)?$where." AND id = {$k}":$where;
			}
			$orderby=$orderby?$orderby:"id DESC";
			$page=max(isset($page)?intval($page):1,1);
			$result=$l->getlist(DB_PRE.'member_cache',$where,$orderby,$page,15);
			if(is_array($result))
			{
				foreach($result as $t_key => $t_r)
				{
					$m_t_r=cache_read('cache_membermodel'.$t_r['modelid'].'.cache.php',DIRCMS_ROOT.'data/cache_model/');
					$result[$t_key]['membermodel']=$m_t_r['modelname'];
					$g_t_r=cache_read('membergroup'.$t_r['groupid'].'.cache.php',DIRCMS_ROOT.'data/cache/');
					$result[$t_key]['membergroup']=$g_t_r['groupname'];
					$g_d_r=cache_read('membergrade'.$t_r['gradeid'].'.cache.php',DIRCMS_ROOT.'data/cache/');
					$result[$t_key]['membergrade']=$g_d_r['gradename'];
				}
			}
			$pages=$l->pages;
			$r=cache_read('membergroup.cache.php',DIRCMS_ROOT.'data/cache/');
			$membergroup='<select name="info[groupid]">';
			foreach($r as $v)
			{
				$membergroup.='<option value='.$v['id'].'>'.$v['groupname'].'</option>';
			}
			$membergroup.='</select>';
			include admin_tlp('member_check');
			break;
		case 'edit':
			include DIRCMS_ROOT.'include/member.class.php';
			$member=new member();
			$id=intval($id);
			if($id==ADMIN_FOUNDERS && $_userid!=ADMIN_FOUNDERS)showmsg('您无权修改网站创始人的信息!');

			if(isset($do_submit))
			{
				if(trim($password))
				{
					$info['password']=$member->password(trim($password));
				}
				$db->update(DB_PRE.'member',$info,'id='.intval($id));
				$db->update(DB_PRE.'member_cache',$info,'id='.intval($id));
				showmsg('会员信息修改成功!',ADMIN_FILE.'?file=member&action=manage');
			}
			$data=$member->get($id);
			@extract($data);
			include admin_tlp('member_edit');
			break;
		case 'add':
			include DIRCMS_ROOT.'include/member.class.php';
			$member=new member();

			if(isset($do_submit))
			{
				if(UC)
				{
					$password=trim($pwd);
					@extract($info);
					require DIRCMS_ROOT.'api/passport_server_ucenter.php';
				}
				$userid=$member->register($info['modelid'],$info['username'],$pwd,$info['email'],0);
				if(!$userid)
				{
					showmsg($member->msg);
				}
				else
				{
					showmsg('会员添加成功!',ADMIN_FILE.'?file=member&action=manage');
				}
			}
			$r=cache_read('cache_membermodel.cache.php',DIRCMS_ROOT.'data/cache_model/');
			$membermodel='<select name="info[modelid]">';			
			foreach($r as $v)
			{				
				$membermodel.='<option value='.$v['id'].'>'.$v['modelname'].'</option>';
			}
			$membermodel.='</select>';
			include admin_tlp('member_add');
			break;
		case 'sendmsg':
			if(isset($do_submit))
			{
				$db->insert(DB_PRE.'message',$info);
				$db->query("UPDATE `".DB_PRE."member` SET `".DB_PRE."member`.`message`=`".DB_PRE."member`.`message`+1");
				$db->query("UPDATE `".DB_PRE."member_cache` SET `".DB_PRE."member_cache`.`message`=`".DB_PRE."member_cache`.`message`+1");
				showmsg('短信群发成功!',ADMIN_FILE.'?file=member&action=sendmsg');
			}
			require_once(DIRCMS_ROOT.'include/form.class.php');	
			$form=new form();
			include admin_tlp('member_sendmsg');
			break;
	}
?>