<?

$type = (isset($_GET['t']) ? $_GET['t'] : 'all');

$sqltype = " `act` != 0";
if ($type == 'all') {
	$sqltype = "`act` != 0";
}
if ($type == 'users') {
	$sqltype = "`act` = '3'";
}
if ($type == 'trash') {
	$sqltype = "`act` = '2'";
}
if ($type == 'queue') {
	$sqltype = "`act` = '0'";
}
if ($type == 'admins') {
	$sqltype = "`act` = '5'";
}

if ($type == 'freeze') {
	$sqltype = "`act` = '4'";
}
if ($type == 'new') {
	$sqltype = " `date_create` > '". mktime(0, 0, 0) ."'";
}

if ($type != 'trash') {
	$sqltype = $sqltype.' AND `act` != 2';
}
if ($user['id_level'] < 2 AND $type == 'trash') {
	Redirection('/apanel');
}
$newsType = [
	'all' 		=> 'Все новости',
	'queue' 	=> 'В очереди на публикацию',
	'users' 	=> 'Новости от пользователей',
	'pub' 		=> 'Все опубликованные',
	'freeze'	=> 'В заморозке',
	'trash' 	=> 'На удаление (корзина)',	
	'new' 		=> 'Новые за сутки',
	'admins' 	=> 'Новости от администрации',	
];

$BreadCrumbs = [
	'Админ панель' 	 => '/apanel/',
	'новости' 		 => '/apanel/news-type/',
	$newsType[$type] => '/apanel/news-type/?t='. $type
];

$PageConfig = [	
	'title' 		=> 'Новости - '. $newsType[$type],
	'NameKeyPage' 	=> $urlPage ,//urlPage urlType
	//'BreadCrumbs' 	=> $BreadCrumbs,
];
PageConfig($PageConfig);	

$count_max = 9;
$count = $sql->rowCount("SELECT * FROM `news` WHERE {$sqltype} ");

$page_num = (!empty($_GET['page']) ? (int) $_GET['page'] : 1); 
$page = page('?t='. $type .'&page=',  $page_num , $count , $count_max);
$page['count'] = $count;
$page['page_num'] = $page_num;
$page['count_max'] = $count_max;

if (isset($_GET['GetSort'], $_GET['GetSortType'])) {
	$GetSort = $_GET['GetSort'];
	$GetSortType = $_GET['GetSortType'];	
	
	if ($GetSortType == 1) {
		$sortid = $sql->fetch("SELECT `sort` FROM `news` ORDER BY `sort` DESC LIMIT 1");
		$sortid = ($sortid['sort'] + 1);
		$pages = '1';
	}
	if ($GetSortType == 0) {
		$sortid = $sql->fetch("SELECT `sort` FROM `news` ORDER BY `sort` ASC LIMIT 1");
		$sortid = ($sortid['sort'] - 1);
		$pages = 'end';
	}
	$sortid = (int) $sortid;
	$sql->query("UPDATE `news` SET `sort` = ? WHERE `link` = ?", [$sortid, $GetSort]);
	CacheDelete::news($GetSort);
	Redirection('?t='. $type .'&page='. $pages, 'Успешно');
}
$news = [];
$query = $sql->fetchAll("SELECT * FROM `news` WHERE {$sqltype} ORDER BY `sort` DESC LIMIT {$page['start']} , {$page['num']};");
foreach ($query AS $p) {
	if (FileSystem::TextStrlen($p['title']) > 79) {
		$p['title'] = FileSystem::TextCut($p['title'] , 80);
	}
	$p['text'] = output($p['text']);
	$p['ToggleBottom'] = false;
	$news[] = $p;
}

$t->assign('page', $page);
$t->assign('news', $news);
echo $t->fetch('news.razd');
