<?php

namespace JetStudioModule\DataModel;

use Jet\DataModel;
use Jet\UI;
use Jet\Tr;
use Jet\UI_dialog;
use Jet\UI_messages;
use Jet\MVC_View;
use JetStudio\JetStudio_UI;

/**
 * @var MVC_View $this
 */

if( ($problematic_classes = DataModels::getProblematicClasses()) ) {

	require 'problems.phtml';

	return;
}


$what_to_edit = DataModels::getCurrentWhatToEdit();

$errors = [];
$warnings = [];
$class = null;

if( DataModels::getCurrentModel() ) {
	$class = DataModels::getCurrentModel()->createClass();

	$errors = $class->getErrors();
	$warnings = $class->getWarnings();
}

$allow_to_create_related = (
	DataModels::getCurrentModel() &&
	DataModels::getCurrentModel()->canHaveRelated()
);

?>
<?= $this->render( 'js' ); ?>

	<div class="toolbar" id="main-toolbar" style="display: grid;grid-template-columns: 1fr 2fr;gap: 10px;">

		<div style="display: flex;align-items: center;justify-content: left;gap: 10px;">
			<div class="dropdown">
				<button type="button" class="btn btn-light dropdown-toggle" data-toggle="dropdown" aria-haspopup="true"
				        aria-expanded="false">
					<?= UI::icon( 'plus' ) ?> <?= Tr::_( 'Create a new DataModel' ) ?>
				</button>
				<div class="dropdown-menu" style="padding: 10px;">
					<a class="dropdown-item" href="#" onclick="DataModel.create.openDialog('<?=DataModel::MODEL_TYPE_MAIN?>');">Main</a>
					<?php if( $allow_to_create_related ): ?>
						<a class="dropdown-item" href="#" onclick="DataModel.create.openDialog('<?=DataModel::MODEL_TYPE_RELATED_1TON?>');">1toN</a>
						<a class="dropdown-item" href="#" onclick="DataModel.create.openDialog('<?=DataModel::MODEL_TYPE_RELATED_1TO1?>');">1to1</a>
					<?php endif; ?>
				</div>
			</div>
			<?php if( DataModels::getCurrentModel() ): ?>
				<?= UI::button_create( Tr::_( 'Property' ) )
					->setOnClick( 'DataModel.property.create.openDialog()' )
					->setIcon( 'plus' );
				?>

				<?= UI::button_create( Tr::_( 'External relation' ) )
					->setOnClick( 'DataModel.relation.create.openDialog()' )
					->setIcon( 'plus' );
				?>

				<?= UI::button_create( Tr::_( 'Key' ) )
					->setOnClick( 'DataModel.key.create.openDialog()' )
					->setIcon( 'plus' );
				?>
			<?php endif; ?>
		</div>
		
		<div>
			<?php if( $what_to_edit ): ?>
				<?= $this->render( $what_to_edit . '/edit/toolbar' ); ?>
			<?php endif; ?>
		</div>
	</div>

	<div id="main-col" style="display: grid;grid-template-columns: 1fr 2fr;gap: 10px;">
		<?php if( $class ): ?>
			<div>
				<?= $this->render( 'list' ); ?>
			</div>
			<div style="padding: 10px;">
				<?= JetStudio_UI::messages(); ?>
				<?php

				foreach( $warnings as $warning ) {
					echo '<div style="padding: 5px">' . UI_messages::createWarning( $warning ) . '</div>';
				}

				foreach( $errors as $error ) {
					echo '<div style="padding: 5px">' . UI_messages::createDanger( $error ) . '</div>';
				}

				$what_to_edit = DataModels::getCurrentWhatToEdit();
				?>
				<div class="container-fluid">
					<?php require $what_to_edit . '/edit/form.phtml'; ?>
				</div>
			</div>
		<?php else: ?>
			<div style="grid-column: span 2;">
				<?= $this->render( 'list' ); ?>
			</div>
		<?php endif; ?>
	</div>


<?= $this->render( 'model/create/'.DataModel::MODEL_TYPE_MAIN.'/dialog' ); ?>

<?php if( $allow_to_create_related ): ?>
	<?= $this->render( 'model/create/'.DataModel::MODEL_TYPE_RELATED_1TON.'/dialog' ); ?>
	<?= $this->render( 'model/create/'.DataModel::MODEL_TYPE_RELATED_1TO1.'/dialog' ); ?>
<?php endif; ?>

<?= $this->render( 'property/create/dialog' ); ?>
<?= $this->render( 'key/create/dialog' ); ?>

<?= $this->render( 'relation/create/dialog' ); ?>

<?php
$dialog_SQL_create = UI::dialog( 'dialog_SQL_create', Tr::_( 'SQL - create' ), UI_dialog::SIZE_LARGE );
echo $dialog_SQL_create->start();
?><textarea id="SQL_create_result" style="width: 100%;height: 400px;"></textarea><?php
echo $dialog_SQL_create->end();

$dialog_SQL_update = UI::dialog( 'dialog_SQL_update', Tr::_( 'SQL - update' ), UI_dialog::SIZE_LARGE );
echo $dialog_SQL_update->start();
?><textarea id="SQL_update_result" style="width: 100%;height: 400px;"></textarea><?php
echo $dialog_SQL_update->end();

$dialog_class_source = UI::dialog( 'dialog_class_source', Tr::_( 'Class source code' ), UI_dialog::SIZE_LARGE );
echo $dialog_class_source->start();
?><textarea id="class_source_result" style="width: 100%;height: 400px;"></textarea><?php
echo $dialog_class_source->end();

echo $this->render( 'key/delete/dialog' );
echo $this->render( 'relation/delete/dialog' );
