<?
$title = $l = 'Баг-трекер - '.stripcslashes(htmlspecialchars($tracker['title']));
require (CORE.'head.php');
$ank = profile::getUser($tracker['id_user']);
$id_type = DB::$pdo -> queryFetch("SELECT * FROM `bag_tracker_types` WHERE `id` = '".$tracker['id_type']."' LIMIT 1;");
$id_kat = DB::$pdo -> queryFetch("SELECT * FROM `bag_tracker_kat` WHERE `id` = '".$tracker['id_kat']."' LIMIT 1;");
if ($user['id'] == 1 && isset($_GET['ans']))
{
    if (isset($_POST['cfms']))
    {
        if (!isset($_POST['text']) || $_POST['text'] == NULL || system::utf_strlen(system::check($_POST['text'])) < 1)
        {
            $errMsg = 'Введите текст!';
        }
        if (!isset($errMsg))
        {
            notif::init()->comment_tracker([
                'id_author' => $tracker['id_user'],
                'id_user' => $user['id'],
                'id_post' => $tracker['id'],
                'text' => system::check($_POST['text'])
            ]);
            //DB:: $pdo->query("UPDATE `users` SET `count_new_notice` = `count_new_notice`+1 WHERE `id` = '".$tracker['id_user']."' LIMIT 1;");
            system::redirect('Ответ успешно отправлен!', '/bag_tracker/?tracker='.$tracker['id']);
        }
    }
    if (isset($errMsg))
    {
        system::show($errMsg);
    }
    ?>
    <div class="app-block">
    <div class="list">
        <form method="post" class="post-add" data-type="arbour" action="/bag_tracker/?tracker=<?echo $tracker['id'];?>&ans">
            <div class="_gray _dbl">
                Текст:
            </div>
            <textarea name="text"></textarea>
            <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/?tracker=<?echo $tracker['id'];?>">
        <?echo system::icon('arr-l')?> Назад
    </a>
    <?
    require (CORE.'foot.php');
    exit;
}
if ($user['id'] == 1 && isset($_GET['edit']))
{
    if (isset($_POST['cfms']))
    {
        if (!isset($_POST['title']) || $_POST['title'] == NULL || system::utf_strlen(system::check($_POST['title'])) < 1)
        {
            $errMsg = 'Пустое название!';
        }
        else if (system::utf_strlen(system::check($_POST['title'])) > 20)
        {
            $errMsg = 'Название превышает 20 знаков!';
        }
        else if (!isset($_POST['opis']) || $_POST['opis'] == NULL || system::utf_strlen(system::check($_POST['opis'])) < 1)
        {
            $errMsg = 'Пустое описание!';
        }
        else if (!isset($_POST['id_type']) || $_POST['id_type'] == NULL || !is_numeric($_POST['id_type']) || $_POST['id_type'] < 0 || DB::$pdo -> querySingle("SELECT COUNT(*) FROM `bag_tracker_types` WHERE `id` = '".abs(intval($_POST['id_type']))."'") == 0)
        {
            $errMsg = 'Укажите тип!';
        }
        else if (!isset($_POST['id_kat']) || $_POST['id_kat'] == NULL || !is_numeric($_POST['id_kat']) || $_POST['id_kat'] < 0 || DB::$pdo -> querySingle("SELECT COUNT(*) FROM `bag_tracker_kat` WHERE `id` = '".abs(intval($_POST['id_kat']))."'") == 0)
        {
            $errMsg = 'Укажите категорию!';
        }
        else if (!isset($_POST['prioritet']) || $_POST['prioritet'] == NULL || !is_numeric($_POST['prioritet']) || $_POST['prioritet'] < 0 || $_POST['prioritet'] > 2)
        {
            $errMsg = 'Укажите приоритет!';
        }
        else if (!isset($_POST['status']) || $_POST['status'] == NULL || !is_numeric($_POST['status']) || $_POST['status'] < 0 || $_POST['status'] > 1)
        {
            $errMsg = 'Укажите статус!';
        }
        if (!isset($errMsg))
        {
            DB::$pdo -> query("UPDATE `bag_tracker` SET `id_type` = '".abs(intval($_POST['id_type']))."', `id_kat` = '".abs(intval($_POST['id_kat']))."', `prioritet` = '".abs(intval($_POST['prioritet']))."', `title` = '".system::check($_POST['title'])."', `opis` = '".system::check($_POST['opis'])."', `status` = '".abs(intval($_POST['status']))."' WHERE `id` = '".$tracker['id']."' LIMIT 1");
            DB::$pdo -> query("INSERT INTO `bag_tracker_log` (`id_tracker`, `type_user`, `title`, `opis`, `time`) VALUES ('".$tracker['id']."', '1', 'отредактировал отчет', '', '".time()."');");
            system::redirect('Отчет успешно изменен!', '/bag_tracker/?tracker='.$tracker['id'].'&edit');
        }
    }
    if (isset($errMsg))
    {
        system::show($errMsg);
    }
    ?>
     <div class="app-block">
    <div class="list">
        <form method="post" class="post-add" data-type="arbour" action="/bag_tracker/?tracker=<?echo $tracker['id'];?>&edit">
            <div class="_gray _dbl">
                Название:
            </div>
            <input class="main-input _w-100" type="text" name="title" value="<?=$tracker['title']?>" />
            <div class="_gray _dbl">
                Описание:
            </div>
            <textarea name="opis"><?=$tracker['opis']?></textarea>
            <div class="_gray _dbl">
                Тип:
            </div>
            <select class="main-input _w-100" name="id_type">
                <?
                $query_gifts = DB :: $pdo -> query("SELECT * FROM `bag_tracker_types` ORDER BY `name` ASC");
                while($r = $query_gifts -> fetch())
                {
                ?>
                    <option value="<?echo $r['id'];?>"<?echo ($tracker['id_type'] == $r['id'] ?" selected='selected'":null);?>>
                        <?echo stripcslashes(htmlspecialchars($r['name']));?>
                    </option>
                <?
                }
                ?>
            </select>
            <div class="_gray _dbl">
                Категория:
            </div>
            <select class="main-input _w-100" name="id_kat">
                <?
                $query_gifts = DB :: $pdo -> query("SELECT * FROM `bag_tracker_kat` ORDER BY `name` ASC");
                while($r = $query_gifts -> fetch())
                {
                ?>
                    <option value="<?echo $r['id'];?>"<?echo ($tracker['id_kat'] == $r['id'] ?" selected='selected'":null);?>>
                        <?echo stripcslashes(htmlspecialchars($r['name']));?>
                    </option>
                <?
                }
                ?>
            </select>
            <div class="_gray _dbl">
                Приоритет:
            </div>
            <select class="main-input _w-100" name="prioritet">
                <option value="0"<?echo ($tracker['prioritet'] == 0 ?" selected='selected'":null);?>>
                    Низкий
                </option>
                <option value="1"<?echo ($tracker['prioritet'] == 1 ?" selected='selected'":null);?>>
                    Средний
                </option>
                <option value="2"<?echo ($tracker['prioritet'] == 2 ?" selected='selected'":null);?>>
                    Высокий
                </option>
            </select>
            <div class="_gray _dbl">
                Статус:
            </div>
            <select class="main-input _w-100" name="status">
                <option value="0"<?echo ($tracker['status'] == 0 ?" selected='selected'":null);?>>
                    Активен
                </option>
                <option value="1"<?echo ($tracker['status'] == 1 ?" selected='selected'":null);?>>
                    Закрыт
                </option>
            </select>
            <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/?tracker=<?echo $tracker['id'];?>">
        <?echo system::icon('arr-l')?> Назад
    </a>
    <?
    require (CORE.'foot.php');
    exit;
}
if ($user['id'] == 1 && isset($_GET['dell']))
{
    if (DB::$pdo -> querySingle("SELECT COUNT(*) FROM `bag_tracker_log` WHERE `id_tracker` = '".$tracker['id']."'") > 0)
    {
        DB::$pdo -> query("DELETE FROM `bag_tracker_log` WHERE `id_tracker` = '".$tracker['id']."'");
    }
    DB::$pdo -> query("DELETE FROM `bag_tracker` WHERE `id` = '".$tracker['id']."' LIMIT 1");
    system::redirect('Отчет успешно удален!', '/bag_tracker/');
}
?>
 <div class="app-block">
<div class="list _l-padd-n">
    <div class="_l-padd">
        <div class="_oh">
            <div class="list-flex">
                <div class="post-l">
                    <?echo profile::userAvatar($ank, 3);?>
                </div>
                <div class="post-c">
                    <div class="_name">
                        <?echo profile::goPage($ank);?>
                    </div>
                    <div class="_time">
                        <?echo system::times($tracker['time']);?>
                    </div>
                    <span class="_gray _font-m">
                        <b><?echo stripcslashes(htmlspecialchars($tracker['title']));?></b>
                    </span>
                </div>
            </div>
        </div>
        <div class="_gray" style="margin-top: 20px;">
            <div class="_font-s">
                <?echo emoji::Text($tracker['opis']);?>
            </div>
        </div>
        <div class="_gray" style="margin-top: 20px;">
            <div class="_font-s">
                <b>Тип:</b> <?echo stripcslashes(htmlspecialchars($id_type['name']));?>
            </div>
            <div class="_font-s">
                <b>Категория:</b> <?echo stripcslashes(htmlspecialchars($id_kat['name']));?>
            </div>
            <div class="_font-s">
                <b>Приоритет:</b> 
                <?
                if ($tracker['prioritet'] == 1)
                {
                ?>
                    Средний
                <?
                }
                else if ($tracker['prioritet'] == 2)
                {
                ?>
                    Высокий
                <?
                } else {
                ?>
                    Низкий
                <?
                }
                ?>
            </div>
            <div class="_font-s">
                <b>Статус:</b> 
                <?
                if ($tracker['status'] == 1)
                {
                ?>
                    Закрыт
                <?
                } else {
                ?>
                    Активен
                <?
                }
                ?>
            </div>
        </div>
    </div>
    <?
    if ($user['id'] == 1)
    {
    ?>
        <div class="tb-line"></div>
        <div class="tb-bloc">
            <div class="tb-cont tb-2">
                <div class="tb-item" style="width: 33%;">
                    <a class="tb-c" href="/bag_tracker/?tracker=<?echo $tracker['id'];?>&ans">
                        Ответить
                    </a>
                </div>
                <div class="tb-item" style="width: 33%;">
                    <a class="tb-c" href="/bag_tracker/?tracker=<?echo $tracker['id'];?>&edit">
                        Редактировать
                    </a>
                </div>
                <div class="tb-item" style="width: 33%;">
                    <a class="tb-c" href="/bag_tracker/?tracker=<?echo $tracker['id'];?>&dell">
                        Удалить
                    </a>
                </div>
             </div>
        </div>
    <?
    }
    ?>
</div>
<?
$total = DB::$pdo -> querySingle("SELECT COUNT(*) FROM `bag_tracker_log` WHERE `id_tracker` = '".$tracker['id']."'");
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_log` WHERE `id_tracker` = '".$tracker['id']."' 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>Разработчик</b> <?echo stripcslashes(htmlspecialchars($r['title']));?><br />
                    <?echo system::times($r['time']);?>
                </div>
            </div>
        </div>
    <?
    }
    if ($total > 10)
    {
        echo $pagination -> printPages('/bag_tracker/?tracker='.$tracker['id'].'&'); 
    }
}
?>
<a class="app-action" href="/bag_tracker/">
    <?echo system::icon('arr-l')?> Назад
</a>
<?
require (CORE.'foot.php');
exit;
?>