<?php

namespace JetApplicationModule\Admin\Content\Images;

use Jet\UI_button;
use JetApplication\Content_Gallery;

use Jet\Form_Field_FileImage;
use Jet\UI;
use Jet\Tr;
use Jet\MVC_View;

/**
 * @var MVC_View $this
 * @var Content_Gallery $gallery
 * @var Form_Field_FileImage $files_field
 */
$gallery = $this->getRaw( 'gallery' );
$images = $gallery->getImages();

$router = $this->getController()->getControllerRouter();

$delete_uri = $router->action( 'image_delete' )->URI( $gallery->getId() );

$select_image_mode = $this->getBool('select_image_mode');

if($select_image_mode) {
	$delete_uri = false;
}

if( count( $images ) ): ?>
	<form action="<?= $delete_uri ?>" method="post">
		<div class="row">
			<?php foreach( $images as $image ): ?>
				<div style="float:left;margin: 10px;overflow: hidden;">
					<div class="thumbnail" style="height: <?=$select_image_mode?250:200?>px;width: 200px;padding:10px;overflow: hidden">
						<a href="<?= $image->getURI(); ?>" target="_blank">
							<div
									style="width:180px;height:135px;background-position:center;background-repeat:no-repeat;background-image: url('<?= $image->getThumbnail( 180, 135 )->getURI(); ?>');"
							></div>
						</a>

						<div class="caption">
								<div style="white-space: nowrap;overflow: hidden;max-width: 100%;padding: 2px;">
									<?php if( $delete_uri ): ?>
									<input type="checkbox" name="images[]"
									       value="<?= $image->getId(); ?>"
									       id="image_<?= $image->getId(); ?>">
									<?php endif; ?>
									<?= $image->getFileName(); ?>
								</div>
								
								<?php if($select_image_mode):
									$thb = $image->getThumbnail(200, 200);
								
									?>
									<div style="text-align: center">
									<?= UI::button(
										Tr::_( 'Select this image' )
									)
										->setOnClick( "parent.dialogSelectImage_select({id:'".$image->getId()."',URI:'".$image->getURI()."',thb_URI:'".$thb->getURI()."'});" )
										->setClass(UI_button::CLASS_SUCCESS);
									?>
									</div>
								<?php endif; ?>
							
						</div>
					</div>
				</div>
			<?php endforeach; ?>
		</div>

		<?php if( $delete_uri ): ?>
			<?= UI::button_delete( Tr::_( 'Delete selected images' ) )->setType( UI_button::TYPE_SUBMIT ) ?>
		<?php endif; ?>
	</form>
<?php endif;
