<?php defined('ACCESS') OR die('No direct script access...');
/**
 * Author - koder_alex
 * ISQ - 669906617
 * VK - https://vk.com/koder_alex
 * It is forbidden to give, sell, modify.
 */
thead('Поиск по сообщениям');
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
	if(!empty($_POST['q']))
	{
		$_POST['q'] = implode('+', explode(' ', my_esc($_POST['q'])));
		redirect('/mail/?act=search&q='.$_POST['q'].'&type=1');
	}
	else
		redirect('/mail/');
}
$t = (isset($_GET['type']) && is_numeric($_GET['type']) && (int)$_GET['type'] <= 2? (int)$_GET['type'] : 0);
?>
<a class="new_item_link" href="/mail/?act=write">Написать сообщение</a>
<div class="head_panel"><div class="hp_block">
<form method="post" action="/mail/?act=search" class="qsearch">
<table class="row_table"><tr>
<td class="row_table_main_column"><div class="qs_field_wrap">
<button class="mail_search_button al_back" type="submit"></button>
<div class="iwrap"><input type="text" class="textfield qs_textfield" name="q" value="<?= $config['q']['text'] ?>" autocomplete="off" placeholder="Поиск"></div>
</div></td>
<td class="row_table_last_column">
<a class="button small_button star_button dialogs_filter_button_important" href="/mail?act=important"><?= mysql_result(mysql_query("SELECT COUNT(id) FROM mail_important WHERE `id_user` = '$user[id]' LIMIT 1"), 0) ?></a>
</td></tr></table>
</form></div></div>
<?
$k_post = mysql_result(mysql_query("SELECT COUNT(m.id) FROM `mail` AS m
        WHERE m.msg like '%".$config['q']['text']."%' AND (m.id_kont = '$user[id]' OR m.id_user = '$user[id]') AND m.unlink != '$user[id]'"), 0);
if($k_post)
{
	$k_page = k_page($k_post, $set['p_str']);
	$page = page($k_page);
	$start = $set['p_str'] * $page - $set['p_str'];
	$q = mysql_query("SELECT *, IF(m.id_user = '$user[id]', m.id_kont, m.id_user) AS contact,
		IF (m.read = '0' AND m.id_user = '$user[id]', 1, 0) AS outbox
		FROM `mail` AS m
		WHERE m.msg like '%".$config['q']['text']."%' AND (m.id_kont = '$user[id]' OR m.id_user = '$user[id]') AND m.unlink != '$user[id]'
		ORDER BY m.time DESC LIMIT $start, $set[p_str]");
	while ($post = mysql_fetch_assoc($q))
	{
		$d = d_user($post['contact']);
		?>
		<a class="dialog_item<?= ($post['outbox'] >= 1 ? ' di_unread_outbox' : '') ?>" href="/mail?act=msg&amp;id=<?= $post['id'] ?>">
		  <div class="di_iwrap"><?= $d['avatar'] ?></div>
		  <div class="di_cont">
		    <span class="di_date"><?= vremja($post['time']) ?></span>
		    <div class="di_head">
		      <span class="mi_author"><?= $d['group'] . $d['nick'] . $d['medal'] . $d['online'] ?></span>
		    </div>
		    <div class="di_body">
		      <?= ($post['msg'] ? '<div class="di_text">'. preg_replace('/'.$config['q']['text'].'/i', '<em class="found">$0</em>', text($post['msg'])) .'</div>' : '') ?>
		      <?
		      if($post['attachments'])
		      {
		      	$cnt_files = mysql_fetch_assoc(mysql_query("SELECT folder, COUNT(id) as cnt FROM mail_files_vk WHERE `action` = 'sent' AND `id_mail` = '".$post['id']."' LIMIT 1"));
			    echo '<div class="mi_medias_plain">';
			    if($cnt_files['cnt'] == 1)
			    	echo $config['folder'][$cnt_files['folder']];
			    else
					echo des2num($cnt_files['cnt'], array(' вложение', ' вложения', ' вложений'));
				echo '</div>';
			  }
			  ?>
		    </div>
		    <div class="di_folder"><?= ($post['id_user'] == $user['id'] ? '<span class="di_fld di_outbox_fld">Отправленные</span>' : '<span class="di_fld di_inbox_fld">Полученные</span>') ?></div>
		  </div>
		</a>
		<?
	}
	if ($k_page > 1)
		str_vk('/mail/?act=search&' . ($config['q']['text'] ?  'q='.$config['q']['text'].'&type=1&' : ''), $k_page, $page);
} else 
	show($config['q']['type'][$t], 'null');