<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

?>
<?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="<?= /* @escapeNotVerified */ $containerId ? $containerId : $id . '-wrapper' ?>"
        data-role="<?= /* @escapeNotVerified */ $id ?>-wrapper">
        <div class="fieldset-wrapper-title admin__fieldset-wrapper-title">
            <strong <?php /* @escapeNotVerified */ echo($isCollapsable) ?
                'class="admin__collapsible-title" data-toggle="collapse" data-target="#' . $id . '-content"' :
                'class="title"'; ?>>
                <span><?= /* @escapeNotVerified */ $element->getLegend() ?></span>
            </strong>
            <?= /* @escapeNotVerified */ $titleActions ?>
        </div>
        <div class="fieldset-wrapper-content admin__fieldset-wrapper-content<?= ($isCollapsable) ? ' collapse' : '' ?>"
            id="<?= /* @escapeNotVerified */ $id ?>-content"
            data-role="<?= /* @escapeNotVerified */ $id ?>-content">
<?php endif; ?>

    <?php if (!$element->getNoContainer()): ?>
        <fieldset class="<?= /* @escapeNotVerified */ $cssClass ?>" id="<?= /* @escapeNotVerified */ $id ?>">
        <?php if ($element->getLegend() && !$isWrapped): ?>
            <legend class="<?= /* @escapeNotVerified */ $isField ? 'label admin__field-label' : 'admin__legend legend' ?>">
                <span><?= /* @escapeNotVerified */ $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-<?= /* @escapeNotVerified */ $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 data-mage-init='{"details": {}}' class="details admin__collapsible-block-wrapper" id="details<?= /* @escapeNotVerified */ $id ?>">
                <summary class="details-summary admin__collapsible-title" id="details-summary<?= /* @escapeNotVerified */ $id ?>">
                    <span><?= /* @escapeNotVerified */ $advancedLabel ?></span>
                </summary>
                <div class="details-content admin__fieldset" id="details-content<?= /* @escapeNotVerified */ $id ?>">
                    <?= $element->getAdvancedChildrenHtml() ?>
                </div>
            </details>
        <?php elseif ($element->hasAdvanced() && $isField): ?>
            <div class="nested" id="nested<?= /* @escapeNotVerified */ $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; ?>
