<?php
defined('IN_HS') or exit('Access Denied');
$submenu = array
(
	array($LANG['all_charge_card'], '?mod='.$mod.'&file='.$file.'&action=manage'),
	array($LANG['batch_create_charge_card'], '?mod='.$mod.'&file='.$file.'&action=add')
);
$menu = adminmenu($LANG['charge_card_manage'], $submenu);

$today = date('Y-m-d');
$pagesize = $HS['pagesize'];


	
$result = $db->query("SELECT * FROM ".TABLE_GAMESERVER." ORDER BY id");
while($r = $db->fetch_array($result))
{
	$servers[] = $r;
}

$carddb = get_db();
$cardtb = 'cards';

switch($action)
{
	case 'manage':
		if(!isset($status)) $status = '';
		$page = isset($page) ? intval($page) : 1;
		$offset = ($page-1)*$pagesize;

        $condition = '';
		if($status == 'notused')
		{
			$condition .= " where regtime=0 and enddate>='$today' or enddate='0000-00-00'";
		}
		elseif($status == 'used')
		{
			$condition .= " where regtime>0";
		}
		elseif($status == 'timeout')
		{
			$condition .= " where regtime=0 and enddate<'$today' and enddate!='0000-00-00'";
		}

		$r = $carddb->get_one("select count(*) as num from ".$cardtb." $condition");
		$pages = phppages($r['num'], $page, $pagesize);

        $paycards = array();
		$result=$carddb->query("SELECT * FROM ".$cardtb." $condition ORDER BY id desc limit $offset,$pagesize");
		while($r=$carddb->fetch_array($result))
		{
			$r['status'] = $r['regtime'] ? "<font color='red'>".$LANG['used']."</font>" : (  ($r['enddate']!='0000-00-00' && $r['enddate'] < $today) ? $LANG['out_of_date'] : "<font color='blue'>".$LANG['unused']."</font>" );
			$r['regtime'] = $r['regtime'] ? date("Y-m-d H:i:s",$r['regtime']) : "";
			$r['enddate'] = $r['enddate']=='0000-00-00' ? $LANG['no_limit'] : $r['enddate'];
			$paycards[] = $r;
		}
		include admintpl('paycard_manage1');
		break;

	case 'add':
		if($dosubmit)
		{
			$carddb = get_db($gameid);
		  $price = intval($price);
			//$paycardlen = intval($paycardlen);
			//if($paycardlen>14 || $paycardlen<8) showmessage($LANG['card_number_not_less_than8_greater_than14']);
			//$passwordlen = intval($passwordlen);
			//if($passwordlen>8 || $passwordlen<4) showmessage($LANG['charge_card_number_not_than4_greater_than8']);	
					
			$paycardlen = 14;
			$passwordlen = 8;

			$prefixlen = strlen($prefix);
			if($prefixlen>8 || $prefixlen<1) showmessage($LANG['card_number_prefix_not_than4_greater_than8']);

			$strnum = $paycardlen - strlen($prefix);
			$paycards = array();
			for($i=0; $i<$paycardnum; $i++)
			{
				$cardid = $prefix.random($strnum, '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ');
				$password = random($passwordlen, '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ');
				$adddate = date('Y-m-d');
				$paycards[] = array($cardid,$password);
				$result = $carddb->query("SELECT id FROM ".$cardtb." WHERE serial='$cardid'");
				if($carddb->num_rows($result)) continue;
				$password = md5($password);
				$carddb->query("INSERT INTO ".$cardtb." (serial,passwd,`value`,crtdate) VALUES('$cardid','$password','$price','$adddate')");
			}
			include admintpl('paycard_view');
		}
		else
		{
			$enddate = (date('Y')+5).date('-m-d');
			include admintpl('paycard_add1');
		}
		break;

	case 'delete':
		  if(empty($id)) showmessage($LANG['illegal_parameters']);

			$carddb = get_db($gameid);
		  $ids = is_array($id) ? implode(',',$id) : $id;
		  $carddb->query("DELETE FROM ".$cardtb." WHERE id IN ($ids)");
		  $carddb->affected_rows()>0 ? showmessage($LANG['operation_success'], $forward) : showmessage($LANG['operation_failure']);
		 break;

	case 'deletetimeout':
			$carddb = get_db($gameid);
		  $carddb->query("DELETE FROM ".$cardtb." WHERE chargeid=0 AND enddate<$today");
		  $carddb->affected_rows()>0 ? showmessage($LANG['operation_success'], $forward) : showmessage($LANG['operation_failure']);
		 break;

	case 'deleteused':
			
			$carddb = get_db($gameid);
		  $carddb->query("DELETE FROM ".$cardtb." WHERE chargeid>0");
		  $carddb->affected_rows()>0 ? showmessage($LANG['operation_success'], $forward) : showmessage($LANG['operation_failure']);
		 break;
}

function get_db($id = 0)
{
	global $db,$_userid,$_username;
	if ($id)
	{
		$rs = $db->get_one("SELECT * FROM ".TABLE_GAMESERVER." WHERE id='$id'");
	}
	else
	{
		$rs = $db->get_one("SELECT * FROM ".TABLE_GAMESERVER);
	}
	@extract($rs);
	if(!$dataattr) return "错误类型:unset data_attrib，请联系管理员！";
	if($dataattr == 'mssql')
	{
		if(!extension_loaded('mssql')) return "错误类型:unloaded mssql，请联系管理员！";
		include HS_ROOT.'/include/db_'.$dataattr.'.class.php';
		$mssqlco = @mssql_connect($serverip.':'.$port,$loginname,$loginpass,$dataacc);
		if(!$mssqlco) return "错误类型:connect mssql error，请联系管理员！";
		$sqldb = new db_mssql;
		$sqldb->connect($serverip.':'.$port,$loginname,$loginpass,$dataacc);
	}
	elseif($dataattr == 'mysql')
	{
		$msyqlco = @mysql_connect($serverip.':'.$port,$loginname,$loginpass,$dataacc);
		if(!$msyqlco) return "错误类型:connect mysql error，请联系管理员！";
		$sqldb = new db_mysql;
		$sqldb->connect($serverip.':'.$port,$loginname,$loginpass,$dataacc);
	}
	else
	{
		return "错误类型:data_attrib error，请联系管理员！";
	}
	return $sqldb;
}
?>