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

/** @var $block \Magento\Catalog\Block\Adminhtml\Product\Grid */
/** @var \Magento\Framework\View\Helper\SecureHtmlRenderer $secureRenderer */
?>
<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="local-validation admin__control-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-<?= $block->escapeHtmlAttr($_item->getId()) ?>-block">
            <?= $_item->getAdditionalActionBlockHtml() ?>
        </div>
    <?php endforeach; ?>
    </div>

    <div class="mass-select-wrap">
        <select id="<?= $block->escapeHtmlAttr($block->getHtmlId()) ?>-mass-select" 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'], function($){
        'use strict';
        $('#{$block->getHtmlId()}-mass-select').change(function () {
            var massAction = $('option:selected', this).val();
            switch (massAction) {
script;
    if ($block->getUseSelectAll()):
        $scriptString .= <<<script
                case 'selectAll':
                    return {$block->escapeJs($block->getJsObjectName())}.selectAll();
                    break
                case 'unselectAll':
                    return {$block->escapeJs($block->getJsObjectName())}.unselectAll();
                    break
script;
    endif;
    $scriptString .= <<<script
                case 'selectVisible':
                    return {$block->escapeJs($block->getJsObjectName())}.selectVisible();
                    break
                case 'unselectVisible':
                    return {$block->escapeJs($block->getJsObjectName())}.unselectVisible();
                    break
            }
            this.blur();
        });

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