<?php
if ($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/filter/period.php');
	
	include('includes/filter/search.php');
	
	include('function/encrypt.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 -->";
			
	$datetime_from = DateTime::createFromFormat('Y-m-d', $datetime_from)->setTime(0, 0, 0);
	$datetime_to = DateTime::createFromFormat('Y-m-d', $datetime_to)->setTime(23, 59, 59);
	$datetime_from = $datetime_from->getTimestamp();
	$datetime_to = $datetime_to->getTimestamp();
	
				
	$sql = "SELECT * FROM ".$module." AS module ";
	
	if ($datetime_to){
		$sql .= "WHERE datetime BETWEEN :datetime_from AND :datetime_to ";
	} else {
		$sql .= "WHERE datetime >= :datetime_from ";
	}
	
	if ($search){
		$sql.="AND (
						module LIKE :search OR table_name LIKE :search OR 
						field_name LIKE :search OR message LIKE :search OR 
						datetime LIKE :search) ";
	}
	
	$sql .= "GROUP BY id ";
	
	if ($order == 'datetime'){
		$sql .= "ORDER BY ".$order." ".$orderdir.", id DESC";
	} else {
		$sql .= "ORDER BY ".$order." ".$orderdir.", datetime DESC";
	}
	
	$result = $conn->prepare($sql);
	if ($datetime_to){
		$result->bindValue(':datetime_from', $datetime_from, PDO::PARAM_INT);
		$result->bindValue(':datetime_to', $datetime_to, PDO::PARAM_INT);
	} else {
		$result->bindValue(':datetime_from', $datetime_from, PDO::PARAM_INT);
	}	
	
	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='table'>
			
			<div class='table-head'>
				<div class='table-row'>
					<div class='table-cell size-small'><a href='#' class='order' data-order='datetime'>".LABEL_DATETIME;
	if ($order == 'datetime') 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-small'><a href='#' class='order' data-order='module'>".LABEL_MODULE;
	if ($order == 'module') 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-small'><a href='#' class='order' data-order='table_name'>".LABEL_TABLE_NAME;
	if ($order == 'table_name') 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-small'><a href='#' class='order' data-order='field_name'>".LABEL_FIELD_NAME;
	if ($order == 'field_name') 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-small'><a href='#' class='order' data-order='action'>".LABEL_ACTION;
	if ($order == 'action') print "<i class='ico-toggle material-icons md-24 md-white ".$orderdir."'>expand_less</i>";
	print "</a></div><!-- /.table-cell -->
					<div class='table-cell'><a href='#' class='order' data-order='status'>".LABEL_STATUS;
	if ($order == 'status') 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='c_plain'>".LABEL_PLAIN;
	if ($order == 'c_plain') print "<i class='ico-toggle material-icons md-24 md-white ".$orderdir."'>expand_less</i>";
	print "</a></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 module.* FROM ".$module." AS module 
					WHERE module.id IN ($q_array) GROUP BY module.id ORDER BY FIELD (module.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']]);
			$datetime = date("d.m.Y - H:i", $arr['datetime']);
			
			$status = $arr['status'];
			if ($arr['status'] == 'pending'){
				if (check_column($arr['table_name'], $arr['field_name']."_hash")){
					$sql_count = "SELECT * FROM ".$arr['table_name'];
					$result_count = $conn->prepare($sql_count);
					$result_count->execute();
					$total = $result_count->rowCount();
					if ($total > 0){
						if ($arr['action'] === 'encrypt') {
							$sql_done = "SELECT id FROM ".$arr['table_name']." WHERE ".$arr['field_name']."_hash IS NOT NULL AND ".$arr['field_name']."_hash != ''";
							$result_done = $conn->prepare($sql_done);
							$result_done->execute();
							$done = $result_done->rowCount();
							$status .= " ".$done."/".$total;
						} else if ($arr['action'] === 'decrypt') {
							$sql_done = "SELECT id FROM ".$arr['table_name']." WHERE ".$arr['field_name']."_hash IS NULL AND ".$arr['field_name']."_hash = ''";
							$result_done = $conn->prepare($sql_done);
							$result_done->execute();
							$done = $result_done->rowCount();
							$status .= " ".$done."/".$total;
						}
					}
				}
			}
				
			print "
				<div class='table-row'>
					<div class='table-cell'>".$datetime."</div><!-- /.table-cell -->
					<div class='table-cell'>".$arr['module']."</div><!-- /.table-cell -->
					<div class='table-cell'>".$arr['table_name']."</div><!-- /.table-cell -->
					<div class='table-cell'>".$arr['field_name']."</div><!-- /.table-cell -->
					<div class='table-cell'>".$arr['action']."</div><!-- /.table-cell -->
					<div class='table-cell'>".$status."</div><!-- /.table-cell -->";
			print "
					<div class='table-cell aligncenter c_plain'>";
			if ($arr['c_plain'] == 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><!-- /.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>";
}