<?php

/**
 * @package     Sven.Bluege
 * @subpackage  com_eventgallery
 *
 * @copyright   Copyright (C) 2005 - 2019 Sven Bluege All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die('Restricted access');
use Joomla\CMS\HTML\HTMLHelper;

if (!isset($noTabSet)) {
    $noTabSet = false;
}

function getLabel($field) {

    $tipTitle = JText::_($field->description);
    $title = $field->title;

    $toolTipTitle = HTMLHelper::_('tooltipText', $title ?: $title, '', 0);
    $toolTipContent = HTMLHelper::_('tooltipText', $tipTitle ?: $title, '', 0);

    $labelContent = <<<CONTENT
      <div class="control-label "
           title="$toolTipTitle"
           data-content="$toolTipContent"
           data-placement="top"
      >
      {$field->label}
      </div>
CONTENT;
    return $labelContent;
}

function addTab($name, $label, $noTabSet) {
    if ($noTabSet) return "";
    if (version_compare(JVERSION, '4.0', '<' ) == 1) {
        return JHtml::_('bootstrap.addTab', 'myTab', $name, $label);
    }else {
        return HTMLHelper::_('uitab.addTab', 'myTab', $name, $label);
    }
}

function endTab($noTabSet) {
    if ($noTabSet) return "";
    if (version_compare(JVERSION, '4.0', '<' ) == 1) {
        return JHtml::_('bootstrap.endTab');
    }else {
        return HTMLHelper::_('uitab.endTab');
    }
}

function startTabSet($defaultTabName, $noTabSet) {
    if ($noTabSet) return "";
    if (version_compare(JVERSION, '4.0', '<' ) == 1) {
        return JHtml::_('bootstrap.startTabSet', 'myTab', array('active' => $defaultTabName));
    }else {
        return HTMLHelper::_('uitab.startTabSet', 'myTab', array('active' => $defaultTabName));
    }
}

function endTabSet($noTabSet) {
    if ($noTabSet) return "";
    if (version_compare(JVERSION, '4.0', '<' ) == 1) {
        return JHtml::_('bootstrap.endTabSet');
    }else {
        return HTMLHelper::_('uitab.endTabSet');
    }
}

?>

<div class="adminform form-horizontal">
    <fieldset>

        <?php IF (count($this->form->getFieldsets())>1): ?>


            <?php echo startTabSet('general', $noTabSet)?>
            <?php foreach($this->form->getFieldsets() as $fieldset): ?>
                <?php echo addTab($fieldset->name, JText::_($fieldset->label, true),$noTabSet); ?>
                <?php IF (strlen(JText::_($fieldset->description))>0): ?>
                    <div><?php echo JText::_($fieldset->description); ?></div>
                    <hr>
                <?php ENDIF ?>
                <?php foreach ($this->form->getFieldset($fieldset->name) as $field): ?>
                    <?php $dataShowOn = ''; ?>
                    <?php if ($field->showon) : ?>
                        <?php JHtml::_('script', 'jui/cms.js', array('version' => 'auto', 'relative' => true)); ?>
                        <?php $dataShowOn = ' data-showon=\'' . json_encode(JFormHelper::parseShowOnConditions($field->showon, $field->formControl, $field->group)) . '\''; ?>
                    <?php endif; ?>
                    <div class="control-group" <?php echo $dataShowOn; ?> style="<?php if ($field->hidden) echo "display:none" ?>">
                        <div class="control-label"><?php echo getLabel($field) ?></div>
                        <div class="controls">
                            <?php echo $field->input; ?>
                            <?PHP IF (version_compare(JVERSION, '4.0', '<' ) == 0):?>
                                <?php if (!$field->hidden && !empty(JText::_($field->description))) : ?>
                                    <div id="<?php echo isset($id)?$id:''; ?>">
                                        <small class="form-text text-muted">
                                            <?php echo JText::_($field->description); ?>
                                        </small>
                                    </div>
                                <?php endif; ?>
                            <?php ENDIF; ?>
                        </div>
                    </div>
                <?php endforeach; ?>
                <?php echo endTab($noTabSet); ?>
            <?php endforeach ?>
            <?php echo endTabSet($noTabSet); ?>


        <?php ELSE: ?>
            <?php echo startTabSet('general', $noTabSet)?>
            <?php
                $fieldsets =  array_values($this->form->getFieldsets());
                $fieldset= $fieldsets[0]; ?>
            <?php IF (strlen(JText::_($fieldset->label))>0): ?>
                <?php echo addTab('general', JText::_($fieldset->label, true), $noTabSet); ?>
            <?php ELSE: ?>
                <?php echo addTab('general', JText::_('COM_EVENTGALLERY_EVENTS_OPTIONS_GENERAL_LABEL', true), $noTabSet); ?>
            <?php ENDIF; ?>
            <?php IF (strlen(JText::_($fieldset->description))>0): ?><div><?php echo JText::_($fieldset->description); ?></div><hr><?php ENDIF ?>
            <?php foreach ($this->form->getFieldset($fieldset->name) as $field): ?>
                <?php $dataShowOn = ''; ?>
                <?php if ($field->showon) : ?>
                    <?php JHtml::_('script', 'jui/cms.js', array('version' => 'auto', 'relative' => true)); ?>
                    <?php $dataShowOn = ' data-showon=\'' . json_encode(JFormHelper::parseShowOnConditions($field->showon, $field->formControl, $field->group)) . '\''; ?>
                <?php endif; ?>
                <div class="control-group" <?php echo $dataShowOn; ?>>

                    <?php if (!$field->hidden):?>
                        <div class="control-label"><?php echo getLabel($field) ?></div>
                    <?php endif; ?>
                    <div class="controls">
                        <?php echo $field->input; ?>
                        <?PHP IF (version_compare(JVERSION, '4.0', '<' ) == 0):?>
                            <?php if (!$field->hidden && !empty(JText::_($field->description))) : ?>
                                <div id="<?php echo isset($id)?$id:''; ?>">
                                    <small class="form-text text-muted">
                                        <?php echo JText::_($field->description); ?>
                                    </small>
                                </div>
                            <?php endif; ?>
                        <?php ENDIF; ?>
                    </div>
                </div>
            <?php endforeach; ?>

            <?php echo endTab($noTabSet); ?>
            <?php echo endTabSet($noTabSet); ?>
        <?php ENDIF ?>
    </fieldset>
</div>
