<?
$title = $l = 'Баг-трекер - Управление типами';
require (CORE.'head.php');
if (isset($_GET['dell']) && $_GET['dell'] != NULL && is_numeric($_GET['dell']) && $_GET['dell'] > 0 && DB::$pdo -> querySingle("SELECT COUNT(*) FROM `bag_tracker_types` WHERE `id` = '".abs(intval($_GET['dell']))."'"))
{
    DB::$pdo -> query("DELETE FROM `bag_tracker_types` WHERE `id` = '".abs(intval($_GET['dell']))."' LIMIT 1");
    system::redirect('Тип успешно удален!', '/bag_tracker/?type_list');
}
else if (isset($_GET['add_new']))
{
    if (isset($_POST['cfms']))
    {
        $name = system::check($_POST['name']);
        $msg_str = system::utf_strlen($name);
        if ($msg_str >= 1)
        {
            if ($msg_str <= 20)
            {
                if (DB :: $pdo -> query("INSERT INTO `bag_tracker_types` (`name`, `time`) VALUES (?, ?);", array($name, time())))
                {			
                    system::redirect('Тип успешно добавлен!', '/bag_tracker/?type_list&add_new');
                } else {
                    $errMsg = 'Неизвестная ошибка!';
                }
            } else {
                $errMsg = 'Название превышает 20 знаков!';
            }
        } else {
            $errMsg = 'Пустое название!';
        }
    }
    if (isset($errMsg))
    {
        system::show($errMsg);
    }
    ?>
    <div class="list">
        <form method="post" class="post-add" data-type="arbour" action="/bag_tracker/?type_list&add_new">
            <div class="_gray _dbl">Название:</div>
            <input class="main-input _w-100" type="text" name="name" />
            <div class="_center" style="margin-top: 3px;">
                <input type="submit" class="main-btn _w-100" name="cfms" value="Продолжить" />
            </div>
        </form>
    </div>
    <a class="app-action" href="/bag_tracker/?type_list">
        <?echo system::icon('arr-l')?> Назад
    </a>
<?
} else {
?>
    <a class="app-action" href="/bag_tracker/?type_list&add_new">
        <?echo system::icon('add')?> Добавить тип
    </a>
    <?
    $total = DB::$pdo -> querySingle("SELECT COUNT(*) FROM `bag_tracker_types`");
    if ($total == 0)
    {
    ?>
        <div class="list _gray _font-s _center">
            Типы не найдены!
        </div>
    <?
    } else {
        if ($total > 10)
        {
            $p = (($_GET['p']) ? intval($_GET['p']) : 1);
            $pagination = new pagination(10, $total, $p);
            $total_lin = $pagination -> limit;
        } else {
            $total_lin = '';
        }
        $query_gifts = DB :: $pdo -> query("SELECT * FROM `bag_tracker_types` ORDER BY `id` DESC ".$total_lin."");
        while($r = $query_gifts -> fetch())
        {
        ?>
            <div class="list _l-padd-n">
                <div class="_l-padd">
                    <div class="_oh">
                        <b><?echo stripcslashes(htmlspecialchars($r['name']));?></b><br />
                        <a href="/bag_tracker/?type_list&dell=<?echo $r['id'];?>">
                            Удалить
                        </a>
                    </div>
                </div>
            </div>
        <?
        }
        if ($total > 10)
        {
            echo $pagination -> printPages('/bag_tracker/?type_list&'); 
        }
    }
    ?>
    <a class="app-action" href="/bag_tracker/">
        <?echo system::icon('arr-l')?> Назад
    </a>
<?
}
require (CORE.'foot.php');
exit;
?>