<?php
if (isset($order) && $order == 'datetime') $order = 'id';

if ($sub_page == 1){
	print "
<div class='container'>
	<form action='".$_SERVER['REQUEST_URI']."' method='post' enctype='multipart/form-data'>
		<div class='form'>
			<div class='form-head toggle ";
	if (isset($_SESSION['backend']['toggle_filter']) && $_SESSION['backend']['toggle_filter']) print'toggled';
	print "'>
				<i class='material-icons md-24'>filter_alt</i>".LABEL_FILTER."<i class='ico-toggle material-icons md-24'>expand_less</i>
			</div>
			<div class='form-content ";
	if (isset($_SESSION['backend']['toggle_filter'])) print $_SESSION['backend']['toggle_filter'];
	print "' data-module='".$module."' data-backend-module='".$backend_module."'>
				<input type='hidden' name='csrf_token' value='".htmlspecialchars(csrf_token())."'>
				<input type='hidden' name='filter' value='1' />
				<input type='hidden' name='export' value='0' />
				<input type='hidden' name='order' value='".$order."' />
				<input type='hidden' name='lastorder' value='".$lastorder."' />
				<input type='hidden' name='orderdir' value='".$orderdir."' />";
				
	include('includes/function/c_active.php');
	include('includes/function/c_delete.php');
	
	include('includes/filter/sortorder.php');
	include('includes/filter/language.php');
	
	include('includes/filter/search.php');
					
	print "<hr/>";
	
	include('includes/filter/c_options.php');
	print "
				<div class='form-action'>
					<button type='submit' class='btn search' title='".LABEL_DO_FILTER."'><i class='material-icons md-24'>filter_alt</i>".LABEL_DO_FILTER."</button>
				</div><!-- /.form-action -->
				
			</div><!-- /.form-content -->
		</div><!-- /.form -->";
			
	$sql = "SELECT * FROM ".$module." WHERE label != 'labels' AND label NOT LIKE 'backend_%' ";
	
	if ($sortorder_from AND $sortorder_to){
		$sql .= "AND sortorder BETWEEN :sortorder_from AND :sortorder_to ";
	}
	
	if ($language) $sql .= "AND language = :language ";
	
	if (($c_active_1 AND $c_active_0) OR (!$c_active_1 AND !$c_active_0)){
	} else {
		$sql .= "AND ( ";
		if ($c_active_1 == 1) $sql .= "c_active = '1' ";
		if ($c_active_0 == 1) $sql .= "c_active = '0' ";
		$sql .= ") ";
	}
	
	if ($search){
		$sql.="AND (label LIKE :search) ";
	}
	
	$sql .= "ORDER BY ".$order." ".$orderdir.", id DESC";
	
	$result = $conn->prepare($sql);
	
	if ($sortorder_from AND $sortorder_to){
		$result->bindValue(':sortorder_from', $sortorder_from, PDO::PARAM_INT);
		$result->bindValue(':sortorder_to', $sortorder_to, PDO::PARAM_INT);
	}
	
	if ($language) $result->bindValue(':language', $language, PDO::PARAM_STR);
	
	if ($search){
		$result->bindValue(':search', "%{$search}%", PDO::PARAM_STR);
	}
	
	$result->execute();
		
	$count = $result->rowCount();
	
	$_SESSION['cmt'][$backend_module][$module]['id_list'] = array();
	$_SESSION['cmt'][$backend_module][$module]['pages'] = 1;
	if (isset($admin_settings['module'][$module.'_filter'])) $_SESSION['cmt'][$backend_module][$module]['pages'] = ceil($count / $admin_settings['module'][$module.'_filter']);

	print "
		<div class='button'>
			<div class='multicheck_control'>
				<a href='#' class='multi_activate' title='".LABEL_MULTI_ACTIVATE."'><i class='material-icons md-24'>toggle_on</i></a>
				<a href='#' class='multi_deactivate' title='".LABEL_MULTI_DEACTIVATE."'><i class='material-icons md-24 md-grey'>toggle_off</i></a>
				<a href='#' class='multi_delete' title='".LABEL_MULTI_DELETE."'><i class='material-icons md-24'>delete_forever</i></a>
			</div><!-- /.multicheck_control -->
			<a href='#' class='btn add' data-module='".$module."' data-backend-module='".$backend_module."'><i class='material-icons md-24'>add</i>".LABEL_NEW_ENTRY."</a>
		</div><!-- /.button -->
	
		<div class='table'>
			
			<div class='table-head'>
				<div class='table-row'>
					<div class='table-cell multiselect'>
						<input id='c_multicheck' class='c_multicheck' value='1' type='checkbox' name='c_multicheck'>
					</div><!-- /.table-cell -->
					<div class='table-cell'><a href='#' class='order' data-order='label'>".LABEL_LABEL;
	if ($order == 'label') print "<i class='ico-toggle material-icons md-24 md-white ".$orderdir."'>expand_less</i>";
	print "</a></div><!-- /.table-cell -->
					<div class='table-cell size-xsmall'><a href='#' class='order' data-order='sortorder'>".LABEL_SORTORDER;
	if ($order == 'sortorder') print "<i class='ico-toggle material-icons md-24 md-white ".$orderdir."'>expand_less</i>";
	print "</a></div><!-- /.table-cell -->
					<div class='table-cell size-xxsmall'><a href='#' class='order' data-order='c_active'><i class='material-icons md-24 md-white'>toggle_on</i>";
	if ($order == 'c_active') print "<i class='ico-toggle material-icons md-24 md-white ".$orderdir."'>expand_less</i>";
	print "</a></div><!-- /.table-cell -->
					<div class='table-cell toolbar'></div><!-- /.table-cell -->
				</div><!-- /.table-row -->
			</div><!-- /.table-head -->";
			
	if ($count){
		print "
			<div class='table-body ";
		if (!$_SESSION['backend']['settings']['context']) print "no-context";
		print "'>";
		while($arr = $result->fetch()){
			$_SESSION['cmt'][$backend_module][$module]['id_list'][$arr['id']] = $arr['id'];
		}
	}
}

if (isset($_SESSION['cmt'][$backend_module][$module]['id_list']) && count($_SESSION['cmt'][$backend_module][$module]['id_list']) >= 1){
	
	$q_array = $_SESSION['cmt'][$backend_module][$module]['id_list'];
	if (isset($admin_settings['module'][$module.'_filter']) && count($_SESSION['cmt'][$backend_module][$module]['id_list']) > $admin_settings['module'][$module.'_filter']) {
		$q_array = array_slice($_SESSION['cmt'][$backend_module][$module]['id_list'], 0, $admin_settings['module'][$module.'_filter'], true);
	}
	
	$q_array = join(",", $q_array);
	
	$sql = "SELECT * FROM ".$module." AS module WHERE id IN ($q_array) ORDER BY FIELD (id, $q_array) ";
	if (isset($admin_settings['module'][$module.'_filter'])) $sql.= "LIMIT :limit ";
	$result = $conn->prepare($sql);
	if (isset($admin_settings['module'][$module.'_filter'])) $result->bindValue(':limit', $admin_settings['module'][$module.'_filter'], PDO::PARAM_INT);
	$result->execute();
	
	if ($result->rowCount()){
		while ($arr = $result->fetch()){
			unset($_SESSION['cmt'][$backend_module][$module]['id_list'][$arr['id']]);
			
			print "
				<div class='table-row ";
			if (!$arr['c_active']) print "inactive";
			print "' data-id='".$arr['id']."' data-module='".$module."' data-backend-module='".$backend_module."'>
					<div class='table-cell multiselect'>
						<input id='c_check_".$arr['id']."' class='multicheck c_check_".$arr['id']."' value='1' type='checkbox' name='c_check[".$arr['id']."]'>
					</div><!-- /.table-cell -->
					<div class='table-cell'>".$arr['label']."</div><!-- /.table-cell -->
					<div class='table-cell alignright'>".$arr['sortorder']."</div><!-- /.table-cell -->
					<div class='table-cell aligncenter c_active'>
						<a href='#' id='".$arr['id']."' class='activate' title='";
			if ($arr['c_active'] == 1) print LABEL_DEACTIVATE;
			else print LABEL_ACTIVATE;
			print "'>";
			if ($arr['c_active'] == 1) print "<i class='material-icons md-24'>toggle_on</i>";
			else print "<i class='material-icons md-24 md-grey'>toggle_off</i>";
			print "</a></div><!-- /.table-cell -->
					<div class='table-cell toolbar'>
						<a href='#' class='edit' data-id='".$arr['id']."' data-module='".$module."' data-backend-module='".$backend_module."' title='".LABEL_EDIT."'><i class='material-icons md-22'>edit</i></a>
						<a href='#' class='copy' data-id='".$arr['id']."' data-module='".$module."' data-backend-module='".$backend_module."' title='".LABEL_COPY."'><i class='material-icons md-22'>content_copy</i></a>
						<a href='#' class='delete' data-id='".$arr['id']."' title='".LABEL_DELETE."'><i class='material-icons md-22'>delete_forever</i></a>
					</div><!-- /.table-cell -->
				</div><!-- /.table-row -->";
		}
		
		if (ceil($_SESSION['cmt'][$backend_module][$module]['pages']) != $sub_page){
			$next_page = $sub_page+1;
			print "<input type='hidden' name='sub_page' id='sub_page' value='".$next_page."' />";
		}
		
		if ($sub_page == 1){
			print "
			</div><!-- /.table-body -->
		</div><!-- /.table -->";
		}
	}
}

if ($sub_page == 1){
	print "
	</form>
</div><!-- /.container -->
<div class='context-menu'>
 <ul>
    <li><a href='#' class='edit' title='".LABEL_EDIT."'><i class='material-icons md-24'>edit</i>".LABEL_EDIT."</a></li>
    <li><a href='#' class='copy' title='".LABEL_COPY."'><i class='material-icons md-24'>content_copy</i>".LABEL_COPY."</a></li>
    <li><a href='#' class='delete' title='".LABEL_DELETE."'><i class='material-icons md-24'>delete_forever</i>".LABEL_DELETE."</a></li>
 </ul>
</div>";
}