<?php
if ($order == 'datetime') $order = 'label';
$module_title = "ID";

if ($sub_page == 1){
	print "
<div class='container'>
	<ul class='tabs'>
		<li class='current'><a href='#' title='".ucwords(str_replace('_', ' ', str_replace('id_', ' ', $module)))."'><i class='material-icons md-24'>edit</i>".ucwords(str_replace('_', ' ', str_replace('id_', ' ', $module)))."</a></li>
	</ul>
	
	<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='order' value='".$order."' />
				<input type='hidden' name='lastorder' value='".$lastorder."' />
				<input type='hidden' name='orderdir' value='".$orderdir."' />";
	include('includes/function/c_active.php');
	include('function/c_delete_language.php');
	
	include('includes/filter/search.php');
					
	print "<hr/>
				<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 id IS NOT NULL ";
	
	if ($search){
		$sql.="AND label LIKE :search ";
	}
	
	if ($order == 'label'){
		$sql.="ORDER BY ".$order." ".$orderdir.", id DESC";
	} else {
		$sql.="ORDER BY ".$order." ".$orderdir.", label DESC";
	}
	
	$result = $conn->prepare($sql);
	
	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'>
			<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'><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='iso'>".LABEL_ISO;
	if ($order == 'iso') 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'>".$arr['label']."</div><!-- /.table-cell -->
					<div class='table-cell'>".$arr['iso']."</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>";
}