<?php
$date=date("F j, Y - g:i A");
if (getCharacterGM($users['id']) >= 2){
	if(!isset($_GET['id'])){
		echo '<form action="" method="post"><input type="text" name="name" placeholder="Enter a Character"></input>  <input type="submit" value="Search"></input></form>';
		echo '<form action="" method="post"><input type="text" name="aname" placeholder="Enter a Account Name"></input>  <input type="submit" value="Search"></input></form>';
		if (!isset($_POST['name'])){
		}
		elseif(strlen($_POST['name'])<'3'){
			echo getPanel("warning","Error","You Must enter more than 2 characters");
		}
		elseif(getNamesList($_POST['name'])=='empty'){
			echo getPanel("warning","Error","No Characters Found");
		}
		else{
			$charname = $_POST['name'];
			$char = getNamesList($charname);
			echo '<table class="table table-hover">';
			foreach ($char as $chars){
				echo '<tr><td><a href="?p=gm&tool=accounts&id='.$chars['accid'].'">'.$chars['charname'].'</a></td></tr>';
			}
			echo '</table>';
		}
		if (!isset($_POST['aname'])){
		}
		elseif(strlen($_POST['aname'])<='3'){
			echo getPanel("warning","Error","You Must enter more than 3 characters");
		}
		elseif(getAccountsList($_POST['aname'])=='empty'){
			echo getPanel("warning","Error","No Accounts Found");
		}
		else{
			$charname = $_POST['aname'];
			$char = getAccountsList($charname);
			echo '<table class="table table-hover">';
			foreach ($char as $chars){
				echo '<tr><td><a href="?p=gm&tool=accounts&id='.$chars['id'].'">'.$chars['login'].'</a></td></tr>';
			}
			echo '</table>';
		}
	}
	else{
		$id=$_GET['id'];
		echo '<table class="table table-hover">';
		$acc=getAccounts($id);
		if($acc!='empty'){
			echo '<tr><td>Account ID</td><td>'.$acc['id'].'</td></tr>
			<tr><td>Verification Code</td><td>'.$acc['verification_code'].'</td></tr>
			<tr><td colspan=2>Change Pass:</td></tr>
			<tr><td><form method="POST" action=?p=gm&tool=accounts&id='.$acc['id'].'><input type="hidden" name="accid" value='.$acc['id'].'>
			<input type=text name=pass></input></td><td><button class="btn btn-primary" type="submit">Update Pass</button></form></td></tr>
			';
			if(isset($_POST['accid']) && $_POST['accid']>=1){
				echo updatePass($_POST['pass'],$_POST['accid']);
				insertPassLog($users['id'],$_POST['accid'],$date);
			}
			if(isset($_POST['charid']) && $_POST['charid']>=1){
				echo updateName($_POST['name'],$_POST['charid']);
				insertNameLog($users['id'],$_POST['charid'],$date);
			}
			$chars=getCharacterList($id);
			if($chars!='empty'){
				foreach($chars as $row){
					echo '<tr class="table-success"><td><a href=?p=characters&id='.$row['charid'].'>'.$row['charname'].'</a></td><td>GM Level: '.$row['gmlevel'].'</td></tr>';
					echo '<tr><td colspan=2>Change Name:</td></tr><tr><td><form method="POST" action=?p=gm&tool=accounts&id='.$acc['id'].'><input type="hidden" name="charid" value='.$row['charid'].'>
					<input type=text name="name" placeholder="Verify name is not used"></input></td><td><button class="btn btn-primary" type="submit">Update Name</button></form></td></tr>';
				}
			}
			$ips=getIPRecord($id);
			if($ips!='empty'){
				foreach($ips as $rows){
					echo '<tr><td>Char ID: <a href=?p=characters&id='.$rows['charid'].'>'.$rows['charid'].'</a></td><td>IP: <a href=?p=gm&tool=ip&add='.$rows['client_ip'].'>'.$rows['client_ip'].'</a> Date: '.$rows['login_time'].'</td></tr>';
				}
			}
		}
		echo '</table>';
	}
}
?>