<?
if (!isset($user['id']))
{
    header("Location: /index.php");
    exit;
}
if ($user['id'] == 1 &&  DB::$pdo -> querySingle("SELECT COUNT(*) FROM `bag_tracker_users` WHERE `id_user` = '".$user['id']."' AND `aut` > '0'") == 0)
{
    DB::$pdo -> query("INSERT INTO `bag_tracker_users` (`id_user`, `aut`, `time`) VALUES (?, ?, ?);", array($user['id'], time(), time()));
}
if (isset($_GET['my_invite']) && DB::$pdo -> querySingle("SELECT COUNT(*) FROM `bag_tracker_users` WHERE `id_user` = '".$user['id']."' AND `aut` = '0'") > 0)
{
    include_once 'inc/my_invite.php';
}
if (DB::$pdo -> querySingle("SELECT COUNT(*) FROM `bag_tracker_users` WHERE `id_user` = '".$user['id']."' AND `aut` > '0'") == 0)
{
    header("Location: /index.php");
    exit;
}
if ($user['id'] == 1 && isset($_GET['type_list']))
{
    include_once 'inc/type_list.php';
}
if ($user['id'] == 1 && isset($_GET['kat_list']))
{
    include_once 'inc/kat_list.php';
}
if ($user['id'] == 1 && isset($_GET['users_list']))
{
    include_once 'inc/users_list.php';
}
if (isset($_GET['add_tracker']))
{
    include_once 'inc/add_tracker.php';
}
if (isset($_GET['tracker']) && $_GET['tracker'] != NULL && is_numeric($_GET['tracker']) && $_GET['tracker'] > 0 && DB::$pdo -> querySingle("SELECT COUNT(*) FROM `bag_tracker` WHERE `id` = '".abs(intval($_GET['tracker']))."'") > 0)
{
    $tracker = DB::$pdo -> queryFetch("SELECT * FROM `bag_tracker` WHERE `id` = '".abs(intval($_GET['tracker']))."' LIMIT 1;");
    include_once 'inc/tracker.php';
}
$title = $l = 'Баг-трекер';
require (CORE.'head.php');
?>
<div class="app-block">
  <?  if ($user['id'] == 1)
{
?>
<div class="list _l-padd-n">
    <div class="tb-bloc">
        <div class="tb-cont tb-3">
            <div class="tb-item" style="width: 33.33%;">
                <a class="tb-c" href="/bag_tracker/?type_list">
                   Типы
                </a>
            </div>
            <div class="tb-item" style="width: 33.33%;">
                <a class="tb-c" href="/bag_tracker/?kat_list">
                    Категории
                </a>
            </div>
            <div class="tb-item" style="width: 33.33%;">
                <a class="tb-c" href="/bag_tracker/?users_list">
                    Пользователи
                </a>
            </div>
        </div>
    </div>
</div>


<?}?>
    
    
    
<a class="app-action" href="/bag_tracker/?add_tracker">
   <i class="our our-search"></i> Добавить отчет
</a>
<div class="list _l-padd-n">
    <div class="tb-bloc" style="border-top: 1px solid #dcd9d9;">
        <div class="tb-cont tb-2">
            <div class="tb-item" style="width: 33.33%;">
                <a class="tb-c" href="/bag_tracker/">
                    Все
                </a>
            </div>
            <div class="tb-item" style="width: 33.33%;">
                <a class="tb-c" href="/bag_tracker/?all_trackers">
                    Открытые
                </a>
            </div>
            <div class="tb-item" style="width: 33.33%;">
                <a class="tb-c" href="/bag_tracker/?old_trackers">
                    Закрытые
                </a>
            </div>
        </div>
    </div>
</div>
<?
if (isset($_GET['all_trackers']))
{
    $track_zap = "WHERE `status` = '0'";
    $track_link = '/bag_tracker/?all_trackers&';
}
else if (isset($_GET['old_trackers']))
{
    $track_zap = "WHERE `status` = '1'";
    $track_link = '/bag_tracker/?old_trackers&';
} else {
    $track_zap = "";
    $track_link = '/bag_tracker/?';
}
$total = DB::$pdo -> querySingle("SELECT COUNT(*) FROM `bag_tracker` $track_zap");
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` $track_zap ORDER BY `id` DESC ".$total_lin."");
    while($r = $query_gifts -> fetch())
    {
        $ank = profile::getUser($r['id_user']);
        ?>
        <div class="list _l-padd-n">
            <div class="_l-padd">
                <div class="_oh">
 <span style="float: right;">
  <span style="color:grey"> <b>Статус:</b> </span>
                <?
                if ($r['status'] == 1)
                {
                ?>
                    <span style="color:red"> Закрыт </span>
                <?
                } else {
                ?>
                 <span style="color:green">   Активен </span>
                <?
                }
                ?><br />
                 <span style="color:grey">   <b>Приоритет:</b> </span>
                <?
                if ($r['prioritet'] == 1)
                {
                ?>
                    <span style="color:LightSeaGreen"> Средний </span>
                <?
                }
                else if ($r['prioritet'] == 2)
                {
                ?>
                  <span style="color:DarkCyan">   Высокий</span>
                <?
                } else {
                ?>
                 <span style="color:Teal">    Низкий</span>
                <?
                }
                ?>
                
                </span>
                    <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($r['time']);?>
                            </div>
                            <span class="_gray _font-m">
                                <?echo stripcslashes(htmlspecialchars($r['title']));?>
                            </span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="tb-line"></div>
            <div class="tb-bloc">
                <div class="tb-cont tb-2">
                    <div class="tb-item" style="width: 100%;">
                        <a class="tb-c" href="/bag_tracker/?tracker=<?echo $r['id'];?>">
                            Подробнее
                        </a>
                    </div>
                </div>
            </div>
        </div>
    <?
    }
    if ($total > 10)
    {
        echo $pagination -> printPages($track_link); 
    }
}

require (CORE.'foot.php');
?>