<?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();
if (isset($_POST['peer']) && is_numeric($_POST['peer']) && DB::$pdo->querySingle("SELECT COUNT(id) FROM `users` WHERE `id` = ?", array((int)$_POST['peer'])))
	$peer = d_user((int) $_POST['peer'], false);
else
	$peer = d_user(0, false);
    $k_post = DB::$pdo->querySingle("SELECT COUNT(*) FROM mail WHERE `unlink` != '$user[id]' AND (`id_kont` = '$user[id]' AND `id_user` = '" . $peer['id'] . "' OR `id_kont` = '" . $peer['id'] . "' AND `id_user` = '$user[id]')");
    $data['message'] = array();
    if($k_post > 0) {

        $k_page = ceil($k_post / 10);
        $page = pagePostData($k_page);
        $start = 10 * $page - 10;
        $data['start'] = $start;
        $data['page'] = $page;
        $data['k_page'] = $k_page;
        $data['k_post'] = $k_post;
        $q = DB::$pdo->query("SELECT * FROM mail WHERE `unlink` != '$user[id]' AND (`id_kont` = '" . $peer['id'] . "' AND `id_user` = '$user[id]' OR `id_kont` = '$user[id]' AND `id_user` = '" .$peer['id'] . "') ORDER BY `id` DESC LIMIT $start, 10");
        $qF = DB::$pdo->prepare("SELECT id,folder, name, tmp_name, extension, type FROM mail_files_vk WHERE `action` = 'sent' AND `id_mail` = ?");
        while ($post = $q->fetch())
        {
            ob_start();
            $d = d_user($post['id_user'], true, 'mi_img');
            ?><div class="msg_item<?= ($post['read'] == 0 ? ' mi_unread' : '') . ($user['id'] == $d['id'] ? ' message_outbox' : ' message_inbox') ?>">
                <div class="mi_iwrap"><a href="/id<?= $d['id'] ?>"><?= $d['avatar'] ?></a></div>
                      <div class="mi_cont">
                   <div class="mi_head">
                   <span class="mi_date"><?= system::times($post['time']) ?></span>
                   <a class="mi_author" href="/id<?= $d['id'] ?>"><?=$d['online']. $d['group'] . $d['login'] . $d['Medals']  ?></a>
                </div>
             <div class="mi_body">
                 <?php if($post['attachments']) {
                     $qF->execute([$post['id']]);
                     ?>
                     <div class="cp_attached_wrap">
                         <div class="pi_medias">
                             <?php while($file = $qF->fetch()) { ?>
                                 <a class="medias_thumb thumb_item mr_x_wrap" href="/mail/file/<?= $file['folder'] ?>/<?= $file['tmp_name'] ?>" target="_blank">
                                     <img class="ph_img" src="/mail/file/<?= $file['folder'] ?>/<?= $file['tmp_name'] ?>"></a>
                             <?php } $qF->closeCursor();?>
                         </div>
                     </div><div class="mi_text">
                         <?= system::BBcode(system::textOut($post['msg'])) ?>
                     </div>

                 <?php } else if($post['gifts'] > 0) {
                     $gifts = DB :: $pdo -> queryFetch("SELECT * FROM `user_gifts` WHERE `id` = '".$post['gifts']."' LIMIT 1");
                     ?><div class="mi_text"><div class="mi_medias_plain">
                     <?= ($user['id'] == $d['id'] ? "Вы отправили" : "Вам прислали") ?> подарок
                     </div>
                     </div>
                     <a href="/gifts?id=<?= $user['id'] ?>">
                         <img src="<?=(FILES_SERVER."gifts/{$gifts['id_gift']}.jpg")?>">
                     </a>
                     <div class="mi_text"><?= $gifts['komment'] ?></div>
                 <?php } else { ?>
                     <div class="mi_text">
                         <?= system::BBcode(system::textOut($post['msg'])) ?>
                     </div>
                 <?php } ?>
                    <span style="float: right;margin-top: -10px;text-decoration: underline;">
                        <a href="/mail/?act=delete&amp;id=<?= $post['id'] ?>"><span class="ico ico-dell"></span></a>
                    </span> </div></div></div><?
            $data['message'][] = ob_get_contents();
            ob_end_clean();
        }  
        if(!empty($data['message']) && $k_page > $page) {
            $data['message'][] = '<div class="show_more_wrap"><a class="show_more" id="show_more" onclick="mail.loadMess(\''.$peer['id'].'\')">Показать ещё сообщения</a></div>';
        }
    }
setData($data);