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

<?php
/* @var $block \Magento\Bundle\Block\Adminhtml\Catalog\Product\Composite\Fieldset\Bundle */
/** @var \Magento\Framework\View\Helper\SecureHtmlRenderer $secureRenderer */
?>

<?php $options = $block->decorateArray($block->getOptions(true)); ?>
<?php if (count($options)): ?>
<fieldset id="catalog_product_composite_configure_fields_bundle"
          class="fieldset admin__fieldset composite-bundle<?= $block->getIsLastFieldset() ? ' last-fieldset' : '' ?>">
    <legend class="legend admin__legend">
        <span><?= $block->escapeHtml(__('Bundle Items')) ?></span>
    </legend><br />
    <?php foreach ($options as $option): ?>
        <?php if ($option->getSelections()): ?>
            <?= $block->getOptionHtml($option) ?>
        <?php endif; ?>
    <?php endforeach; ?>
</fieldset>

    <?php $scriptString = <<<script
require([
    "Magento_Catalog/catalog/product/composite/configure"
], function(){
    BundleControl = Class.create();

    BundleControl.prototype = {
        initialize: function (config) {
            this.config = config;
        },

        changeSelection: function (selection) {
            if (selection.multiple) {
                return;
            }
            var parts = selection.id.split('-'), optionId = parts[2],
                showQtyInput = selection.value && selection.value != 'none',
                options = this.config.options[optionId],
                selectionOptions = options && options.selections && options.selections[selection.value] || {};

            selectionOptions.can_change_qty = Number(selectionOptions.can_change_qty) && showQtyInput;
            this.updateQtyInput(optionId, selectionOptions);
        },

        updateQtyInput: function(optionId, selectionOptions) {
            var elem = $('bundle-option-' + optionId + '-qty-input'),
                default_qty = Number(selectionOptions.default_qty);
            if (!elem) {
                return;
            }
            if (selectionOptions.can_change_qty) {
                elem.removeClassName('qty-disabled');
                elem.disabled = false;
                elem.value = default_qty || 1;
            } else {
                elem.addClassName('qty-disabled');
                elem.disabled = true;
                elem.value = default_qty || 0;
            }
        },

        updateForDefaults: function () {
            for (var optionId in this.config.options) {
                var selection = $('bundle-option-' + optionId);
                if (selection) {
                    this.changeSelection(selection);
                }
            }
        }
    };
script;
    $scriptString .= 'ProductConfigure.bundleControl = new BundleControl(' . /* @noEscape */ $block->getJsonConfig() .
     ');';
    $scriptString .= <<<script
});
script;
    ?>
    <?= /* @noEscape */ $secureRenderer->renderTag('script', [], $scriptString, false) ?>
<?php endif; ?>
