<?php

/**
 * Dialog System
 * @version 2.2
 * @package DS
 * @copyright (c) Alexeev Vlad
 */
defined('COT_CODE') or die('Wrong URL');

require_once cot_incfile('ds', 'module');
require_once cot_incfile('extensions');

$title_params = array(
	'DS' => $L['ds_dialogs'],
);
$out['subtitle'] = cot_title('{DS}', $title_params);
$out['head'] .= $R['code_noindex'];

require_once $cfg['system_dir'] . '/header.php';

$dialogs = $db->query("SELECT * FROM $db_ds_dialog ORDER BY lastmsg DESC")->fetchAll();
$t = new XTemplate(cot_tplfile(array('ds', 'admin.list')));
foreach ($dialogs as $row)
{
  $msgfilter = "(dialog = '".$row['id']."')";
  $msg = $db->query("SELECT * FROM $db_ds_msg WHERE $msgfilter ORDER BY id DESC LIMIT 1")->fetch();   //получаем последнее сообщение из диалога

	$t->assign(array(
  	'DS_DELETE' => cot_url('admin', 'm=ds&act=chatdelete&chat='.$row['id']),
		'DS_DATE' => cot_date('datetime_medium', $msg['date']),
		'DS_DATE_STAMP' => $msg['date'],
    'DS_TIME_AGO' => cot_build_timeago($msg['date']),
		'DS_DIAOG_URL' => cot_url('admin', 'm=ds&act=show&chat='.$msg['dialog']),   //ссылка на диалог
    'DS_STATUS' => $msg['status'],  //прочитано или нет
		'DS_TEXT' => cot_string_truncate($msg['text'], 65, true, false, "..."),   //сокрашенный текст где 65 это колл-во символов после чего сокращяем
	));
  $t->assign(cot_generate_usertags(($msg['touser'] == $row['toid'] ? $row['toid'] : $row['fromid']), 'DS_TO_USER_'));
  $t->assign(cot_generate_usertags(($msg['touser'] == $row['toid'] ? $row['fromid'] : $row['toid']), 'DS_FROM_USER_'));

  if(($row['toid'] == 0 || $row['fromid'] == 0)) {
    if($row['toid'] == 0) {
      $t->assign(array(
        'DS_TO_USER_NICKNAME' => $cfg['ds']['supportinfo']['name'],
        'DS_TO_USER_NAME' => $cfg['ds']['supportinfo']['name'],
        'DS_TO_USER_AVATAR_SRC' => $cfg['ds']['supportinfo']['avatar'],
        'DS_TO_USER_AVATAR' => '<img src="'.$cfg['ds']['supportinfo']['avatar'].'" alt="" class="avatar">'
      ));
    }
    if($row['fromid'] == 0) {
      $t->assign(array(
        'DS_FROM_USER_NICKNAME' => $cfg['ds']['supportinfo']['name'],
        'DS_FROM_USER_NAME' => $cfg['ds']['supportinfo']['name'],
        'DS_FROM_USER_AVATAR_SRC' => $cfg['ds']['supportinfo']['avatar'],
        'DS_FROM_USER_AVATAR' => '<img src="'.$cfg['ds']['supportinfo']['avatar'].'" alt="" class="avatar">'
      ));
    }
  }

	$t->parse('MAIN.DS_ROW');
}

$t->parse('MAIN');
$adminmain = $t->text("MAIN");
