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

	$c=new cache();
	require_once(DIRCMS_ROOT.'include/list.class.php');
	$l=new dircmslist();
	switch($action)
	{
		case 'manage':
			if(isset($do_submit))
			{
				$id=$id?$id:array(0);
				$id=array_map('intval',$id);
				$db->mysql_delete(DB_PRE.'card',$id);
				$c->cache_cardid();
				foreach($id as $_id)
				{
					cache_delete("cache_cardid".$_id.".cache.php",DIRCMS_ROOT.'data/cache_pay/');
				}
				showmsg($LANG['MANAGE_SUCEESS'],ADMIN_FILE.'?file=card&action=manage');
			}
			$where=1;
			if(isset($enabled))
			{
				if($enabled==0)
				{
					$where=$where." AND status=0 OR endtime<".TIME;
				}
				else
				{
					$where=$where." AND status=".intval($enabled);
				}
			}
			
			$k=trim(preg_replace('/[!@#\$%\^&\*\(\):\'\"\{\}\[\]\.\?\,\/`\;]+/i','',$k));
			$where=!empty($k)?$where." AND cardid like '%".$k."%'":$where;
			$orderby="id DESC";
			$page=max(isset($page)?intval($page):1,1);
			$result=$l->getlist(DB_PRE.'card',$where,$orderby,$page,15);
			if($result)foreach($result as $key => $v)
			{
				$r=cache_read("cache_pointcard".$v['typeid'].".cache.php",DIRCMS_ROOT.'data/cache_pay/');
				$result[$key]['type']=$r['name'];
			}
			$pages=$l->pages;
			include admin_tlp('card_manage');
			break;
		case 'add':
			if(isset($do_submit))
			{
				@set_time_limit(0);
				$newcard['inputerid']=$_userid;
				$newcard['inputer']=$_username;
				$newcard['regtime']=TIME;
				$newcard['endtime']=TIME+intval($newcard['todate'])*24*3600;
				$newcard['regip']=IP;
				$r=cache_read("cache_pointcard".$newcard['typeid'].".cache.php",DIRCMS_ROOT.'data/cache_pay/');
				$newcard['point']=$r['point'];
				$newcard['amount']=$r['price'];
				$t_cardid=array();
				$r=cache_read("cache_cardid.cache.php",DIRCMS_ROOT.'data/cache_pay/');
				if($r)foreach($r as $_r)
				{
					$t_cardid[]=$_r['cardid'];
				}
				$newcard['len']=intval($newcard['len'])?intval($newcard['len']):16;							
				for($i=0;$i<$newcard['number'];$i++)
				{
					$newcard['cardid']=intval($newcard['pre']);
					$t_rand=getrandnumber($newcard['len']);
					$newcard['cardid']=!in_array($newcard['cardid'].$t_rand,$t_cardid)?$newcard['cardid'].$t_rand:$newcard['cardid'].getrandnumber($newcard['len']);
					$db->insert(DB_PRE.'card',$newcard,true);
					$c->cache_cardid();
				}
				showmsg($LANG['MANAGE_SUCEESS'],ADMIN_FILE.'?file=card&action=manage');
			}
			$pointcardselect='<select name="newcard[typeid]">';
			$r=cache_read("cache_pointcard.cache.php",DIRCMS_ROOT.'data/cache_pay/');
			if($r)foreach($r as $_r)
			{
				$pointcardselect.='<option value='.$_r['id'].'>'.$_r['name'].'</option>';
			}
			$pointcardselect.='</select>';
			include admin_tlp('card_add');
			break;
	}
?>
