<?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) {
    $orderId = $this->getRequest()->getParam('order_id');
    $shipmentId = $this->getRequest()->getParam('shipment_id');
    $order = $block->getOrder();
    $tracking=$orderHelper->getOrderinfo($orderId);
    $shipment = $block->getShipment();
    $paymentMethod = '';
    if ($order->getPayment()) {
        $paymentMethod = $order->getPayment()->getMethodInstance()->getTitle();
    }
    $marketplaceOrders = $block->getSellerOrderInfo($orderId);
    if (count($marketplaceOrders)) {    ?>
        <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 */ __('Shipment #%1', $shipment->getIncrementId()) ?>
                        </span>
                    </h1>
                    <div class="order-date">
                        <?= /* @noEscape */ __('<span class="label">Order Date:</span> %1', '<date>' . $block->formatDate($shipment->getCreatedAt(), \IntlDateFormatter::MEDIUM, true, $block->getTimezoneForStore($order->getStore())) . '</date>') ?>
                    </div>
                    <div class="actions-toolbar order-actions-toolbar">
                        <div class="actions">
                            <a href="<?= $block->escapeUrl($block->getUrl('marketplace/order_shipment/email', ['order_id'=>$orderId,'shipment_id'=>$shipmentId, '_secure' => $this->getRequest()->isSecure()]))?>" onclick="return confirm('<?= /* @noEscape */ __("Are you sure you want to send shipment email to customer?") ?>')" class="action">
                                <button class="button wk-mp-btn" title="<?= /* @noEscape */ __('Send Email To Customer') ?>" type="button">
                                    <span><span><?= /* @noEscape */ __('Send Tracking Information') ?></span></span>
                                </button>
                            </a>
                            <a href="<?= $block->escapeUrl($block->getUrl('marketplace/order_shipment/printpdf', ['order_id'=>$orderId,'shipment_id'=>$shipmentId, '_secure' => $this->getRequest()->isSecure()]));?>" class="action">
                                <button class="button wk-mp-btn" title="<?= /* @noEscape */ __('Shipment Slip') ?>">
                                    <span><span><?= /* @noEscape */ __('Print Shipment') ?></span></span>
                                </button>
                            </a>
                            <?php
                            foreach ($block->getChildNames() as $key => $blockName) {
                                if ($blockName != 'marketplace_order_shipment_items') {
                                    echo $block->getChildHtml($blockName, false);
                                }
                            } ?>
                        </div>
                    </div>
                </div>
                <div class="block block-order-details-view">
                    <div class="block-title">
                        <strong><?= /* @noEscape */ __('Order Information') ?></strong>
                    </div>
                    <div class="block-content">
                        <div class="box-content">
                            <div class="box wk-box">
                                <strong class="box-title wk-order-info-title">
                                    <a href="<?= $block->escapeUrl($block->getUrl('marketplace/order/view', ['id'=>$orderId, '_secure' => $this->getRequest()->isSecure()]));?>">
                                        <?= /* @noEscape */ __('Order # %1', $order->getIncrementId()) ?>
                                    </a>
                                </strong>
                                <div class="box-content">
                                    <div class="wk-row">
                                        <span class="label"><?= /* @noEscape */ __('Order Status')?> : </span>
                                        <span class="value"><?= $block->escapeHtml(ucfirst($order->getStatus())); ?></span>
                                    </div>
                                    <div class="wk-row">
                                        <span class="label"><?= /* @noEscape */ __('Order Date')?> : </span>
                                        <span class="value"><?=/* @noEscape */ $block->formatDate($order->getCreatedAt(), \IntlDateFormatter::MEDIUM, true, $block->getTimezoneForStore($order->getStore())); ?></span>
                                    </div>
                                </div>
                            </div>
                            <?php if ($helper->getSellerProfileDisplayFlag()) { ?>
                                <div class="box wk-box">
                                    <strong class="box-title"><span><?= /* @noEscape */ __('Buyer Information') ?></span></strong>
                                    <div class="box-content">
                                        <div class="box wk-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>
                            <?php } ?>
                        </div>
                    </div>
                </div>
                <?php if ($helper->getSellerProfileDisplayFlag()) { ?>
                    <div class="block block-order-details-view">
                        <div class="block-title">
                            <strong><?= /* @noEscape */ __('Address Information') ?></strong>
                        </div>
                        <div class="block-content">
                            <div class="box-content">
                                <div class="box box-order-billing-address wk-box">
                                    <strong class="box-title">
                                        <span><?= /* @noEscape */ __('Billing Address') ?></span>
                                    </strong>
                                    <div class="box-content">
                                        <address><?= /* @noEscape */ $block->getFormattedAddress($order->getBillingAddress()); ?></address>
                                    </div>
                                </div>
                                <?php if (!$order->getIsVirtual()): ?>
                                    <div class="box box-order-shipping-address wk-box">
                                        <strong class="box-title"><span><?= /* @noEscape */ __('Shipping Address') ?></span></strong>
                                        <div class="box-content">
                                            <address><?= /* @noEscape */ $block->getFormattedAddress($order->getShippingAddress()); ?></address>
                                        </div>
                                    </div>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>
                <?php } ?>
                <div class="block block-order-details-view">
                    <div class="block-title">
                        <strong><?= /* @noEscape */ __('Payment & Shipping Method') ?></strong>
                    </div>
                    <div class="block-content">
                        <div class="box box-order-billing-method wk-box">
                            <strong class="box-title">
                                <span><?= /* @noEscape */ __('Payment Information') ?></span>
                            </strong>
                            <div class="box-content">
                                <?= $block->escapeHtml($paymentMethod); ?>
                            </div>
                        </div>
                        <?php if (!$order->getIsVirtual()): ?>
                            <div class="box box-order-shipping-method wk-box">
                                <strong class="box-title">
                                    <span><?= /* @noEscape */ __('Shipping and Tracking Information') ?></span>
                                </strong>
                                <div class="box-content">
                                    <?php if ($order->getShippingDescription()): ?>
                                        <?= $block->escapeHtml($order->getShippingDescription()) ?>
                                    <?php else: ?>
                                        <?= /* @noEscape */ __('No shipping information available'); ?>
                                    <?php endif; ?>
                                    <?php
                                    if ($tracking->getTrackingNumber()) {
                                        ?>
                                        <p>
                                            <a href="<?= $block->escapeUrl($this->helper(\Magento\Shipping\Helper\Data::class)->getTrackingPopupUrlBySalesModel($block->getShipment())) ?>" title="<?= /* @noEscape */ __('Track this shipment') ?>" target="blank">
                                                <?= /* @noEscape */ __('Track this shipment') ?>
                                            </a>
                                        </p>
                                        <div class="admin__control-table-wrapper">
                                            <table class="data-table admin__control-table wk-mp-list-table" id="wk-mp-shipment-tracking-info">
                                                <thead>
                                                    <tr class="headings">
                                                        <th><?= /* @noEscape */ __('Carrier') ?></th>
                                                        <th><?= /* @noEscape */ __('Title') ?></th>
                                                        <th><?= /* @noEscape */ __('Number') ?></th>
                                                        <th><?= /* @noEscape */ __('Action') ?></th>
                                                    </tr>
                                                </thead>
                                                <tfoot>
                                                    <tr>
                                                        <td>
                                                            <select name="carrier"
                                                            class="select"
                                                            id="wk-mp-tracking-carrier">
                                                                <?php foreach ($block->getCarriers() as $_code => $_name): ?>
                                                                    <option value="<?= $block->escapeHtml($_code) ?>">
                                                                        <?= $block->escapeHtml($_name) ?>
                                                                    </option>
                                                                <?php endforeach; ?>
                                                            </select>
                                                        </td>
                                                        <td >
                                                            <input class="input-text"
                                                                type="text"
                                                                id="wk-mp-tracking-title"
                                                                name="title"
                                                                value="" />
                                                        </td>
                                                        <td >
                                                            <input class="input-text"
                                                                type="text"
                                                                id="wk-mp-tracking-number"
                                                                name="number"
                                                                value="" />
                                                        </td>
                                                        <td>
                                                            <button id="wk-mp-tracking-add">
                                                                <span>
                                                                    <?= /* @noEscape */ __('Add') ?>
                                                                </span>
                                                            </button>
                                                        </td>
                                                    </tr>
                                                </tfoot>
                                            <?php if ($_tracks = $block->getShipment()->getAllTracks()): ?>
                                                <tbody id="wk-mp-shipment-tracking-info-tbody">
                                                <?php $i = 0; foreach ($_tracks as $_track):$i++ ?>
                                                    <tr>
                                                        <td><?= $block->escapeHtml($block->getCarrierTitle($_track->getCarrierCode())) ?></td>
                                                        <td><?= $block->escapeHtml($_track->getTitle()) ?></td>
                                                        <td>
                                                            <?php if ($_track->isCustom()): ?>
                                                                <?= $block->escapeHtml($_track->getNumber()) ?>
                                                            <?php else: ?>
                                                            <a href="<?= $block->escapeUrl($this->helper(\Magento\Shipping\Helper\Data::class)->getTrackingPopupUrlBySalesModel($_track)) ?>" target="blank">
                                                                <?= $block->escapeHtml($_track->getNumber()) ?>
                                                            </a>
                                                            <div id="shipment_tracking_info_response_<?= $block->escapeHtml($_track->getId()) ?>"></div>
                                                            <?php endif; ?>
                                                        </td>
                                                        <td class="col-delete">
                                                            <button class="wk-mp-tracking-action-delete action-delete" data-url="<?= $block->escapeUrl($block->trackingDeleteUrl($orderId, $shipmentId, $_track->getId()))?>">
                                                                <span>
                                                                    <?= /* @noEscape */ __('Delete') ?>
                                                                </span>
                                                            </button>
                                                        </td>
                                                    </tr>
                                                <?php endforeach; ?>
                                                </tbody>
                                            <?php endif; ?>
                                            </table>
                                        </div>
                                        <?php
                                    } ?>
                                </div>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
                <div class="block block-order-details-view wk-order-title-block">
                    <div class="block-title">
                        <strong><?= /* @noEscape */ __('Items Shipped') ?></strong>
                    </div>
                </div>
                <div class="order-details-items ordered">
                    <div class="order-title">
                        <strong><?= /* @noEscape */ __('Items Shipped') ?></strong>
                    </div>
                    <?= $block->getChildHtml('marketplace_order_shipment_items') ?>
                </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() {
                    dataForm.submit();
                });
            });
        </script>
        <script id="sellerOrderShipmentTemplate" type="text/x-jquery-tmpl">
            <tr>
                <td><%- data.carrier %></td>
                <td><%- data.title %></td>
                <td class="<%- data.numberclass %>">
                    <%- data.number %>
                </td>
                <td class="<%- data.numberclasshref %>">
                    <a href="<%- data.trackingPopupUrl %>" target="blank">
                        <%- data.number %>
                    </a>
                </td>
                <td class="col-delete">
                    <button class="wk-mp-tracking-action-delete action-delete" data-url="<%- data.trackingDeleteUrl %>">
                        <span>
                            <?= /* @noEscape */ __('Delete') ?>
                        </span>
                    </button>
                </td>
            </tr>
        </script>
        <?php
        $formData = [
            'addTrackingAjaxUrl' => $block->trackingAddUrl($orderId, $shipmentId)
        ];
        $serializedFormData = $this->helper(
            \Magento\Framework\Json\Helper\Data::class
        )->jsonEncode($formData);
        ?>
        <script type="text/x-magento-init">
            {
                "*": {
                    "sellerOrderShipment": <?= /* @noEscape */ $serializedFormData; ?>
                }
            }
        </script>
        <?php
    }
} else { ?>
    <h2 class="wk-mp-error_msg">
        <?= /* @noEscape */ __("To Become Seller Please Contact to Admin."); ?>
    </h2>
    <?php
} ?>
