<?
$group = (int)abs($_GET['group']);
$group = DB :: $pdo -> queryFetch("SELECT * FROM `groups` WHERE `id` = ? LIMIT 1;", array($group));
$ank = profile :: getUser($group['id_user']);
if (isset($user['id']))
{
    $group_user = DB :: $pdo -> queryFetch("SELECT * FROM `groups_users` WHERE `id_user` = ? AND `id_group` = ? LIMIT 1;", array($user['id'], $group['id']));
}
if (!isset($group) || $group['id'] <= 0)
{
$title = $l = "Ошибка";
   
    require (CORE."head.php");
  
    ?>
    <div class="list _gray _font-s _center">Группа не найдена!</div>
    <?
    echo system :: touch("/groups/", system :: icon("arr-l")."Назад");
    require (CORE."foot.php");
    exit;
}
// Подключаемые файлы
if (isset($user['id']) && (($user['id']==$ank['id'] && $group['d_user'] == 0 && $group['block'] == 0) || admin :: getAccess($user,  array('a'=>3,'m'=>1))) && isset($_GET['edit']))
{require('edit.php');exit;} # Изменить группу
else if (isset($user['id']) && (($user['id']==$ank['id'] && $group['d_user'] == 0 && $group['block'] == 0) || admin :: getAccess($user,  array('a'=>3,'m'=>1)) || ($group_user['admin'] > 0 && $group['block'] == 0)) && isset($_GET['dell_komm']))
{require('dell_komm.php');exit;} #Удалить комментарий
else if (isset($user['id']) && (($user['id']==$ank['id'] && $group['d_user'] == 0 && $group['block'] == 0) || admin :: getAccess($user,  array('a'=>3,'m'=>1))) && isset($_GET['edit_komm']))
{require('edit_komm.php');exit;} #Изменить комментарий
else if (isset($_GET['komm_files']) && isset($user['id']) && ($group['who_komm']==0 || ($group['who_komm']==1 && isset($group_user) && $group_user['id']>0) || ($group['who_komm']==2 && $group_user['admin']>=1)))
{require('komm_files.php');exit;} #Добавить фото
else if (isset($_GET['komm'])){require('komm.php');exit;} #Запись комментировать
else if (isset($_GET['members']))
{require('members.php');exit;} #Пользователи
else if (isset($user['id']) && (($user['id']==$ank['id'] && $group['d_user'] == 0 && $group['block'] == 0) || admin :: getAccess($user,  array('a'=>3,'m'=>1))) && isset($_GET['ava']))
{require('ava.php');exit;} #Ава
else if (isset($user['id']) && (($user['id']==$ank['id'] && $group['d_user'] == 0 && $group['block'] == 0) || admin :: getAccess($user,  array('a'=>3,'m'=>1))) && isset($_GET['del_gr']))
{require('del_gr.php');exit;} #Удалить группу
else if (isset($user['id']) && admin :: getAccess($user,  array('a'=>3,'m'=>1)) && isset($_GET['block_gr']))
{require('block_gr.php');exit;} #Заблокировать группу
else if (isset($user['id']) && isset($_GET['in']) && $group['block'] == 0 && DB :: $pdo -> querySingle("SELECT COUNT(*) FROM `groups_users` WHERE `id_group` = '".$group['id']."' AND `id_user` = '".$user['id']."'") == 0)
{
DB :: $pdo -> query("INSERT INTO `groups_users` (`id_user`, `id_group`, `time`) VALUES (?, ?, ?);", array($user['id'], $group['id'], time()));header("Location: /groups/?group=".$group['id']."");
exit;
}
else if (isset($user['id']) && isset($_GET['out']) && DB :: $pdo -> querySingle("SELECT COUNT(*) FROM `groups_users` WHERE `id_group` = '".$group['id']."' AND `id_user` = '".$user['id']."'") > 0)
{
    DB :: $pdo -> query("DELETE FROM `groups_users` WHERE `id_user` = '".$user['id']."' AND `id_group` = '".$group['id']."' LIMIT 1");
    if ($user['id'] == $ank['id'])
    {
        DB :: $pdo -> query("UPDATE `groups` SET `d_user` = '1' WHERE `id` = '".$group['id']."' LIMIT 1");
    }
    header("Location: /groups/?group=".$group['id']."");
    exit;
}
else if (isset($user['id']) && $group['d_user'] == 1 && $group['block'] == 0 && isset($_GET['get_admin']) && DB :: $pdo -> querySingle("SELECT COUNT(*) FROM `groups_users` WHERE `id_group` = '".$group['id']."' AND `id_user` = '".$user['id']."'") > 0)
{
    DB :: $pdo -> query("UPDATE `groups` SET `id_user` = '".$user['id']."',`d_user` = '0' WHERE `id` = '".$group['id']."' LIMIT 1");
    DB :: $pdo -> query("UPDATE `groups_users` SET `admin` = '2' WHERE `id_user` = '".$user['id']."' AND `id_group` = '".$group['id']."' LIMIT 1");
    header("Location: /groups/?group=".$group['id']."");
    exit;
}


$title = $l = group :: getName($group); #Тайтл
$lBack = "/groups"; #Ссылка в профиль
require (CORE."head.php");
require('inc.php');

// Включения ф-ций
 $actionsLink = array();
   if(isset($group_user['id_user'])) {
        if (isset($user['id']) && (($user['id']==$ank['id'] && $group['d_user'] == 0) || admin :: getAccess($user,  array('a'=>3,'m'=>1)))) {
            $actionsLink[] = ['link'=>"/groups/?group={$group['id']}&amp;edit=1", 'text'=>"Редактировать информацию"];
            $actionsLink[] = ['link'=>"/groups/?group={$group['id']}&amp;ava=1",'text'=> "Изменить фотографию"];
        }
        if (isset($user) && admin :: getAccess($user,  array('a'=>3,'m'=>1))) {
            $actionsLink[] = ['link'=> "/groups/?group={$group['id']}&block_gr=1", 'text'=>($group['block'] > 0 ? "Раз" : "За" )."блокировать группу"];
        }
        if (isset($user) && (($user['id'] == $ank['id'] && $group['d_user'] == 0 && $group['block'] == 0) || admin :: getAccess($user,  array('a'=>3))))
        {
             $actionsLink[] = ['link'=> "/groups/?group={$group['id']}&del_gr=1", 'text'=>"Удалить группу"];
        }
    }
?>
  <div class="app-block">
    <div class="list _l-padd-n" style="overflow: visible;">
        <div class="_l-padd" style="padding-bottom: 0px;">
      <? if (isset($user['id']) && (($user['id']==$ank['id'] && $group['d_user'] == 0) || admin :: getAccess($user,  array('a'=>3,'m'=>1)))) { ?> <?=UI :: toggleMenu($actionsLink)?><?}?>
        <div class="list-flex">
            <div class="post-l">
                <?=group :: Avatar($group,0)?>
            </div>
            <div class="post-c">
                    <span class="_name size--big"><?=$group['name']?></span>
                    <?
                    if ($group['d_user'] == 1)
                    {
                    ?>
                        <img src="/template/groups_img/no_admin.png" title="Группа без Главного администратора!" alt="*" />
                    <?
                    }
                    if ($group['block'] > 0)
                    {
                        if (isset($user) && admin :: getAccess($user,  array('a'=>3,'m'=>1)))
                        {
                        ?>
                            <a href="/groups/?group=<?echo $group['id'];?>&block_gr" title="Разблокировать группу">
                               <img class="sim_groups96" src="/template/groups_img/block.png" alt="*" />
                            </a>
                        <?
                        } else {
                        ?>
                            <img class="sim_groups96" src="/template/groups_img/block.png" title="Группа заблокирована администрацией сайта!" alt="*" />
                        <?
                        }
                    }
                    ?>
                <?php
                        if($group['deviz']!= NULL) {
        ?>
        <div class="p-status _l-padd_ _gray" style="padding-bottom: 0px;"><span><?=system :: check($group['deviz'])?></span></div>
        <?php
        }
        ?>
        </div>
            </div>
        </div>
        <?


        if (isset($user['id']))
        {
        ?>
            <div class="_l-padd">
                <?
                if (isset($group_user['id_user']))
                {
                ?>
                    <a class="action-btn _dbl _center _lh-n" href="/groups/?group=<?=$group['id']?>&out=1">Выйти из группы</a>
                <?php
                    if (isset($user['id']) && $group['d_user'] == 1) {
                ?>
                    <a class="action-btn _dbl _center _lh-n" style="margin-top: 5px;" href="/groups/?group=<?=$group['id']?>&get_admin=1">Стать главным администратором</a>
                        <?php
                        }
                        ?>
                <?
                } else {
                    ?>
                        <a class="main-btn _dbl _center _lh-n" href="/groups/?group=<?=$group['id']?>&in=1">
                            Вступить в группу
                        </a>
                    <?
                    
                }
                ?>
            </div>
        <?
        }
        ?>
    </div>
 <div><a href="#" class="app-toggler app-toggler-white _center" data-id="group--info" data-class="app-toggler-open" onclick="App.toggle(this,event);return false;">Информация<span class="app-toggler-icon app-icons"></span></a></div>
            <!-- inform. block -->      
            
        <div id="group--info" class="list" style="display: none;">
            <div><span class="_gray">Дата создания:</span> <span><?=system :: times($group['time'])?></span></div>
            <?=($group['opis'] != NULL ? "<div><span class=\"_gray\">Описание:</span> <span>{$group['opis']}</span></div>" : '')?>
            
            <div><span class="_gray">Участники:</span> <span><a href="/groups/?group=<?=$group['id']?>&members"><?echo DB :: $pdo -> querySingle("SELECT COUNT(*) FROM `groups_users` WHERE `id_group` = '".$group['id']."'");?> чел.
         </a></span></div>
        </div>
    </div>
<?
$mess_cc = DB :: $pdo -> querySingle("SELECT COUNT(*) FROM `groups_komm` WHERE `id_group` = '".$group['id']."' AND `id_post` = '0'");
if (isset($user['id']) && $group['block'] == 0 && ($group['who_komm']==0 || ($group['who_komm']==1 && isset($group_user) && $group_user['id']>0) || ($group['who_komm']==2 && $group_user['admin']>=1)))

{
    require('send_komment.php');
    ?>
    <div class="app-block">
        <div>
            <form method="post" action="">
                <div>
                    <textarea name="komment" class="app-textarea"  id="komment" data-id="emoji-block_1" placeholder="Расскажите  что у Вас нового.."></textarea>
                </div>
                 <?     if (DB :: $pdo -> querySingle("SELECT COUNT(*) FROM `groups_files` WHERE `id_user` = '".$user['id']."' AND `id_group` = '".$group['id']."' AND `time` = '0'") > 0)
            {
            ?>
                <div class="list">
                    <?
                    $query_f = DB :: $pdo -> query("SELECT * FROM `groups_files` WHERE `id_user` = '".$user['id']."' AND `id_group` = '".$group['id']."' AND `time` = '0' ORDER BY `id` DESC");
                    while($r_f = $query_f -> fetch())
                    {
                    ?>
                        <div class="sim_groups119">
                            <img class="sim_groups116" src="<?echo "".FILES_SERVER."/groups/files/".$r_f['id'].".jpg";?>" alt="*" />
                            <a class="sim_groups117" href="/groups/?group=<?echo $group['id'];?>&komm_files&dell_file=<?echo $r_f['id'];?>">
                                <i class="sim_groups118"></i>
                            </a>
                        </div>
                    <?
                    }
                    ?>
                </div>
            <?
            }
            ?>
                <div class="list _bg-blue">
              
                    <div class="list-fle">
                        <input class="main-btn" type="submit" name="cfms" value="Создать запись" />
                     <?   if (isset($user['id']) && (($user['id']==$ank['id'] && $group['d_user'] == 0) || admin :: getAccess($user,  array('a'=>3,'m'=>1)) || $group_user['admin'] > 0))
            {?>
                        <label class="_gray" style="margin-left: 10px;"><input type="checkbox" name="as_admin" value="1" />От именни группы</label>
                     <?}   ?>
                        <a href="/groups/?group=<?echo $group['id'];?>&komm_files" style="margin-left: 15px;float: right;margin-top: 10px;"><i class="fa fa-picture-o fa-2x"></i></a>
              
                        <?=emoji :: Smiles("emoji-block_1","komment")?>
                    </div>
                </div>
                    
            </form>
        </div>
        </div>
    <?
}
//Пагинация
if ($mess_cc > 10)
{
    $p = (($_GET['p']) ? intval($_GET['p']) : 1);
    $pagination = new pagination(10, $mess_cc, $p);
    $total_lin = $pagination -> limit;
} else {
    $total_lin = '';
}

//Вывод записей 
$query = DB :: $pdo -> query("SELECT * FROM `groups_komm` WHERE `id_group` = '".$group['id']."' AND `id_post` = '0' ORDER BY `id` DESC ".$total_lin."");
while($r = $query -> fetch())
{
$post_ank = profile :: getUser($r['id_user']);
$post_cc = DB :: $pdo -> querySingle("SELECT COUNT(*) FROM `groups_komm` WHERE `id_group` = '".$group['id']."' AND `id_post` = '".$r['id']."'");
?>
 <div class="app-block">
            <div class="list _l-padd-n">
                <div class="_l-padd">
                <? if (isset($user['id']) && (($user['id']==$ank['id'] && $group['d_user'] == 0) || admin :: getAccess($user,  array('a'=>3,'m'=>1)) || $group_user['admin'] > 0))
        {?>
                    <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">
                         <?  if (isset($user['id']) && (($user['id']==$ank['id'] && $group['d_user'] == 0) || admin :: getAccess($user,  array('a'=>3,'m'=>1))))
                    {?>  <a href="/groups/?group=<?echo $group['id'];?>&edit_komm=<?echo $r['id'];?>">Редактировать</a> <?}?>
                            <a href="/groups/?group=<?echo $group['id'];?>&dell_komm=<?echo $r['id'];?>">Удалить</a>
                            </ul>
                            </div>
                        </div>
                <?php } ?>
                <div class="list-flex">
            <?if ($r['as_admin'] == 1)
            {?>  
             <div class="post-l"><?=group :: Avatar($group, 2)?></div>
           
            <?} else {?> 
             <div class="post-l"><?=profile :: userAvatar($post_ank,3)?></div>
            <?}?>
            <div class="post-c">
                <div>
            <? if ($r['as_admin'] == 1)
            {?><span class="_name _font-s"><?=$group['name'];?></span>
            <?} else {?><span class="_name _font-s"><?=profile :: goPage($post_ank)?></span>
            <?}?>  
                        <br />
            <span><a class="_time" href="/groups/?group=<?echo $group['id'];?>&komm=<?echo $r['id'];?>"> <?=system :: times($r['time']);?></a></span>
                </div>
                </div>
                </div>
                <div>
                    
                <?=group:: Komments($r['id'])?>
                    
                </div>
                                <div>
              
                </div>
                
                </div>



                <div class="_line2"></div>
                <div class="_l-padd app-touch-bottom ">
                     <a href="/groups/?group=<?echo $group['id'];?>&komm=<?echo $r['id'];?>" class="post-bottom-menu-h">Комментировать <span style="color: #666b7b;padding: 5px;background: #e2e2e2;text-align: center;border-radius: 3px;"> <?=$post_cc;?></span></a>
                     
                </div>
            </div>
            </div>
<?
}
// Пагинация
if ($mess_cc > 10)
{
    echo $pagination -> printPages("/groups/?group=".$group['id']."&"); 
}
// Если нету записей 
if($mess_cc == 0){
			echo "<div class=\"_l-padd _center _gray _font-s\">Записи в группе отсутствуют.</div>";
		}
//Назад
echo"<div class='app-block'>";
echo system :: touch("/groups/", system :: icon("arr-l")."Назад");
echo"</div>";

//Низ
require (CORE."foot.php");
?>