<?php
	// 23.07.2015
	$table = "users"; #Таблица
	$title = $l = "Пользователи"; #Тайтл 
	$lBack = "#";
	$thisLink = "/".$thisPage; #Линк 

    // Сортировка	
	$getSex = "";
	$qSex = "";
	$getOnline = "";
	$qOnline = "";

    //Фильтрация 	
	if(isset($_POST['filter'])) {
		$f_sex = intval($_POST['sex']);
		if(isset($_POST['fonline'])){ $gon = "&fon=1"; } else { $gon = ""; }
		system :: redirect(NULL, $thisLink."?fsex={$f_sex}{$gon}");
	}
    // Определения пола 
	if(isset($_GET['fsex']) && in_array(intval($_GET['fsex']), [0,1]) ) { 
		$filter['sex'] = intval($_GET['fsex']); 
		$qSex = " AND `info_sex` = '{$filter['sex']}'";
		$getSex = "fsex={$filter['sex']}&amp;"; 
	}
	// Поиск по имени и фамили 
	$search = "";
	$getSearch = "";
	
	$qSearch = "";
	if(isset($_GET['q'])) {
	    $qSearch = trim(system::check($_GET['q']));
	    if($qSearch!="") {
	    $search = " AND (`info_first_name` LIKE '%".$qSearch."%' OR `info_last_name` LIKE '%".$qSearch."%')";
	    $getSearch = "q={$qSearch}&";
	    }
	}
	if(isset($_POST['search'])) {
	    $qSearch = trim(system::check($_POST['search']));
	    if($qSearch!="") {
	    $search = " AND (`info_first_name` LIKE '%".$qSearch."%' OR `info_last_name` LIKE '%".$qSearch."%')";
	    $getSearch = "q={$qSearch}&";
	    }
	}
	
	//Онлай определения 
	
	if(isset($_GET['fon']) && intval($_GET['fon']) == 1) {
		$filter['online'] = 1;
		$getOnline = "fon=1&amp;";
		$qOnline = " AND `last_time` > '".(time()-$config['TIME_ONLINE'])."'";
	}
	
	require (CORE."head.php");	 #Шапка 
        //Табла и вывод 
		$query = "SELECT * FROM `{$table}` WHERE `id` <> '{$config['SYS_ID']}'{$search}{$qSex}{$qOnline} ORDER by `id`  DESC ";
		$total = DB :: $pdo -> querySingle("SELECT COUNT(*) FROM `{$table}` WHERE `id` <> '{$config['SYS_ID']}'{$search}{$qSex}{$qOnline} ORDER by `id`  DESC ");
		
		//Пагинация
		if($total > 10) {
			$p = (($_GET['p']) ? intval($_GET['p']) : 1);
			$pagination = new pagination(10, $total, $p);
			$query .= $pagination -> limit;
		}
		
		$q = DB :: $pdo -> query("{$query}");

		$i = 0;
		$in = 0;
		
		?>
		<div class="app-block">
		    <div class="sw-block"><div class="sw-cont sw-2">
		<div class="sw-item"><a href="/people" class="sw-c<?=($getOnline == "" ? " sw-act" : '' )?>">Все</a></div>
		<div class="sw-item"><a href="/people?fs&fon=1" class="sw-c<?=($getOnline == "fon=1&amp;" ? " sw-act" : '' )?>">Онлайн </a></div>
			
		 
	</div></div>
	

	
	
	<div class="app-search">
			<form method="post" action="">
			<table class="_row-table">
				<tbody>
					<tr>
						<td class="_row-table-main">
							<input type="text" class="app-search-input" value="<?=system::check($qSearch)?>" name="search" placeholder="Введите ник для поиска..">
						</td>
						<td class="_row-table-last">
							<input type="submit" name="s_cfms" class="main-btn" value="Найти">
						</td>
					</tr>
				</tbody>
			</table>
			</form>
		</div>
		<div class="list _lh-n _l-padd-n _gray">
			<a href="#" class="_fl-r app-toggler" style="border-bottom: 0px;" data-id="people-filter" data-class="app-toggler-open" onclick="App.toggle(this,event);return false;">Фильтр <span class="app-toggler-icon app-icons"></span></a>
			<div class="_l-padd _bold">Найдено <?=$total." ".(profile :: getPhrase($total, array('человек','человека','людей')))?></div>
		</div>
		<div class="list" id="people-filter" style="display: none;">
			<form method="post" action="">
				<div>
					<div class="_gray _bold">Пол</div>
					<select class="main-input _dbl _w-100" name="sex" id="sex">
						<option value="2">&nbsp;&nbsp;Любой&nbsp;&nbsp;</option>
						<option value="1"<?=( isset($filter['sex']) && $filter['sex'] == 1 ? " selected" : '' )?>>&nbsp;&nbsp;Мужской&nbsp;&nbsp;</option>
						<option value="0"<?=( isset($filter['sex']) && $filter['sex'] == 0 ? " selected" : '' )?>>&nbsp;&nbsp;Женский&nbsp;&nbsp;</option>
					</select>
				</div>
				<div>
					<div class="_gray _bold">Другое</div>
					<label class="_gray"><input type="checkbox" name="fonline"<?=(isset($filter['online']) ? "checked=\"checked\"" : '' )?> value="1"> Сейчас на сайте</label>
				</div>
				<div style="margin-top: 5px;">
					<input type="submit" name="filter" class="main-btn _w-100" value="Применить">
				</div>
			</form>
		</div>
	
		<?php
		//Вывод обитателей 
		
		while($r = $q -> fetch()) {
			++$i;
			
			?>
			<div class="list _l-padd-n">
            <div class="_l-padd">
                 <?	if($r['id'] != $user['id']) { ?>
                 <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">
                         
                    <a href="/mail?act=show&peer=<?=$r['id']?>">Написать</a> 
                            
                            <a href="/gifts?id=<?=$r['id']?>&amp;cat=1">Подарок</a>
                            </ul>
                            </div>
                        </div>
                        <?}?>
            <div class="_oh">
            <div class="list-flex">
           <div class="post-l post-l emotion-show" data-emotions="<?=system::textOut($r['emotions'])?>">
				 	<?=profile :: userAvatar($r,0)?>
				</div>
				
                    
              <div class="post-c">
                    <div class="_name">
                    <a href="<?=profile :: getLink($r)?>"><?=($qSearch != "" ? preg_replace('~' . preg_quote($qSearch) . '~iu', '<span class="highlight">$0</span>', profile :: getFullName($r, true)) : profile :: getFullName($r, true))." ".profile :: ver($r)." ".profile :: cake($r)?> 
                     <?php if($r['us_browser_new'] != "") {?>
                     <?=_getbrowser_mini($r['us_browser_new'],2)?>
                    <?}?>
                    </a>                    </div>
                
           
          
                     <span class="_gray _font-m">
                         
                         <?
                          $lMusici = DB::$pdo->queryFetch("SELECT * FROM `listen_music` WHERE `id_user` = '". $r['id'] ."' AND `time` > '".time()."' LIMIT 1");
            if($lMusici !== false) {
                ?>
               
                            <img src="/template/emoji/1f3b6_x1.png">
                        
                      <?= $lMusici['artist'] ?>
                               –
                                <?= $lMusici['title'] ?><br>
                     
                <?
                unset($lMusici);
            } else {

                ?>
                	<?php if($r['status'] != "") {?>
<span> <?=emoji:: Text($r['status'])?></span><br>
			
				<?}}?>

                         
                         
                         
                         
                         
                         
                         
                         
                         
                         
                         
                         
                         
                         
                         
                        </span>            
                     	<?php if($r['info_hometown'] != "") {?>
  						
  			<i class="fa fa-map-marker fa-0px"></i> <b><?=$r['info_hometown']?></b><br />
  				<?php }?>
  				 <?if ($user['id'] == 1){?>
             <span class="_gray _font-m"><i class="fa fa-money fa-0px"></i> Винетов:
           <?=system::textOut($r['line'])?>
           </span>
           <?}?>
                     </div>
            </div>
            </div>
                   
            </div>
                                 
                
                        </div>
			
			
			<?
			
			
				
		
		}
		if($i == 0) {
			?>
			<div class="list _center _font-s _gray"><div class="_l-padd">Никого не найдено</div></div>
			<?php
		}
		 
	if($total > 10) echo $pagination -> printPages($thisLink."?{$getSex}{$getOnline}{$getSearch}"); #Пагинация
	echo "</div>";
	require (CORE."foot.php");	