<?php
    $kat_id = (int)abs($_GET['add_gift']);
    $kat = DB :: $pdo -> queryFetch("SELECT * FROM `gifts_kat` WHERE `id` = ? LIMIT 1;", array($kat_id));
    if (!isset($kat) || $kat['id'] <= 0)
    {
    ?>
        <div class="list _gray _font-s _center">Категория не найдена!</div>
    <?
        echo system :: back("/gifts?admin", "Назад");
        require (CORE."foot.php");
    }
    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($_FILES['filik_ob']) && isset($_FILES['filik_ob']['tmp_name']) && ((preg_match('#\.jpe?g$#i',$_FILES['filik_ob']['name']) && $imgc=@imagecreatefromjpeg($_FILES['filik_ob']['tmp_name'])) || (preg_match('#\.gif$#i',$_FILES['filik_ob']['name']) && $imgc=@imagecreatefromgif($_FILES['filik_ob']['tmp_name'])) || (preg_match('#\.png$#i',$_FILES['filik_ob']['name']) && $imgc=@imagecreatefrompng($_FILES['filik_ob']['tmp_name']))))
            {
            	if (DB :: $pdo -> query("INSERT INTO `gifts` (`id_kat`, `cena`, `do`) VALUES (?, ?, ?);", array($kat['id'], $cena, $do)))
                {
                    $last_id = DB :: $pdo -> lastInsertId();
                    copy($_FILES['filik_ob']['tmp_name'], FILES_PATH."gifts/".$last_id.".jpg");
                    chmod(FILES_PATH."gifts/".$last_id.".jpg", 0777);
        	    	system :: redirect("Подарок успешно добавлен.", "/gifts?admin&add_gift=".$kat['id']."");
        	    } else {
        	    	$errMsg = "Неизвестная ошибка.!";
        	    }
            } else {
                $errMsg = "Отсутствет изображение!";
            }
        }
    }
    if (isset($errMsg))
    {
        system :: show($errMsg);
    }
    ?>
    <div class="list">
        <form method="post" class="post-add" data-type="arbour" enctype="multipart/form-data" action="">
            <div class="_gray _dbl">Стоимость:</div>
            <input class="main-input _w-100" type="text" name="cena" />
            <div class="_gray _dbl">Изображение:</div>
            <input class="_gray _dbl" name="filik_ob" accept="image/*,image/png,image/gif,image/jpg" type="file" />
            <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>
    <?
    echo system :: back("{$thisLink}&amp;adm_cat=".$kat['id'], "Назад");
    require (CORE."foot.php");
    ?>