<?php
    $title = $l = "Пользователи - ".group :: getName($group)."";
    $lBack ="/groups/?group=".$group['id']."";
    require (CORE."head.php");


    $total = DB :: $pdo -> querySingle("SELECT COUNT(*) FROM `groups_users` WHERE `id_group` = '".$group['id']."'");
 
    ?>
   
    <div class="app-block">
    <div class="list _bold _gray">
                Участников: <?echo $total;?>
    </div>
    <?
    if ($total == 0)
    {
    ?>
        <div class="list _gray _font-s _center">Участники не найдены!</div>
    <?
    } else {
        if (isset($user['id']) && (($user['id']==$ank['id'] && $group['d_user'] == 0) || $user['id'] == 1) && (isset($_GET['adm_us']) || isset($_GET['dell_us'])))
        {
            if (isset($_GET['adm_us']) && DB :: $pdo -> querySingle("SELECT COUNT(*) FROM `groups_users` WHERE `id_group` = '".$group['id']."' AND `id` = '".intval($_GET['adm_us'])."'") > 0)
            {
                $gr_user = DB :: $pdo -> queryFetch("SELECT * FROM `groups_users` WHERE `id` = ? AND `id_group` = ? LIMIT 1;", array(intval($_GET['adm_us']), $group['id']));
                if ($gr_user['admin'] < 2)
                {
                    if ($gr_user['admin'] == 0)
                    {
                        DB :: $pdo -> query("UPDATE `groups_users` SET `admin` = '1' WHERE `id` = '".$gr_user['id']."' AND `id_group` = '".$group['id']."' LIMIT 1");
                    } else {
                        DB :: $pdo -> query("UPDATE `groups_users` SET `admin` = '0' WHERE `id` = '".$gr_user['id']."' AND `id_group` = '".$group['id']."' LIMIT 1");
                    }
                }
            }
            else if (isset($_GET['dell_us']) && DB :: $pdo -> querySingle("SELECT COUNT(*) FROM `groups_users` WHERE `id_group` = '".$group['id']."' AND `id` = '".intval($_GET['dell_us'])."'") > 0)
            {
                $gr_user = DB :: $pdo -> queryFetch("SELECT * FROM `groups_users` WHERE `id` = ? AND `id_group` = ? LIMIT 1;", array(intval($_GET['dell_us']), $group['id']));
                if ($gr_user['admin'] < 2)
                {
                    DB :: $pdo -> query("DELETE FROM `groups_users` WHERE `id` = '".$gr_user['id']."' AND `id_group` = '".$group['id']."' LIMIT 1");
                    DB :: $pdo -> query("UPDATE `groups` SET `count_users` = `count_users`-1 WHERE `id` = ? LIMIT 1;", array($group['id']));
                    
                }
            }
            header("Location: /groups/?group=".$group['id']."&members");
            exit;
        }
        if ($total > 10)
        {
            $p = (($_GET['p']) ? intval($_GET['p']) : 1);
            $pagination = new pagination(10, $total, $p);
            $total_lin = $pagination -> limit;
        } else {
            $total_lin = '';
        }
        $query = DB :: $pdo -> query("SELECT * FROM `groups_users` WHERE `id_group` = '".$group['id']."' ORDER BY `id` DESC ".$total_lin."");
        while($r = $query -> fetch())
        {
        $post_ank = profile :: getUser($r['id_user']);
        ?>
            <div class="list">
                    <div class="post-l">
                    <a href="<?=profile :: getLink($post_ank)?>">
                        <?=profile :: userAvatar($post_ank, 0)?>
                    </a>
                    </div>
                     <?
                    if (isset($user['id']) && (($user['id']==$ank['id'] && $group['d_user'] == 0) || $user['id'] == 1) && $r['admin'] != 2)
                    {
                    ?>  <div>
                            <a class="toggle-menu-act" onclick="UI.toggleMenu(this);">
                                <span></span>
                            </a>
                            <div class="toggle-menu">
                            <div class="toggle-menu-arrow"><span></span></div>
                            <ul class="toggle-menu-block">
                                <li>
                                    <?
                                    if ($r['admin'] == 0)
                                    {
                                    ?>
                                        <a href="/groups/?group=<?echo $group['id'];?>&members&adm_us=<?echo $r['id'];?>">
                                            Дать должность
                                        </a>
                                    <?
                                    } else {
                                    ?>
                                        <a href="/groups/?group=<?echo $group['id'];?>&members&adm_us=<?echo $r['id'];?>">
                                            Снять с должности
                                        </a>
                                    <?
                                    }
                                    ?>
                                </li>
                                <li>
                                    <a href="/groups/?group=<?echo $group['id'];?>&members&dell_us=<?echo $r['id'];?>">
                                        Исключить
                                    </a>
                                </li>
                            </ul>
                            </div>
                        </div>
                    <?
                    }
                    ?>
                    <div class="post-c">
                        <div class="_name"><?=profile :: goPage($post_ank)?></div>
                        <div class="_bold">
                        <?
                        if ($post_ank['id'] == $ank['id'])
                        {
                        ?>
                            Владелец
                        <?
                        }
                        else if ($r['admin'] > 0)
                        {
                        ?>
                            Администратор
                        <?
                        }
                        ?>
                        </div>
                    </div>
                </div>
        <?
        }
        if ($total > 10)
        {
            echo $pagination -> printPages("/groups/?group=".$group['id']."&members&"); 
        }
    }
    echo system :: touch("/groups/?group=".$group['id']."", system :: icon("arr-l")."Назад");
    echo "</div>";
    require (CORE."foot.php");