<?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);
$orderId = $this->getRequest()->getParam('id');
$order = $block->getOrder();
$orderStatusLabel = $order->getStatusLabel();
$paymentCode = '';
$paymentMethod = '';
if ($order->getPayment()) {
    $paymentCode = $order->getPayment()->getMethod();
    $paymentMethod = $order->getPayment()->getMethodInstance()->getTitle();
}

$marketplaceOrders = $block->getSellerOrderInfo($orderId);
if (count($marketplaceOrders)) {
    $adminPayStatus = $block->getAdminPayStatus($orderId);
    $qtyToRefundAvail = $block->getQtyToRefundCollection($orderId);

    $tracking = $orderHelper->getOrderinfo($orderId);
    if ($tracking != "") {
        $disabled = $tracking->getTrackingNumber()==''? "":"readonly='readonly'";
        $shipmentId = $tracking->getShipmentId();
        $invoiceId=$tracking->getInvoiceId();
        $creditmemoId=$tracking->getCreditmemoId();

        $isCanceled=$tracking->getIsCanceled();
        if ($isCanceled) {
            $orderStatusLabel = 'Canceled';
        }

        if ($shipmentId) {
            $itemShipStatus = 'Shipped';
        } else {
            $itemShipStatus = '';
        }
        if ($invoiceId) {
            $itemInvoiceStatus = 'Invoiced';
        } else {
            $itemInvoiceStatus = '';
        }
        if ($creditmemoId && !$qtyToRefundAvail) {
            $shippingamount=$tracking->getShippingCharges();
            $refundedShippingAmount=$tracking->getRefundedShippingCharges();
            if ($shippingamount-$refundedShippingAmount == 0) {
                $itemRefundStatus = 'Refunded';
            } else {
                $itemRefundStatus = '';
            }
        } else {
            $itemRefundStatus = '';
        }

        $itemCancelStatus = $isCanceled;

        $invoiceId = $tracking->getInvoiceId();
    }
    ?>
    <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 */ __('Manage 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 class="actions-toolbar order-actions-toolbar">
                    <div class="actions">
                        <a onclick="this.target='_blank';" href="<?= $block->escapeUrl($block->getUrl('marketplace/order/printorder', ['id'=>$orderId, '_secure' => $this->getRequest()->isSecure()]));?>" class="action">
                            <button class="button wk-mp-btn " title="<?= /* @noEscape */ __('Print') ?>" type="submit" id="save_butn" >
                                <span><span><?= /* @noEscape */ __('Print') ?></span></span>
                            </button>
                        </a>
                        <?php if ($itemCancelStatus!="1" && $order->canCancel() && $itemInvoiceStatus!="Invoiced") {?>
                            <a href="<?= $block->escapeUrl($block->getUrl('marketplace/order/cancel', ['id'=>$orderId, '_secure' => $this->getRequest()->isSecure()]))?>" onclick="return confirm('<?= /* @noEscape */ __("Are you sure you want to cancel this order?") ?>')" class="action">
                                <button class="button wk-mp-btn" title="<?= /* @noEscape */ __('Cancel Order') ?>" type="button">
                                    <span><span><?= /* @noEscape */ __('Cancel Order') ?></span></span>
                                </button>
                            </a>
                            <?php
                        } ?>
                        <?php if ($itemCancelStatus!="1" && !$order->isCanceled()) {?>
                            <a href="<?= $block->escapeUrl($block->getUrl('marketplace/order/email', ['id'=>$orderId, '_secure' => $this->getRequest()->isSecure()]))?>" onclick="return confirm('<?= /* @noEscape */ __("Are you sure you want to send order email to customer?") ?>')" class="action">
                                <button class="button wk-mp-btn" title="<?= /* @noEscape */ __('Send Email To Customer') ?>" type="button">
                                    <span><span><?= /* @noEscape */ __('Send Email') ?></span></span>
                                </button>
                            </a>
                            <?php
                        } ?>
                        <?php if ($itemInvoiceStatus!="Invoiced" && $order->canInvoice() && $itemCancelStatus!="1") {?>
                            <a href="<?= $block->escapeUrl($block->getUrl('marketplace/order/invoice', ['id'=>$orderId, '_secure' => $this->getRequest()->isSecure()]))?>" class="action">
                                <button class="button wk-mp-btn" title="<?= /* @noEscape */ __('Create Invoice to confirm collected amount from buyer for this order') ?>" onclick="return confirm('<?= /* @noEscape */ __("Are you sure you want to create invoice?") ?>')" type="button">
                                    <span><span><?= /* @noEscape */ __('Invoice') ?></span></span>
                                </button>
                            </a>
                            <?php
                        } elseif ($itemInvoiceStatus=="Invoiced" && $itemRefundStatus!="Refunded" && $order->canCreditmemo() && $itemCancelStatus!="1") {
                            if ($paymentCode == 'mpcashondelivery' && !$adminPayStatus) { ?>
                                <a href="<?= $block->escapeUrl($block->getUrl('mpcashondelivery/order/payadmin', ['id'=>$orderId, '_secure' => $this->getRequest()->isSecure()]))?>" class="action">
                                    <button class="button wk-mp-btn" title="<?= /* @noEscape */ __('Pay Admin for his commission for this order') ?>" onclick="return confirm('<?= /* @noEscape */ __("If you pay admin commission then you can not do refund for buyer in future. Are you sure you want to pay admin for his commission?") ?>')" type="button">
                                        <span><span><?= /* @noEscape */ __('Pay Admin Commission') ?></span></span>
                                    </button>
                                </a>
                                <a href="<?= $block->escapeUrl($block->getUrl('marketplace/order_creditmemo/create', ['order_id'=>$orderId, '_secure' => $this->getRequest()->isSecure()]))?>" class="action">
                                    <button class="button wk-mp-btn" title="<?= /* @noEscape */ __('Create Credit Memo') ?>" type="button">
                                        <span><span><?= /* @noEscape */ __('Credit Memo') ?></span></span>
                                    </button>
                                </a>
                                <?php
                            } elseif ($paymentCode != 'mpcashondelivery') { ?>
                                <a href="<?= $block->escapeUrl($block->getUrl('marketplace/order_creditmemo/create', ['order_id'=>$orderId, '_secure' => $this->getRequest()->isSecure()]))?>" class="action">
                                    <button class="button wk-mp-btn" title="<?= /* @noEscape */ __('Create Credit Memo') ?>" type="button">
                                        <span><span><?= /* @noEscape */ __('Credit Memo') ?></span></span>
                                    </button>
                                </a>
                                <?php
                            }
                        } ?>
                        <?php
                        if ($itemShipStatus!="Shipped" && $itemRefundStatus!="Refunded" && $itemCancelStatus!="1" && $block->isOrderCanShip($order)) { ?>
                            <button class="button wk-mp-btn" title="<?= /* @noEscape */ __('Create Shipment for Order') ?>" type="button" id="mp-create-shipment-btn">
                                <span><span><?= /* @noEscape */ __('Ship') ?></span></span>
                            </button>
                            <?php
                        } ?>
                    </div>
                </div>
            </div>
            <?php
            $_links = $block->getLinks();
            ?>
            <ul class="items order-links">
                <?php foreach ($_links as $_link): ?>
                    <?php if (strpos($_link['url'], "marketplace/order/view") === false): ?>
                        <li class="nav item">
                            <a href="<?= $block->escapeUrl($_link['url']) ?>">
                                <?= $block->escapeHtml($_link['label']) ?>
                            </a>
                        </li>
                    <?php else: ?>
                        <li class="nav item current"><strong><?= $block->escapeHtml($_link['label']) ?></strong></li>
                    <?php endif; ?>
                <?php endforeach; ?>
            </ul>
            <div class="order-details-items ordered">
                <div class="order-title">
                    <strong><?= /* @noEscape */ __('Items Ordered') ?></strong>
                </div>
                <?= $block->getChildHtml('marketplace_order_items') ?>
            </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 ($block->isOrderCanShip($order)): ?>
                        <?php if ($helper->getSellerProfileDisplayFlag()) { ?>
                            <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>
                            <?php
                        } ?>
                        <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>
                            <?php
                            if ($itemShipStatus!="Shipped" && $itemCancelStatus!="1" && $block->isOrderCanShip($order)) { ?>
                                <form action="<?= $block->escapeUrl($block->getUrl('marketplace/order/shipment', ['_secure' => $this->getRequest()->isSecure()]))?>" id="marketplace-shipping-form" method="post">
                                    <input class="required-entry" type="hidden" value="<?= $block->escapeHtml($order->getId()); ?>" name="id"/>
                                    <table class="data-table orders wk-mp-list_table" id="my-orders-table">
                                        <col width="1" />
                                        <col width="1" />
                                        <tbody>
                                            <?php
                                            foreach ($block->getChildNames() as $key => $blockName) {
                                                if ($blockName != 'marketplace_order_items') {
                                                    /* @noEscape */ echo $block->getChildHtml($blockName, false);
                                                }
                                            } ?>
                                            <tr>
                                                <td>
                                                    <input class="carrier input-text" value="<?= $block->escapeHtml($tracking->getCarrierName()); ?>" <?= /* @noEscape */ $disabled;?> type="text" name="carrier" placeholder="<?= /* @noEscape */ __('Carrier') ?>"/>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    <input class="required-entry input-text" value="<?= $block->escapeHtml($tracking->getTrackingNumber()); ?>" <?= /* @noEscape */ $disabled;?> type="text" name="tracking_id" placeholder="<?= /* @noEscape */ __('Tracking Number') ?>"/>
                                                </td>
                                            </tr>
                                        </tbody>
                                    </table>
                                </form>
                                <?php
                            } ?>
                        </div>
                    <?php endif; ?>
                    <?php if ($helper->getSellerProfileDisplayFlag()) { ?>
                        <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">
                            <?= $block->escapeHtml($paymentMethod); ?>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="buttons-set">
        <p class="back-link">
            <a href="<?= $block->escapeUrl($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-shipping-form');
            dataForm.mage('validation', {});

            $('#mp-create-shipment-btn').click(function() {
                var confimation = confirm('<?= /* @noEscape */ __("Are you sure you want to create shipment?") ?>');
                if (confimation) {
                    dataForm.submit();
                }
            });
        });
    </script>
    <?php
}
