<? include('checkcookie.php'); ?>
<h3>Просмотр пользователей</h3>
<br>
<?
if(isset($_POST["id"])){
	$id=$_POST["id"];
	$number=$_POST["number"];
	$username=trim($_POST["username"]);
	$password=trim($_POST["password"]);
	$referer=$_POST["referer"];
	$wmid=$_POST["wmid"];
	$pemail=$_POST["pemail"];
	$qw_purse=$_POST["qw_purse"];
	$ym_purse=$_POST["ym_purse"];
	$lp_purse=$_POST["lp_purse"];
	$pm_purse=$_POST["pm_purse"];
	$py_purse=$_POST["py_purse"];
	$reyting=$_POST["reyting"];
	$money=round($_POST["money"],3);
	$email=$_POST["email"];
if($password == '') {
mysql_query("UPDATE b_users SET username='$username', referer='$referer', `wmid` = '$wmid', pemail='$pemail', `qw_purse`='$qw_purse', `ym_purse`='$ym_purse', `lp_purse`='$lp_purse', `pm_purse`='$pm_purse', `py_purse`='$py_purse', money='$money',email='$email', `reyting`='$reyting' where id='$id'");
}else{
mysql_query("UPDATE b_users SET username='$username', password='".md5($password)."', referer='$referer', `wmid` = '$wmid', pemail='$pemail', `qw_purse`='$qw_purse', `ym_purse`='$ym_purse', `lp_purse`='$lp_purse', `pm_purse`='$pm_purse', `py_purse`='$py_purse', `reyting`='$reyting', money='$money',email='$email' where id='$id'");
}
	echo "<font color=\"green\"><b>Сохранено.</b></font><br><br>";
}

if(isset($_GET["id"])){
	$id=$_GET["id"];
	if ($_GET["option"]=="edit"){
		$tablae = mysql_query("SELECT * FROM b_users where id='$id'"); 
		while ($registroe = mysql_fetch_array($tablae))
		{ 
			?>
			<form method="post" id='MainForm' action="adminmain.php?p=userlist">
			<table class='adn'>
			<tr class='lineb'>
			<td align=center>Значение</td>
			<td align="left"><table class="adn"><tr><td align="left" style="border: none; padding: 0">Описание</td><td align="right" style="border: none; padding: 0"><a href="#" onclick="document.getElementById('MainForm').submit(); return false" class="liv">Сохранить</a></td></tr></table></td>
			</tr>
			<tr class='liney ell'>
			<td align=left>
				 <input type="hidden" name="id" value="<?= $registroe["id"] ?>"><?= $registroe["id"] ?></td>
				 <td align=left>ID Пользователя</td>
				 </tr>
				 <tr class='liney ell'>
				 <td align=left><input type="text" name="username" value="<?= $registroe["username"] ?>">
				 </td><td align=left>Никнейм пользователя</td>
				 </tr>
				 <tr class='liney ell'>
				 <td align=left><input type="text" name="email" value="<?= $registroe["email"] ?>">
				 </td><td align=left>E-Mail пользователя</td>
				 </tr>
				 <tr class='liney ell'>
				 <td align=left>
				 <input type="text" name="password" value=""></td>
				 <td align=left>Пароль (если не хотите менять пароль оставте пустым)</td>
				 </tr>
				 <tr class='liney ell'>
				 <td align=left>
				<input type="text" name="referer" value="<?= $registroe["referer"] ?>"> | <? if($registroe["referer"]!=0 and $registroe["referer"]!='')
				{
				
				$rrr=mysql_fetch_assoc(mysql_query("SELECT username FROM b_users WHERE id='".$registroe["referer"]."'"));
				echo $rrr["username"]; 
				}?></td><td align=left> Реферер (вводить ID) </td></tr>
				<tr class='liney ell'>
				<td align=left><input type="text" name="wmid" value="<?= $registroe["wmid"] ?>"></td>
				<td align=left>WMID</td>
				</tr><tr class='liney ell'>
				<td align=left><input type="text" name="pemail" value="<?= $registroe["pemail"] ?>"></td>
				<td align=left>WM кошелёк</td>
				</tr><tr class='liney'>
				<td align=left><input type="text" name="qw_purse" value="<?= $registroe["qw_purse"] ?>"></td>
				<td align=left>QiWi</td>
				</tr><tr class='liney ell'>
				<td align=left><input type="text" name="ym_purse" value="<?= $registroe["ym_purse"] ?>"></td>
				<td align=left>Yandex</td>
				</tr><tr class='liney'>
				<td align=left><input type="text" name="lp_purse" value="<?= $registroe["lp_purse"] ?>"></td>
				<td align=left>LiqPay</td>
				</tr><tr class='liney ell'>
				<td align=left><input type="text" name="pm_purse" value="<?= $registroe["pm_purse"] ?>"></td>
				<td align=left>Perfect</td>
				</tr><tr class='liney'>
				<td align=left><input type="text" name="py_purse" value="<?= $registroe["py_purse"] ?>"></td>
				<td align=left>Payeer</td>
				</tr><tr class='liney ell'>
				<td align=left><input type="text" name="money" value="<?=(float)$registroe["money"] ?>"></td>
				<td align=left>Баланс</td>
				</tr><tr class='liney'>
				<td align=left><input type="text" name="reyting" value="<?=$registroe["reyting"] ?>"></td>
				<td align=left>Рейтинг</td>
				</tr>
			</table>
			</form>
			<?
		}
		exit();
	}
	if ($_GET["option"]=="delete"){
		$id=$_GET["id"];
		mysql_query("DELETE FROM b_users WHERE id='$id'");

		$user=$_POST["user"];
		mysql_query("update b_users set referer='' where referer='$user'");

		$referer=$_POST["referer"];
		mysql_query("update b_users set referals=referals-1 where username='$referer'");

		echo "<font color=\"#cc0000\"><b>Пользователь $user удален.</b></font><br><br>";
	}
}

$curpage="1";

if(isset($_GET["action"])){
	if($_GET["action"]=="sorting"){
		$sortfield=$_POST["sf"];
		$sortdir=$_POST["sd"];
		$searchtext=$_POST["sete"];
		$searchfield=$_POST["sefo"];
		$curpage=$_POST["cp"];
	}
	
	if($_GET["action"]=="search"){
		$searchtext=$_POST["stroka"];
		$searchfield=$_POST["sf1"];
		$sortfield=$_POST["sofi"];
		$sortdir=$_POST["sodir"];
		$curpage=$_POST["cp1"];
	}
	
	if($_GET["action"]=="changepage"){
		$curpage=$_POST["curpage"];
		if($curpage=='') $curpage=1;
		if(!is_numeric($curpage)) $curpage=1;
		if($curpage<1) $curpage=1;
		
		$res=mysql_query("select count(*) from b_users");
		$kol=mysql_result($res,0,0);
		$floatpages=$kol/30;
		$intpages=intval($kol/30);
		if($floatpages>$intpages) { $pages=$intpages+1; }else{ $pages=$intpages; }

		if($curpage>$pages) $curpage=$pages;

		$searchtext=$_POST["sete"];
		$searchfield=$_POST["sefo"];
		$sortfield=$_POST["sofi"];
		$sortdir=$_POST["sodir"];
	}
}
?>
<br><br>
<b><center>Сортировка списка пользователей</center></b>

<form method="post" action="adminmain.php?p=userlist&action=sorting">
Сортировка по полю:
<select name="sf">
	<option value="id">ID</option>
	<option value="username">Имя пользователя</option>
	<option value="referer">Реферер</option>
	<option value="visits">Кол-во визитов</option>
	<option value="money">Сумма денег на балансе</option>
	<option value="joindate">Дата регистрации</option>
	<option value="lastlogdate">Дата последнего входа</option>
	<option value="number">Номер</option>
</select>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Направление:
<select name="sd">
	<option value="asc">По возрастанию</option>
	<option value="desc">По убыванию</option>
</select>
<input type="submit" value="Сортировать" class="button">
<input type="hidden" value="<?=$searchtext ?>" name="sete">
<input type="hidden" value="<?=$searchfield ?>" name="sefi">
<input type="hidden" value="<?=$curpage ?>" name="cp">
</form><br><br>
<br><b><center>Поиск пользователей</center></b><br>
<form method="post" action="adminmain.php?p=userlist&action=search">
Строка поиска:
<input type="text" size="30" maxlength="150" value="<?=$searchtext ?>" name="stroka">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Поиск по полю:
<select name="sf1"><option value="number">Номер</option>
	<option value="id">ID</option>
	<option value="username">Имя пользователя</option>
	<option value="referer">Реферер</option>
	<option value="visits">Кол-во визитов</option>
	<option value="money">Сумма денег на балансе</option>
	<option value="joindate">Дата регистрации</option>
	<option value="lastlogdate">Дата последнего входа</option>
	
</select>
<input type="submit" value="Поиск" class="button">
<input type="hidden" value="<?=$sortdir ?>" name="sodir">
<input type="hidden" value="<?=$sortfield ?>" name="sofi">
<input type="hidden" value="<?=$curpage ?>" name="cp1">
</form><br><br>


<br><b><center>Переход на страницу</center></b><br>
<form method="post" action="adminmain.php?p=userlist&action=changepage">
Страница:
<input type="text" size="30" maxlength="150" value="<?=$curpage ?>" name="curpage">
<input type="submit" value="Переход" class="button">
<input type="hidden" value="<?=$sortdir ?>" name="sodir">
<input type="hidden" value="<?=$sortfield ?>" name="sofi">
<input type="hidden" value="<?=$searchtext ?>" name="sete">
<input type="hidden" value="<?=$searchfield ?>" name="sefi">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Всего страниц:&nbsp;
<?
	$res=mysql_query("select count(*) from b_users");
	$kol=mysql_result($res,0,0);
	$floatpages=$kol/30;
	$intpages=intval($kol/30);
	if($floatpages>$intpages) { $pages=$intpages+1; }else{ $pages=$intpages; }
	echo "1 - $pages";
?>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;На одной странице отображается 30 пользователей!!!
</form>
<table class='adn'><tr class='liney ell'><td>
<form method="post" action="adminmain.php?p=userlist&action=changepage">
<input type="hidden" value="<?=$curpage-1 ?>" name="curpage">
<input type="submit" value="Назад" class="button">
<input type="hidden" value="<?=$sortdir ?>" name="sodir">
<input type="hidden" value="<?=$sortfield ?>" name="sofi">
<input type="hidden" value="<?=$searchtext ?>" name="sete">
<input type="hidden" value="<?=$searchfield ?>" name="sefi"></form>
</td><td>
<form method="post" action="adminmain.php?p=userlist&action=changepage">
<input type="hidden" value="<?=$curpage+1 ?>" name="curpage">
<input type="submit" value="Вперёд" class="button">
<input type="hidden" value="<?=$sortdir ?>" name="sodir">
<input type="hidden" value="<?=$sortfield ?>" name="sofi">
<input type="hidden" value="<?=$searchtext ?>" name="sete">
<input type="hidden" value="<?=$searchfield ?>" name="sefi"></form>
</td><td>
<form method="post" action="adminmain.php?p=userlist&action=changepage">
<input type="hidden" value="1" name="curpage">
<input type="submit" value="В начало списка" class="button">
<input type="hidden" value="<?=$sortdir ?>" name="sodir">
<input type="hidden" value="<?=$sortfield ?>" name="sofi">
<input type="hidden" value="<?=$searchtext ?>" name="sete">
<input type="hidden" value="<?=$searchfield ?>" name="sefi"></form></td><td>
<form method="post" action="adminmain.php?p=userlist&action=changepage">
<input type="hidden" value="<?=$pages ?>" name="curpage">
<input type="submit" value="В конец списка" class="button">
<input type="hidden" value="<?=$sortdir ?>" name="sodir">
<input type="hidden" value="<?=$sortfield ?>" name="sofi">
<input type="hidden" value="<?=$searchtext ?>" name="sete">
<input type="hidden" value="<?=$searchfield ?>" name="sefi"></form></td></tr></table>
<br>
<table class='adn'>
	<tr class="lineb">
		<td>№</th>
		<td>Имя пользователя</td>
		<td>Referer</td>
		<td>Рефов</td>
		<td>Просмотров</td>
		<td>На счету</td>
		<td>Дата реги</td>
		<td>Посл. вход</td>
		<td></td>
		<td></td>
	</tr>
	<?

	$sql="SELECT * FROM b_users";
	if($searchfield!='')
	{
		$sql.=" where $searchfield like '$searchtext%'";
	}
	if($sortfield!='')
	{
		if($sortdir=="asc") { $sql.=" ORDER BY $sortfield ASC"; }else{ $sql.=" ORDER BY $sortfield DESC"; }
	}
	if($curpage!='')
	{
		$p1=30*($curpage-1);
		$p2=30*$curpage-1;
		$sql.=" limit $p1,$p2";
	}

	$tabla = mysql_query($sql); 
	while ($registro = mysql_fetch_array($tabla))
	{
		$wmid=$registro["wmid"];
		$referer=$registro["id"];
		$sql="select count(*) from b_users where referer='$referer'";
		$res=mysql_query($sql);
		$res=mysql_result($res,0,0);
		?>
		<tr class='liney ell'>
			<td><?=$registro["id"] ?></td>
			<td><?=$registro["username"] ?></td>
			<td><?=$registro["referer"] ?></td>
			<td><?=$res ?></td>
			<td><?=$registro["visits"] ?></td>
			<td><?=(float)$registro["money"] ?></td>
			<td><?=$registro["joindate"] ?></td>
			<td><?=$registro["lastlogdate"] ?></td>
			<td><?=$registro["number"] ?></td>

			<td>
				<form method="post" action="adminmain.php?p=userlist&id=<?= $registro["id"] ?>&option=edit">
				<input type="submit" value="Редактировать" class="button">
				</form>
			</td>
			<td>
				<form method="post" action="adminmain.php?p=userlist&id=<?= $registro["id"] ?>&option=delete">
				<input type="hidden" value="<?=$registro["username"] ?>" name="user">
				<input type="hidden" value="<?=$registro["referer"] ?>" name="referer">
				<input type="submit" value="удалить" class="button">
				</form>
			</td>
		</tr>
		<?
	} 
	?>
</table>