<?
if (isset($_GET['q'])) {
	if (strlen2($_GET['q']) < 2) {
		header('Location: ?func=search');
	}
  // Искомое
  $search = urldecode($_GET['q']);
  
  // По юзерам 
  $act_users = " `telegram` like '%%" . $sql->esc($search) . "%%' OR `vkcom` like '%%" . $sql->esc($search) . "%%' OR `login` like '%%" . $sql->esc($search) . "%%' OR `ip` like '" . ip2long($search) . "' OR `city` like '%%" . $sql->esc($search) . "%%' OR `wmid` like '%%" . $sql->esc($search) . "%%' OR `purse` like '%%" . $sql->esc($search) . "%%' OR `id` like '%%" . $sql->esc($search) . "%%'";
   
  // По товарам
  $act_goods = " (`name` like '%%" . $sql->esc($search) . "%%' OR `msg` like '%%" . $sql->esc($search) . "%%') AND unlink = 0";
 
  $count = $sql -> fetch("
  SELECT COUNT(*) AS goods,
  (SELECT COUNT(*) FROM `users` WHERE $act_users) AS 'users'
  FROM `shop_goods` WHERE $act_goods");
}

function text_search($str) {
  global $search;
  
  $str = str_ireplace($search, '<span style="color: red;">' . $search . '</span>', $str);
  return $str;
}

$system['title'] = 'Поиск по сайту';
require SYS . 'header.php';

if (isset($search) && !isset($_GET['sort'])) {
?>
<a class="div_1" href="/?func=search&amp;q=<?= text($search)?>&amp;sort=users"><i class="icon-user"></i> Пользователи (<?= $count['users']?>)</a>

<a class="div_1" href="/?func=search&amp;q=<?= text($search)?>&amp;sort=shop"><i class="icon-shopping-cart"></i> Товары (<?= $count['goods']?>)</a>



<a class="div_2" href="/?func=search"><i class="icon-search"></i> Новый поиск</a>

<?
} elseif (isset($search) && isset($_GET['sort']) && $_GET['sort'] == 'shop') {
  
  $k_post = $sql -> selectCount("SELECT COUNT(*) FROM `shop_goods` WHERE $act_goods AND `active` != '2' AND `unlink` = '0' ");
  $k_page = k_page($k_post, $system['page_str']);
  $page = page($k_page);
  $start =  $system['page_str'] * $page -  $system['page_str'];

  if ($k_post == 0) {
?>
    <div class="alert" style="margin: 2px;">Список товаров пуст</div>
<?
  } else {
    $array = $sql -> select("SELECT * FROM `shop_goods` WHERE $act_goods AND `active` != '2' AND `unlink` = '0' LIMIT $start, $system[page_str]");
    
    foreach ($array AS $post) {
      if ($post['screens']) {
        $screen = unserialize($post['screens']);
        if (is_file(SRC . 'screen/shop/' . $screen[0] . '.png')) {
          $_src = '/_src/img.php?link=' . base64_encode(SRC.'screen/shop/' . $screen[0] . '.png') . '&type=1';
        }
      } else {
        $_src = '/_src/img.php?link=' . base64_encode(ROOT.'_images/images/no_image.png') . '&type=1';
      }
?>
      <div class="media <?= ($num % 2 ? "nav1" : "nav2")?>">
        <img src="<?= $_src?>" class="img-polaroid pull-left" /> 
        <div class="media-body"><a href="/?func=shop.goods&amp;id=<?= $post['id']?>" class="media-heading"><?= text_search(text($post['name']))?></a> <br />
        <span class="on"><b><?= $post['wmr']?></b> wmr</span><br />
        <?= text_search(text_out($post['msg']))?>
        </div>
      </div>
<?
    }
    
    if ($k_page > 1) { 
      str('/?func=' . $func . '&amp;q=' . text($search) . '&amp;sort=shop&amp;', $k_page, $page);
    }
  }
?>
<div class="foot">
<a href="/?func=search&amp;q=<?= text($search)?>"><i class="icon-search"></i> К результатам</a>
</div>
<?
} elseif (isset($search) && isset($_GET['sort']) && $_GET['sort'] == 'users') {
  $k_post = $sql -> selectCount("SELECT COUNT(*) FROM `users` WHERE $act_users");

  if ($k_post == 0) {
    ?>
    <div class="alert" style="margin: 2px;">Список пользователей пуст</div>
    <?
  } else {
    $k_page = k_page($k_post, $system['page_str']);
    $page = page($k_page);
    $start =  $system['page_str'] * $page -  $system['page_str'];

    $array = $sql -> select("SELECT * FROM `users` WHERE $act_users LIMIT $start, $system[page_str]");

    foreach ($array AS $anketa) {
      $num++;
      ?>
      <div class="<?= ($num % 2 ? "nav1" : "nav2")?>">
        <?= login($anketa['id'], 1)?> <br />
        <span class="ank_n">IP:</span> <span class="ank_d"><?= long2ip($anketa['ip'])?></span><br />
        <span class="ank_n">Регистрация:</span> <span class="ank_d"><?= vremja($anketa['date_reg'])?></span><br />
        <span class="ank_n">Последняя активность:</span> <span class="ank_d"><?= vremja($anketa['date_last'])?></span><br />
        <span class="ank_n">Рейтинг:</span> <span class="ank_d">(<?= $anketa['rating']/100?>)</span><br />
        <a href="?func=shop.seller&amp;id=<?= $anketa['id']?>"><i class="icon-shopping-cart"></i> Товары в магазине</a> (<?= $sql -> selectCount("SELECT COUNT(*) FROM `shop_goods` WHERE `id_user` = '$anketa[id]' AND `active` = '1' AND `unlink` = '0'")?>)<br />
        <? if ($level > $anketa['level']) { ?>
        <a href="?func=user.profile&amp;id=<?= $anketa['id']?>&amp;action"><i class="icon-hand-right"></i> Действие над <?= login($anketa['id'], 0)?></a><br />
        <? } ?>
      </div>
      <?
    }

    if ($k_page > 1) { 
      str('/?func=' . $func . '&amp;q=' . text($search) . '&amp;sort=users&amp;', $k_page, $page);
    }
  }
?>
<div class="foot">
<a href="/?func=search&amp;q=<?= text($search)?>"><i class="icon-search"></i> К результатам</a>
</div>
<?
} else {
?>
<form class="form-search form_t" action="/?" method="GET">
  <input type="hidden"  name="func" value="search" />
  <input type="text" class="input-medium search-query" name="q" />
  <button type="submit" class="btn">Поиск</button>
</form>
<?
}