<?php
/**
 * Copyright 2023 Adobe
 * All Rights Reserved.
 */
// phpcs:disable Magento2.Templates.ThisInTemplate
// @codingStandardsIgnoreFile

/** @var $block \Magento\Sales\Block\Order\Recent */
/** @var $viewModel \Magento\OrderCancellationUi\ViewModel\Config */
$viewModel = $block->getViewModel();
?>
<div class="block block-dashboard-orders">
<?php
    $_orders = $block->getOrders();
    $count = count($_orders);
?>
    <div class="block-title order">
        <strong><?= $block->escapeHtml(__('Recent Orders')) ?></strong>
        <?php if ($count > 0): ?>
            <a class="action view" href="<?= $block->escapeUrl($block->getUrl('sales/order/history')) ?>">
                <span><?= $block->escapeHtml(__('View All')) ?></span>
            </a>
        <?php endif; ?>
    </div>
    <div class="block-content">
    <?= $block->getChildHtml() ?>
    <?php if ($count > 0): ?>
        <div class="table-wrapper orders-recent">
            <table class="data table table-order-items recent" id="my-orders-table">
                <caption class="table-caption"><?= $block->escapeHtml(__('Recent Orders')) ?></caption>
                <thead>
                    <tr>
                        <th scope="col" class="col id"><?= $block->escapeHtml(__('Order #')) ?></th>
                        <th scope="col" class="col date"><?= $block->escapeHtml(__('Date')) ?></th>
                        <th scope="col" class="col shipping"><?= $block->escapeHtml(__('Ship To')) ?></th>
                        <th scope="col" class="col total"><?= $block->escapeHtml(__('Order Total')) ?></th>
                        <th scope="col" class="col status"><?= $block->escapeHtml(__('Status')) ?></th>
                        <th scope="col" class="col actions"><?= $block->escapeHtml(__('Action')) ?></th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($_orders as $_order): ?>
                        <tr>
                            <td data-th="<?= $block->escapeHtml(__('Order #')) ?>" class="col id"><?= $block->escapeHtml($_order->getRealOrderId()) ?></td>
                            <td data-th="<?= $block->escapeHtml(__('Date')) ?>" class="col date"><?= $block->escapeHtml($block->formatDate($_order->getCreatedAt())) ?></td>
                            <td data-th="<?= $block->escapeHtml(__('Ship To')) ?>" class="col shipping"><?= $_order->getShippingAddress() ? $block->escapeHtml($_order->getShippingAddress()->getName()) : "&nbsp;" ?></td>
                            <td data-th="<?= $block->escapeHtml(__('Order Total')) ?>" class="col total"><?= /* @noEscape */ $_order->formatPrice($_order->getGrandTotal()) ?></td>
                            <td data-th="<?= $block->escapeHtml(__('Status')) ?>" class="col status"><?= $block->escapeHtml($_order->getStatusLabel()) ?></td>
                            <td data-th="<?= $block->escapeHtml(__('Actions')) ?>" class="col actions">
                                <?php if ($_order->getStatus() != 'received'): ?>
                                    <a href="<?= $block->escapeUrl($block->getViewUrl($_order)) ?>" class="action view">
                                        <span><?= $block->escapeHtml(__('View Order')) ?></span>
                                    </a>
                                    <?php if ($this->helper(\Magento\Sales\Helper\Reorder::class)
                                        ->canReorder($_order->getEntityId())
                                    ): ?>
                                        <a href="#" data-post='<?= /* @noEscape */
                                        $this->helper(\Magento\Framework\Data\Helper\PostHelper::class)
                                            ->getPostData($block->getReorderUrl($_order))
                                        ?>' class="action order">
                                            <span><?= $block->escapeHtml(__('Reorder')) ?></span>
                                        </a>
                                    <?php endif ?>
                                    <?php if ($viewModel->canCancel($_order->getEntityId())): ?>
                                        <a href="#" class="cancel-order"
                                           id="cancel-order-<?=/* @noEscape */ $_order->getId() ?>" data-mage-init='{
                                           "cancelOrderModal":{
                                               "url": "<?=/* @noEscape */ $block->getBaseUrl(); ?>",
                                               "order_id": "<?= $block->escapeHtml(__($_order->getId())); ?>"
                                           }
                                        }'>
                                            <span><?= $block->escapeHtml(__('Cancel Order')) ?></span>
                                        </a>
                                        <?= $this->getLayout()->createBlock("Magento\Framework\View\Element\Template")
                                            ->setOrder($_order)
                                            ->setReasons($viewModel->getCancellationReasons($_order->getEntityId()))
                                            ->setTemplate("Magento_OrderCancellationUi::cancel-order-modal.phtml")->toHtml()
                                        ?>
                                    <?php endif ?>
                                <?php endif ?>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    <?php else: ?>
        <div class="message info empty">
            <span><?= $block->escapeHtml(__('You have placed no orders.')) ?></span>
        </div>
    <?php endif; ?>
    </div>
</div>
