<?php
$t = "Форум";
$max 		= $VarIO->DefS("forum_category_per_page", 25);
$k_post 	= $db->Count("forum_cat","WHERE `sid` = '".$site->id."'");
$k_page 	= $core->k_page($k_post, $max);
$page 		= $core->page($k_page);
$start 		= $max * $page-$max;
$elements	= [];

$data = $db->Query("SELECT * FROM `forum_cat` WHERE `sid` = '".$site->id."' ORDER BY `cid` ASC LIMIT $start, $max"); 
while ($elem = mysqli_fetch_assoc($data)){
	$elements[$elem["cid"]] = [
		"id"		=>	$elem["cid"],
		"name"		=>	$elem["name"],
		"desc"		=>	$elem["txt"],
		"count"		=>	0,
		"lastItems"	=>	[
			"isExits"	=> false,
			"item"		=> []
		],
	];
	
	$elements[$elem["cid"]]["count"] = $db->Count("forum_topics","WHERE `sid` = '".$site->id."' AND `cid` = '".$elem["cid"]."'");
	$item = $db->getArray("SELECT * FROM `forum_topics` WHERE `sid` = '".$site->id."' AND `cid` = '".$elem["cid"]."' ORDER BY `tid` DESC LIMIT 1");
	if ($item != NULL){
		$elements[$elem["cid"]]["lastItems"]["isExits"] = true;
		$elements[$elem["cid"]]["lastItems"]["item"] = [
			"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"])
			],
			"user"			=>	getUserInfo($item["uid"]),
		];
	}
}

twig("/forum.lm", ["block"	=>	"list_category","elems" => $elements, "pages" => [
	"elemsCount"	=> 	$k_post,
	"api"			=>	$core->twig_page_mod($k_page, $page, ["msg","userID"]),
]]);

?>