<?
if (!defined('webid')) {
    exit();
}

if ($user_status != 'admin') {
    exit();
}
if (isset($_POST[pay])) {
    $sql = mysql_fetch_array(mysql_query("SELECT * FROM tb_payme WHERE id = '$_POST[pay]'"));
    mysql_query("UPDATE tb_users SET paid = paid + '$sql[money]', lastpay = '" . time() . "' + 86400 WHERE id = '$sql[username]'");
    mysql_query("INSERT INTO tb_history (user, date, amount, method, status) VALUES('$sql[username]','". time() . "','$sql[money]','WebMoney','Выплата сделана')");
    mysql_query("INSERT INTO tb_allhistory (idus,data,doing,title) values ('$sql[username]','" . time() . "','5','Выплата WebMoney на сумму <b>$sql[money] $sitevalname</b>')");
	mysql_query("UPDATE tb_payx SET sumpay = sumpay + '$sql[money]' WHERE id = '1'");
    mysql_query("DELETE FROM tb_payme WHERE id = '$_POST[pay]'");
}
if (isset($_POST[del])) {
    mysql_query("DELETE FROM tb_payme WHERE id = '$_POST[del]'");
}
if (isset($_POST[back])) {
    $sql = mysql_fetch_array(mysql_query("SELECT * FROM tb_payme WHERE id = '$_POST[back]'"));
    mysql_query("UPDATE tb_users SET money = money + '$sql[money]' WHERE id = '$sql[username]'");
    mysql_query("DELETE FROM tb_payme WHERE id = '$_POST[back]'");
}

$sql = mysql_query("SELECT * FROM tb_payme");
$res = mysql_fetch_array(mysql_query("select sitename, comment from tb_site where id='1'"));
$sitename = $res["sitename"];
$comment = $res["comment"];
$comment = str_ireplace(":sitename:",$sitename,$comment);
?>
<table width="65%" border="0" cellpadding="0" cellspacing="1" class="tab">
    <thead style="background: #ebebeb;">
        <th width="60" align="center" style="padding: 3px;">ID</th>
        <th width="60" align="center" style="padding: 3px;">ID user</th>
        <th width="100" align="center" style="padding: 3px;">WMR</th>
        <th width="100" align="center" style="padding: 3px;">Money</th>
        <th width="100" align="center" style="padding: 3px;">IP</th>
        <th colspan="4">Действия</th>
    </thead>
<?
while ($row = mysql_fetch_array($sql)) {
    $userinf = mysql_fetch_array(mysql_query("SELECT username FROM tb_users WHERE id = $row[username]"));
    $text = str_ireplace(":username:",$userinf['username'],$comment);
    ?>
    <tr>
        <td align="center" class="rt"><?=$row['id']?></td>
        <td align="center" class="rt"><?=$row['username']?></td>
        <td align="center" class="rt"><?=$row['pemail']?></td>
        <td align="center" class="rt"><?=$row['money']?></td>
        <td align="center" class="rt"><?=$row['ip']?></td>
        <td align="center" class="rt"><a href='wmk:payto?Purse=<?= $row['pemail'] ?>&Amount=<?=$row['money'] ?>&Desc=<?=$text ?>&BringToFront=Y' id="save">Оплатить</a></td>
        <td align="center" class="rt"><form action="" method="post"><input type="hidden" name="pay" value="<?=$row['id']?>" /><input type="submit" id="save" value="Оплачено" /></form></td>
        <td align="center" class="rt"><form action="" method="post"><input type="hidden" name="del" value="<?=$row['id']?>" /><input type="submit" id="save" value="Удалить" /></form></td>
        <td align="center" class="rt"><form action="" method="post"><input type="hidden" name="back" value="<?=$row['id']?>" /><input type="submit" id="save" value="Вернуть" /></form></td>
    </tr>
    <?
}
?>
</table>