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

/**
 * Last ordered items sidebar
 *
 * @var $block \Magento\Sales\Block\Reorder\Sidebar
 * @var \Magento\Framework\View\Helper\SecureHtmlRenderer $secureRenderer
 */
?>
<div class="block block-reorder" data-bind="scope: 'lastOrderedItems'">
    <div class="block-title no-display"
         data-bind="css: {'no-display': !lastOrderedItems().items || lastOrderedItems().items.length === 0}">
        <strong id="block-reorder-heading" role="heading" aria-level="2">
            <?= $block->escapeHtml(__('Recently Ordered')) ?>
        </strong>
    </div>
    <div class="block-content no-display"
         data-bind="css: {'no-display': !lastOrderedItems().items || lastOrderedItems().items.length === 0}"
         aria-labelledby="block-reorder-heading">
        <form method="post" class="form reorder"
              action="<?= $block->escapeUrl($block->getFormActionUrl()) ?>" id="reorder-validate-detail">
            <strong class="subtitle"><?= $block->escapeHtml(__('Last Ordered Items')) ?></strong>
            <ol id="cart-sidebar-reorder" class="product-items product-items-names"
                data-bind="foreach: lastOrderedItems().items">
                <li class="product-item">
                    <div class="field item choice">
                        <label class="label" data-bind="attr: {'for': 'reorder-item-' + id}">
                            <span><?= $block->escapeHtml(__('Add to Cart')) ?></span>
                        </label>
                        <div class="control">
                            <input type="checkbox" name="order_items[]"
                                   data-bind="attr: {
                                        id: 'reorder-item-' + id,
                                        value: id,
                                        title: is_saleable ? '<?= $block->escapeHtml(__('Add to Cart')) ?>' :
                                         '<?= $block->escapeHtml(__('Product is not salable.')) ?>'
                                   },
                                   disable: !is_saleable"
                                   class="checkbox" data-validate='{"validate-one-checkbox-required-by-name": true}'/>
                        </div>
                    </div>
                    <strong class="product-item-name">
                        <a data-bind="attr: {href: url}" class="product-item-link">
                            <span data-bind="text: name"></span>
                        </a>
                    </strong>
                </li>
            </ol>
            <div id="cart-sidebar-reorder-advice-container"></div>
            <div class="actions-toolbar">
                <div class="primary"
                     data-bind="visible: isShowAddToCart">
                    <button type="submit" title="<?= $block->escapeHtml(__('Add to Cart')) ?>"
                            class="action tocart primary">
                        <span><?= $block->escapeHtml(__('Add to Cart')) ?></span>
                    </button>
                </div>
                <div class="secondary">
                    <a class="action view"
                       href="<?= $block->escapeUrl($block->getUrl('customer/account')) ?>#my-orders-table">
                        <span><?= $block->escapeHtml(__('View All')) ?></span>
                    </a>
                </div>
            </div>
        </form>
    </div>
    <?php $scriptString = <<<script
    require(["jquery", "mage/mage"], function(jQuery){
        jQuery('#reorder-validate-detail').mage('validation', {
            errorPlacement: function(error, element) {
                error.appendTo('#cart-sidebar-reorder-advice-container');
            }
        });
    });
script;
    ?>
    <?= /* @noEscape */ $secureRenderer->renderTag('script', [], $scriptString, false) ?>
</div>
<script type="text/x-magento-init">
{
    "*": {
        "Magento_Ui/js/core/app": {
            "components": {
                "lastOrderedItems": {
                    "component": "Magento_Sales/js/view/last-ordered-items"
                }
            }
        }
    }
}
</script>
