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

if (isset($_POST['reload_ajax']) && $_POST['reload_ajax']){	
	$action = 'reload';
	if (isset($_POST['files_data'])) {
		$files_data = [];
		foreach ($_POST['files_data'] AS $key => $value){
			$files_data[$key] = $value;
		}
	}
} else {
	print "
<div class='form ";
	if ($tab != 'files') print "hidden";
	print "' data-id='files'>
	<div id='box_files'>";
}
	
print "
		<div class='form-content'>";
if (isset($files_data) && is_array($files_data)){
	foreach ($files_data AS $key => $value){
		foreach ($value AS $sub_key => $sub_value){
			if ($sub_key == 'id_media'){
				print "<input type='hidden' name='files_data[".$key."][".$sub_key."]' class='files-data' value='".$sub_value."' />";
			} else if ($sub_value) {
				print "<input type='hidden' name='files_data[".$key."][".$sub_key."]' value='".$sub_value."' />";
			}
		}
	}
}

if (isset($library_modules) && is_array($library_modules)){
	foreach ($library_modules AS $key => $value){
		if ($key == 'files'){
			print "
			<div class='form-row upload-box files'>
				<input type='hidden' name='session_module' value='".$module."' />
				<input type='hidden' name='multi_update' value='0' />";
			if (isset($files_parent_guidv4) && $files_parent_guidv4) print "<input type='hidden' name='files_parent_guidv4' value='".$files_parent_guidv4."' />";
			print "
				<input type='file' class='field hidden mediaupload files' id='mediaupload_files' name='upload[]' multiple>
				<div class='upload-note'>
					<i class='material-icons md-96'>upload_file</i>".LABEL_DRAG_UPLOAD."
				</div><!-- /.upload-note -->
				<div class='form-action'>
					<button type='button' class='btn media-gallery' data-module='files' data-backend-module='library' data-file='files_filter'";
			if (isset($files_parent_guidv4) && $files_parent_guidv4) print "data-guidv4='".$files_parent_guidv4."' ";
			print "title='".$value['text']."' data-session-module='".$module."'><i class='material-icons md-24'>".$value['icon']."</i>".LABEL_CHOOSE."</button>
					<button type='button' class='btn mediaupload-trigger files' data-session-module='".$module."'><i class='material-icons md-24'>upload_file</i>".LABEL_UPLOAD."</button>
				</div><!-- /.form-action -->
			</div><!-- /.form-row -->";
		}
	}
}
			
print "
			<div class='form-action remove-all files ";
if (!isset($files_data) OR count($files_data) == 0) print "hidden";
print "'>
				<button type='button' class='btn remove-all-media files' name='submit' title='".LABEL_REMOVE_MEDIA."'><i class='material-icons md-24'>delete_forever</i>".LABEL_REMOVE_MEDIA."</button>
			</div><!-- /.form-action -->
			
			<div class='table files ";
if (!isset($files_data) OR count($files_data) == 0) print "hidden";
print "'>
			
				<div class='table-head'>
					<div class='table-row'>
						<div class='table-cell size-small'><a href='#'>".LABEL_ID;
print "</a></div><!-- /.table-cell -->
						<div class='table-cell'><a href='#'>".LABEL_FILE;
print "</a></div><!-- /.table-cell -->
						<div class='table-cell size-medium'><a href='#'>".LABEL_CATEGORY;
print "</a></div><!-- /.table-cell -->";

if (isset($c_language) && $c_language) {
	print "<div class='table-cell size-small'><a href='#'>".LABEL_LANGUAGE;
	print "</a></div><!-- /.table-cell -->";
}
print "
						<div class='table-cell toolbar'></div><!-- /.table-cell -->
					</div><!-- /.table-row -->
				</div><!-- /.table-head -->
				
				<div class='table-body sortable'>";
if (isset($files_data) && is_array($files_data) && count($files_data) > 0){
	foreach ($files_data AS $key => $value){
		$sql_tmp = "SELECT file, datetime, filesize, category, language FROM files WHERE id = :id LIMIT 1";
		$result_tmp = $conn->prepare($sql_tmp);
		$result_tmp->bindValue(':id', $value['id_media'], PDO::PARAM_INT);
		$result_tmp->execute();
		if ($result_tmp->rowCount()){
			$arr_tmp = $result_tmp->fetch();
			$datetime = date("d.m.Y - H:i", $arr_tmp['datetime']);
			$filesize = formatSizeUnits($arr_tmp['filesize']);
			
			if (isset($c_language) && $c_language){
				$tmp_language = NULL;
				if (isset($arr_tmp['language']) && $arr_tmp['language']){
					$sql_sub = "SELECT title 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 = $arr_sub['title'];
					}
				}
			}
			print "
					<input type='hidden' name='files_editor[".$value['id_media']."]' value='".$arr_tmp['file']."' data-category='".$arr_tmp['category']."' ";
			if (isset($c_language) && $c_language) print "data-language='".$tmp_language."' ";
			print "/>
					<div class='table-row' data-id='".$value['id_media']."' id='".$value['id_media']."' data-module='".$module."_files' data-backend-module='library' data-file='files' >
						<div class='table-cell sortorder'>#".$value['id_media']."</div><!-- /.table-cell -->
						<div class='table-cell'>".$arr_tmp['file']."</div><!-- /.table-cell -->
						<div class='table-cell'>".$arr_tmp['category']."</div><!-- /.table-cell -->";
			if (isset($c_language) && $c_language) print "<div class='table-cell'>".$tmp_language."</div><!-- /.table-cell -->";
			print "
						<div class='table-cell toolbar'>
							<a href='#' class='edit-media' data-id='".$value['id_media']."' data-module='".$module."_files' data-backend-module='library' data-file='files' title='".LABEL_EDIT."'><i class='material-icons md-24'>edit</i></a>
							<a href='#' class='remove-media files' data-id='".$value['id_media']."' title='".LABEL_DELETE."'><i class='material-icons md-24'>delete_forever</i></a>
						</div><!-- /.table-cell -->
					</div><!-- /.table-row -->";				
				
		}
	}
}
print "
				</div><!-- /.table-body -->
			</div><!-- /.table -->
			<hr />";

include('includes/form/error.php');
print "
			<div class='form-action'>
				<button type='submit' class='btn save' name='submit' title='".LABEL_SAVE."'><i class='material-icons md-24'>save_alt</i>".LABEL_SAVE."</button>
			</div><!-- /.form-action -->
			
		</div><!-- /.form-content -->";
if (!isset($_POST['reload_ajax']) OR !$_POST['reload_ajax']){
	print "
	</div>
</div><!-- /.form -->

<template id='skeleton-box' class='files'>
	<div class='table-row' data-module='".$module."_files' data-backend-module='library' data-file='images' >
		<div class='table-cell sortorder'><span class='skeleton'>&nbsp;</span></div><!-- /.table-cell -->
		<div class='table-cell icon'><span class='skeleton'>&nbsp;</span>
		</div><!-- /.table-cell -->
		<div class='table-cell category'><span class='skeleton'>&nbsp;</span></div><!-- /.table-cell -->";
if (isset($c_language) && $c_language) print "<div class='table-cell language'><span class='skeleton'>&nbsp;</span></div><!-- /.table-cell -->";
print "
		<div class='table-cell toolbar'>
			<a href='#' class='edit-media' data-module='".$module."_files' data-backend-module='library' data-file='files' title='".LABEL_EDIT."'><i class='material-icons md-24'>edit</i></a>
			<a href='#' class='remove-media files' title='".LABEL_REMOVE."'><i class='material-icons md-24'>delete_forever</i>".LABEL_REMOVE."</a>
		</div><!-- /.table-cell -->
	</div><!-- /.table-row -->
</template>";
}
