<?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);

	$c=new cache();
	switch($action)
	{
		case 'manage':
			if(isset($do_submit)) 
			{
				foreach($id as $val)
				{
					$db->update(DB_PRE.'membergrade',array('gradename'=>$gradename[$val],'grade'=>$grade[$val],'amount'=>$amount[$val],'point'=>$point[$val]),'id='.intval($val));
				}
				$c->cache_membergrade();
				showmsg($LANG['MANAGE_SUCEESS'],ADMIN_FILE.'?file=membergrade&action=manage');
			}
			$result=$db->fetch_all("SELECT * FROM ".DB_PRE."membergrade ORDER BY ".DB_PRE."membergrade.grade ASC,id DESC");
			include admin_tlp('membergrade_manage');			
			break;
		case 'add':
			if(isset($do_submit)) 
			{
				if(!$newmembergrade['gradename'])showmsg('会员等级名称不能为空!',ADMIN_FILE.'?file=membergrade&action=add');
				$db->insert(DB_PRE.'membergrade',$newmembergrade,true);
				$c->cache_membergrade();
				showmsg($LANG['MANAGE_SUCEESS'],ADMIN_FILE.'?file=membergrade&action=manage');
			}
			include admin_tlp('membergrade_add');
			break;
		case 'disable':
			$isdisabled=intval($isdisabled);
			if($db->query("UPDATE `".DB_PRE."membergrade` SET `".DB_PRE."membergrade`.status={$isdisabled} WHERE `".DB_PRE."membergrade`.`id`=".intval($id)." AND `".DB_PRE."membergrade`.`issystem`=0",true))
			{
				$c->cache_membergrade();
				showmsg($LANG['MANAGE_SUCEESS'],ADMIN_FILE.'?file=membergrade&action=manage');
			}
			else 
			{
				showmsg($LANG['MANAGE_ERROR'],ADMIN_FILE.'?file=membergrade&action=manage');
			}
			break;
		case 'delete':
			$r=$db->fetch_one("SELECT issystem FROM `".DB_PRE."membergrade` WHERE `".DB_PRE."membergrade`.`id`=".intval($id));
			if($r['issystem']==1)showmsg('禁止删除系统自带会员级别!',ADMIN_FILE.'?file=membergrade&action=manage',2000);
			$db->query("DELETE FROM `".DB_PRE."membergrade` WHERE `".DB_PRE."membergrade`.`id`=".intval($id),true);
			cache_delete('membergrade'.$id.'.cache.php',DIRCMS_ROOT.'data/cache/');
			$c->cache_membergrade();
			showmsg($LANG['MANAGE_SUCEESS'],ADMIN_FILE.'?file=membergrade&action=manage');
			break;
	}
?>