<?php
if (!defined('webid')) {
    exit();
}
if ($user_status != 'admin') {
    exit();
}
$promotion_temp_s = mysql_fetch_array( mysql_query("SELECT(SELECT sum(turnover) FROM promotion_temp)temppromo"));
$sqld = mysql_fetch_array(mysql_query("SELECT status FROM promotion_status WHERE id = '1'")) or die(mysql_error());
if (isset($_POST['save'])) {
    
    if($sqld['status'] == 0) {
    $des = $_POST['description'];
    $texto = $_POST['texto'];
	$data_k = ($_POST['dataend'] * 86400);
    $dates = (time() + $data_k);
    $pr1 = $_POST['prom1'];
    $pr2 = $_POST['prom2'];
    $pr3 = $_POST['prom3'];
    $pr4 = $_POST['prom4'];
    mysql_query("UPDATE promotion_status SET description = '$des', texto = '$texto', dateend = '$dates', prom1 = '$pr1', prom2 = '$pr2', prom3 = '$pr3', prom4 = '$pr4', status = '1', datestart = '".time()."' WHERE id = '1'") or die(mysql_error());
    } 
	else 
	{
	    ?>
        <script type="text/javascript">alert("Ошибка! Акция уже запущена, нельзя создавать дубликат, дождитесь окончания или удалите предыдущую акцию, затем повторите операцию!")</script>
        <?
	}
}
if (isset($_GET['del'])) {
    mysql_query("UPDATE promotion_status SET description = '', texto = '', dateend = '0', prom1 = '0', prom2 = '0', prom3 = '0', prom4 = '0', status = '0', datestart = '0' WHERE id = '1'") or die(mysql_error());
	mysql_query("DELETE FROM promotion_temp WHERE date < '".time()."' ");
}
?>
<form action="" method="post">
<table width="500px" 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="40%">Название акции</td>
        <td class="rt"><input type="text" name="description" size="40" value="" maxlength="100" /></td>
    </tr>
	
    <tr>
        <td class="lt" width="40%">Краткое описание акции</td>
        <td class="rt"><input type="text" name="texto" size="40" value="" maxlength="200" /></td>
    </tr>    
	<tr>
        <td class="lt" width="40%">Длительность акции в днях от текущего момента</td>
        <td class="rt"><input type="text" name="dataend" size="10" value="" maxlength="10" /></td>
    </tr>
	<tr>
        <td class="lt"><select style="width: 99%;" name="prom1">
        <option value="1">Бригадир</option>
        <option value="2">Мастер</option>
        <option value="3">Бизнесмен</option>
        <option value="4">Гуру</option>
        </select></td>
        <td class="rt">Статус за пополнение от 10 до 99 рублей</td>
    </tr>
	<tr>
        <td class="lt"><select style="width: 99%;" name="prom2">
        <option value="1">Бригадир</option>
        <option value="2">Мастер</option>
        <option value="3">Бизнесмен</option>
        <option value="4">Гуру</option>
        </select></td>
        <td class="rt">Статус за пополнение от 100 до 249</td>
    </tr>
	<tr>
        <td class="lt"><select style="width: 99%;" name="prom3">
        <option value="1">Бригадир</option>
        <option value="2">Мастер</option>
        <option value="3">Бизнесмен</option>
        <option value="4">Гуру</option>
        </select></td>
        <td class="rt">Статус за пополнение от 250 до 499</td>
    </tr>
	<tr>
        <td class="lt"><select style="width: 99%;" name="prom4">
        <option value="1">Бригадир</option>
        <option value="2">Мастер</option>
        <option value="3">Бизнесмен</option>
        <option value="4">Гуру</option>
        </select></td>
        <td class="rt">Статус за пополнение от 500 и выше</td>
    </tr>
</table>
</form><br /><br />
<table width="85%" border="0" cellpadding="0" cellspacing="1" class="tab">
<?
$sql = mysql_query("SELECT * FROM promotion_status WHERE status = '1'");
while ($row = mysql_fetch_array($sql)) {
    if($row['prom1'] == 1) { $status1 = 'Бригадир';} elseif ($row['prom1'] == 2) { $status1 = 'Мастер';} elseif ($row['prom1'] == 3) { $status1 = 'Бизнесмен';} elseif ($row['prom1'] == 4) { $status1 = 'Гуру';}
	if($row['prom2'] == 1) { $status2 = 'Бригадир';} elseif ($row['prom2'] == 2) { $status2 = 'Мастер';} elseif ($row['prom2'] == 3) { $status2 = 'Бизнесмен';} elseif ($row['prom2'] == 4) { $status2 = 'Гуру';}
	if($row['prom3'] == 1) { $status3 = 'Бригадир';} elseif ($row['prom3'] == 2) { $status3 = 'Мастер';} elseif ($row['prom3'] == 3) { $status3 = 'Бизнесмен';} elseif ($row['prom3'] == 4) { $status3 = 'Гуру';}
	if($row['prom4'] == 1) { $status4 = 'Бригадир';} elseif ($row['prom4'] == 2) { $status4 = 'Мастер';} elseif ($row['prom4'] == 3) { $status4 = 'Бизнесмен';} elseif ($row['prom4'] == 4) { $status4 = 'Гуру';}
	?>
    <tr>
        <td colspan="9" 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">Дата завершения</td>
        <td class="lt" style="background: #ebebeb;" align="center">Оборот</td>
        <td class="lt" style="background: #ebebeb;" align="center">от 10 до 99 рублей</td>
        <td class="lt" style="background: #ebebeb;" align="center">от 100 до 249 рублей</td>
        <td class="lt" style="background: #ebebeb;" align="center">от 250 до 499 рублей</td>
        <td class="lt" style="background: #ebebeb;" align="center">от 500 и больше</td>
        <td class="lt" style="background: #ebebeb;" align="center">Действие</td>
    </tr>
	<tr>
        <td class="rt" align="center" rowspan="10"><?=$row['description']?></td>
        <td class="rt" align="center"><?=date('d.m.Y H:i ', $row['dateend'])?></td>
        <td class="rt" align="center"><?=$promotion_temp_s['temppromo']?></td>
        <td class="rt" align="center"><?=$status1?></td>
        <td class="rt" align="center"><?=$status2?></td>
        <td class="rt" align="center"><?=$status3?></td>
        <td class="rt" align="center"><?=$status4?></td>
        <td class="rt" align="center"><a href="http://<?=$_SERVER['HTTP_HOST']?>/admin/?adm=19&del=<?=$row['id']?>" title="Удалить"><img src="../theme/img/ref_del.png" border="0" /></a></td>
    </tr>	    
    <?
}
?>
 </table>