<?php

namespace JetStudioModule\DataModel;

use Jet\DataModel_Definition_Model_Related;
use Jet\Tr;
use Jet\UI;

$current = DataModels::getCurrentModel();

$form = $current->getEditForm();

if($current instanceof DataModel_Definition_Model_Related):
	$main = $current->getMainModelDefinition();
	$parent = $current->getParentModelDefinition();
	?>
<div class="card">
	<div class="card-body">
		<table>
			<tr>
				<td><?=Tr::_('Main DataModel:')?></td>
				<td><a href="?class=<?=$main->getClassName()?>"><?=$main->getClassName()?> (<?=$main->getModelName()?>)</a></td>
			</tr>
			<tr>
				<td><?=Tr::_('Parent DataModel:')?></td>
				<td><a href="?class=<?=$parent->getClassName()?>"><?=$parent->getClassName()?> (<?=$parent->getModelName()?>)</a></td>
			</tr>
		</table>
	</div>
</div>
<br>
<?php
endif;

echo $form->start();
?>

<div class="form-fields">
<?php
echo $form->field( 'model_name' );
echo $form->field( 'database_table_name' );

echo $form->field( 'id_controller_class' );

if(
	$current->getIDControllerDefinition() &&
	($id_controller_options = $current->getIDControllerDefinition()->getOptionsList())
):
	?>
	<div class="section-title" style="grid-column: span 2"><?= Tr::_( 'ID controller options' ) ?></div>
	<?php
	foreach( $id_controller_options as $id_option ) {
		echo $form->field( '/id_controller_options/' . $id_option );
	}
endif;




if( $form->fieldExists( 'default_order_by' ) ):
	echo $form->field( 'default_order_by' );

	$order_by_options = $current->getOrderByOptions();

	if( $order_by_options ): ?>
		<label class="control-label"><?= Tr::_( 'Default order by:' ) ?></label>

		<div style="display: grid;grid-template-columns: 1fr 1fr;gap: 20px;">
			<div class="list-group" id="order_by_options_source" style="padding: 10px;">
				<span><?= Tr::_( 'Available' ) ?></span>
				<?php foreach( $order_by_options as $id => $label ): ?>
					<div class="list-group-item order_by_options_item"
					     data-id="<?= $id ?>"
					     data-label="<?= $label ?>"
					     onclick="DataModel.edit.defaultOrderBy.selectItem('<?= $id ?>')"
					>
						<?= UI::icon( 'plus' ) ?>
						<?= $label ?>
					</div>
				<?php endforeach; ?>
			</div>
			<div>
				<span><?= Tr::_( 'Selected' ) ?></span>
				<div id="order_by_options_selected" class="list-group" style="padding: 10px;">

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

endif;
?>
</div>
<?php
echo $form->end();

