<style type="text/css">
    #edit_kon {
        cursor: pointer;
    }
</style>
<?php
if (!defined('webid')) {
    exit();
}

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

if($_POST['addkon']) {
    $sql = mysql_query("SELECT id FROM konkurs WHERE type = '$_POST[type]'");
    if (mysql_num_rows($sql) > 0) {
        ?>
        <script type="text/javascript">alert("Нельзя создавать одновременно два одинаковых конкурса")</script>
        <?
    } else {
        mysql_query("INSERT INTO konkurs (name, description, type, start_date, end_date, min_type, price1, price2, price3) VALUES ('$_POST[name]', '$_POST[description]', '$_POST[type]', '$_POST[start_date]', '$_POST[end_date]', '$_POST[min_type]', '$_POST[price1]', '$_POST[price2]', '$_POST[price3]')") or die(mysql_error());
    }
}

if ($_POST['save']) {
    mysql_query("UPDATE konkurs SET name='$_POST[name]', description = '$_POST[description]', type = '$_POST[type]', start_date = '$_POST[start_date]', end_date = '$_POST[end_date]', min_type = '$_POST[min_type]', price1 = '$_POST[price1]', price2 = '$_POST[price2]', price3 = '$_POST[price3]' WHERE id = '$_POST[id]'");
    
}


if (isset($_POST['edit'])) {
    $sql_e = mysql_fetch_array(mysql_query("SELECT * FROM konkurs WHERE id = '$_POST[id]'"));
    $e_id = $sql_e['id'];
    $e_name = $sql_e['name'];
    $e_description = $sql_e['description'];
    $e_type = $sql_e['type'];
    $e_start_date = $sql_e['start_date'];
    $e_end_date = $sql_e['end_date'];
    $e_min_type = $sql_e['min_type'];
    $e_price1 = $sql_e['price1'];
    $e_price2 = $sql_e['price2'];
    $e_price3 = $sql_e['price3'];
    $btn = 'save';
    $title = 'Сохранить';
} else {
    $title = 'Создать';
    $btn = 'addkon';
    $e_name = '';
    $e_description = '';
    $e_type = 1;
    $e_start_date = date('d.m.Y', time());
    $e_end_date = '';
    $e_min_type = 0;
    $e_price1 = 0;
    $e_price2 = 0;
    $e_price3 = 0;
}

if (isset($_POST['del'])) {
    mysql_query("DELETE FROM konkurs_temp WHERE idkonkurs = '$_POST[id]'");
    mysql_query("DELETE FROM konkurs WHERE id = '$_POST[id]'");
}

if (isset($_POST['send'])) {
    mysql_query("UPDATE tb_users SET money = money + '$_POST[money]' WHERE id = '$_POST[id]'") or die(mysql_error());
    if ($_POST['oplata'] == 1) {
        mysql_query("UPDATE konkurs_end SET oplata1 = 1 WHERE user1 = '$_POST[id]' and idkonkurs = '$_POST[idkon]'") or die(mysql_error());
    }
    if ($_POST['oplata'] == 2) {
        mysql_query("UPDATE konkurs_end SET oplata2 = 1 WHERE user2 = '$_POST[id]' and idkonkurs = '$_POST[idkon]'") or die(mysql_error());
    }
    if ($_POST['oplata'] == 3) {
        mysql_query("UPDATE konkurs_end SET oplata3 = 1 WHERE user3 = '$_POST[id]' and idkonkurs = '$_POST[idkon]'") or die(mysql_error());
    }
}
?>
<form action="" method="post">
<table width="70%" border="0" cellpadding="0" cellspacing="1" class="tab">
    <tr>
        <td style="background: #ebebeb; color: #1c62b9; text-align: center; padding: 10px 0;" class="lt" width="300">Поле</td>
        <td style="background: #ebebeb; color: #1c62b9; text-align: center;" class="lt"><div style="position: relative;">Описание<div style="position: absolute; right: 0; top: -6px;"><input type="hidden" name="id" value="<?=$e_id?>" /><input type="submit" value="<?=$title?>" id="save" name="<?=$btn?>" /></div></div></td>
    </tr>
    <tr>
        <td class="lt"><input type="text" name="name" style="width: 98%;" value="<?=$e_name?>" /></td>
        <td class="rt" align="left">Название конкурса</td>
    </tr>
    <tr>
        <td class="lt"><textarea name="description" style="width: 98%; height: 100px;"><?=$e_description?></textarea></td>
        <td class="rt" align="left">Описание конкурса</td>
    </tr>
    <tr>
        <td class="lt"><select name="type" style="width: 99%;"><option value="1" <?if($e_type == 1) echo 'selected'?>>Конкурс рекламодателей</option><option value="2" <?if($e_type == 2) echo 'selected'?>>Конкурс пользователей</option></select></td>
        <td class="rt" align="left">Тип конкурса</td>
    </tr>
    <tr>
        <td class="lt"><input type="text" name="start_date" style="width: 98%;" value="<?=$e_start_date?>" /></td>
        <td class="rt" align="left">Дата начала конкурса (dd.mm.yyyy)</td>
    </tr>
    <tr>
        <td class="lt"><input type="text" name="end_date" style="width: 98%;" value="<?=$e_end_date?>" /></td>
        <td class="rt" align="left">Дата окончания конкурса (dd.mm.yyyy)</td>
    </tr>
    <tr>
        <td class="lt"><input type="text" name="min_type" style="width: 98%;" value="<?=$e_min_type?>" /></td>
        <td class="rt" align="left">Минимальное значения для участия в конкурсе</td>
    </tr>
    <tr>
        <td class="lt"><input type="text" name="price1" style="width: 31%;" value="<?=$e_price1?>" />&nbsp;<input type="text" name="price2" style="width: 30%;" value="<?=$e_price2?>" />&nbsp;<input type="text" name="price3" style="width: 30%;" value="<?=$e_price3?>" /></td>
        <td class="rt" align="left">Призы за (1/2/3) места</td>
    </tr>
</table>
</form><br /><br/>
<center><h4 style="color: #1c62b9;">Активные конкурсы</h4></center>
<table width="95%" border="0" cellpadding="0" cellspacing="1" class="tab">
    <tr>
        <td class="lt" style="background: #ebebeb" align="center">Название</td>
        <td class="lt" style="background: #ebebeb" align="center">Описание</td>
        <td class="lt" style="background: #ebebeb" align="center">Тип</td>
        <td class="lt" style="background: #ebebeb" align="center">Начало</td>
        <td class="lt" style="background: #ebebeb" align="center">Завершение</td>
        <td class="lt" style="background: #ebebeb" align="center">Ставка</td>
        <td class="lt" style="background: #ebebeb" align="center">Призы</td>
        <td class="lt" style="background: #ebebeb" align="center">Действия</td>
    </tr>
    <?
    $sql2 = mysql_query("SELECT * FROM konkurs");
    if (mysql_num_rows($sql2) > 0) {
        while ($row = mysql_fetch_array($sql2)) {
            if ($row['type'] == 1) $type = 'Конкурс рекламодателей';
            if ($row['type'] == 2) $type = 'Конкурс пользователей';
            ?>
            <tr>
                <td class="rt"><?=$row['name']?></td>
                <td class="rt"><?=$row['description']?></td>
                <td class="rt" align="center"><?=$type?></td>
                <td class="rt" align="center"><?=$row['start_date']?></td>
                <td class="rt" align="center"><?=$row['end_date']?></td>
                <td class="rt" align="center"><?=$row['min_type']?></td>
                <td class="rt" align="center"><?=$row['price1']?>/<?=$row['price2']?>/<?=$row['price3']?></td>
                <td class="rt" align="center"><form action="" method="post"><input type="hidden" name="id" value="<?=$row['id']?>" /><input type="hidden" name="edit" /><input type="image" src="http://<?=$_SERVER['HTTP_HOST']?>/theme/img/edit.png" title="Редактировать" /></form><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><br/>
<center><h4 style="color: #800000;">Завершенные конкурсы</h4></center>
<table width="100%" border="0">
    <tr>
        <td width="50%">
        <table width="93%" border="0" cellpadding="0" cellspacing="1" class="tab" style="margin-left: 25px;">
        <tr>
            <td colspan="5" style="background: #E8EA91; color: #1c62b9; font-weight: bold;" class="lt" align="center">Конкурс рекламодателей</td>
        </tr>
        <tr>
            <td class="lt" style="background: #ebebeb;" align="center">№ конкурса</td>
            <td class="lt" style="background: #ebebeb;" align="center">ID пользователя</td>
            <td class="lt" style="background: #ebebeb;" align="center">Приз</td>
            <td class="lt" style="background: #ebebeb;" align="center">Состояние</td>
            <td class="lt" style="background: #ebebeb;" align="center">Действие</td>
        </tr>
        <?
        $get_end_kon1 = mysql_fetch_array(mysql_query("SELECT * FROM konkurs_end WHERE type = 1 ORDER BY id DESC"));
        if ($get_end_kon1['id'] != 0) {
        //первый пользователь
        if ($get_end_kon1['user1'] == 0) {
            $usk1 = '-';
            $usp1 = '-';
        } else {
            $usk1 = $get_end_kon1['user1'];
            $usp1 = $get_end_kon1['price1'];
        }
        if ($get_end_kon1['oplata1'] == 0 and $get_end_kon1['user1'] != 0) {
            $btn1 = '<form action="" method="post"><input type="hidden" name="id" value="' . $get_end_kon1['user1'] . '" /><input type="hidden" name="money" value="' . $get_end_kon1['price1'] . '" /><input type="hidden" name="oplata" value="1" /><input type="hidden" name="idkon" value="' . $get_end_kon1['idkonkurs'] . '" /><input type="submit" name="send" id="save" value="оплатить" /></form>';
            $usoplata1 = '<font color="#800000">не оплачено</font>';
        } else {
            
            $btn1 = '';
        }
		if ($get_end_kon1['oplata1'] == 0) { $usoplata1 = '<font color="#800000">не оплачено</font>'; } else { $usoplata1 = '<font color="#84AC0B">оплачено</font>'; }
        //второй пользователь
        if ($get_end_kon1['user2'] == 0) {
            $usk2 = '-';
            $usp2 = '-';
        } else {
            $usk2 = $get_end_kon1['user2'];
            $usp2 = $get_end_kon1['price2'];
        }
        if ($get_end_kon1['oplata2'] == 0 and $get_end_kon1['user2'] != 0) {
            $btn2 = '<form action="" method="post"><input type="hidden" name="id" value="' . $get_end_kon1['user2'] . '" /><input type="hidden" name="money" value="' . $get_end_kon1['price2'] . '" /><input type="hidden" name="oplata" value="2" /><input type="hidden" name="idkon" value="' . $get_end_kon1['idkonkurs'] . '" /><input type="submit" name="send" id="save" value="оплатить" /></form>';
            $usoplata2 = '<font color="#800000">не оплачено</font>';
        } else {
           
			$btn2 = '';
        }
		 if ($get_end_kon1['oplata2'] == 0) { $usoplata2 = '<font color="#800000">не оплачено</font>'; } else { $usoplata2 = '<font color="#84AC0B">оплачено</font>'; }
        //третий пользователь
        if ($get_end_kon1['user3'] == 0) {
            $usk3 = '-';
            $usp3 = '-';
        } else {
            $usk3 = $get_end_kon1['user3'];
            $usp3 = $get_end_kon1['price3'];
        }
        if ($get_end_kon1['oplata3'] == 0 and $get_end_kon1['user3'] != 0) {
            $btn3 = '<form action="" method="post"><input type="hidden" name="id" value="' . $get_end_kon1['user3'] . '" /><input type="hidden" name="money" value="' . $get_end_kon1['price3'] . '" /><input type="hidden" name="oplata" value="3" /><input type="hidden" name="idkon" value="' . $get_end_kon1['idkonkurs'] . '" /><input type="submit" name="send" id="save" value="оплатить" /></form>';
            $usoplata3 = '<font color="#800000">не оплачено</font>';
        } else {
            
            $btn3 = '';
        }
		if ($get_end_kon1['oplata3'] == 0) { $usoplata3 = '<font color="#800000">не оплачено</font>'; } else { $usoplata3 = '<font color="#84AC0B">оплачено</font>'; }
        ?>
        <tr>
            <td class="rt" align="center" rowspan="3"><?=$get_end_kon1['idkonkurs']?></td>
            <td class="rt" align="center"><?=$usk1?></td>
            <td class="rt" align="center"><?=$usp1?></td>
            <td class="rt" align="center"><?=$usoplata1?></td>
            <td class="rt" align="center"><?=$btn1?></td>
        </tr>
        <tr>
            <td class="rt" align="center"><?=$usk2?></td>
            <td class="rt" align="center"><?=$usp2?></td>
            <td class="rt" align="center"><?=$usoplata2?></td>
            <td class="rt" align="center"><?=$btn2?></td>
        </tr>
        <tr>
            <td class="rt" align="center"><?=$usk3?></td>
            <td class="rt" align="center"><?=$usp3?></td>
            <td class="rt" align="center"><?=$usoplata3?></td>
            <td class="rt" align="center"><?=$btn3?></td>
        </tr>
        <?
        } else {
            ?>
            <tr><td colspan="5" class="rt" style="color: #800000; font-weight: bold;" align="center">нет конкурсов</td></tr>
            <?
        }
        ?>
        </table>
        </td>
        <td width="50%">
        <table width="93%" border="0" cellpadding="0" cellspacing="1" class="tab" style="margin-left: 15px;">
        <tr>
            <td colspan="5" style="background: #E8EA91; color: #1c62b9; font-weight: bold;" class="lt" align="center">Конкурс пользователей</td>
        </tr>
        <tr>
            <td class="lt" style="background: #ebebeb;" align="center">№ конкурса</td>
            <td class="lt" style="background: #ebebeb;" align="center">ID пользователя</td>
            <td class="lt" style="background: #ebebeb;" align="center">Приз</td>
            <td class="lt" style="background: #ebebeb;" align="center">Состояние</td>
            <td class="lt" style="background: #ebebeb;" align="center">Действие</td>
        </tr>
        <?
        $get_end_kon1 = mysql_fetch_array(mysql_query("SELECT * FROM konkurs_end WHERE type = 2 ORDER BY id DESC"));
        if ($get_end_kon1['id'] != 0) {
        //первый пользователь
        if ($get_end_kon1['user1'] == 0) {
            $usk1 = '-';
            $usp1 = '-';
        } else {
            $usk1 = $get_end_kon1['user1'];
            $usp1 = $get_end_kon1['price1'];
        }
        if ($get_end_kon1['oplata1'] == 0 and $get_end_kon1['user1'] != 0) {
            $btn1 = '<form action="" method="post"><input type="hidden" name="id" value="' . $get_end_kon1['user1'] . '" /><input type="hidden" name="money" value="' . $get_end_kon1['price1'] . '" /><input type="hidden" name="oplata" value="1" /><input type="hidden" name="idkon" value="' . $get_end_kon1['idkonkurs'] . '" /><input type="submit" name="send" id="save" value="оплатить" /></form>';
            $usoplata1 = '<font color="#800000">не оплачено</font>';
        } else {
            
           
            $btn1 = '';
        }
		if ($get_end_kon1['oplata1'] == 0) { $usoplata1 = '<font color="#800000">не оплачено</font>'; } else { $usoplata1= '<font color="#84AC0B">оплачено</font>'; }
        //второй пользователь
        if ($get_end_kon1['user2'] == 0) {
            $usk2 = '-';
            $usp2 = '-';
        } else {
            $usk2 = $get_end_kon1['user2'];
            $usp2 = $get_end_kon1['price2'];
        }
        if ($get_end_kon1['oplata2'] == 0 and $get_end_kon1['user2'] != 0) {
            $btn2 = '<form action="" method="post"><input type="hidden" name="id" value="' . $get_end_kon1['user2'] . '" /><input type="hidden" name="money" value="' . $get_end_kon1['price2'] . '" /><input type="hidden" name="oplata" value="2" /><input type="hidden" name="idkon" value="' . $get_end_kon1['idkonkurs'] . '" /><input type="submit" name="send" id="save" value="оплатить" /></form>';
            $usoplata2 = '<font color="#800000">не оплачено</font>';
        } else {
            
            
            $btn2 = '';
        }
		if ($get_end_kon1['oplata2'] == 0) { $usoplata2 = '<font color="#800000">не оплачено</font>'; } else { $usoplata2= '<font color="#84AC0B">оплачено</font>'; }
        //третий пользователь
        if ($get_end_kon1['user3'] == 0) {
            $usk3 = '-';
            $usp3 = '-';
        } else {
            $usk3 = $get_end_kon1['user3'];
            $usp3 = $get_end_kon1['price3'];
        }
        if ($get_end_kon1['oplata3'] == 0 and $get_end_kon1['user3'] != 0) {
            $btn3 = '<form action="" method="post"><input type="hidden" name="id" value="' . $get_end_kon1['user3'] . '" /><input type="hidden" name="money" value="' . $get_end_kon1['price3'] . '" /><input type="hidden" name="oplata" value="3" /><input type="hidden" name="idkon" value="' . $get_end_kon1['idkonkurs'] . '" /><input type="submit" name="send" id="save" value="оплатить" /></form>';
            $usoplata3 = '<font color="#800000">не оплачено</font>';
        } else {
            
            
            $btn3 = '';
        }
		if ($get_end_kon1['oplata3'] == 0) { $usoplata3 = '<font color="#800000">не оплачено</font>'; } else { $usoplata3= '<font color="#84AC0B">оплачено</font>'; }
        ?>
        <tr>
            <td class="rt" align="center" rowspan="3"><?=$get_end_kon1['idkonkurs']?></td>
            <td class="rt" align="center"><?=$usk1?></td>
            <td class="rt" align="center"><?=$usp1?></td>
            <td class="rt" align="center"><?=$usoplata1?></td>
            <td class="rt" align="center"><?=$btn1?></td>
        </tr>
        <tr>
            <td class="rt" align="center"><?=$usk2?></td>
            <td class="rt" align="center"><?=$usp2?></td>
            <td class="rt" align="center"><?=$usoplata2?></td>
            <td class="rt" align="center"><?=$btn2?></td>
        </tr>
        <tr>
            <td class="rt" align="center"><?=$usk3?></td>
            <td class="rt" align="center"><?=$usp3?></td>
            <td class="rt" align="center"><?=$usoplata3?></td>
            <td class="rt" align="center"><?=$btn3?></td>
        </tr>
        <?
        } else {
            ?>
            <tr><td colspan="5" class="rt" style="color: #800000; font-weight: bold;" align="center">нет конкурсов</td></tr>
            <?
        }
        ?>
        </table>
        </td>
    </tr>
</table>