<?

$gift_id = (int)abs($_GET['edit_gift']);
$gift = DB :: $pdo -> queryFetch("SELECT * FROM `gifts` WHERE `id` = ? LIMIT 1;", array($gift_id));
if (!isset($gift) || $gift['id'] <= 0)
{
?>
    <div class="list _gray _font-s _center">Подарок не найден!</div>
<?
} else {
    if (isset($_POST['cfms']))
    {
        if (isset($_POST['do']) && is_numeric($_POST['do']) && intval($_POST['do']) > 0 && (!isset($_POST['show_do']) || !is_numeric($_POST['show_do']) || intval($_POST['show_do']) <= 0 || intval($_POST['show_do']) >= 4))
        {
            $errMsg = "Укажите длительность показа!";
        } else {
            if (!isset($_POST['cena']) || !is_numeric($_POST['cena']) || intval($_POST['cena']) <= 0)
            {
                $cena = 0;
            } else {
            $cena = intval($_POST['cena']);
            }
          
            if (isset($_POST['do']) && is_numeric($_POST['do']) && intval($_POST['do']) > 0)
            {
                if (isset($_POST['show_do']) && is_numeric($_POST['show_do']) && intval($_POST['show_do']) > 0 && intval($_POST['show_do']) < 4)
                {
                    if ($_POST['show_do'] == 1)
                    {
                        $do = intval($_POST['do'])*60*60;
                    }
                    else if ($_POST['show_do'] == 2)
                    {
                        $do = intval($_POST['do'])*60*60*24;
                    }
                    else if ($_POST['show_do'] == 3)
                    {
                        $do = intval($_POST['do'])*60*60*24*30;
                    }
                    $do = time()+$do;
                }else{
                    $do = 0;
                }
            }else{
                $do = 0;
            }
             if (isset($_POST['gifts_text']) && $_POST['gifts_text'] != NULL)
            {
                $nnm = $_POST['gifts_text'];
            } else {
                $nnm = '';
            }
    		if (DB :: $pdo -> query("UPDATE `gifts` SET `gifts_text` = '".$nnm."', `cena` = '".$cena."', `do` = '".$do."' WHERE `id` = '".$gift['id']."' LIMIT 1"))
            {			
    			system :: redirect("Подарок успешно изменен.", "/gifts?admin&edit_gift=".$gift['id']."");
    		} else {
    			$errMsg = "Неизвестная ошибка.!";
    		}
        }
    }
    if (isset($errMsg))
    {
        system :: show($errMsg);
    }
    ?>
    <div class="list">
    <form method="post" class="post-add" data-type="arbour" action="/gifts?admin&amp;edit_gift=<?echo $gift['id'];?>">
    <div class="_gray _dbl">Стоимость:</div>
    <input class="main-input _w-100" type="text" value="<?echo $gift['cena'];?>" name="cena" />
      <div class="_gray _dbl">Текст:</div>
    <input class="main-input _w-100" type="text" value="<?echo $gift['gifts_text'];?>" name="gifts_text" />
    <div class="_gray _dbl">Время показа:</div>
    <input class="main-input _w-100" type="text" name="do" />
    <br />
    <select class="main-input _w-100" name="show_do">
    <option value="1">Часы</option>
    <option value="2">Сутки</option>
    <option value="3">Месяцы</option>
    </select>
    <div class="_center" style="margin-top: 3px;">
    <input type="submit" class="main-btn _w-100" name="cfms" value="Продолжить" />
    </div>
    </form>
    <div class="p-error"></div>
    </div>
    <?
}
if (isset($gift) && $gift['id'] > 0)
{
    echo system :: touch("/gifts?admin&adm_gift=".$gift['id']."", system :: icon("arr-l")."Назад");
} else {
    echo system :: touch("/gifts?admin", system :: icon("arr-l")."Назад");
}
require (CORE."foot.php");
?>