<?php
if (getCharacterGM($users['id']) >= 3){
	if(!isset($_GET['id'])){
		echo '<table class="table table-hover"><tr class="table-primary"><td>Player</td><td>Campaign</td><td>Value</td></tr>';
		$date=date("Ymj");
		foreach($campaigns as $camp) {
			$list=getCampaignList($camp['var_granted']);
			if ($list == 'error'){
			}
			elseif ($list == 'empty'){
			}
			else{
				foreach($list as $player){
					if($player['value']==1){
						echo '<tr class="table-danger"><td><a href=?p=gm&tool=fic&id='.$player['charid'].'>'.getCharacterName($player['charid']).'</a></td><td>'.$camp['name'].'</td><td>'.$player['value'].'</td></tr>';
					}
					elseif($player['value']==2){
						echo '<tr class="table-success"><td><a href=?p=gm&tool=fic&id='.$player['charid'].'>'.getCharacterName($player['charid']).'</a></td><td>'.$camp['name'].'</td><td>'.$player['value'].'</td></tr>';
					}
				}
			}
		}
		echo '</table>';
	}
	else{
		$id=$_GET['id'];
		$char=getCharacter($id);
		$char = $char[0];
		if (!isset($_POST['reward'])) {
		}
		else{
			setPlayerVariable($id,$_POST['reward'],2);
			foreach($campaigns as $camp) {
				if($camp['var_granted']==$_POST['reward']){
					if($camp['item1']=='any'){
						insertDeliveryBox($id,getCharacterName($id),getCharacterVar($id,$camp['var_granted'].'item1'),1,'FIC Reward');
					}
					elseif($camp['item1']=='None'){
					}
					else{
						insertDeliveryBox($id,getCharacterName($id),$camp['item1'],1,'FIC Reward');
					}
					if($camp['item2']=='any'){
						insertDeliveryBox($id,getCharacterName($id),getCharacterVar($id,$camp['var_granted'].'item2'),1,'FIC Reward');
					}
					elseif($camp['item2']=='None'){
					}
					else{
						insertDeliveryBox($id,getCharacterName($id),$camp['item2'],1,'FIC Reward');
					}
					if($camp['item3']=='any'){
						insertDeliveryBox($id,getCharacterName($id),getCharacterVar($id,$camp['var_granted'].'item3'),1,'FIC Reward');
					}
					elseif($camp['item3']=='None'){
					}
					else{
						insertDeliveryBox($id,getCharacterName($id),$camp['item3'],1,'FIC Reward');
					}
					if($camp['gil']>0){
						insertDeliveryBox($id,getCharacterName($id),65535,$camp['gil'],'FIC Reward');
					}
				}
			}
			echo getPanel("success","Success","The campaign has been completed and rewards distrubted.");
		}
		if (!isset($_POST['complete'])) {
		}
		else{
			setPlayerVariable($id,$_POST['complete'],2);
			echo getPanel("success","Success","The campaign has been completed.");
		}
		if (!isset($_POST['delete'])) {
		}
		else{
			setPlayerVariable($id,$_POST['delete'],0);
			echo getPanel("success","Success","The campaign has been deleted.");
		}
		echo '
		Reward Claim sends rewards to players Inbox and Completes Campaign<br>
		Complete Claim marks it complete but does not Send Rewards<br>
		Delete Claim sends it back to Player to finish

		<table class="table table-hover">
		<tr class="table-primary">
		<td><b>Campaign</td>
		<td><b>Level Required</td>
		<td><b>Rank Required</td>
		<td><b>Merits Required</td>
		<td><b>Campaign Required</td>
		<td><b>Item Required</td>
		<td><b>Key Item Required<br>Requires Manual Check</td>
		<td><b>Item Reward</td>
		<td><b>Item Reward</td>
		<td><b>Item Reward</td>
		<td><b>Gil Reward</td>
		<td><b>Claim</td>
		</tr>';
		foreach($campaigns as $camp) {
			$list=getCharacterVar($id,$camp['var_granted']);
			if($list >= 1){
				$unable=0;
				$lreq="Incomplete";
				$nreq="Incomplete";
				$mreq="Incomplete";
				$vreq="Incomplete";
				$ireq="Incomplete";
				$kreq="";
				$new=$camp['new'];
				$existing=$camp['existing'];
				$lvlr=getCharacterMaxLevel($id);
				if($lvlr>=$camp['level_required']){
					$lreq="Completed";
				}
				$rankr=getCharacterMaxRank($id);
				if($rankr>=$camp['rank_required']){
					$nreq="Completed";
				}
				$meritr=getCharacterExp($id);
				if($meritr['merits']>=$camp['merits_required']){
					$mreq="Completed";
				}
				$varr=getCharacterVar($id,$camp['var_required']);
				if(($varr>=1 && $varr!='No Var') || $camp['var_required']==""){
					$vreq="Completed";
				}
				$itemr=getCharacterItem($id,$camp['item_required']);
				if($itemr>=1 || $camp['item_required']==""){
					$ireq="Completed";
				}
				if($camp['key_item_required']==""){
					$kreq="Completed";
				}
				if($list==2){
					echo '<tr class="table-danger"> ';
				}
				else{
					echo '<tr class="table-info"> ';
				}
				echo '
				<td>
				'.$camp['name'].'</td>
				<td>'.$camp['level_required'].'<br> '.$lreq.'</td>
				<td>'.$camp['rank_required'].'<br>'.$nreq.'</td>
				<td>'.$camp['merits_required'].'<br> '.$mreq.'</td>
				<td>'.$camp['var_required'].'<br> '.$vreq.'</td>';
				if($camp['item_required']>=1){
					echo'<td>
					<a href="?p=items&id='.$camp['item_required'].'">
					<img src=http://static.ffxiah.com/images/icon/'.$camp['item_required'].'.png alt='.ucwords(str_replace('_',' ',getItemName($camp['item_required']))).' height=32 width=32>
					'.ucwords(str_replace('_',' ',getItemName($camp['item_required']))).'</a> <br> '.$ireq.'
					</td>';
				}
				else{
					echo '<td>'.$ireq.'</td>';
				}
				echo'
				<td>'.$camp['key_item_required'].'<br><a href=?p=characters&id='.$id.'>'.getCharacterName($id).'</a></td>';
				if($camp['item1']=='any'){
					$item1=getCharacterVar($id,$camp['var_granted'].'item1');
					echo'<td>
					<a href="?p=items&id='.$item1.'">
					<img src=http://static.ffxiah.com/images/icon/'.$item1.'.png alt='.ucwords(str_replace('_',' ',getItemName($item1))).' height=32 width=32>
					'.ucwords(str_replace('_',' ',getItemName($item1))).'</a>
					</td>';
				}
				elseif($camp['item1']=='None'){
					echo '<td>None</td>';
				}
				else{
					echo'<td>
					<a href="?p=items&id='.$camp['item1'].'">
					<img src=http://static.ffxiah.com/images/icon/'.$camp['item1'].'.png alt='.ucwords(str_replace('_',' ',getItemName($camp['item1']))).' height=32 width=32>
					'.ucwords(str_replace('_',' ',getItemName($camp['item1']))).'</a>
					</td>';
				}
				if($camp['item2']=='any'){
					$item2=getCharacterVar($id,$camp['var_granted'].'item2');
					echo'<td>
					<a href="?p=items&id='.$item2.'">
					<img src=http://static.ffxiah.com/images/icon/'.$item2.'.png alt='.ucwords(str_replace('_',' ',getItemName($item2))).' height=32 width=32>
					'.ucwords(str_replace('_',' ',getItemName($item2))).'</a>
					</td>';
				}
				elseif($camp['item2']=='None'){
					echo '<td>None</td>';
				}
				else{
					echo'<td>
					<a href="?p=items&id='.$camp['item2'].'">
					<img src=http://static.ffxiah.com/images/icon/'.$camp['item2'].'.png alt='.ucwords(str_replace('_',' ',getItemName($camp['item2']))).' height=32 width=32>
					'.ucwords(str_replace('_',' ',getItemName($camp['item2']))).'</a>
					</td>';
				}
				if($camp['item3']=='any'){
					$item3=getCharacterVar($id,$camp['var_granted'].'item3');
					echo'<td>
					<a href="?p=items&id='.$item3.'">
					<img src=http://static.ffxiah.com/images/icon/'.$item3.'.png alt='.ucwords(str_replace('_',' ',getItemName($item3))).' height=32 width=32>
					'.ucwords(str_replace('_',' ',getItemName($item3))).'</a>
					</td>';
				}
				elseif($camp['item3']=='None'){
					echo '<td>None</td>';
				}
				else{
					echo'<td>
					<a href="?p=items&id='.$camp['item3'].'">
					<img src=http://static.ffxiah.com/images/icon/'.$camp['item3'].'.png alt='.ucwords(str_replace('_',' ',getItemName($camp['item3']))).' height=32 width=32>
					'.ucwords(str_replace('_',' ',getItemName($camp['item3']))).'</a>
					</td>';
				}
				echo '
				<td>'.$camp['gil'].'</td>';
				$unablev=getCharacterVar($id,$camp['unable_var']);
				if($unablev==2){
					$unable=1;
					echo 'Unable Var 1<br>';
				}
				elseif($unablev==1){
					$unable=1;
					echo 'Unable Var 1<br>';
				}
				$unablev=getCharacterVar($id,$camp['unable_var2']);
				if($unablev==2){
					$unable=1;
					echo 'Unable Var 2<br>';
				}elseif($unablev==1){
					$unable=1;
					echo 'Unable Var 2<br>';
				}
				if($unable==1 || $list==2){
					echo '<td>Unable to Claim<br>
					<form action="" method="post">
					<input type="hidden" name="delete" value="'.$camp['var_granted'].'">
					<input type="submit" value="Delete Claim"></input></form</td>';
				}
				else{
					echo '<td>
					<form action="" method="post">
					<input type="hidden" name="reward" value="'.$camp['var_granted'].'">
					<input type="submit" value="Reward Claim"></input></form>

					<form action="" method="post">
					<input type="hidden" name="complete" value="'.$camp['var_granted'].'">
					<input type="submit" value="Complete Claim"></input></form>

					<form action="" method="post">
					<input type="hidden" name="delete" value="'.$camp['var_granted'].'">
					<input type="submit" value="Delete Claim"></input></form></td>';
				}
				echo '</tr>';
			}
		}
		$ips=getIPRecord($char['id']);
		foreach($ips as $rows) {
                        echo '<table style="border:1px solid black" class="table table-hover"><tr class="table-primary"><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></td><td> Date: '.$rows['login_time'].'</td></tr></table>';
		}
		echo '</table><a href=?p=gm&tool=fic>Back</a><br><br>';

	}
}
?>
