<?php
$file_dir = "../".$module."/original/";

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='multi_update' value='0' />
				<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."' />
				<input type='hidden' name='c_grid' value='".$c_grid."' />";
				
	if (isset($c_levels) && $c_levels){
		if (isset($_POST['parent_guidv4'])) $parent_guidv4 = addslashes(trim(strip_tags(stripslashes($_POST['parent_guidv4']))));
		else {
			if (isset($_GET['parent_guidv4'])) $parent_guidv4 = addslashes(trim(strip_tags(stripslashes($_GET['parent_guidv4']))));
			else $parent_guidv4 = NULL;
		}
		if (isset($parent_guidv4) && $parent_guidv4){
			print "<input type='hidden' name='parent_guidv4' class='parent_guidv4' id='parent_guidv4' value='".$parent_guidv4."' />";
		}
	}
					
	include('function/c_delete.php');
	
	
	if (isset($c_period) && $c_period) include('includes/filter/period.php');
	include('includes/filter/language.php');
	
	
	include('includes/filter/search.php');
					
	print "<hr/>

				<input type='file' class='field hidden multiupload' id='multiupload' name='upload[]' multiple>
				<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 -->";
			
	if (isset($c_period) && $c_period) {
		$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();
	}
	
	$level_db = $module."_levels";
	if (isset($level_module) && $level_module){
		$level_db = $level_module."_levels";
	}
	
	$sql = "SELECT module.*, GROUP_CONCAT(levels.guidv4) AS level FROM ".$module." AS module 
					LEFT JOIN ".$module."_levels_links AS levels ON module.id = levels.id_module ";
	
	if (isset($c_period) && $c_period) {
		if ($datetime_to){
			$sql .= "WHERE datetime BETWEEN :datetime_from AND :datetime_to ";
		} else {
			$sql .= "WHERE datetime >= :datetime_from ";
		}
	} else $sql .= "WHERE 1 ";
	
	if (isset($c_levels) && $c_levels && $parent_guidv4){
		$sql .= "AND levels.guidv4 = :parent_guidv4 ";
	} else {
		$sql .= "AND levels.guidv4 IS NULL ";
	}
				
	if ($language) $sql .= "AND language = :language ";
	
	if ($search){
		$sql.="AND (
						file LIKE :search OR text LIKE :search OR source LIKE :search OR copyright LIKE :search OR 
						datetime LIKE :search) ";
	}
	
	$sql .= "GROUP BY module.id ";
	
	if ($order == 'datetime'){
		$sql .= "ORDER BY ".$order." ".$orderdir.", id DESC";
	} else {
		$sql .= "ORDER BY ".$order." ".$orderdir;
	}
	
	$result = $conn->prepare($sql);
	
	if (isset($c_period) && $c_period) {
		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 (isset($c_levels) && $c_levels && $parent_guidv4){
		 $result->bindValue(':parent_guidv4', $parent_guidv4, PDO::PARAM_STR);
	}
	
	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_delete' title='".LABEL_MULTI_DELETE."'><i class='material-icons md-24'>delete_forever</i></a>
				<a href='#' class='multi_edit' title='".LABEL_MULTI_EDIT."' data-module='".$module."' data-backend-module='".$backend_module."'><i class='material-icons md-24'>edit</i></a>
			</div><!-- /.multicheck_control -->
			<a href='#' class='btn grid ";
	if ($c_grid) print 'active';
	print "'><i class='material-icons md-24'>grid_view</i>".LABEL_GRID."</a>
			<a href='#' class='btn list ";
	if (!$c_grid) print 'active';
	print "'><i class='material-icons md-24'>view_list</i>".LABEL_LIST."</a>
			<a href='#' class='btn add' data-module='".$module."_levels' data-backend-module='".$backend_module."'><i class='material-icons md-24'>folder</i>".LABEL_NEW_FOLDER."</a>
			<a href='#' class='btn multiupload-trigger'><i class='material-icons md-24'>upload_file</i>".LABEL_UPLOAD."</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 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'><a href='#' class='order' data-order='file'>".LABEL_FILE;
	if ($order == 'file') 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-medium'><a href='#' class='order' data-order='filesize'>".LABEL_FILESIZE;
	if ($order == 'filesize') print "<i class='ico-toggle material-icons md-24 md-white ".$orderdir."'>expand_less</i>";
	print "</a></div><!-- /.table-cell -->";

	if (isset($c_languages) && $c_languages) {
		print "<div class='table-cell size-small'><a href='#' class='order' data-order='language'>".LABEL_LANGUAGE;
		if ($order == 'language') print "<i class='ico-toggle material-icons md-24 md-white ".$orderdir."'>expand_less</i>";
		print "</a></div><!-- /.table-cell -->";
	}
	print "
					<div class='table-cell size-xxsmall'><a href='#'><i class='material-icons md-24 md-white'>check_circle</i></a></div><!-- /.table-cell -->";
	
	if (!$c_grid){
		print "
					<div class='table-cell toolbar size-2'></div><!-- /.table-cell -->";
	}
	print "
				</div><!-- /.table-row -->
			</div><!-- /.table-head -->";

	if ($c_grid){
		print "
		</div><!-- /.table -->
		<div class='table-grid ";
		if (!$_SESSION['backend']['settings']['context']) print "no-context";
		print "'>";
		while($arr = $result->fetch()){
			$_SESSION['cmt'][$backend_module][$module]['id_list'][$arr['id']] = $arr['id'];
		}
	} else {
		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($c_levels) && $c_levels){
		
		if (isset($parent_guidv4) && $parent_guidv4){
			$sql_tmp = "SELECT parent_guidv4 FROM ".$module."_levels WHERE language = :language AND guidv4 = :guidv4 GROUP BY guidv4 ORDER BY title ASC LIMIT 1";
			$result_tmp = $conn->prepare($sql_tmp);
			if (isset($language) && $language) $result_tmp->bindValue(':language', $language, PDO::PARAM_STR);
			else $result_tmp->bindValue(':language', $_SESSION['backend']['language'], PDO::PARAM_STR);
			$result_tmp->bindValue(':guidv4', $parent_guidv4, PDO::PARAM_STR);
			$result_tmp->execute();
			if ($result_tmp->rowCount()){		
				$arr_tmp = $result_tmp->fetch();
				if ($c_grid){
					print "
			<div class='table-cell change_folder' data-folder='".$arr_tmp['parent_guidv4']."'>
				<div class='grid-box'>
					<div class='grid-title strong'>".LABEL_BACK."</div><!-- /.grid-title -->
					<div class='grid-content'>
						<i class='material-icons out'>drive_file_move_rtl</i>
					</div><!-- /.grid-content -->
					<div class='caption'>&nbsp;</div><!-- /.caption -->
				</div><!-- /.grid-box -->
			</div><!-- /.table-cell -->";
				} else {
					print "
				<div class='table-row change_folder' data-folder='".$arr_tmp['parent_guidv4']."'>
					<div class='table-cell'></div><!-- /.table-cell -->
					<div class='table-cell'></div><!-- /.table-cell -->
					<div class='table-cell icon strong'><i class='material-icons out md-40'>folder</i>".LABEL_BACK."</div><!-- /.table-cell -->
					<div class='table-cell'></div><!-- /.table-cell -->";
					if (isset($c_languages) && $c_languages) print "<div class='table-cell'></div><!-- /.table-cell -->";
					print "
					<div class='table-cell'></div><!-- /.table-cell -->
					<div class='table-cell toolbar'></div><!-- /.table-cell -->
				</div><!-- /.table-row -->";
				}
			}
		}
		
		$sql_tmp = "SELECT * FROM ".$module."_levels WHERE 
								language = :language ";
		if (isset($parent_guidv4) && $parent_guidv4) $sql_tmp .= "AND parent_guidv4 = :parent_guidv4 ";
		else $sql_tmp .= "AND (parent_guidv4 IS NULL OR parent_guidv4 = '') ";
		$sql_tmp .= "GROUP BY guidv4 ORDER BY title ASC";
		$result_tmp = $conn->prepare($sql_tmp);
		if (isset($language) && $language) $result_tmp->bindValue(':language', $language, PDO::PARAM_STR);
		else $result_tmp->bindValue(':language', $_SESSION['backend']['language'], PDO::PARAM_STR);
		if (isset($parent_guidv4) && $parent_guidv4) $result_tmp->bindValue(':parent_guidv4', $parent_guidv4, PDO::PARAM_STR);
		$result_tmp->execute();
		if ($result_tmp->rowCount()){		
			while($arr_tmp = $result_tmp->fetch()){
				if ($c_grid){
					print "
			<div class='table-cell change_folder' data-folder='".$arr_tmp['guidv4']."' data-id='".$arr_tmp['id']."' data-module='".$module."_levels' data-backend-module='".$backend_module."'>
				<div class='grid-box'>
					<div class='grid-title strong'>".$arr_tmp['title']."</div><!-- /.grid-title -->
					<div class='grid-action'>
						<a href='#' class='edit' data-id='".$arr_tmp['id']."' data-module='".$module."_levels' data-backend-module='".$backend_module."' title='".LABEL_EDIT."'><i class='material-icons md-24'>edit</i></a>
						<a href='#' class='delete_level' data-id='".$arr_tmp['id']."' title='".LABEL_DELETE."'><i class='material-icons md-24'>delete_forever</i></a>
					</div><!-- /.grid-action -->
					<div class='grid-content'>
						<i class='material-icons out'>folder</i>
					</div><!-- /.grid-content -->
					<div class='caption'>&nbsp;</div><!-- /.caption -->
				</div><!-- /.grid-box -->
			</div><!-- /.table-cell -->";
				} else {
					$datetime = date("d.m.Y - H:i", $arr_tmp['datetime']);
	
					if (isset($c_languages) && $c_languages){
						$tmp_language = NULL;
						$sql_sub = "SELECT label FROM languages WHERE iso = :iso LIMIT 1";
						$result_sub = $conn->prepare($sql_sub);
						$result_sub->bindValue(':iso', $arr_tmp['language'], PDO::PARAM_STR);
						$result_sub->execute();
						if ($result_sub->rowCount()){
							$arr_sub = $result_sub->fetch();
							$tmp_language = id_label($arr_sub['title']);
						}
					}
					
					print "
				<div class='table-row change_folder' data-folder='".$arr_tmp['guidv4']."' data-id='".$arr_tmp['id']."' data-module='".$module."_levels' data-backend-module='".$backend_module."'>
					<div class='table-cell'></div><!-- /.table-cell -->
					<div class='table-cell'>".$datetime."</div><!-- /.table-cell -->
					<div class='table-cell icon strong'><i class='material-icons out md-40'>folder</i>".$arr_tmp['title']."</div><!-- /.table-cell -->
					<div class='table-cell'></div><!-- /.table-cell -->";
					if (isset($c_languages) && $c_languages) print "<div class='table-cell'>".$tmp_language."</div><!-- /.table-cell -->";
					print "
					<div class='table-cell'></div><!-- /.table-cell -->
					<div class='table-cell toolbar'>
						<a href='#' class='edit' data-id='".$arr_tmp['id']."' data-module='".$module."_levels' data-backend-module='".$backend_module."' title='".LABEL_EDIT."'><i class='material-icons md-22'>edit</i></a>
						<a href='#' class='delete_level' data-id='".$arr_tmp['id']."' title='".LABEL_DELETE."'><i class='material-icons md-22'>delete_forever</i></a>
					</div><!-- /.table-cell -->
				</div><!-- /.table-row -->";
				}
			}
		}
	}
}

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()){
		
		$tables_data = NULL;
		$sql_tables = "SHOW TABLES LIKE '%\\_images'";
		$result_tables = $conn->prepare($sql_tables);
		$result_tables->execute();
		if ($result_tables->rowCount()){
			while ($arr_tables = $result_tables->fetch(PDO::FETCH_COLUMN)){
				$tables_data[] = $arr_tables;
			}
		}
		
		
		while ($arr = $result->fetch()){
			unset($_SESSION['cmt'][$backend_module][$module]['id_list'][$arr['id']]);
			$datetime = date("d.m.Y - H:i", $arr['datetime']);
			$filesize = formatSizeUnits($arr['filesize']);
			
			$media_used = false;
			foreach ($tables_data AS $key => $value){
				$sql_check = "SELECT id FROM ".$value." WHERE id_media = :id_media ";
				$result_check = $conn->prepare($sql_check);
				$result_check->bindValue(':id_media', $arr['id'], PDO::PARAM_INT);
				$result_check->execute();
				if ($result_check->rowCount()){
					$media_used = true;
					break;
				}
			}
			
			if ($c_grid){
				print "
				<div class='table-cell edit' data-id='".$arr['id']."' data-module='".$module."' data-backend-module='".$backend_module."'>
					<div class='grid-box multiselect'>
						<input id='c_check_".$arr['id']."' class='multicheck c_check_".$arr['id']."' value='1' type='checkbox' name='c_check[".$arr['id']."]'>
						<div class='grid-title'>".$datetime." ".LABEL_CLOCK." - ".$filesize."</div><!-- /.grid-title -->
						<div class='grid-action'>";
				if ($media_used) print "<i class='material-icons md-24'>check_circle</i>";
				else print "<i class='material-icons md-24'>cancel</i>";
				print "
						</div><!-- /.grid-action -->
						<div class='grid-content'>
							<img src='".$file_dir.$arr['file']."' />
						</div><!-- /.grid-content -->
						<div class='caption'>".$arr['file']."</div><!-- /.caption -->
					</div><!-- /.grid-box -->
				</div><!-- /.table-cell -->";
			} else {
				if (isset($c_languages) && $c_languages){
					$tmp_language = NULL;
					$sql_tmp = "SELECT label FROM languages WHERE iso = :iso LIMIT 1";
					$result_tmp = $conn->prepare($sql_tmp);
					$result_tmp->bindValue(':iso', $arr['language'], PDO::PARAM_STR);
					$result_tmp->execute();
					if ($result_tmp->rowCount()){
						$arr_tmp = $result_tmp->fetch();
						$tmp_language = id($arr_tmp['label']);
					}
				}
				print "
				<div class='table-row' 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'>".$datetime."</div><!-- /.table-cell -->
					<div class='table-cell edit' data-id='".$arr['id']."' data-module='".$module."' data-backend-module='".$backend_module."'><img src='".$file_dir.$arr['file']."' />".$arr['file']."</div><!-- /.table-cell -->
					<div class='table-cell'>".$filesize."</div><!-- /.table-cell -->";
				if (isset($c_languages) && $c_languages) print "<div class='table-cell'>".$tmp_language."</div><!-- /.table-cell -->";
				print "
					<div class='table-cell'>";
				if ($media_used) print "<i class='material-icons md-24'>check_circle</i>";
				else print "<i class='material-icons md-24'>cancel</i>";
				print "
					</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='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){
	if ($c_grid){
		print "
		</div><!-- /.table-grid -->";
	} else {
		print "
			</div><!-- /.table-body -->
		</div><!-- /.table -->";
	}
	
	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='delete' title='".LABEL_DELETE."'><i class='material-icons md-24'>delete_forever</i>".LABEL_DELETE."</a></li>
 </ul>
</div>";

}

	
if ($c_grid){
	print "
<template id='skeleton-box'>
	<div class='table-cell edit' data-module='".$module."' data-backend-module='".$backend_module."'>
		<div class='grid-box multiselect'>
			<div class='grid-title'><span class='skeleton'>&nbsp;</span></div><!-- /.grid-title -->
			<div class='grid-action'><i class='material-icons md-24'>cancel</i></div><!-- /.grid-action -->
			<div class='grid-content'>
				<span class='skeleton'>
					<img src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII='/>
				</span>
			</div><!-- /.grid-content -->
			<div class='caption'><span class='skeleton'>&nbsp;</span></div><!-- /.caption -->
		</div><!-- /.grid-box -->
	</div><!-- /.table-cell -->
</template>";
} else {
	print "
<template id='skeleton-box'>
	<div class='table-row' data-module='".$module."' data-backend-module='".$backend_module."'>
		<div class='table-cell multiselect'><span class='skeleton'>&nbsp;</span></div><!-- /.table-cell -->
		<div class='table-cell date'><span class='skeleton'>&nbsp;</span></div><!-- /.table-cell -->
		<div class='table-cell file'>
			<span class='skeleton'>
				<img src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=' />
			</span>
			<span class='skeleton'>&nbsp;</span>
		</div><!-- /.table-cell -->
		<div class='table-cell filesize'><span class='skeleton'>&nbsp;</span></div><!-- /.table-cell -->";
if (isset($c_languages) && $c_languages) print "<div class='table-cell language'><span class='skeleton'>&nbsp;</span></div><!-- /.table-cell -->";
print "
		<div class='table-cell'><i class='material-icons md-24'>cancel</i></div><!-- /.table-cell -->
		<div class='table-cell toolbar'>
			<a href='#' class='edit hidden' data-module='".$module."' data-backend-module='".$backend_module."' title='".LABEL_EDIT."'><i class='material-icons md-24'>edit</i></a>
			<a href='#' class='delete hidden' title='".LABEL_DELETE."'><i class='material-icons md-24'>delete_forever</i></a>
		</div><!-- /.table-cell -->
	</div><!-- /.table-row -->
</template>";
}