<?php
/**
 * Copyright 2014 Adobe
 * All Rights Reserved.
 */
?>
<?php
/** @var $element \Magento\Framework\Data\Form\Element\Fieldset */
$element = $block->getElement();
$containerId = $element->getFieldsetContainerId();
$id = $element->getHtmlId();
$isCollapsable = $element->getCollapsable();
$isWrapped = $containerId || $element->getHeaderBar() || $isCollapsable;
$titleActions = '<div class="actions">' . $element->getHeaderBar() . '</div>';
$isField = $element->getFieldsetType() == 'field';
$advancedAfter = $element->getAdvancedPosition() == 'after'; // To place advanced options inside or after fieldset
$advancedLabel = $element->getAdvancedLabel();
if (!isset($advancedLabel)) {
    $advancedLabel = __('Additional Settings');
}

$cssClass = ($isField) ? 'field ' . $element->getClass() : 'fieldset admin__fieldset ' . $element->getClass();

if ($isField) {
    $count = $element->getCountBasicChildren();
    $cssClass .= ($element->hasAdvanced()) ? ' complex' : '';
}
?>

<?php
/**
 * @todo investigate situations, when the following is needed:
 * echo $element->getHeaderBar();
 * echo $element->getSubFieldsetHtml();
 */ ?>

<?php if ($isWrapped): ?>
    <div class="fieldset-wrapper <?= ($isCollapsable) ? 'admin__collapsible-block-wrapper ' : '' ?>"
        id="<?=  $block->escapeHtmlAttr($containerId ? $containerId : $id . '-wrapper') ?>"
        data-role="<?=  $block->escapeHtmlAttr($id) ?>-wrapper">
        <div class="fieldset-wrapper-title admin__fieldset-wrapper-title">
            <strong <?= /* @noEscape */ $isCollapsable ?
                'class="admin__collapsible-title" data-bs-toggle="collapse" data-bs-target="#' . $id . '-content"' :
                'class="title"'; ?>>
                <span><?= $block->escapeHtml($element->getLegend()) ?></span>
            </strong>
            <?= /* @noEscape */ $titleActions ?>
        </div>
        <div class="fieldset-wrapper-content admin__fieldset-wrapper-content<?= ($isCollapsable) ? ' collapse' : '' ?>"
            id="<?=  $block->escapeHtmlAttr($id) ?>-content"
            data-role="<?=  $block->escapeHtmlAttr($id) ?>-content">
<?php endif; ?>

    <?php if (!$element->getNoContainer()): ?>
        <fieldset class="<?=  $block->escapeHtmlAttr($cssClass) ?>" id="<?=  $block->escapeHtmlAttr($id) ?>">
        <?php if (is_string($element->getBeforeElementHtml()) && strlen($element->getBeforeElementHtml())): ?>
            <?= $element->getBeforeElementHtml() ?>
        <?php endif ?>
        <?php if ($element->getLegend() && !$isWrapped): ?>
            <legend class="<?= /* @noEscape */ $isField ? 'label admin__field-label' : 'admin__legend legend' ?>">
                <span><?= $block->escapeHtml($element->getLegend()) ?></span>
            </legend><br />
        <?php endif; ?>
    <?php endif; ?>


    <div class="messages">
        <?php if ($element->getComment() && !$isField): ?>
            <div class="message message-notice"><?= $block->escapeHtml($element->getComment()) ?></div>
        <?php endif; ?>
    </div>


    <?= ($isField) ? '<div class="control admin__field-control">' : '' ?>

    <?php if ($element->hasHtmlContent() && !$isField): ?>
        <?= $element->getHtmlContent() ?>
    <?php else: ?>

        <?php if ($isField && $count > 1): ?>
            <div class="fields-group-<?= /* @noEscape */ $count ?>">
        <?php endif; ?>

        <?= $element->getBasicChildrenHtml() ?>

        <?= ($isField && $count > 1) ? '</div>' : '' ?>

        <?php if ($element->getComment() && $isField): ?>
            <div class="note"><?= $block->escapeHtml($element->getComment()) ?></div>
        <?php endif; ?>

        <?php if ($element->hasAdvanced() && !$isField): ?>
            <?= (!$element->getNoContainer() && $advancedAfter)  ? '</fieldset>' : '' ?>
            <details class="details admin__collapsible-block-wrapper" id="details<?= /* @noEscape */ $id ?>">
                <summary class="details-summary admin__collapsible-title" id="details-summary<?= /* @noEscape */ $id?>">
                    <span><?= $block->escapeHtml($advancedLabel) ?></span>
                </summary>
                <div class="details-content admin__fieldset" id="details-content<?= /* @noEscape */ $id ?>">
                    <?= $element->getAdvancedChildrenHtml() ?>
                </div>
            </details>
        <?php elseif ($element->hasAdvanced() && $isField): ?>
            <div class="nested" id="nested<?= /* @noEscape */ $id ?>">
                <?= $element->getAdvancedChildrenHtml() ?>
            </div>
        <?php endif; ?>

        <?= ($isField) ? '</div>' : '' ?>

    <?php endif; ?>


    <?php if (!$element->getNoContainer() && !$advancedAfter): ?>
        </fieldset>
    <?php endif; ?>

<?php if ($isWrapped): ?>
        </div>
    </div>
<?php endif; ?>
