<?php

$cat 		= $db->getObject("SELECT * FROM `forum_cat` WHERE `sid` = '".$site->id."' AND `cid` = '".$id."'");
if ($cat == NULL){
	twig("/forum.lm", ["block"	=>	"no_found_category"]);
}
$t 			= "Форум - ".$cat->name;
$max 		= $VarIO->DefS("forum_items_per_page", 25);
$k_post 	= $db->Count("forum_topics","WHERE `sid` = '".$site->id."' AND `cid` = '".$id."'");
$k_page 	= $core->k_page($k_post,$max);
$page 		= $core->page($k_page);
$start 		= $max*$page-$max;
$elements	= [];
$data = $db->Query("SELECT *, (SELECT COUNT(*) FROM `forum_message` WHERE `forum_topics`.`tid` = `forum_message`.`tid`) AS `ccc` FROM `forum_topics` WHERE `sid` = '".$site->id."' AND `cid` = '".$id."' ORDER BY `lastUpdate` DESC LIMIT $start, $max");
#echo ellynet("SELECT *, (SELECT COUNT(*) FROM `forum_message` WHERE `forum_topics`.`tid` = `forum_message`.`tid`) AS `ccc` FROM `forum_topics` WHERE `sid` = '".$site->id."' AND `cid` = '".$id."' ORDER BY `lastUpdate` DESC LIMIT $start, $max"); 
while ($item = mysqli_fetch_assoc($data)){
	$elements[$item["tid"]] = [
		"id"			=>	$item["tid"],
		"name"			=>	$item["name"],
		"message"		=>	$core->drawString($item["sms"]),
		"timeAdd"		=>	$item["timeadd"],
		"timeEdit"		=>	$item["lastUpdate"],
		"isClosed"		=>	$item["closed"],
		"closeInfo"		=>	[
			"time"			=>	$item["close_time"],
			"message"		=>	$core->drawString($item["close_txt"]),
			"user"			=>	getUserInfo($item["close_who"])
		],
		"lastItems"	=>	[
			"isExits"	=> true,
			"item"		=> [
				"timeAdd"		=>	$item["timeadd"],
				"message"		=>	$core->drawString($item["sms"]),
				"user"			=>	getUserInfo($item["uid"]),
			]
		],
		"user"			=>	getUserInfo($item["uid"]),
	];

	$last = $db->getArray("SELECT * FROM `forum_message` WHERE `tid` = '".$item["tid"]."' ORDER BY `mid` DESC LIMIT 1");
	if ($last != NULL){
		$elements[$item["tid"]]["lastItems"]["item"]  = [
			"timeAdd"		=>	$last["timeadd"],
			"message"		=>	$core->drawString($last["sms"]),
			"user"			=>	getUserInfo($last["uid"]),
		];
	} 
}
twig("/forum.lm", ["block"	=>	"list_topics", "elems" => $elements, "category" => [
	"id"		=>	$cat->cid,
	"name"		=>	$cat->name,
	"desc"		=>	$core->drawString($cat->txt),
	"count"		=>	$db->Count("forum_topics","WHERE `sid` = '".$site->id."' AND `cid` = '".$cat->cid."'"),
	"lastItems"	=>	[
		"isExits"	=> false,
		"item"		=> []
	],
], "pages" => [
	"elemsCount"	=> 	$k_post,
	"api"			=>	$core->twig_page_mod($k_page, $page, ["msg","userID"]),
]]);
?>