<?php
/************************************************************************
 * Copyright 2025 Adobe
 * All Rights Reserved.
 *
 * NOTICE: All information contained herein is, and remains
 * the property of Adobe and its suppliers, if any. The intellectual
 * and technical concepts contained herein are proprietary to Adobe
 * and its suppliers and are protected by all applicable intellectual
 * property laws, including trade secret and copyright laws.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Adobe.
 * ***********************************************************************
 */

// phpcs:disable Magento2.Templates.ThisInTemplate

/* @var \Magento\Sales\Block\Adminhtml\Order\Creditmemo\View\Form $block */
/* @var \Magento\Framework\View\Helper\SecureHtmlRenderer $secureRenderer */
/* @var \Magento\Tax\Helper\Data $helper */
/* @var \Magento\Framework\Escaper $escaper */
?>

<?php $helper = $this->helper(\Magento\Tax\Helper\Data::class); ?>
<?php $_creditMemo = $block->getCreditmemo(); ?>
<?php $_order = $_creditMemo->getOrder() ?>

<?php
$creditMemoStatus = isset($_creditMemo->getStates()[$_creditMemo->getState()])
    ? $_creditMemo->getStates()[$_creditMemo->getState()]
    : null;
$memoAdminDate = $block->formatDate($_creditMemo->getCreatedAt(), \IntlDateFormatter::MEDIUM);
?>

<div class="admin__page-section creditmemo-view-information">
    <div class="admin__page-section-title">
        <span class="title"><?= $escaper->escapeHtml(__('Credit Memo Information')) ?></span>
    </div>
    <div class="admin__page-section-content">
        <div class="admin__page-section-item creditmemo-information">
            <div class="admin__page-section-item-title">
                <?php $confirmationEmailStatusMessage = $_creditMemo->getEmailSent()
                    ? __('The credit memo confirmation email was sent')
                    : __('The credit memo confirmation email is not sent');
                ?>
                <span class="title">
                    <?= $escaper->escapeHtml(__('Memo # %1', $_creditMemo->getIncrementId())) ?>
                    (<span><?= $escaper->escapeHtml($confirmationEmailStatusMessage) ?></span>)
                </span>
            </div>
            <div class="admin__page-section-item-content">
                <table class="admin__table-secondary creditmemo-information-table">
                    <tr>
                        <th><?= $escaper->escapeHtml(__('Credit Memo Date')) ?></th>
                        <td><?= $escaper->escapeHtml($memoAdminDate) ?></td>
                    </tr>
                    <?php if ($creditMemoStatus): ?>
                        <tr>
                            <th><?= $escaper->escapeHtml(__('Status')) ?></th>
                            <td><?= $escaper->escapeHtml($creditMemoStatus) ?></td>
                        </tr>
                    <?php endif; ?>
                </table>
            </div>
        </div>
    </div>
</div>

<?= $block->getChildHtml('order_info') ?>

<section class="admin__page-section">
    <div class="admin__page-section-title">
        <span class="title"><?= $escaper->escapeHtml(__('Payment &amp; Shipping Method')) ?></span>
    </div>
    <div class="admin__page-section-content">

        <?php if (!$_order->getIsVirtual()): ?>
        <div class="admin__page-section-item order-payment-method">
        <?php else: ?>
        <div class="admin__page-section-item order-payment-method order-payment-method-virtual">
        <?php endif; ?>
            <?php /* Billing Address */?>
            <div class="admin__page-section-item-title">
                <span class="title"><?= $escaper->escapeHtml(__('Payment Information')) ?></span>
            </div>
            <div class="admin__page-section-item-content">
                <div class="order-payment-method-title"><?= $block->getChildHtml('order_payment') ?></div>
                <div class="order-payment-currency">
                    <?= $escaper->escapeHtml(
                        __('The order was placed using %1.', $_order->getOrderCurrencyCode())
                    ); ?>
                </div>
                <div class="order-payment-additional"><?= $block->getChildHtml('order_payment_additional') ?></div>
            </div>
        </div>

        <?php if (!$_order->getIsVirtual()): ?>
        <div class="admin__page-section-item order-shipping-address">
            <?php /* Shipping Address */ ?>
            <div class="admin__page-section-item-title">
                <span class="title"><?= $escaper->escapeHtml(__('Shipping Information')) ?></span>
            </div>
            <div class="shipping-description-wrapper admin__page-section-item-content">
                <div class="shipping-description-title">
                    <?= $escaper->escapeHtml($_order->getShippingDescription()) ?>
                </div>
                <div class="shipping-description-content">
                    <?= $escaper->escapeHtml(__('Total Shipping Charges')) ?>:

                    <?php if ($helper->displayShippingPriceIncludingTax()): ?>
                        <?php $_excl = $block->displayShippingPriceInclTax($_order); ?>
                    <?php else: ?>
                        <?php $_excl = $block->displayPriceAttribute('shipping_amount', false, ' '); ?>
                    <?php endif; ?>
                    <?php $_incl = $block->displayShippingPriceInclTax($_order); ?>

                    <?= /* @noEscape */ $_excl ?>
                    <?php if ($helper->displayShippingBothPrices() && $_incl != $_excl): ?>
                        (<?= $escaper->escapeHtml(__('Incl. Tax')) ?> <?= /* @noEscape */ $_incl ?>)
                    <?php endif; ?>
                </div>
            </div>
        </div>
        <?php endif; ?>
    </div>
</section>

<?php $_items = $block->getCreditmemo()->getAllItems() ?>
<?php if (count($_items)): ?>
    <div id="creditmemo_items_container">
        <?= $block->getChildHtml('creditmemo_items') ?>
    </div>
<?php else: ?>
    <section class="admin__page-section">
        <div class="admin__page-section-title">
            <span class="title"><?= $escaper->escapeHtml(__('Items Refunded')) ?></span>
        </div>
        <div class="no-items admin__page-section-content"><?= $escaper->escapeHtml(__('No Items')) ?></div>
    </section>
<?php endif; ?>

<section class="admin__page-section">
    <div class="admin__page-section-title">
        <span class="title"><?= $escaper->escapeHtml(__('Memo Total')) ?></span>
    </div>
    <div class="admin__page-section-content">
        <div class="admin__page-section-item order-comments-history">
            <div class="admin__page-section-item-title">
                <span class="title"><?= $escaper->escapeHtml(__('Credit Memo History')) ?></span>
            </div>
            <div class="admin__page-section-item-content"><?= $block->getChildHtml('order_comments') ?></div>
        </div>
        <div class="admin__page-section-item order-totals" id="history_form">
            <div class="admin__page-section-item-title">
                <span class="title"><?= $escaper->escapeHtml(__('Credit Memo Totals')) ?></span>
            </div>
            <div class="admin__page-section-content"><?= $block->getChildHtml('creditmemo_totals') ?></div>
        </div>
    </div>
</section>
<?php $scriptString = '{ "*" : {"Magento_Sales/js/comment" : {}}}';?>
<?= /* @noEscape */ $secureRenderer->renderTag('script', ['type' => 'text/x-magento-init'], $scriptString, false) ?>
