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

if ($user_status != 'admin') {
    exit();
}

if (isset($_POST['del'])) {
    mysql_query("DELETE FROM tb_ads WHERE id = '" . $_POST['id'] . "'");
    mysql_query("DELETE FROM tb_visits WHERE ident = '" . $_POST['id'] . "'");
}

if (isset($_GET['edit'])) {
    if (isset($_POST['save'])) 
	{
        mysql_query("UPDATE tb_ads SET description='$_POST[addescription]', fromus='$_POST[adfromus]', plan='$_POST[adplan]', balance='$_POST[adbalance]', texto='$_POST[adtexto]', url='$_POST[adurl]', price='$_POST[adprice]', timer='$_POST[adtimer]', highlight='$_POST[adhighlight]', obper='$_POST[adobper]', paused='$_POST[adpaused]', modered='$_POST[admodered]' WHERE id='$_GET[edit]'");
        ?>
        <script type="text/javascript">window.location.replace('/admin/?adm=20');</script>
        <?
    }
    $sql = mysql_fetch_array(mysql_query("SELECT * FROM tb_ads WHERE id = '" . $_GET['edit'] . "'"));
    
	if($sql['timer'] == 6) $prc = $payuser['dyn5'];
    if($sql['timer'] == 5) $prc = $payuser['dyn10'];
    if($sql['timer'] == 0) $prc = $payuser['dyn20'];
    if($sql['timer'] == 1) $prc = $payuser['dyn30'];
    if($sql['timer'] == 2) $prc = $payuser['dyn40'];
    if($sql['timer'] == 3) $prc = $payuser['dyn50'];
    if($sql['timer'] == 4) $prc = $payuser['dyn60'];	
	?>
    <form action="" method="post"> 
    <table width="60%" border="0" cellpadding="0" cellspacing="1" class="tab">
    <thead>
        <th colspan="2" style="background: #ebebeb; padding: 5px;" align="right"><input type="submit" value="Сохранить" id="save" name="save" /></th>
    </thead>
    <tr>
        <td class="lt" width="200"><?=$sql['id']?></td>
        <td class="rt">ID Площадки</td>
    </tr>
    <tr>
        <td class="lt"><input type="text" name="adtexto" size="30" value="<?=$sql['texto']?>" /></td>
        <td class="rt">Краткое описание</td>
    </tr>
    <tr>
        <td class="lt"><input type="text" name="addescription" size="30" value="<?=$sql['description']?>" /></td>
        <td class="rt">Заголовок</td>
    </tr>    
	<tr>
        <td class="lt"><input type="text" name="adfromus" size="30" value="<?=$sql['fromus']?>" /></td>
        <td class="rt">Рекламодатель</td>
    </tr>	
    <tr>
        <td class="lt"><input type="text" name="adplan" size="30" value="<?=$sql['plan']?>" /></td>
        <td class="rt">Просмотров</td>
    </tr>    
	<tr>
        <td class="lt"><input type="text" name="adbalance" size="30" value="<?=$sql['balance']?>" /></td>
        <td class="rt">Баланс</td>
    </tr>
    <tr>
        <td class="lt"><input type="text" name="adurl" size="30" value="<?=$sql['url']?>" /></td>
        <td class="rt">URL сайта (включая http://)
		<a style="margin-left: 110px;" target="_blank" href="http://online.us.drweb.com/result/?url=<?=$sql['url']?>" id="save">Сканировать на наличие вирусов</a></td>
    </tr>    
	<tr>
        <td class="lt"><input type="text" name="adprice" size="30" value="<?=$sql['price']?>" /></td>
        <td class="rt">Стоимость одного просмотра для рекламодателя</td>
    </tr>
	<tr>
        <td class="lt" style="font-size: 14px;">&nbsp;<?=$prc?></td>
        <td class="rt">Стоимость одного просмотра для пользователя (не редактируется, зависит от глобальных настроек цен, и от выбраного время просмотра ссылки)</td>
    </tr>
    <tr>
        <td class="lt"><select style="width: 99%;" name="adtimer">
		<option value="6"<?if($sql['timer']=='6') echo 'selected'?>>5</option>
        <option value="5"<?if($sql['timer']=='5') echo 'selected'?>>10</option>
        <option value="0"<?if($sql['timer']=='0') echo 'selected'?>>20</option>
        <option value="1"<?if($sql['timer']=='1') echo 'selected'?>>30</option>
        <option value="2"<?if($sql['timer']=='2') echo 'selected'?>>40</option>
        <option value="3"<?if($sql['timer']=='3') echo 'selected'?>>50</option>
        <option value="4"<?if($sql['timer']=='4') echo 'selected'?>>60</option>
        </select></td>
        <td class="rt">Время просмотра ссылки (изменяя время ссылки не забудьте повысить или понизить цену для рекламодателя (цена юеру повысится автоматически))</td>
    </tr>
	<tr>
        <td class="lt"><select style="width: 99%;" name="adhighlight">
        <option value="0"<?if($sql['highlight']=='0') echo 'selected'?>>Нет</option>
        <option value="1"<?if($sql['highlight']=='1') echo 'selected'?>>Да</option>
        </select></td>
        <td class="rt">Выделить ссылку (изменяя выделение ссылки не забудьте повысить или понизить цену для рекламодателя)</td>
    </tr>	
	<tr>
        <td class="lt"><select style="width: 99%;" name="adobper">
        <option value="0"<?if($sql['obper']=='0') echo 'selected'?>>Нет</option>
        <option value="1"<?if($sql['obper']=='1') echo 'selected'?>>Да</option>
        </select></td>
        <td class="rt">Последующий переход на сайт (изменяя переход на сайт рекламоетеля не забудьте повысить или понизить цену для рекламодателя)</td>
    </tr>	
	<tr>
        <td class="lt"><select style="width: 99%;" name="adpaused">
        <option value="0"<?if($sql['paused']=='0') echo 'selected'?>>Нет</option>
        <option value="1"<?if($sql['paused']=='1') echo 'selected'?>>Да</option>
        </select></td>
        <td class="rt">Пауза </td>
    </tr>	
	<tr>
        <td class="lt"><select style="width: 99%;" name="admodered">
        <option value="0"<?if($sql['modered']=='0') echo 'selected'?>>На модерации</option>
        <option value="1"<?if($sql['modered']=='1') echo 'selected'?>>Отмодерирована</option>
        </select></td>
        <td class="rt">Статус модерации</td>
    </tr>

    </table>
    </form>
    <?
} else {
?>
<style type="text/css">
.cons {
    width: 400px;
    background: #fafafa;
    margin-bottom: 20px;
    padding: 10px;
    -moz-border-radius: 3px;
    -webkit-border-radius: 3px;
    -khtml-border-radius: 3px;
    border-radius: 3px;
    -moz-box-shadow: 0 0 2px rgba(0,0,0,0.2);
	-webkit-box-shadow: 0 0 2px rgba(0,0,0,0.2);
    -khtml-box-shadow: 0 0 2px rgba(0,0,0,0.2);
    box-shadow: 0 0 2px rgba(0,0,0,0.2);
    
}
</style>
<script type="text/javascript">
function gofilter(mode) {
    setCookie("user",mode,"Mon, 01-Jan-2020 00:00:00 GMT");
    window.location.reload();
    return false;
}

function setCookie (name, value, expires, path, domain, secure) {
    document.cookie = name + "=" + escape(value) +
    ((expires) ? "; expires=" + expires : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}
</script>
<div class="cons">
<form action="" method="post">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td style="padding: 5px;" width="100"><input type="text" name="names" style="width: 99%;" /></td>
<td width="200" style="padding: 5px;"><select name="namev" style="width: 99%;"><option value="id">ID площадки</option><option value="fromus">ID рекламодателя</option><option value="url">Ссылка</option></select></td>
<td style="padding: 5px;"><input type="submit" name="search" value="Поиск" id="save" /></td>
</tr>
</table>
</form>
</div>
<?
if (isset($_COOKIE['user'])) {
    $filter = $_COOKIE['user'];
}
if ($filter == 1) {
    $zapros = "ORDER BY balance DESC";
}
if ($filter == 2) {
    $zapros = "ORDER BY id ASC";
}
if (isset($_POST['search'])) {
    if ($_POST['names'] == '') {
        $zapros = '';
    } else {
        $zapros = 'WHERE '.$_POST['namev'].'=\''.$_POST['names'].'\'';
    }
}
$kolvo = 20;
$allnews = mysql_num_rows(mysql_query("SELECT id FROM tb_ads $zapros"));
$vsego = intval($allnews / $kolvo);
if ($vsego == 0) $vsego = 1; $nowpage = $_GET['pg'];
if ($nowpage == '' or $nowpage <= 0) { $nowpage = 1; } else { $nowpage = intval($_GET["pg"]); }
if ($nowpage - 1 > $vsego) $nowpage = $vsego;
if (!isset($_GET["pg"]) or $_GET['pg'] == '') $nowpage = 1;
$gg = $vsego * $kolvo;
if ($gg < $allnews) { $vsego = $vsego + 1; }
$pages = $vsego / $kolvo;
$pages1 = floor($pages);
$pg1 = $kolvo * ($nowpage - 1);
$pg2 = $kolvo;
if ($nowpage > $vsego) { $pg1 = 0; $pg2 = $kolvo; }
if ($pages > $pages1) {
    $pages = $pages1 + 1;
}

$sql = mysql_query("SELECT * FROM  tb_ads $zapros limit $pg1,$pg2") or die(mysql_error());
echo "<table width='70%' style='margin-bottom: 5px;'><tr><td nowrap='nowrap'>";
$nextp = $nowpage + 1;
$lastp = $nowpage - 1;
if ($nowpage != 1) { echo "<a href='/admin/?adm=20&pg=$lastp' class=\"link\"><span class='text14'>"; } else { echo "<span class='textgray'>"; } echo "&larr;&nbsp;Предыдущие</span>";
if ($nowpage == 1) { echo "</a>"; } echo "</td><td width='90%' align='center'>Страница $nowpage из $vsego</td><td nowrap='nowrap'>";
if ($nowpage == $vsego) { echo "<span class='textgray'>"; } else { echo "<a href='/admin/?adm=20&pg=$nextp' class=\"link\"><span class='text14'>"; } echo "Следующие&nbsp;&rarr;</span>"; 
if ($nowpage == $vsego) { echo "</a>"; } echo "</td></tr></table>";
?>
<table width="95%" border="0" cellpadding="0" cellspacing="1" class="tab">
<tr align="center">
<td class="rt" style="background: #ebebeb;"><a href="#" onclick="javascript:gofilter(2);">ID</a></td>
<td class="rt" style="background: #ebebeb;">План</td>
<td class="rt" style="background: #ebebeb;">Заголовок</td>
<td class="rt" style="background: #ebebeb;">Цена user</td>
<td class="rt" style="background: #ebebeb;">Цена rec.</td>
<td class="rt" style="background: #ebebeb;">Просмотров</td>
<td class="rt" style="background: #ebebeb;"><a href="#" onclick="javascript:gofilter(1);">Баланс</a></td>
<td class="rt" style="background: #ebebeb;">Дата удаления</td>
<td class="rt" style="background: #ebebeb;">Дата остановки</td>
<td class="rt" style="background: #ebebeb;">Рекламд.</td>
<td class="rt" style="background: #ebebeb;" colspan="3" width="120">Действия</td>
</tr>
<?
while ($row = mysql_fetch_array($sql)) {
    if ($row['paused']) {
        $usclass = 'class="bt"';
    } else {
        $usclass = 'class="lt"';
    }
	
	if ($row['stopdate'] == '') {
		$stopdate = '-';
		$deldatte = '-';
	} else {
		$stopdate = date('d.m.Y', $row['stopdate']);
		$deldatte = date('d.m.Y', $row['stopdate']+5259486);
	}
	
	if($row['timer'] == 6) {$time = '5 сек'; $prc = $payuser['dyn5'];} 
	elseif($row['timer'] == 5) {$time = '10 сек'; $prc = $payuser['dyn10'];} 
	elseif($row['timer'] == 0) {$time = '20 сек'; $prc = $payuser['dyn20'];} 
	elseif($row['timer'] == 1) {$time = '30 сек'; $prc = $payuser['dyn30'];} 
	elseif($row['timer'] == 2) {$time = '40 сек'; $prc = $payuser['dyn40'];} 
	elseif($row['timer'] == 3) {$time = '50 сек'; $prc = $payuser['dyn50'];} 
	elseif($row['timer'] == 4) {$time = '60 сек'; $prc = $payuser['dyn60'];}
	
	?>
    <tr>
    <td <?=$usclass?> align="center"><?=$row['id']?></td>
    <td <?=$usclass?> align="center"><?=$time?></td>
	<td <?=$usclass?>><?=substr($row['description'], 0, 12)?> ...</td>
	<td <?=$usclass?>><?=$prc?> руб.</td>
	<td <?=$usclass?>><?=$row['price']?> руб.</td>
    <td <?=$usclass?> align="center"><?=$row['plan']?></td>
    <td <?=$usclass?> align="center"><?=$row['balance']?></td>
    <td <?=$usclass?> align="center"><?=$deldatte?></td>
    <td <?=$usclass?> align="center"><?=$stopdate?></td>
	<td <?=$usclass?> align="center"><a target="_blank" href="/#:id<?=$row['fromus']?>">ID:<?=$row['fromus']?></a></td>
    
	<td <?=$usclass?> align="center"><a target="_blank" href="http://online.us.drweb.com/result/?url=<?=$row['url']?>"><input type="submit" value="Сканировать" id="save" /></a></td>
    <td <?=$usclass?> align="center"><form action="/admin/?adm=20&edit=<?=$row['id']?>" method="post"><input type="submit" value="Редк." id="save" /></form></td>
    <td <?=$usclass?> align="center"><form action="" method="post"><input type="hidden" name="id" value="<?=$row['id']?>" /><input type="submit" value="Удал." id="save" name="del" /></form></td>
    </tr>
    <?
}
?>
</table>
<?}?>