<?php
namespace JetStudioModule\Bases;

use Jet\MVC_View;
use Jet\UI;
use Jet\Tr;
use JetStudio\JetStudio_UI;
use Jet\Locale;
use Jet\UI_dialog;

/**
 * @var MVC_View $this
 */

$base = Main::getCurrentBase();

$form = $base->getEditForm();


$add_locale_form = $base->getAddLocaleForm();
$sort_locales_form = $base->getSortLocalesForm();

$sort_dialog = UI::dialog( 'locales_sort_dialog', Tr::_( 'Sort locales' ), UI_dialog::SIZE_LARGE );

?>
<!--suppress JSUnresolvedReference -->
<script type="text/javascript">
	const Bases = {
		editBase: {
			addLocale: function () {
				Dialog_SelectLocale.open(function (locale_data) {

					$('#add_locale_form__locale').val(locale_data.locale);
					$('#add_locale_form').submit();

				});
			},

			sortLocales: function () {
				$('#locales_sort_dialog').modal('show');


				setTimeout(function () {
					$('#locales_sort_area').sortable({
						stop: function () {
							const locales = [];

							$('#locales_sort_area').find('.locale-sort-item').each(function (i, item) {
								const id = $(item).data('locale');

								locales.push(id);
							});

							$('#sort_locales_form__locales').val(locales.join(','));
						}

					});

				}, 1000)

			}
		}
	}

	function copyBaseIDtoClipboard()
	{
		const id_input = document.getElementById("base_id_to_cb");
		id_input.select();
		document.execCommand("copy");
	}
</script>


<div id="main-toolbar" style="display: grid;grid-template-columns: 1fr 3fr;" class="toolbar">
	<div>
		<?= UI::button_create( Tr::_( 'Create a new base' ) )->setUrl( Main::getActionUrl('create_new_base') ) ?>
	</div>
	<div>
		<?= UI::button_save()->setOnClick( "$('#{$form->getId()}').submit()" ) ?>
		
		<?= UI::button_create( Tr::_( 'Add locale' ) )->setOnClick( 'Bases.editBase.addLocale()' ) ?>
		<?= UI::button_edit( Tr::_( 'Sort locales' ) )->setOnClick( 'Bases.editBase.sortLocales()' ) ?>
	</div>
</div>

<div id="main-edit-area" style="display: grid;grid-template-columns: 1fr 3fr;gap: 5px;">
	<div>
		<?= $this->render( 'bases_list' ); ?>
	</div>

	<div style="padding: 10px;">
		<?= $form->start() ?>

		<div class="section-title"><?= Tr::_( 'Base' ) ?> <span style="font-weight: bolder"><?= $base->getName() ?></span>

			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			<a href="#" onclick="copyBaseIDtoClipboard();" class="entity-id">
				ID: <?=UI::icon('clipboard')?>
				<input id="base_id_to_cb" readonly value="<?= $base->getId() ?>"/>
			</a>

		</div>
		
		<?= JetStudio_UI::messages(); ?>

		<div class="form-fields">
			<?= $form->field( 'name' ) ?>
			<?= $form->field( 'is_default' ) ?>
			<?= $form->field( 'is_secret' ) ?>
			<?= $form->field( 'is_active' ) ?>
			<?= $form->field( 'SSL_required' ) ?>
			<?= $form->field( 'initializer' ) ?>
		</div>
		
		<?php foreach( $base->getLocales() as $locale ): ?>
			<div>
				<div class="section-title">
					<span style="padding-left: 30px;"><?= UI::flag( $locale ) ?></span>
					<span style="padding-left: 10px;"><?= $locale->getName( Locale::getCurrentLocale() ) ?></span>
					<span style="padding-left: 10px;font-size: 12px;"><?= $locale ?></span>
					<span style="padding-left: 10px;font-size: 12px;"><?= $locale->getName( $locale ) ?></span>
				</div>


				<div class="form-fields">
					<?= $form->field( '/' . $locale . '/is_active' ) ?>
					<?= $form->field( '/' . $locale . '/SSL_required' ) ?>
					<?= $form->field( '/' . $locale . '/title' ) ?>

					<label class="control-label"><?= Tr::_( 'URLs:' ); ?></label>
					<div>
						<?php for( $u = 0; $u < 100; $u++ ):
							if( !$form->fieldExists( '/' . $locale . '/URLs/' . $u ) ) {
								break;
							}
							
							$field = $form->field( '/' . $locale . '/URLs/' . $u );
							$field->input()->addCustomCssStyle( 'width: 400px' );
							?>
							<?= $field->row()->start() ?>
							<?= $field->error() ?>
							<div class="input-group" style="margin: 2px;">
								<span class="input-group-prepend">
									<span class="input-group-text"><?= $base->getSSLRequired() ? 'https://' : 'http://' ?></span>
								</span>
								<?= $field->input() ?>
							</div>
							<?= $field->row()->end() ?>
						<?php endfor; ?>
						
					</div>

					<?=$form->field('/' . $locale . '/meta_tags')?>
					<?=$form->field('/' . $locale . '/params')?>
				</div>
			</div>
		<?php endforeach; ?>
  
		
		<?= $form->end() ?>
		
	</div>
</div>


<?= $add_locale_form->start() ?>
<?= $add_locale_form->field( 'locale' ) ?>
<?= $add_locale_form->end() ?>


<?= $sort_dialog->start() ?>

<div id="locales_sort_area">
	<?php foreach( $base->getLocales() as $l ): ?>
		<div class="list-group-item locale-sort-item" data-locale="<?= $l ?>"
		     style="display: grid;grid-template-columns: 20px 40px 80px 1fr 1fr;align-items: center;gap: 5px;"
		>
			<div><span class="fa fa-arrows-v" style="font-size:16px;"></span></div>
			<div style="font-size: 30px;"><?= UI::flag( $l ) ?></div>

			<div><?= $l ?></div>
			<div><?= $l->getName( Locale::getCurrentLocale() ) ?></div>
			<div><?= $l->getName( $l ) ?></div>
		</div>
	<?php endforeach; ?>
</div>
<?= $sort_dialog->footer() ?>

<?= $sort_locales_form->start() ?>
<?= $sort_locales_form->field( 'locales' ) ?>
<?= UI::button_save() ?>
<?= $sort_locales_form->end() ?>

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



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