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

/** @var \Magento\Framework\View\Helper\SecureHtmlRenderer $secureRenderer */
?>
<?php
/**
 * @methods
 *  getTitle() - string
 *  getSaveUrl() - string
 *  getSections() - array
 *  getForm() - html
 */
?>
<div data-mage-init='{"floatingHeader": {}}' class="page-actions"><?= $block->getSaveButtonHtml() ?></div>
<form action="<?= $block->escapeUrl($block->getSaveUrl()) ?>" method="post" id="config-edit-form"
      enctype="multipart/form-data">
    <?= $block->getBlockHtml('formkey') ?>

    <?php $scriptString = <<<script
    window.setCacheAction = function(id, button) {
        document.getElementById(id).value = button.id;

        configForm.submit();
    }
script;
    ?>
    <?= /* @noEscape */ $secureRenderer->renderTag('script', [], $scriptString, false); ?>

    <input type="hidden" id="catalog_action" name="catalog_action" value="" />
    <input type="hidden" id="jscss_action" name="jscss_action" value="" />

    <?= $block->getChildHtml('form') ?>
    <div class="entry-edit">
        <div class="entry-edit-head">
            <h4 class="icon-head head-edit-form fieldset-legend"><?= $block->escapeHtml(__('Catalog')) ?></h4>
        </div>
        <fieldset id="catalog">
            <table class="form-list">
                <tbody>
                    <?php foreach ($block->getCatalogData() as $_item): ?>
                        <?php /* disable reindex buttons. functionality moved to index management*/?>
                        <?php
                        if ($_item['buttons'][0]['name'] != 'clear_images_cache') {
                            continue;
                        }
                        ?>
                    <tr>
                        <td class="label"><label><?= $block->escapeHtml($_item['label']) ?></label></td>
                        <td class="value">
                            <?php foreach ($_item['buttons'] as $_button): ?>
                                <?php $clickAction = "setCacheAction('catalog_action',this)"; ?>
                                <?php if (isset($_button['warning']) && $_button['warning']): ?>
                                    <?php //phpcs:disable ?>
                                    <?php $clickAction = "if (confirm('" . addslashes($_button['warning']) .
                                        "')) {{$clickAction}}"; ?>
                                    <?php //phpcs:enable ?>
                                <?php endif; ?>
                                <button
                                    id="<?= $block->escapeHtmlAttr($_button['name']) ?>"
                                    type="button"
                                    class="scalable
                                <?php if (isset($_button['disabled']) && $_button['disabled']):?>disabled<?php endif;?>"
                                ><span><span><span><?= $block->escapeHtml($_button['action']) ?></span></span></span>
                                </button>
                                <?php if (!isset($_button['disabled']) || !$_button['disabled']):?>
                                    <?= /* @noEscape */ $secureRenderer->renderEventListenerAsTag(
                                        'onclick',
                                        /* @noEscape */ $clickAction,
                                        '#' . $block->escapeJs($_button['name'])
                                    ) ?>
                                <?php endif; ?>
                                <?php if (isset($_button['comment'])): ?> <br />
                                    <small><?= $block->escapeHtml($_button['comment']) ?></small>
                                <?php endif; ?>
                            <?php endforeach; ?>
                        </td>
                        <td><small>&nbsp;</small></td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </fieldset>
    </div>

    <div class="entry-edit">
        <div class="entry-edit-head">
            <h4 class="icon-head head-edit-form fieldset-legend"><?= $block->escapeHtml(__('JavaScript/CSS')) ?></h4>
        </div>

        <fieldset id="jscss">
            <table class="form-list">
                <tbody>
                    <tr>
                        <td class="label"><label><?= $block->escapeHtml(__('JavaScript/CSS Cache')) ?></label></td>
                        <td class="value">
                            <button id='jscss_action'
                                    type="button"
                                    class="scalable">
                                <span><span><span><?= $block->escapeHtml(__('Clear')) ?></span></span></span>
                            </button>
                            <?= /* @noEscape */ $secureRenderer->renderEventListenerAsTag(
                                'onclick',
                                "setCacheAction('jscss_action', this)",
                                '#jscss_action'
                            ) ?>
                        </td>
                    </tr>
                </tbody>
            </table>
        </fieldset>
    </div>
</form>
<?php $scriptString = <<<script
    require(["jquery","mage/mage"],function($){
       $('#config-edit-form').mage('form').mage('validation');
    });
script;
?>

<?= /* @noEscape */ $secureRenderer->renderTag('script', [], $scriptString, false); ?>
