<?php
namespace JetApplicationModule\Test\Forms;

use Jet\Form;
use Jet\Form_Renderer;
use Jet\MVC_View;
use Jet\Tr;
use Jet\UI;
use Jet\UI_button;

/**
 * @var MVC_View $this
 * @var Form $form
 * @var DefinitionTest_FormGenerator $test_object
 */
$form = $this->getRaw( 'form' );
$test_object = $this->getRaw( 'test_object' );

$form->setAction( '#' . $form->getId() );

$main_getters = $this->getRaw( 'main_getters' );
$sub_entities_getters = $this->getRaw( 'sub_entities_getters' );
$sub_entity_getters = $this->getRaw( 'sub_entity_getters' );

$form->renderer()
	->setDefaultLabelWidth( [
		Form_Renderer::LJ_SIZE_MEDIUM => 3
	] )
	->setDefaultFieldWidth( [
		Form_Renderer::LJ_SIZE_MEDIUM => 9
	] );

?>

<h2><?= Tr::_( 'Form test - generated by definition' ) ?></h2>

<?= $form->start() ?>
<?= $form->message() ?>

<?php
foreach( $form->getFields() as $field ) {
	if( $field->getName()[0] != '/' ) {
		echo $field;
	}
}
?>
<?php foreach( DefinitionTest_FormGenerator::getLocales() as $locale ): ?>
	<h3><?= UI::locale( $locale ) ?> <?= $locale->getName( $locale ) ?></h3>
	<?php if($form->fieldExists( '/sub_entities/' . $locale . '/title' )): ?>
		<?= $form->field( '/sub_entities/' . $locale . '/title' ) ?>
	<?php endif; ?>
	<?php if($form->fieldExists( '/sub_entities/' . $locale . '/text' )): ?>
		<?= $form->field( '/sub_entities/' . $locale . '/text' ) ?>
	<?php endif; ?>
<?php endforeach; ?>

<?php if($form->fieldExists('/sub_entity/some_text')): ?>
<h3><?=Tr::_('Sub entity')?></h3>
<?= $form->field( '/sub_entity/some_text' ); ?>
<?php endif; ?>

<div class="row">
	<div class="offset-md-3">
		<?= UI::button( Tr::_( 'Submit' ) )->setType( UI_button::TYPE_SUBMIT ) ?>
	</div>
</div>

<?= $form->end(); ?>


<?php if( $test_object ): ?>
	<div class="row">
		<div class="offset-md-4">
			<hr/>
			<?= Tr::_( 'Form sent:' ) ?>

			<table class="table table-bordered">
				<?php foreach( $main_getters as $getter ):
					$value = $test_object->{$getter}();
					?>
					<tr>
						<td><?= $getter; ?>:</td>
						<td><?= is_array( $value ) ? var_export( $value, true ) : $value ?></td>
					</tr>
				<?php endforeach; ?>
			</table>
			
			<?php foreach( DefinitionTest_FormGenerator::getLocales() as $locale ):
				$sub_entity = $test_object->getSubEntities()[(string)$locale];
				?>
				<h4><?= UI::locale( $locale ) ?> <?= $locale->getName( $locale ) ?></h4>

				<table class="table table-bordered">
					<?php foreach( $sub_entities_getters as $getter ):
						$value = $sub_entity->{$getter}();
						?>
						<tr>
							<td style="width: 200px;"><?= $getter; ?>:</td>
							<td><?= is_array( $value ) ? var_export( $value, true ) : $value ?></td>
						</tr>
					<?php endforeach; ?>
				</table>
			
			<?php endforeach; ?>
			
			<?php
			$sub_entity = $test_object->getSubEntity();
			?>
			<h3><?=Tr::_('Sub entity')?></h3>
			<table class="table table-bordered">
				
				<?php foreach( $sub_entity_getters as $getter ):
					$value = $sub_entity->{$getter}();
					?>
					<tr>
						<td style="width: 200px;"><?= $getter; ?>:</td>
						<td><?= is_array( $value ) ? var_export( $value, true ) : $value ?></td>
					</tr>
				<?php endforeach; ?>
			</table>


		</div>
	</div>
<?php endif; ?>

