<?php

/**
 * @package         Tassos Code Snippets
 * @version         1.0.1-RC2 Free
 *
 * @author          Tassos Marinos <info@tassos.gr>
 * @link            https://www.tassos.gr
 * @copyright       Copyright © 2026 Tassos Marinos All Rights Reserved
 * @license         GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later
 */

defined('_JEXEC') or die;

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

/** @var \Tassos\CodeSnippets\Administrator\View\Snippet\HtmlView $this */

$app   = Factory::getApplication();
$input = $app->input;

$wa = $this->document->getWebAssetManager();
$wa->useScript('keepalive')
    ->useScript('form.validate');

$layout = 'edit';
$tmpl   = $input->get('tmpl', '', 'cmd') === 'component' ? '&tmpl=component' : '';
?>

<form action="<?php echo Route::_('index.php?option=com_tassoscodesnippets&layout=' . $layout . $tmpl . '&id=' . (int) $this->item->id); ?>" method="post" name="adminForm" id="snippet-form" class="form-validate">
    <div class="row title-alias form-vertical mb-3">
        <div class="col-12">
            <?php echo $this->form->renderField('title'); ?>
        </div>
    </div>
    <?php echo $this->form->renderField('alias'); ?>

    <?php if ((int) $this->item->state === -3 && !empty($this->item->last_error)) : ?>
        <div class="alert alert-danger">
            <h4 class="alert-heading">
                <span class="icon-warning" aria-hidden="true"></span>
                <?php echo Text::_('COM_TASSOSCODESNIPPETS_ERROR_ALERT_TITLE'); ?>
            </h4>
            <p><?php echo Text::_('COM_TASSOSCODESNIPPETS_ERROR_ALERT_DESC'); ?></p>
            <?php if (!empty($this->item->last_error_date)) : ?>
                <p><strong><?php echo Text::_('COM_TASSOSCODESNIPPETS_ERROR_DATE'); ?>:</strong> <?php echo HTMLHelper::_('date', $this->item->last_error_date, Text::_('DATE_FORMAT_LC2')); ?></p>
            <?php endif; ?>
            <details>
                <summary style="cursor:pointer;font-weight:bold;"><?php echo Text::_('COM_TASSOSCODESNIPPETS_ERROR_DETAILS'); ?></summary>
                <pre style="background:#fff;padding:10px;margin-top:8px;font-size:12px;overflow:auto;max-height:300px;"><?php echo $this->escape($this->item->last_error); ?></pre>
            </details>
        </div>
    <?php endif; ?>

    <div class="main-card">
        <?php echo HTMLHelper::_('uitab.startTabSet', 'myTab', ['active' => 'details', 'recall' => true, 'breakpoint' => 768]); ?>

        <?php echo HTMLHelper::_('uitab.addTab', 'myTab', 'details', Text::_('COM_TASSOSCODESNIPPETS_TAB_DETAILS')); ?>
        <div class="row">
            <div class="col-lg-9">
                <?php
                // Render all fields in the details fieldset except those handled by global layout
                $excludeFields = ['catid', 'access', 'language', 'tags', 'state', 'id', 'title', 'alias'];
                foreach ($this->form->getFieldset('details') as $field) {
                    if (!in_array($field->fieldname, $excludeFields)) {
                        echo $field->renderField();
                    }
                }
                ?>
            </div>
            <div class="col-lg-3">
                <?php echo LayoutHelper::render('joomla.edit.global', $this); ?>
            </div>
        </div>
        <?php echo HTMLHelper::_('uitab.endTab'); ?>

        <?php if (count($this->form->getFieldset('options')) > 0) : ?>
        <?php echo HTMLHelper::_('uitab.addTab', 'myTab', 'options', Text::_('COM_TASSOSCODESNIPPETS_TAB_OPTIONS')); ?>
        <div class="row">
            <div class="col-lg-6">
                <?php echo $this->form->renderFieldset('options'); ?>
            </div>
        </div>
        <?php echo HTMLHelper::_('uitab.endTab'); ?>
        <?php endif; ?>

        <?php echo HTMLHelper::_('uitab.addTab', 'myTab', 'publishing_rules', Text::_('COM_TASSOSCODESNIPPETS_TAB_PUBLISHING_RULES')); ?>
        <div class="row">
            <div class="col-12">
                <?php echo $this->form->renderFieldset('publishing_rules'); ?>
            </div>
        </div>
        <?php echo HTMLHelper::_('uitab.endTab'); ?>

        <?php echo HTMLHelper::_('uitab.addTab', 'myTab', 'publishing', Text::_('JGLOBAL_FIELDSET_PUBLISHING')); ?>
        <div class="row">
            <div class="col-md-6">
                <?php echo LayoutHelper::render('joomla.edit.publishingdata', $this); ?>
            </div>
            <div class="col-md-6">
                <?php echo $this->form->renderField('ordering'); ?>
            </div>
        </div>
        <?php echo HTMLHelper::_('uitab.endTab'); ?>

        <?php echo HTMLHelper::_('uitab.endTabSet'); ?>
    </div>

    <input type="hidden" name="task" value="">
    <?php echo HTMLHelper::_('form.token'); ?>
</form>
