<?php


if(isset($_GET['dell']))
{
	$dell = (int)$_GET['dell'];
	$mysql->query("DELETE FROM db_users WHERE Uid = '$dell'");
}

if(isset($_GET['edit']))
{
	if(isset($_POST['login']))
	{
		$login = clean($_POST['login']);
		$email = clean($_POST['email']);
		$moneyIn = intval($_POST['moneyin']);
		$moneyOut = intval($_POST['moneyout']);
		$Uid = intval($_POST['uid']);
		$moder = intval($_POST['moder']);
		
		$q = $mysql->prepare("UPDATE db_users SET Login = ?, Email = ?, MoneyIn = ?, MoneyOut = ?, ModerChat = ? WHERE Uid = ?");
		$q->execute(array($login, $email, $moneyIn, $moneyOut, $moder, $Uid));
		
		if(isset($_POST['uban']))
		{
			$uban = intval($_POST['uban']);
			$mysql->query("UPDATE db_users SET ChatBan = '$uban' WHERE Uid = '$Uid'");
			
		}
		elseif(isset($_POST['ban']))
		{
			$srok = intval($_POST['srok']);
			$ban = intval($_POST['ban']);
			if($ban == 1){
				if($srok == 1)
				{
					$timeBan = time() + (60*60*24);
					
				}
				elseif($srok == 2)
				{
					$timeBan = time() + (60*60*24*2);
					
				}
				elseif($srok == 3)
				{
					$timeBan = time() + (60*60*24*100500);
					
				}
				$q = $mysql->prepare("UPDATE db_users SET ChatBan = ?, DateBanChat = ? WHERE Uid = ?");
				$q->execute(array(1, $timeBan, $Uid));
			}
		}
		
		echo '<div class="alert alert-success fade in block-inner">
                        <button type="button" class="close" data-dismiss="alert">×</button>
                        Пользователь отредактирован!
                    </div>';
	}
	$id = intval($_GET['edit']);
	
	$q = $mysql->prepare("SELECT * FROM db_users WHERE Uid = ?");
	$q->execute(array($id));
	$r = $q->fetch();
?>

	<form class="form-horizontal" role="form" method="post" action=""><!-- Basic inputs -->
	<input type="hidden" name="uid" value="<?=$r['Uid']; ?>">
            <div class="panel panel-default">
                <div class="panel-heading"><h6 class="panel-title"><i class="icon-bubble4"></i> Редактирование пользователя <b><?=$r['Login']; ?></b></h6></div>
                <div class="panel-body">
                    
                    <div class="form-group"><label class="col-sm-2 control-label">Логин: </label>

                        <div class="col-sm-10"><input type="text" name="login" value="<?=$r['Login']; ?>" class="form-control"></div>
                    </div>
					
					<div class="form-group"><label class="col-sm-2 control-label">E-Mail: </label>

                        <div class="col-sm-10"><input type="email" name="email" value="<?=$r['Email']; ?>" class="form-control"></div>
                    </div>
					
					<div class="form-group"><label class="col-sm-2 control-label">Баланс на покупки: </label>

                        <div class="col-sm-10"><input type="text" name="moneyin" value="<?=$r['MoneyIn']; ?>" class="form-control"></div>
                    </div>
					
					
					<div class="form-group"><label class="col-sm-2 control-label">Баланс на вывод: </label>

                        <div class="col-sm-10"><input type="text" name="moneyout" value="<?=$r['MoneyOut']; ?>" class="form-control"></div>
                    </div>
					
					<div class="form-group"><label class="col-sm-2 control-label">Всего пополнено: </label>

                        <div class="col-sm-10"><input type="text" disabled value="<?=$r['MoneyEnter']; ?>" class="form-control"></div>
                    </div>
					
					<div class="form-group"><label class="col-sm-2 control-label">Всего выплачено: </label>

                        <div class="col-sm-10"><input type="text" disabled value="<?=$r['MoneyVivod']; ?>" class="form-control"></div>
                    </div>
					
					
					<div class="form-group"><label class="col-sm-2 control-label">Модератор в чате: </label>

                        <div class="col-sm-10"><select name="moder">
						<option value="1">Да
						<option value="0">Нет
						</select>
						</div>
                    </div>
					
					<?
					if($r['ChatBan'] == 1)
					{
					?>
					<div class="form-group"><label class="col-sm-2 control-label">Разбанить в чате: </label>

                        <div class="col-sm-10"><select name="uban">
						<option value="0">Да
						<option value="1">Нет
						</select>
						</div>
                    </div>
					<?
					}
					else
					{
					?>
					<div class="form-group"><label class="col-sm-2 control-label">Забанить в чате: </label>

                        <div class="col-sm-10"><select name="ban">
						<option value="1">Да
						<option value="0">Нет
						</select>
						</div>
						
						<div class="col-sm-10"><select name="srok">
						<option value="1">24 часа
						<option value="2">48 часов
						<option value="2">Навсегда
						</select>
						</div>
                    </div>
					<?
					}
					?>
					

                    
                   <div class="form-actions text-right">
                        <button type="submit" class="btn btn-primary">Сохранить</button>
                    </div>
                </div>
            </div>
           
           
            </form>
<?	
	return;
}
?>











<div class="tabbable page-tabs">
           
            <div class="tab-content"> <!-- First tab content -->
                <div class="tab-pane active fade in" id="inside"> <!-- Default datatable inside panel -->
                    <div class="panel panel-default">
                        <div class="panel-heading"><h6 class="panel-title"><i class="icon-table"></i> Список пользователей</h6></div>
                        <div class="datatable">
                            <table class="table">
                                <thead>
                                <tr>
                                    <th>#ID</th>
                                    <th>Логин</th>
                                    <th>E-Mail</th>
                                    <th>Баланс вывод</th>
                                    <th>Баланс покупки</th>
                                    <th>Дата регистрации</th>
                                    <th>Действие</th>
                                </tr>
                                </thead>
                                <tbody>
								<?
								$q = $mysql->query("SELECT * FROM db_users ORDER BY Uid DESC");
								while($row = $q->fetch())
								{
								?>
                                <tr>
                                    <td><?=$row['Uid']; ?></td>
                                    <td><?=$row['Login']; ?></td>
                                    <td><?=$row['Email']; ?></td>
                                    <td><?=$row['MoneyOut']; ?></td>
                                    <td><?=$row['MoneyIn']; ?></td>
                                    <td><?=date("d.m.Y", $row['DateReg']); ?></td>
                                    <td><a href="/developer/index.php/?admpage=users&edit=<?=$row['Uid']; ?>" class="btn btn-link">Редактировать</a> 
									|| <a href="/developer/index.php/?admpage=users&dell=<?=$row['Uid']; ?>" class="btn btn-link">Удалить</a></td>
                                </tr>
								<? } ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
					
					
                    </div>
                    </div>
                    </div>