<?php
$file_dir = "../images/original/";
if (isset($_POST['images_parent_guidv4']) && $_POST['images_parent_guidv4']){
	$images_parent_guidv4 = addslashes(trim(strip_tags(stripslashes($_POST['images_parent_guidv4']))));
}

if (isset($_POST['reload_ajax']) && $_POST['reload_ajax']){	
	$action = 'reload';
	if (isset($_POST['images_data'])) {
		foreach ($_POST['images_data'] AS $key => $value){
			$images_data[$key] = $value;
			if (isset($value['c_enlarge']) && $value['c_enlarge']) $images_data[$key]['c_enlarge'] = $value['c_enlarge'];
			else $images_data[$key]['c_enlarge'] = 0;
		}
	}
} else {
	print "
<div class='form ";
	if ($tab != 'images') print "hidden";
	print "' data-id='images'>
	<div id='box_images'>";
}
	
print "
		<div class='form-content'>";
if (isset($images_data) && is_array($images_data)){
	foreach ($images_data AS $key => $value){
		foreach ($value AS $sub_key => $sub_value){
			if ($sub_key == 'id_media'){
				print "<input type='hidden' name='images_data[".$key."][".$sub_key."]' class='images-data' value='".$sub_value."' />";
			} else if ($sub_key == 'c_enlarge'){
				print "<input type='hidden' name='images_data[".$key."][".$sub_key."]' value='".$sub_value."' />";
			} else if ($sub_value) {
				print "<input type='hidden' name='images_data[".$key."][".$sub_key."]' value='".$sub_value."' />";
			}
		}
		$sql_tmp = "SELECT c_lightbox_watermark, c_thumbnails_watermark FROM images 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();
			if ($arr_tmp['c_lightbox_watermark']) {
				print "<input type='hidden' name='images_data[".$key."][c_lightbox_watermark]' value='".$arr_tmp['c_lightbox_watermark']."' />";
			}
			if ($arr_tmp['c_thumbnails_watermark']) {
				print "<input type='hidden' name='images_data[".$key."][c_thumbnails_watermark]' value='".$arr_tmp['c_thumbnails_watermark']."' />";
			}
		}
	}
}
if (isset($images_data_ph) && is_array($images_data_ph)){
	foreach ($images_data_ph AS $key => $value){
		foreach ($value AS $sub_key => $sub_value){
			print "<input type='hidden' name='images_data_ph[".$key."][".$sub_key."]' value='".$sub_value."' />";
		}
	}
}

if (isset($library_modules) && is_array($library_modules)){
	foreach ($library_modules AS $key => $value){
		if ($key == 'images'){
			print "
			<div class='form-row upload-box images'>
				<input type='hidden' name='session_module' value='".$module."' />
				<input type='hidden' name='multi_update' value='0' />";
			if (isset($images_parent_guidv4) && $images_parent_guidv4) print "<input type='hidden' name='images_parent_guidv4' value='".$images_parent_guidv4."' />";
			print "
				<input type='file' class='field hidden mediaupload images' id='mediaupload_images' name='upload[]' data-backend-module='library' 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='images' data-backend-module='library' data-file='images_filter' ";
			if (isset($images_parent_guidv4) && $images_parent_guidv4) print "data-guidv4='".$images_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 images' 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 images ";
if (!isset($images_data) OR count($images_data) == 0) print "hidden";
print "'>
				<button type='button' class='btn remove-all-media images' name='submit' title='".LABEL_REMOVE_MEDIA."'><i class='material-icons md-24'>delete_forever</i>".LABEL_REMOVE_MEDIA."</button>
			</div><!-- /.form-action -->";

print "
			<div class='table-grid sortable images ";
if (!isset($images_data) OR count($images_data) == 0) print "hidden";
print "'>";
if (isset($images_data) && is_array($images_data) && count($images_data) > 0){
	foreach ($images_data AS $key => $value){
		$sql_tmp = "SELECT file, datetime, filesize FROM images 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']);
	
			print "
				<input type='hidden' name='images_editor[".$value['id_media']."]' data-path='".$file_dir."' value='".$arr_tmp['file']."' />
				<div class='table-cell' data-id='".$value['id_media']."' id='".$value['id_media']."' data-module='".$module."_images' data-backend-module='library' data-file='images' >
					<div class='grid-box multiselect'>
						<div class='grid-title'>#".$value['id_media']."</div><!-- /.grid-title -->
						<div class='grid-action'>
							<a href='#' class='edit-media' data-id='".$value['id_media']."' data-module='".$module."_images' data-backend-module='library' data-file='images' title='".LABEL_EDIT."'><i class='material-icons md-24'>edit</i></a>
							<a href='#' class='remove-media images' data-id='".$value['id_media']."' title='".LABEL_REMOVE."'><i class='material-icons md-24'>delete_forever</i>".LABEL_REMOVE."</a>
						</div><!-- /.grid-action -->
						<div class='grid-content'>
							<img src='".$file_dir.$arr_tmp['file']."' />
						</div><!-- /.grid-content -->
						<div class='caption'>".$arr_tmp['file']."</div><!-- /.caption -->
					</div><!-- /.grid-box -->
				</div><!-- /.table-cell -->";
		}
	}
}
print "
			</div><!-- /.table -->
			<hr />";
			
						
$has_images = (isset($images_data) && is_array($images_data) && count($images_data) > 0);

if (!$has_images){
	
	$images_suggestions = [];

	if (isset($c_images) && $c_images){
		if (function_exists('images_prepare_suggestions')) $images_suggestions = images_prepare_suggestions($module, $id, $language, $content_data, $parent_guidv4 ?? [], $images_data ?? [], $images_parent_guidv4);
	}

	if (isset($images_suggestions) && is_array($images_suggestions) && count($images_suggestions) > 0){
		print "
			<div class='images-suggestions'>
				<div class='form-action'>
					<button type='button' class='btn images-suggestions-toggle' data-toggle-icon='visibility_off' data-toggle-label='".LABEL_HIDE_SUGGESTIONS."'>
						<i class='material-icons md-24'>image_search</i>".LABEL_SHOW_SUGGESTIONS."
					</button>
				</div><!-- /.form-action -->
			
				<div class='table-grid images-suggestions-grid hidden'>";

		foreach ($images_suggestions AS $image){
			$id_media = (int)$image['id'];
			$file = htmlspecialchars((string)$image['file'], ENT_QUOTES, 'UTF-8');
	
			print "
					<div class='table-cell image-suggestion' data-id='".$id_media."'>";

			foreach (['title', 'alt', 'photographer', 'text', 'source', 'copyright', 'link', 'linktext'] AS $field){
				$value = htmlspecialchars((string)($image[$field] ?? ''), ENT_QUOTES, 'UTF-8');
				print "
						<input type='hidden' disabled='disabled' name='ajax_data[".$id_media."][".$field."]' value='".$value."' />";
			}
	
			print "
						<div class='grid-box multiselect'>
							<input id='c_check_".$id_media."' class='multicheck image-suggestion-check c_check_".$id_media."' value='1' type='checkbox' name='c_check[".$id_media."]' />
							<div class='grid-title'>#".$id_media."</div><!-- /.grid-title -->
							<div class='grid-content'>
								<img src='".$file_dir.$file."' />
							</div><!-- /.grid-content -->
							<div class='caption'>".$file."</div><!-- /.caption -->
						</div><!-- /.grid-box -->
					</div><!-- /.table-cell -->";
		}
	
		print "
				</div><!-- /.table-grid -->
				<div class='button absolute'>
					<a href='#' class='btn select images-suggestions-select'><i class='material-icons md-24'>add</i>".LABEL_USE_FILES."</a>
				</div><!-- /.button -->
			</div><!-- /.images-suggestions -->
			<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='images'>
	<div class='table-cell' data-module='".$module."_images' data-backend-module='library' data-file='images' >
		<div class='grid-box multiselect'>
			<div class='grid-title'><span class='skeleton'>&nbsp;</span></div><!-- /.grid-title -->
			<div class='grid-action'>
				<a href='#' class='edit-media' data-module='".$module."_images' data-backend-module='library' data-file='images' title='".LABEL_EDIT."'><i class='material-icons md-24'>edit</i></a>
				<a href='#' class='remove-media images' title='".LABEL_REMOVE."'><i class='material-icons md-24'>delete_forever</i>".LABEL_REMOVE."</a>
			</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>";
}
