<?
$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_users` WHERE `id` = '".abs(intval($_GET['dell']))."'"))
{
    DB::$pdo -> query("DELETE FROM `bag_tracker_users` WHERE `id` = '".abs(intval($_GET['dell']))."' LIMIT 1");
    system::redirect('Пользователь успешно исключен!', '/bag_tracker/?users_list');
}
else if (isset($_GET['add_new']))
{
    if (isset($_POST['cfms']))
    {
        if (!isset($_POST['id_user']) && $_POST['id_user'] == NULL || $_POST['id_user'] <= 0 || !is_numeric($_POST['id_user']))
        {
            $errMsg = 'Укажите ID пользователя!';
        }
        else if (DB::$pdo -> querySingle("SELECT COUNT(*) FROM `users` WHERE `id` = '".abs(intval($_POST['id_user']))."'") == 0)
        {
            $errMsg = 'Пользователь не найден!';
        }
        else if (DB::$pdo -> querySingle("SELECT COUNT(*) FROM `bag_tracker_users` WHERE `id_user` = '".abs(intval($_POST['id_user']))."'") > 0)
        {
            $errMsg = 'Пользователь уже есть в базе!';
        }
        if (!isset($errMsg))
        {
            DB::$pdo -> query("INSERT INTO `bag_tracker_users` (`id_user`, `time`) VALUES (?, ?);", array(abs(intval($_POST['id_user'])), time()));
            system::redirect('Пользователь успешно добавлен!', '/bag_tracker/?users_list&add_new');
        }
    }
    if (isset($errMsg))
    {
        system::show($errMsg);
    }
    ?>
    <div class="list">
        <form method="post" class="post-add" data-type="arbour" action="/bag_tracker/?users_list&add_new">
            <div class="_gray _dbl">ID пользователя:</div>
            <input class="main-input _w-100" type="text" name="id_user" />
            <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/?users_list">
        <?echo system::icon('arr-l')?> Назад
    </a>
<?
} else {
?>
    <a class="app-action" href="/bag_tracker/?users_list&add_new">
        <?echo system::icon('add')?> Отправить приглашение
    </a>
    <?
    $total = DB::$pdo -> querySingle("SELECT COUNT(*) FROM `bag_tracker_users` WHERE `aut` > '0'");
    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_users` WHERE `aut` > '0' 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">
                        <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>
                        </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/?users_list&dell=<?echo $r['id'];?>">
                                Исключить
                            </a>
                        </div>
                    </div>
                </div>
           </div>
        <?
        }
        if ($total > 10)
        {
            echo $pagination -> printPages('/bag_tracker/?users_list&'); 
        }
    }
    ?>
    <a class="app-action" href="/bag_tracker/">
        <?echo system::icon('arr-l')?> Назад
    </a>
<?
}
require (CORE.'foot.php');
exit;
?>