<?php
/**
 * Webkul Software.
 *
 * @category  Webkul
 * @package   Webkul_Marketplace
 * @author    Webkul
 * @copyright Copyright (c) Webkul Software Private Limited (https://webkul.com)
 * @license   https://store.webkul.com/license.html
 */
$helper = $this->helper(\Webkul\Marketplace\Helper\Data::class);
$orderHelper = $this->helper(\Webkul\Marketplace\Helper\Orders::class);
$isPartner = $helper->isSeller();
if ($isPartner == 1) {
    $order_id = $this->getRequest()->getParam('order_id');
    $order = $block->Order->load($order_id);
    $orderStatusLabel = $order->getStatusLabel();
    $paymentCode = '';
    $payment_method = '';
    if ($order->getPayment()) {
        $paymentCode = $order->getPayment()->getMethod();
        $payment_method = $order->getPayment()->getMethodInstance()->getTitle();
    }

    $marketplace_orders = $block->getSellerOrderInfo($order_id);
    if (count($marketplace_orders)) {
        $tracking=$orderHelper->getOrderinfo($order_id);
        $invoiceId=$tracking->getInvoiceId();
        if ($tracking!="" && $paymentCode == 'mpcashondelivery') {
            $codcharges=$tracking->getCodCharges();
        }
        $is_canceled=$tracking->getIsCanceled();
        if ($is_canceled) {
            $orderStatusLabel='Canceled';
        }
        ?>
        <div class="wk-mp-design">
            <div class="fieldset wk-mp-fieldset">
                <div class="page-title-wrapper">
                    <h1 class="page-title">
                        <span data-ui-id="page-title-wrapper" class="base">
                            <?= /* @noEscape */ __('Order #%1', $order->getRealOrderId()) ?>
                        </span>
                    </h1>
                    <span class="order-status"><?= $block->escapeHtml($orderStatusLabel)?></span>
                    <div class="order-date">
                        <?= /* @noEscape */ __('<span class="label">Order Date:</span> %1', '<date>' . $block->formatDate($order->getCreatedAt(), \IntlDateFormatter::MEDIUM, true, $block->getTimezoneForStore($order->getStore())) . '</date>') ?>
                    </div>
                </div>
                <?php if ($helper->getSellerProfileDisplayFlag()) { ?>
                    <div class="block block-order-details-view">
                        <div class="block-title">
                            <strong><?= /* @noEscape */ __('Buyer Information') ?></strong>
                        </div>
                        <div class="block-content">
                            <div class="box-content">
                                <div class="box">
                                    <div class="wk-row">
                                        <span class="label"><?= /* @noEscape */ __('Customer Name')?> : </span>
                                        <span class="value"><?= $block->escapeHtml($order->getCustomerName()); ?></span>
                                    </div>
                                    <div class="wk-row">
                                        <span class="label"><?= /* @noEscape */ __('Email')?> : </span>
                                        <span class="value"><?= $block->escapeHtml($order->getCustomerEmail()); ?></span>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                <?php } ?>
                <div class="block block-order-details-view">
                    <div class="block-title">
                        <strong><?= /* @noEscape */ __('Order Information') ?></strong>
                    </div>
                    <div class="block-content">
                        <?php if ($helper->getSellerProfileDisplayFlag()) { ?>
                            <?php if ($block->isOrderCanShip($order)): ?>
                                <div class="box box-order-shipping-address">
                                    <strong class="box-title"><span><?= /* @noEscape */ __('Shipping Address') ?></span></strong>
                                    <div class="box-content">
                                        <address><?= /* @noEscape */ $block->getFormattedAddress($order->getShippingAddress()); ?></address>
                                    </div>
                                </div>

                                <div class="box box-order-shipping-method">
                                    <strong class="box-title">
                                        <span><?= /* @noEscape */ __('Shipping Method') ?></span>
                                    </strong>
                                    <div class="box-content">
                                    <?php if ($order->getShippingDescription()): ?>
                                        <?= $block->escapeHtml($order->getShippingDescription()) ?>
                                    <?php else: ?>
                                        <?= /* @noEscape */ __('No shipping information available'); ?>
                                    <?php endif; ?>
                                    </div>
                                </div>
                            <?php endif; ?>
                            <div class="box box-order-billing-address">
                                <strong class="box-title">
                                    <span><?= /* @noEscape */ __('Billing Address') ?></span>
                                </strong>
                                <div class="box-content">
                                    <address><?= /* @noEscape */ $block->getFormattedAddress($order->getBillingAddress()); ?></address>
                                </div>
                            </div>
                        <?php } ?>
                        <div class="box box-order-billing-method">
                            <strong class="box-title">
                                <span><?= /* @noEscape */ __('Payment Method') ?></span>
                            </strong>
                            <div class="box-content">
                                <?= /* @noEscape */ $payment_method; ?>
                            </div>
                        </div>
                    </div>
                </div>
                <h2><?= /* @noEscape */ __('ITEMS TO REFUND') ?></h2>
                <?= $block->getChildHtml('marketplace_order_new_creditmemo_items') ?>
            </div>
        </div>
        <div class="buttons-set">
            <p class="back-link">
                <a href="<?= $block->escapeHtml($block->getUrl('marketplace/order/history', ['_secure' => $this->getRequest()->isSecure()]));?>" class="left">&laquo; <?= /* @noEscape */ __('Back To My Orders') ?></a>
            </p>
        </div>
        <script>
            require([
                "jquery",
                "mage/mage"
            ], function($){
                var dataForm = $('#marketplace-creditmemo-form');
                dataForm.mage('validation', {});

                $('#submit_creditMemo').click(function(){
                    if ($('#mp-creditmemo-do-offline')) $('#mp-creditmemo-do-offline').attr('value',0);
                    dataForm.submit()
                });

                $('#submit_creditMemo_offline').click(function(){
                    if ($('#mp-creditmemo-do-offline')) $('#mp-creditmemo-do-offline').attr('value',1);
                    dataForm.submit();
                });

                $('#send-email').click(function(){
                    if (this.checked == true){
                        $('#notify-customer').removeAttr('disabled');
                    }else{
                        $('#notify-customer').attr('disabled','disabled');
                    }
                });
            });
        </script>
        <?php
    }
} else { ?>
    <h2 class="wk-mp-error-msg">
        <?= /* @noEscape */ __("To Become Seller Please Contact to Admin."); ?>
    </h2>
    <?php
} ?>
