<? include('checkcookie.php'); ?>
<h3>Просмотр пользователей</h3>
<br>
<?
if(isset($_POST["id"]))
{
	$id=$_POST["id"];
	$username=$_POST["username"];
        $password = myHashData(trim(strip_tags($_POST["password"])));
	$referer=$_POST["referer"];
        $referer2=$_POST["referer2"];
	$wmid=$_POST["wmid"];
	$pemail=$_POST["pemail"];
	$country=$_POST["country"];
	$money=$_POST["money"];
	$money_cr=$_POST["money_cr"];
	$user_status=$_POST["user_status"];
        $ball=$_POST["ball"];

mysql_query("UPDATE tb_users SET lastreferer='$lastreferer' where id='$id'"); 
mysql_query("UPDATE tb_users SET username='$username' where id='$id'"); 
mysql_query("UPDATE tb_users SET password='$password' where id='$id'"); 
mysql_query("UPDATE tb_users SET referer='$referer' where id='$id'"); 
mysql_query("UPDATE tb_users SET referer2='$referer2' where id='$id'"); 
mysql_query("UPDATE tb_users SET wmid='$wmid' where id='$id'"); 
mysql_query("UPDATE tb_users SET pemail='$pemail' where id='$id'"); 
mysql_query("UPDATE tb_users SET money='$money' where id='$id'"); 
mysql_query("UPDATE tb_users SET money_cr='$money_cr' where id='$id'"); 
mysql_query("UPDATE tb_users SET user_status='$user_status' where id='$id'"); 
mysql_query("UPDATE tb_users SET ball='$ball' 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 tb_users where id='$id'"); 
		while ($registroe = mysql_fetch_array($tablae))
		{ 
			?>
			<form method="post" action="adminmain.php?p=userred">
<table  width="100%" cellspacing="0" cellpadding="0">
	<thead><tr>
				<th>Id: </th>
                                <th><input type="hidden" name="id" value="<?= $registroe["id"] ?>"><?= $registroe["id"] ?></th></tr></thead>
                                <tr>
                                <td>Имя пользователя: </td><td><input type="text" name="username" value="<?= $registroe["username"] ?>">
				</td></tr><td>Пароль:  </td><td><input type="text" name="password" value="">
				</td></tr><td>Реферер:  </td><td><input type="text" name="referer" value="<?= $registroe["referer"] ?>">
                                </td></tr><td>Реферер 2ур:  </td><td><input type="text" name="referer2" value="<?= $registroe["referer2"] ?>">
				</td></tr><td>WMID:  </td><td><input type="text" name="wmid" value="<?= $registroe["wmid"] ?>">
				</td></tr><td>WM кошелек:  </td><td><input type="text" name="pemail" value="<?= $registroe["pemail"] ?>">
				</td></tr><td>Денег на балансе:  </td><td><input type="text" name="money" value="<?= $registroe["money"] ?>">
                                </td></tr><td>Денег на рекламном счете:  </td><td><input type="text" name="money_cr" value="<?= $registroe["money_cr"] ?>">
	                        </td></tr><td>Карьерный рост(баллов):  </td><td><input type="text" name="ball" value="<?= $registroe["ball"] ?>">
				</td></tr><td>Статус:&nbsp;  </td><td>(<?= $registroe["user_status"] ?>)&nbsp;&nbsp;
				<select name="user_status">
					<option value="<?= $registroe["user_status"] ?>"></option>
					<option value="admin">Администратор</option>
					<option value="user">Пользователь</option>
					<option value="moder">Модератор форума</option>
				</select>
				</td></tr><tr><td colspan='2' align='center'>				
				<input type="submit" value="сохранить" class="button"></td></tr></table>
			</form>
   <? exit;
		}
	}
	if ($_GET["option"]=="delete")
	{
		$id=$_GET["id"];
		mysql_query("DELETE FROM tb_users WHERE id='$id'");

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

		$referer=$_POST["referer"];
		mysql_query("update tb_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 tb_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>
<fieldset><legend>Сортировка списка пользователей</legend>
<form method="post" action="adminmain.php?p=userred&action=sorting">
Сортировка по полю:
<select name="sf">
	<option value="id">ID</option>
	<option value="username">Имя пользователя</option>
	<option value="referer">Реферер</option>
	<option value="referer2">Реферер 2ур</option>
	<option value="money">Сумма денег на балансе</option>
	<option value="wmid">WMID</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>
</fieldset>

<fieldset><legend>Поиск пользователей</legend>
<form method="post" action="adminmain.php?p=userred&action=search">
Строка поиска:
<input type="text" size="30" maxlength="150" value="<?=$searchtext ?>" name="stroka">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Поиск по полю:
<select name="sf1">
	<option value="id">ID</option>
	<option value="username">Имя пользователя</option>
	<option value="referer">Реферер</option>
        <option value="referer2">Реферер 2ур</option>
	<option value="money">Сумма денег на балансе</option>
	<option value="wmid">WMID</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>
</fieldset>

<fieldset><legend>Переход на страницу</legend>
<form method="post" action="adminmain.php?p=userred&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 tb_users");
	$kol=mysql_result($res,0,0);
	$floatpages=$kol/5;
	$intpages=intval($kol/5);
	if($floatpages>$intpages) { $pages=$intpages+1; }else{ $pages=$intpages; }
	echo "1 - $pages";
?>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;На одной странице отображается 30 пользователей!!!
</form>
</fieldset>
<br>
<table  width="100%" cellspacing="0" cellpadding="0">
	<tr>
		<th>№</th>
                <th>смс</th>
		<th>Имя пользователя</th>
		<th>Реферер</th>
                <th>Реферер 2ур</th>
		<th>Рефов</th>
		<th>На счету</th>
		<th>WMID</th>
		<th></th>
		<th></th>		<th></th>
	</tr>
	<?

	$sql="SELECT * FROM tb_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=5*($curpage-1);
		$p2=5*$curpage-1;
		$sql.=" limit $p1,$p2";
	}

	$tabla = mysql_query($sql); 
	while ($registro = mysql_fetch_array($tabla))
	{
                $username=$registro["username"];
		$wmid=$registro["wmid"];
		$referer=$registro["username"];
		$passHash=$registro["password"];
		$sql="select count(*) from tb_users where referer='$referer'";
		$res=mysql_query($sql);
		$res=mysql_result($res,0,0);
		?>
		<tr>
			<td><?=$registro["id"] ?></td>
<td><a href="wmk:msgto?to=<?=$registro["wmid"]; ?>&BringToFront=y"><img src="/images/mail.png"></a></td>
			<td><?=$registro["username"] ?></td>
			<td><?=$registro["referer"] ?></td>
                        <td><?=$registro["referer2"] ?></td>
			<td><?=$res ?></td>
			<td><?=$registro["money"] ?></td>
			<td><a href="https://passport.webmoney.ru/asp/certview.asp?wmid=<?=$wmid ?>" target="_blank"><?= $wmid ?></a></td>
			<td><?=$registro["user_status"] ?></td>
			<td>
				<form method="post" action="adminmain.php?p=userred&id=<?= $registro["id"] ?>&option=edit">
				<input type="submit" value="Редактировать" class="button">
				</form>
			</td>
			<td>
				<form method="post" action="adminmain.php?p=userred&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>