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

/** @var \Magento\Framework\View\Helper\SecureHtmlRenderer $secureRenderer */
?>
<?= /* @noEscape */ $block->getSomething() ?>
<div id="<?= $block->getHtmlId() ?>" class="admin__grid-massaction">

    <?php if ($block->getHideFormElement() !== true): ?>
    <form action="" id="<?= $block->getHtmlId() ?>-form" method="post">
    <?php endif ?>
        <div class="admin__grid-massaction-form">
    <?= $block->getBlockHtml('formkey') ?>
            <select
                id="<?= $block->getHtmlId() ?>-select"
                class="required-entry local-validation admin__control-select"
                <?= /* @noEscape */ $block->getUiId('select') ?>>
                <option class="admin__control-select-placeholder" value="" selected>
                    <?= $block->escapeHtml(__('Actions')) ?></option>
                <?php foreach ($block->getItems() as $_item): ?>
                    <option value="<?= $block->escapeHtmlAttr($_item->getId()) ?>"
                        <?= ($_item->getSelected() ? ' selected="selected"' : '') ?>>
                        <?= $block->escapeHtml($_item->getLabel()) ?>
                    </option>
                <?php endforeach; ?>
            </select>
            <span class="outer-span" id="<?= $block->getHtmlId() ?>-form-hiddens"></span>
            <span class="outer-span" id="<?= $block->getHtmlId() ?>-form-additional"></span>
            <?= $block->getApplyButtonHtml() ?>
        </div>
    <?php if ($block->getHideFormElement() !== true):?>
    </form>
    <?php endif ?>
    <div class="no-display">
    <?php foreach ($block->getItems() as $_item): ?>
        <div id="<?= $block->getHtmlId() ?>-item-<?= /* @noEscape */ $_item->getId() ?>-block">
            <?php if ('' != $_item->getBlockName()):?>
                <?= $block->getChildHtml($_item->getBlockName()) ?>
            <?php endif;?>
        </div>
    <?php endforeach; ?>
    </div>

    <div class="mass-select-wrap">
        <select
            id="<?= $block->getHtmlId() ?>-mass-select"
            class="action-select-multiselect _disabled"
            disabled="disabled"
            data-menu="grid-mass-select">
            <optgroup label="<?= $block->escapeHtmlAttr(__('Mass Actions')) ?>">
                <option disabled selected></option>
                <?php if ($block->getUseSelectAll()):?>
                    <option value="selectAll">
                        <?= $block->escapeHtml(__('Select All')) ?>
                    </option>
                    <option value="unselectAll">
                        <?= $block->escapeHtml(__('Unselect All')) ?>
                    </option>
                <?php endif; ?>
                <option value="selectVisible">
                    <?= $block->escapeHtml(__('Select Visible')) ?>
                </option>
                <option value="unselectVisible">
                    <?= $block->escapeHtml(__('Unselect Visible')) ?>
                </option>
            </optgroup>
        </select>
        <label for="<?= $block->getHtmlId() ?>-mass-select"></label>
    </div>

<?php $scriptString = <<<script
    require(['jquery', 'domReady!'], function($){
        'use strict';
script;
$scriptString .= '$(\'#' . $block->getHtmlId() . '-mass-select\')';
$scriptString .= <<<script
            .removeClass('_disabled')
            .prop('disabled', false)
            .change(function () {
            var massAction = $('option:selected', this).val();
            this.blur();
            switch (massAction) {
script;
if ($block->getUseSelectAll()):
    $scriptString .= '
                case \'selectAll\':
                    return ' . $block->escapeJs($block->getJsObjectName()) . '.selectAll();
                    break;
                case \'unselectAll\':
                    return ' . $block->escapeJs($block->getJsObjectName()) . '.unselectAll();
                    break;';
endif;
    $scriptString .= '
                case \'selectVisible\':
                    return  ' . $block->escapeJs($block->getJsObjectName()) . '.selectVisible();
                    break;
                case \'unselectVisible\':
                    return ' . $block->escapeJs($block->getJsObjectName()) . '.unselectVisible();
                    break;
            }
        });
    });';

if (!$block->getParentBlock()->canDisplayContainer()):
    $scriptString .= $block->escapeJs($block->getJsObjectName()) .
        '.setGridIds(\'' . $block->escapeJs($block->getGridIdsJson()) .'\');';
endif;
?>
    <?= /* @noEscape */ $secureRenderer->renderTag('script', [], $scriptString, false); ?>
</div>
