<?php
$sumfingIncl=true;
require '../config.thing.php';
$set = get_adm_opts();
include 'translate.php';
require '../fb/facebook.php';

$token = '';
$id = '';
$output='';

if(!isset($_GET['check']) || empty($_GET['check'])) die(_("Bad request"));
$admin=$_GET['check'];

$token = (isset($_GET['token'])) ? $_GET['token'] : $_POST['token'];
$id = (isset($_GET['id'])) ? $_GET['id'] : $_POST['id'];

// Init the Facebook SDK
$facebook = new Facebook(array(
	'appId'  => FB_APPID,
    'secret' => FB_SECRET,
	'cookie' => true,
));

// Get the current user
$user = $facebook->getUser();

$checkuser = is_user_fakeid($admin);
if($user != $checkuser->fb_id) die(_("Access denied"));
unset($checkuser);

//if admin made post
if(isset($_POST['submit'])) {
	
	$ban=$_POST['bandel'];
	$tickets=$_POST['freetickets'];
	$user=$_POST['userid'];
	$name=$_POST['username'];
	$email=$_POST['email'];
	$output='';
	
	if($tickets != 0) {
		make_tickets($tickets,$user,'admin',$name,$email);
		$output= $tickets . _(" tickets added");
	}
	
	switch($ban) {
		
		case 'ban':
		if(ban_user($admin,$user, 'y'))
		$output = _("This user is banned now!");
		break;
		
		case 'activate':
		if(ban_user($admin,$user, 'n'))
		$output = _("This user is activated now!");
		break;
		
		case 'del':
		if(delete_user_tickets($admin,$user))
		$output = _("All tickets were deleted!");
		break;
	}
	
}


//admin user manage form
if(isset($_GET['usr']) && !empty($_GET['usr'])) {
	$user = save_and_return('',$_GET['usr']);
	$userid = $user->fb_id;
	$email = $user->email;
	$name = $user->name;
	$status=($user->banned == 'n') ? 'Active' : 'Banned';
}

// Get options
$set = get_adm_opts();	
?>

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo _("User management");?></title>
<link href="../bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css" media="screen">
<!-- Javascripts -->
<script src="../js/jquery.js" type="text/javascript"></script>
<script src="../bootstrap/js/bootstrap.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
	
	<?php if($status == 'Active') { ?>
		$('#ban_btn').show();
		$('#activate_btn').hide();
	<?php } else {?>
		$('#ban_btn').hide();
		$('#activate_btn').show();	
	<?php }?>
	$('#freet').click(function() {
		$(".rad").removeAttr("checked");
	});
	$('.rad').click(function() {
		$('#freet').val(0);
	});
	$('#subm').click(function() {
		$(this).hide();
	});
});
</script>
</head>
<body>
<div class="container">
  
	<h3><?php echo _("User manager");?></h3>
    <div class="thumbnails">
    <?php if($output != '') {?>
    <div class="alert alert-success text-center"><?php echo $output;?></div>
	<?php }?>
    	<div class="span5 well">
			<p>
			<img src="https://graph.facebook.com/<?php echo $userid;?>/picture" class="img-polaroid" align="left" style="margin-right:8px">
			<?php echo _("Name:") . ' ' . $name;?><br>
			<?php echo _("Status:") . ' ' . $status;?><br>
			<?php echo _("Joined:") . ' ' . $user->regdate;?>
			</p>
            
            <?php if(isset($set->sec_form)) { 
				  	$sec_data = get_user_secdata($user->fake_id);
				    
					if(is_array($sec_data)) {
						foreach($sec_data as $v) {
							$data = explode(",",$v);
							echo '<p>'.$data[0].' -> <strong>'.$data[1].'</strong></p>';	
						}
					}
					
				  }
			?>
               
            
		</div>
		
		<div class="span5 well">
		
			<form method="post" action="mnguser.php?usr=<?php echo $userid.'&check='.$admin;?>">
				<input type="hidden" name="userid" value="<?php echo $userid;?>">
				<input type="hidden" name="username" value="<?php echo $name;?>">
				<input type="hidden" name="email" value="<?php echo $email;?>">
				
                <label class="radio" id="ban_btn">
                	<input type="radio" name="bandel" value="ban" class="rad"> <?php echo _("Ban user");?></p>
                </label>
                <label class="radio" id="activate_btn">
					<input type="radio" name="bandel" value="activate" class="rad"> <?php echo _("Activate user");?>
				</label>
                <label class="radio">
					<input type="radio" name="bandel" value="del" class="rad"> <?php echo _("Remove all user tickets");?>
				</label>
                <label class="dropdown">
                    <select name="freetickets" id="freet" class="input-mini">
                    <?php for($i=0;$i<=15;$i++){
                            echo '<option value="'.$i.'">'.$i.'</option>';
                          }?>
                    </select>
                     <?php echo _(" bonus tickets");?>
				</label>
				
				<input type="submit" name="submit" id="subm" value="<?php echo _("Confirm");?>" class="btn btn-primary">
				
			
			</form>
		
		</div>
       
    </div>
		
	<div class="span12">
		<h4><?php echo _("User tickets:");?></h4>
        
        <div>
        	<span class="label label-success"><?php echo _("BONUS");?></span>&nbsp;&nbsp;
            <span class="label label-info"><?php echo _("SLOT MACHINE");?></span>&nbsp;&nbsp;
            <span class="label label-important"><?php echo _("REFERRED");?></span>
            <span class="label label-warning"><?php echo _("WINNER");?></span>
            <span class="label label-inverse"><?php echo _("WIN REQUESTED");?></span>
        </div>
        <hr>
		<?php
			$usertickets = get_user_tickets($userid);
			if(isset($usertickets)) {
				echo '<p>'. _("Total: ") . count($usertickets) . '</p>';
				foreach($usertickets as $tic) {
					$ticket = explode("|",$tic);
					switch($ticket[3]) {
						case 'admin':
						$tickclass = ($ticket[4] == 1) ? 'warning' : 'success';
						break;
						case 'slot':
						$tickclass = ($ticket[4] == 1) ? 'warning' : 'info';
						break;
						default:
						$tickclass = ($ticket[4] == 1) ? 'warning' : 'important';
					}
					
					if($ticket[4] == 2){ 
						$tickclass = 'inverse';
					}
					?>
					<div>
						<div class="span6 label label-<?php echo $tickclass;?>" style="margin:5px; padding:3px; font-weight:normal">
							<?php echo _("ID # ") . $ticket[0] . ' &bull; ' . _("Date: ") . $ticket[1] . ' &bull; ' . _("Code: ") . $ticket[2];?>
						</div>
					</div>
					<?php
				}
			}
			//print_r($usertickets);
		?>
	</div>
  
</div>
</body>
</html>