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

/** @var Magento\Catalog\Block\Adminhtml\Product\Edit\Tab\Inventory $block */
?>
<script>
    require(['jquery'], function($){
        $('[data-role=toggle-editability-all]').change(function(e) {
            var toggler = $(this);
            var field = toggler.parents('.field');
            var someEditable = $('input[type!="checkbox"], select, textarea', field);
            var someEditableCheckboxes = $('input[type="checkbox"]', field).not(toggler);
            if(someEditableCheckboxes.length){
                someEditable.prop('disabled', !toggler.prop('checked') || someEditableCheckboxes.prop('checked'));
                someEditableCheckboxes.prop('disabled', !toggler.prop('checked'));
            } else {
                someEditable.prop('disabled', !toggler.prop('checked'));
            }
        });
        $('[data-role=toggle-editability]').change(function(e) {
            var useConfigSettings = $(this);
            var field = useConfigSettings.parents('.field');
            var someEditable = $('input[type!="checkbox"], select, textarea', field);
            someEditable.prop('disabled', useConfigSettings.prop('checked'));
        });
    });
</script>

<?php
$defaultMinSaleQty = $block->getDefaultConfigValue('min_sale_qty');
if (!is_numeric($defaultMinSaleQty)) {
    $defaultMinSaleQty = json_decode($defaultMinSaleQty, true);
    $defaultMinSaleQty = (float) $defaultMinSaleQty[\Magento\Customer\Api\Data\GroupInterface::CUST_GROUP_ALL] ?? 1;
}
?>
<div class="fieldset-wrapper form-inline advanced-inventory-edit">
    <div class="fieldset-wrapper-title">
        <strong class="title">
            <span><?= $block->escapeHtml(__('Advanced Inventory')) ?></span>
        </strong>
    </div>
    <div class="fieldset-wrapper-content">
        <fieldset class="fieldset" id="table_cataloginventory">
        <div class="field">
            <label class="label" for="inventory_manage_stock">
                <span><?= $block->escapeHtml(__('Manage Stock')) ?></span>
            </label>

            <div class="control">
                <div class="fields-group-2">
                    <div class="field">
                        <select id="inventory_manage_stock" name="<?= /* @noEscape */ $block->getFieldSuffix() ?>[manage_stock]"
                                class="select" disabled="disabled">
                            <option value="1"><?= $block->escapeHtml(__('Yes')) ?></option>
                            <option value="0"
                                <?php if ($block->getFieldValue('manage_stock') == 0) :?>
                                    selected="selected"
                                <?php endif; ?>><?= $block->escapeHtml(__('No')) ?></option>
                        </select>
                    </div>
                    <div class="field choice">
                        <input name="<?= /* @noEscape */ $block->getFieldSuffix() ?>[use_config_manage_stock]" type="checkbox"
                               id="inventory_use_config_manage_stock" data-role="toggle-editability" value="1"
                               checked="checked" disabled="disabled"/>
                        <label for="inventory_use_config_manage_stock"
                               class="label"><span><?= $block->escapeHtml(__('Use Config Settings')) ?></span></label>
                    </div>
                    <div class="field choice">
                        <input type="checkbox" id="inventory_manage_stock_checkbox" data-role="toggle-editability-all"/>
                        <label for="inventory_manage_stock_checkbox"
                               class="label"><span><?= $block->escapeHtml(__('Change')) ?></span></label>
                    </div>
                </div>
            </div>
            <div class="field-service" value-scope="<?= $block->escapeHtmlAttr(__('[GLOBAL]')) ?>"></div>
        </div>

        <div class="field required">
            <label class="label" for="inventory_qty">
                <span><?= $block->escapeHtml(__('Qty')) ?></span>
            </label>

            <div class="control">
                <div class="fields-group-2">
                    <div class="field">
                        <input type="text" class="input-text required-entry validate-number" id="inventory_qty"
                               name="<?= /* @noEscape */ $block->getFieldSuffix() ?>[qty]"
                               value="<?= $block->getDefaultConfigValue('qty') * 1 ?>" disabled="disabled"/>
                    </div>
                    <div class="field choice">
                        <input type="checkbox" id="inventory_qty_checkbox" data-role="toggle-editability-all"/>
                        <label for="inventory_qty_checkbox"
                               class="label"><span><?= $block->escapeHtml(__('Change')) ?></span></label>
                    </div>
                </div>
            </div>
            <div class="field-service" value-scope="<?= $block->escapeHtmlAttr(__('[GLOBAL]')) ?>"></div>
        </div>

        <div class="field with-addon">
            <label class="label" for="inventory_min_qty">
                <span><?= $block->escapeHtml(__('Out-of-Stock Threshold')) ?></span>
            </label>

            <div class="control">
                <div class="fields-group-2">
                    <div class="field">
                        <input type="text" class="input-text validate-number" id="inventory_min_qty"
                               name="<?= /* @noEscape */ $block->getFieldSuffix() ?>[min_qty]"
                               value="<?= $block->getDefaultConfigValue('min_qty') * 1 ?>" disabled="disabled"/>
                    </div>
                    <div class="field choice">
                        <input type="checkbox" id="inventory_use_config_min_qty"
                               name="<?= /* @noEscape */ $block->getFieldSuffix() ?>[use_config_min_qty]" value="1"
                               data-role="toggle-editability" checked="checked" disabled="disabled"/>
                        <label for="inventory_use_config_min_qty" class="label">
                            <span><?= $block->escapeHtml(__('Use Config Settings')) ?></span>
                        </label>
                    </div>
                    <div class="field choice">
                        <input type="checkbox" id="inventory_min_qty_checkbox" data-role="toggle-editability-all"/>
                        <label for="inventory_min_qty_checkbox"
                               class="label"><span><?= $block->escapeHtml(__('Change')) ?></span></label>
                    </div>
                </div>
            </div>
            <div class="field-service" value-scope="<?= $block->escapeHtmlAttr(__('[GLOBAL]')) ?>"></div>
        </div>

        <div class="field">
            <label class="label" for="inventory_min_sale_qty">
                <span><?= $block->escapeHtml(__('Minimum Qty Allowed in Shopping Cart')) ?></span>
            </label>

            <div class="control">
                <div class="fields-group-2">
                    <div class="field">
                        <input type="text" class="input-text validate-number" id="inventory_min_sale_qty"
                               name="<?= /* @noEscape */ $block->getFieldSuffix() ?>[min_sale_qty]"
                               value="<?= $defaultMinSaleQty * 1 ?>"
                               disabled="disabled"/>
                    </div>
                    <div class="field choice">
                        <input type="checkbox" id="inventory_use_config_min_sale_qty"
                               name="<?= /* @noEscape */ $block->getFieldSuffix() ?>[use_config_min_sale_qty]"
                               value="1"
                               data-role="toggle-editability"
                               checked="checked"
                               disabled="disabled"/>
                        <label for="inventory_use_config_min_sale_qty"
                               class="label"><span><?= $block->escapeHtml(__('Use Config Settings')) ?></span></label>
                    </div>
                    <div class="field choice">
                        <input type="checkbox" id="inventory_min_sale_qty_checkbox" data-role="toggle-editability-all"/>
                        <label for="inventory_min_sale_qty_checkbox"
                               class="label"><span><?= $block->escapeHtml(__('Change')) ?></span></label>
                    </div>
                </div>
            </div>
            <div class="field-service" value-scope="<?= $block->escapeHtmlAttr(__('[GLOBAL]')) ?>"></div>
        </div>

        <div class="field">
            <label class="label" for="inventory_max_sale_qty">
                <span><?= $block->escapeHtml(__('Maximum Qty Allowed in Shopping Cart')) ?></span>
            </label>

            <div class="control">
                <div class="fields-group-2">
                    <div class="field">
                        <input type="text" class="input-text validate-number" id="inventory_max_sale_qty"
                               name="<?= /* @noEscape */ $block->getFieldSuffix() ?>[max_sale_qty]"
                               value="<?= $block->getDefaultConfigValue('max_sale_qty') * 1 ?>"
                               disabled="disabled"/>
                    </div>
                    <div class="field choice">
                        <input type="checkbox"
                               id="inventory_use_config_max_sale_qty"
                               name="<?= /* @noEscape */ $block->getFieldSuffix() ?>[use_config_max_sale_qty]"
                               value="1"
                               data-role="toggle-editability"
                               checked="checked"
                               disabled="disabled"/>
                        <label for="inventory_use_config_max_sale_qty"
                               class="label"><span><?= $block->escapeHtml(__('Use Config Settings')) ?></span></label>
                    </div>
                    <div class="field choice">
                        <input type="checkbox" id="inventory_max_sale_checkbox" data-role="toggle-editability-all"/>
                        <label for="inventory_max_sale_checkbox"
                               class="label"><span><?= $block->escapeHtml(__('Change')) ?></span></label>
                    </div>
                </div>
            </div>
            <div class="field-service" value-scope="<?= $block->escapeHtmlAttr(__('[GLOBAL]')) ?>"></div>
        </div>

        <div class="field">
            <label class="label" for="inventory_is_qty_decimal">
                <span><?= $block->escapeHtml(__('Qty Uses Decimals')) ?></span>
            </label>

            <div class="control">
                <div class="fields-group-2">
                    <div class="field">
                        <select id="inventory_is_qty_decimal"
                                name="<?= /* @noEscape */ $block->getFieldSuffix() ?>[is_qty_decimal]"
                                class="select"
                                disabled="disabled">
                            <option value="0"><?= $block->escapeHtml(__('No')) ?></option>
                            <option value="1"
                                <?php if ($block->getDefaultConfigValue('is_qty_decimal') == 1) :?>
                                    selected="selected"
                                <?php endif; ?>><?= $block->escapeHtml(__('Yes')) ?></option>
                        </select>
                    </div>
                    <div class="field choice">
                        <input type="checkbox" id="inventory_is_qty_decimal_checkbox" data-role="toggle-editability-all"/>
                        <label for="inventory_is_qty_decimal_checkbox"
                               class="label"><span><?= $block->escapeHtml(__('Change')) ?></span></label>
                    </div>
                </div>
            </div>
            <div class="field-service" value-scope="<?= $block->escapeHtmlAttr(__('[GLOBAL]')) ?>"></div>
        </div>

        <div class="field">
            <label class="label" for="inventory_backorders">
                <span><?= $block->escapeHtml(__('Backorders')) ?></span>
            </label>

            <div class="control">
                <div class="fields-group-2">
                    <div class="field">
                        <select id="inventory_backorders"
                                name="<?= /* @noEscape */ $block->getFieldSuffix() ?>[backorders]"
                                class="select"
                                disabled="disabled">
                            <?php foreach ($block->getBackordersOption() as $option) :?>
                                <?php $_selected = ($option['value'] == $block->getDefaultConfigValue('backorders')) ? ' selected="selected"' : '' ?>
                                <option
                                    value="<?= $block->escapeHtmlAttr($option['value']) ?>"<?= /* @noEscape */ $_selected ?>><?= $block->escapeHtml($option['label']) ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="field choice">
                        <input type="checkbox" id="inventory_use_config_backorders"
                               name="<?= /* @noEscape */ $block->getFieldSuffix() ?>[use_config_backorders]"
                               value="1"
                               data-role="toggle-editability"
                               checked="checked"
                               disabled="disabled"/>
                        <label for="inventory_use_config_backorders"
                               class="label"><span><?= $block->escapeHtml(__('Use Config Settings')) ?></span></label>
                    </div>
                    <div class="field choice">
                        <input type="checkbox" id="inventory_backorders_checkbox" data-role="toggle-editability-all"/>
                        <label for="inventory_backorders_checkbox"
                               class="label"><span><?= $block->escapeHtml(__('Change')) ?></span></label>
                    </div>
                </div>

            </div>
            <div class="field-service" value-scope="<?= $block->escapeHtmlAttr(__('[GLOBAL]')) ?>"></div>
        </div>

        <div class="field">
            <label class="label" for="inventory_notify_stock_qty">
                <span><?= $block->escapeHtml(__('Notify for Quantity Below')) ?></span>
            </label>

            <div class="control">
                <div class="fields-group-2">
                    <div class="field">
                        <input type="text" class="input-text validate-number" id="inventory_notify_stock_qty"
                               name="<?= /* @noEscape */ $block->getFieldSuffix() ?>[notify_stock_qty]"
                               value="<?= $block->getDefaultConfigValue('notify_stock_qty') * 1 ?>"
                               disabled="disabled"/>
                    </div>
                    <div class="field choice">
                        <input type="checkbox"
                               id="inventory_use_config_notify_stock_qty"
                               name="<?= /* @noEscape */ $block->getFieldSuffix() ?>[use_config_notify_stock_qty]"
                               value="1"
                               data-role="toggle-editability"
                               checked="checked"
                               disabled="disabled"/>
                        <label for="inventory_use_config_notify_stock_qty"
                               class="label"><span><?= $block->escapeHtml(__('Use Config Settings')) ?></span></label>
                    </div>
                    <div class="field choice">
                        <input type="checkbox" id="inventory_notify_stock_qty_checkbox" data-role="toggle-editability-all"/>
                        <label for="inventory_notify_stock_qty_checkbox"
                               class="label"><span><?= $block->escapeHtml(__('Change')) ?></span></label>
                    </div>
                </div>
            </div>
            <div class="field-service" value-scope="<?= $block->escapeHtmlAttr(__('[GLOBAL]')) ?>"></div>
        </div>

        <div class="field">
            <label class="label" for="inventory_enable_qty_increments">
                <span><?= $block->escapeHtml(__('Enable Qty Increments')) ?></span>
            </label>

            <div class="control">
                <div class="fields-group-2">
                    <div class="field">
                        <select id="inventory_enable_qty_increments"
                                name="<?= /* @noEscape */ $block->getFieldSuffix() ?>[enable_qty_increments]"
                                class="select"
                                disabled="disabled">
                            <option value="1"><?= $block->escapeHtml(__('Yes')) ?></option>
                            <option value="0"
                                <?php if ($block->getDefaultConfigValue('enable_qty_increments') == 0) :?>
                                    selected="selected"
                                <?php endif; ?>><?= $block->escapeHtml(__('No')) ?></option>
                        </select>
                    </div>
                    <div class="field choice">
                        <input type="checkbox" id="inventory_use_config_enable_qty_increments"
                               name="<?= /* @noEscape */ $block->getFieldSuffix() ?>[use_config_enable_qty_increments]"
                               value="1"
                               data-role="toggle-editability"
                               checked="checked"
                               disabled="disabled"/>
                        <label for="inventory_use_config_enable_qty_increments"
                               class="label"><span><?= $block->escapeHtml(__('Use Config Settings')) ?></span></label>
                    </div>
                    <div class="field choice">
                        <input type="checkbox" id="inventory_enable_qty_increments_checkbox" data-role="toggle-editability-all"/>
                        <label for="inventory_enable_qty_increments_checkbox"
                               class="label"><span><?= $block->escapeHtml(__('Change')) ?></span></label>
                    </div>
                </div>
            </div>
            <div class="field-service" value-scope="<?= $block->escapeHtmlAttr(__('[GLOBAL]')) ?>"></div>
        </div>

        <div class="field">
            <label class="label" for="inventory_qty_increments">
                <span><?= $block->escapeHtml(__('Qty Increments')) ?></span>
            </label>

            <div class="control">
                <div class="fields-group-2">
                    <div class="field">
                        <input type="text" class="input-text validate-number" id="inventory_qty_increments"
                               name="<?= /* @noEscape */ $block->getFieldSuffix() ?>[qty_increments]"
                               value="<?= $block->getDefaultConfigValue('qty_increments') * 1 ?>"
                               disabled="disabled"/>
                    </div>
                    <div class="field choice">
                        <input type="checkbox"
                               id="inventory_use_config_qty_increments"
                               name="<?= /* @noEscape */ $block->getFieldSuffix() ?>[use_config_qty_increments]"
                               value="1"
                               data-role="toggle-editability"
                               checked="checked"
                               disabled="disabled"/>
                        <label for="inventory_use_config_qty_increments"
                               class="label"><span><?= $block->escapeHtml(__('Use Config Settings')) ?></span></label>
                    </div>
                    <div class="field choice">
                        <input type="checkbox" id="inventory_qty_increments_checkbox" data-role="toggle-editability-all"/>
                        <label for="inventory_qty_increments_checkbox"
                               class="label"><span><?= $block->escapeHtml(__('Change')) ?></span></label>
                    </div>
                </div>
            </div>
            <div class="field-service" value-scope="<?= $block->escapeHtmlAttr(__('[GLOBAL]')) ?>"></div>
        </div>

        <div class="field">
            <label class="label" for="inventory_stock_availability">
                <span><?= $block->escapeHtml(__('Stock Availability')) ?></span>
            </label>

            <div class="control">
                <div class="fields-group-2">
                    <div class="field">
                        <select id="inventory_stock_availability"
                                name="<?= /* @noEscape */ $block->getFieldSuffix() ?>[is_in_stock]" class="select"
                                disabled="disabled">
                            <option value="1"><?= $block->escapeHtml(__('In Stock')) ?></option>
                            <option value="0"<?php if ($block->getDefaultConfigValue('is_in_stock') == 0) :?> selected<?php endif; ?>><?= $block->escapeHtml(__('Out of Stock')) ?></option>
                        </select>
                    </div>
                    <div class="field choice">
                        <input type="checkbox" id="inventory_stock_availability_checkbox" data-role="toggle-editability-all"/>
                        <label for="inventory_stock_availability_checkbox"
                               class="label"><span><?= $block->escapeHtml(__('Change')) ?></span></label>
                    </div>
                </div>
            </div>
            <div class="field-service" value-scope="<?= $block->escapeHtmlAttr(__('[GLOBAL]')) ?>"></div>
        </div>
        </fieldset>
    </div>
</div>
