<?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.
 */
$data = array();
$k_post = DB::$pdo -> querySingle("SELECT COUNT(m.id) FROM `mail` AS m,
        (SELECT MAX(id) AS id FROM `mail` WHERE `unlink` != '$user[id]'  AND (`id_kont` = '$user[id]' OR `id_user` = '$user[id]') GROUP BY IF(`id_user` = '$user[id]', `id_kont`, `id_user`)) as max
        WHERE m.id = max.id AND (m.id_kont = '$user[id]' OR m.id_user = '$user[id]') AND m.unlink != '$user[id]'");
if ($k_post) {
    $k_page = ceil($k_post / 10);
    $page = 1;
    if (filter_has_var(INPUT_POST, 'page')) {
        $page = filter_input(INPUT_POST, 'page', FILTER_SANITIZE_NUMBER_INT);
    }

    if ($page < 1) {
        $page = 1;
    }
    if ($page > $k_page) {
        $page = $k_page;
    }
    $start = 10 * $page - 10;
    $i = 0;
    $q = DB::$pdo->query("SELECT *, IF(m.id_user = :user_id, m.id_kont, m.id_user) AS contact,
	 		SUM(IF (m.read = '0' AND m.id_user = :user_id, 1, 0)) AS outbox,
	 		(SELECT COUNT(*) FROM mail WHERE `id_kont` = :user_id and `id_user` = contact AND `read` = '0' AND `unlink` != :user_id) as inbox FROM `mail` AS m,
	        (SELECT MAX(id) AS id FROM `mail` WHERE `unlink` != :user_id  AND (`id_kont` = :user_id OR `id_user` = :user_id) GROUP BY IF(`id_user` = :user_id, `id_kont`, `id_user`)) as max
	        WHERE m.id = max.id AND (m.id_kont = :user_id OR m.id_user = :user_id) AND m.unlink != :user_id
	        GROUP BY contact ORDER BY m.time DESC, inbox DESC LIMIT $start, 10", array(':user_id' => $user['id']));
    while ($post = $q->fetch()) {
        $d = d_user($post['contact']);
        ob_start();
        ?>
        <a class="dialog_item<?= ($post['outbox'] >= 1 ? ' di_unread_outbox' : '') . ($post['inbox'] >= 1 ? ' di_unread_inbox' : '') ?>"
           href="/mail/?act=show&amp;peer=<?= $post['contact'] ?>" data-id="<?= $post['contact'] ?>">
            <div class="di_iwrap"><?= $d['avatar'] ?></div>
            <div class="di_cont">
                <span class="di_date"><?= system::times($post['time']) ?></span>
                <div class="di_head">
                    <span class="mi_author"><?= $d['online'] . $d['group'] . $d['login'] . $d['Medals'] ?></span><?= ($post['inbox'] >= 1 ? '<span class="di_unread_cnt"><span class="di_hide"> (+</span>' . $post['inbox'] . '<span class="di_hide">)</span></span>' : '') ?>
                </div>
                <div class="di_body">
                    <?php if($post['attachments']) {
                        ?><div class="mi_medias_plain">Фотография</div><?
                    } else if($post['gifts'] > 0) { ?>
                        <div class="mi_medias_plain">Подарок</div>
                    <?php }else if(preg_match('/^\[stickers=([0-9]{1,9}+)\]$/', $post['msg'], $m) === 1 && count($m) > 1) { ?>
                        <div class="mi_medias_plain">Стикер</div>
                    <?php } else { ?>
                        <?= ($post['msg'] ? '<div class="di_text">' . short_text(hideLink(system::BBcode(system::textOut($post['msg']))), 14) . '</div>' : '') ?>
                    <?php } ?>
                </div>
            </div>
        </a>
        <?
        $data['contact'][] = ob_get_contents();
        ob_end_clean();
    }
    if (!empty($data['contact'])) {
        $data['contact'] = array_reverse($data['contact']);
    }
}
setData($data);