<?php
/**
 * @package    VP Email Manager
 * @copyright  Copyright (C) 2012-2021 VirtuePlanet Services LLP. All rights reserved.
 * @license    GNU General Public License version 2 or later; see LICENSE.txt
 * @author     Abhishek Das <info@virtueplanet.com>
 * @link       https://www.virtueplanet.com
 */

defined('_JEXEC') or die;

use \Joomla\CMS\HTML\HTMLHelper;
use \Joomla\CMS\Factory;
use \Joomla\CMS\Uri\Uri;
use \Joomla\CMS\Router\Route;
use \Joomla\CMS\Language\Text;


HTMLHelper::addIncludePath(JPATH_COMPONENT . '/helpers/html');
HTMLHelper::_('behavior.tooltip');
HTMLHelper::_('behavior.formvalidation');
HTMLHelper::_('formbehavior.chosen', 'select');
HTMLHelper::_('behavior.keepalive');
HTMLHelper::_('script', 'com_vpem/app.js', ['relative' => true, 'version' => '1.0']);
HTMLHelper::_('stylesheet', 'com_vpem/style.css', ['relative' => true, 'version' => '1.0']);

// Import CSS
$document = Factory::getDocument();

$document->addScriptDeclaration("
	Joomla.submitbutton = function (task) {
		if (task == 'template.cancel' || task == 'template.settype') {
			Joomla.submitform(task, document.getElementById('template-form'));
		} else {
			if (task != 'template.cancel' && document.formvalidator.isValid(document.id('template-form'))) {
				Joomla.submitform(task, document.getElementById('template-form'));
			} else {
				alert('" . $this->escape(Text::_('JGLOBAL_VALIDATION_FORM_FAILED')) . "');
			}
		}
	}
");

$document->addStyleDeclaration("
	.cm-mustache {
		color: #0ca;
	}
	.modal-body {
		overflow-y: inherit !important;
	}
");
?>
<form action="<?php echo JRoute::_('index.php?option=com_vpem&layout=edit&id=' . (int) $this->item->id); ?>" method="post" name="adminForm" id="template-form" class="form-validate" data-id="<?php echo (int) $this->item->id; ?>">
	<div class="form-inline form-inline-header">
		<?php if (!empty($this->type)) : ?>
			<div class="control-group">
				<div class="control-label">
					<?php echo $this->form->getLabel('name'); ?>
				</div>
				<div class="controls">
					<?php echo $this->form->getInput('name'); ?>
					<?php if (!$this->form->getField('name')->value) : ?>
						<div class="field-hint small muted"><?php echo JText::_('COM_VPEM_FORM_HINT_TEMPLATE_NAME'); ?></div>
					<?php endif; ?>
				</div>
			</div>
		<?php endif; ?>
		<div class="control-group">
			<div class="control-label">
				<?php echo $this->form->getLabel('type'); ?>
			</div>
			<div class="controls">
				<?php echo $this->form->getInput('type'); ?>
				<?php if (!$this->form->getField('type')->value) : ?>
					<div class="field-hint small muted text-error"><?php echo JText::_('COM_VPEM_FORM_HINT_TYPE_NAME'); ?></div>
				<?php endif; ?>
			</div>
		</div>
	</div>
	
	<?php if (!empty($this->type)) : ?>
	
		<?php echo JHtml::_('bootstrap.startTabSet', 'myTab', array('active' => 'template')); ?>
		
			<?php echo JHtml::_('bootstrap.addTab', 'myTab', 'template', JText::_('COM_VPEM_TAB_TEMPLATE', true)); ?>
				<div class="row-fluid">
					<div class="span8 form-vertical">
						<?php echo $this->form->renderField('subject'); ?>
						<div class="control-group">
							<div class="control-label">
								<?php echo $this->form->getLabel('body'); ?>
							</div>
							<div id="body-editor-container" class="controls">
								<?php echo $this->form->getInput('body'); ?>
							</div>
						</div>
						<div class="clearfix"></div>
						<?php echo $this->form->renderField('css_style'); ?>
					</div>
					<div class="span4 form-vertical">
						<?php if (!empty($this->variables)) : ?>
							<?php foreach ($this->variables as $group_name => $variables) : ?>
								<div class="vpem-accordion">
									<h5 class="vpem-accordion-title">
										<?php echo $group_name; ?>
										<span class="vpem-plus">
											<img src="<?php echo JUri::root(true); ?>/media/com_vpem/images/plus.svg" alt="Plus" width="20" height="20" />
										</span>
										<span class="vpem-minus">
											<img src="<?php echo JUri::root(true); ?>/media/com_vpem/images/minus.svg" alt="Minus" width="20" height="20" />
										</span>
									</h5>
									<div class="vpem-accordion-content">
										<?php foreach ($variables as $variable) : ?>
											<div class="vpem-accordion-item">
												<input type="text" class="vpem-accordion-item-input" value="<?php echo $variable->value; ?>" readonly="readonly" onclick="javascript:this.focus();this.select();" onfocus="javascript:this.focus();this.select();" />
												<div class="vpem-accordion-item-name"><?php echo $variable->text; ?></div>
											</div>
										<?php endforeach; ?>
									</div>
								</div>
							<?php endforeach; ?>
						<?php endif; ?>
					</div>
				</div>
			<?php echo JHtml::_('bootstrap.endTab'); ?>
		
			<?php echo JHtml::_('bootstrap.addTab', 'myTab', 'publishing', JText::_('COM_VPEM_TAB_PUBLISHING', true)); ?>
				<div class="row-fluid">
					<div class="span12 form-horizontal">
						<?php echo $this->form->renderField('state'); ?>
						<?php echo $this->form->renderField('created_by'); ?>
						<?php echo $this->form->renderField('created_on'); ?>
						<?php echo $this->form->renderField('modified_by'); ?>
						<?php echo $this->form->renderField('modified_on'); ?>
						<?php if ($this->state->params->get('save_history', 1)) : ?>
							<div class="control-group">
								<div class="control-label"><?php echo $this->form->getLabel('version_note'); ?></div>
								<div class="controls"><?php echo $this->form->getInput('version_note'); ?></div>
							</div>
						<?php endif; ?>
						<?php echo $this->form->renderField('id'); ?>
					</div>
				</div>
			<?php echo JHtml::_('bootstrap.endTab'); ?>
		
		<?php echo JHtml::_('bootstrap.endTabSet'); ?>
	
	<?php endif; ?>

	<?php echo $this->form->getInput('checked_out'); ?>
	<?php echo $this->form->getInput('checked_out_time'); ?>
	<input type="hidden" name="task" value=""/>
	<?php echo JHtml::_('form.token'); ?>
</form>

<?php echo JHtml::_('bootstrap.renderModal', 'previewModal', array('title'  => JText::_('COM_VPEM_TITLE_PREVIEW'), 'footer' => ''), $this->loadTemplate('preview_modal')); ?>
