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

if ($user_status != 'admin') {
    exit();
}
	$sql = mysql_fetch_array(mysql_query("SELECT * FROM tb_users WHERE id = '" . $_GET['edit'] . "'"));
	$kolvo_ref_kon = mysql_fetch_array( mysql_query( "SELECT(SELECT count(*) FROM konkurs_ref WHERE id > '0')kon, (SELECT count(*) FROM konkurs_ref WHERE status = '1' )activerefkon"));
	
	if (isset($_POST['del'])) {
		mysql_query("DELETE FROM konkurs_ref WHERE id = '" . $_POST['id'] . "'");
		mysql_query("DELETE FROM konkurs_ref_temp WHERE idkonkurs = '" . $_POST['id'] . "'");
	}
    ?>

<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="300%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td style="padding: 5px;" width="150" ><font style="color: #0099FF;">Конкурсов всего:<?=$kolvo_ref_kon['kon']?><br>Всего активных:<?=$kolvo_ref_kon['activerefkon']?></font></td>
<td width="150" style="padding: 5px;">
    <select name="namev" style="width: 99%;">
	    <option value="">Все</option>
	    <option value="1">Активные</option>
		<option value="0">Завершеные</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 == 2) {
    $zapros = "ORDER BY id ASC";
}
if (isset($_POST['search'])) {
    if ($_POST['namev'] == '') {
        $zapros = 'WHERE status = 1';
    } else {
        $zapros = 'WHERE status = '.$_POST['namev'].'';
    }
}
$kolvo = 20;
$allnews = mysql_num_rows(mysql_query("SELECT id FROM konkurs_ref $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  konkurs_ref $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=6&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=6&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;" align ="left" width="4%">Реферер</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;">Статус</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;">*</td>
</tr>
<?
while ($row = mysql_fetch_array($sql)) {
    if ($row['status'] == '0') {
		$usclass = 'class="bt"'; $usclass1 = 'class="fb"';
	} else {
		$usclass = 'class="lt"'; $usclass1 = 'class="fs"';
	}
	$name_referer = mysql_fetch_array( mysql_query( "SELECT * FROM tb_users WHERE id = '".$row['referername']."'" ) );
	
    ?>
    <tr>
    <td <?=$usclass?> width="3%" align="center"><?=$row['id']?></td>
	<td class="fn">
	    <a href="/#:id<?=$name_referer['id']?>" target="_blank" >
		    <img src="/avp/<?=$name_referer['avatar']?>" width="40" height="40" style=" margin-top: 5px; margin-left: 3px;" class="avatar" /> 
		</a>
	</td>
    <td <?=$usclass?> width="30%" align="left"><?=$row['description']?></td>
    <td <?=$usclass?> width="10%" align="left" ><?=$row['name']?></td>
    <td <?=$usclass?> width="7%" align="left">I-e место <?=$row['comrefprice1']?>р.<br>II-e место <?=$row['comrefprice2']?>р.<br>III-e место <?=$row['comrefprice3']?>р.</td>
    <td <?=$usclass1?> width="3%" align="left"><? if($row['status'] == 1) {echo 'Активен';} else {echo 'Завершен';}?></td>
    <td <?=$usclass?> width="11%" align="left">
	<?	if($row['lider1'] > 0 OR $row['lider2'] > 0 OR $row['lider3'] > 0) {
			if($row['lider1'] > 0) { ?>
				I-e место ID: <?=$row['lider1']?><br>
			<? } else { ?>I-e место <font style="color: #FF0000;">нет победителя<br></font><? }
			if($row['lider2'] > 0) { ?>
				II-e место ID: <?=$row['lider2']?><br>
			<? } else { ?>II-e место <font style="color: #FF0000;">нет победителя<br></font><? }
			if($row['lider3'] > 0) { ?>
				III-e место ID: <?=$row['lider3']?>
			<? } else { ?>III-e место <font style="color: #FF0000;">нет победителя<br></font><? }
		} else {?><center>Победителей нет!</center><?}
	?>
	</td>
    <td <?=$usclass?> width="20%" align="left">
	<?
	$kolvo_ref_kon_temp = mysql_fetch_array( mysql_query( "SELECT(SELECT count(*) FROM konkurs_ref_temp WHERE `idkonkurs` = '".$row['id']."' AND summ >= '".$row['min_type']."' ORDER BY summ DESC limit 3)kondf"));
	if($kolvo_ref_kon_temp['kondf'] > 0) {
		$kon_ref_temp = mysql_query("SELECT * FROM `konkurs_ref_temp` WHERE `idkonkurs` = '".$row['id']."' AND summ >= '".$row['min_type']."' ORDER BY summ DESC limit 3" );
		$ifv=1;
		while ($kon_row_temp = mysql_fetch_assoc($kon_ref_temp)) 
		{	
			$name_referer_temp = mysql_fetch_array( mysql_query( "SELECT * FROM tb_users WHERE id = '".$kon_row_temp['iduser']."'" ) );
			?><div style=" background: #d5d5d5; margin-top: 1px; padding-left: 5px; padding-right: 5px;">Место:<?
			echo $ifv;
			?><div style="text-align: center; margin-top: -15px;">Логин:<?
			echo $name_referer_temp['username'];
			?></div><div style="text-align: right; margin-top: -15px;">Очек:<?
			echo $kon_row_temp['summ'];		
			?></div></div><?	
			$ifv++;		
		}
	} else {
		?><center>Участников нет!</center><?
	}
	
	
	
	?>
	</td>
	<td <?=$usclass?> width="11%" align="left">Начало: <?=date('d.m.Y H:m', $row['start_date'])?><br>Завершение: <?=date('d.m.Y H:m', $row['end_date'])?></td>
	<td <?=$usclass?> align="center"><form action="" method="post"><input type="hidden" name="id" value="<?=$row['id']?>" /><input type="hidden" name="del" /><input type="image" src="http://<?=$_SERVER['HTTP_HOST']?>/theme/img/ref_del.png" title="Удалить" /></form></td>
    </tr>
	<?
}
?>
</table>