<?php
/**
 * Banyo Theme - Custom Order Success Page
 */

declare(strict_types=1);

use Banyo\CustomCheckout\ViewModel\OrderSuccess;
use Magento\Checkout\Block\Onepage\Success;
use Magento\Framework\Escaper;

/** @var Success $block */
/** @var Escaper $escaper */
/** @var OrderSuccess $viewModel */
$viewModel = $block->getData('order_success_view_model');

// Get order information from the view model
$order = $viewModel->getOrder();
$orderItems = $viewModel->getOrderItems();
$shippingAddress = $viewModel->getShippingAddress();
$billingAddress = $viewModel->getBillingAddress();
$paymentMethod = $viewModel->getPaymentMethod();
?>
<?php
/** @var \Magento\Directory\Model\CountryFactory $countryFactory */
$countryFactory = \Magento\Framework\App\ObjectManager::getInstance()
    ->get(\Magento\Directory\Model\CountryFactory::class);

$countryName = '';
if ($shippingAddress->getCountryId()) {
    $country = $countryFactory->create()->loadByCode($shippingAddress->getCountryId());
    $countryName = $country->getName();
}
?>



<div class="checkout-success py-4">
    <div class="bg-gray-200 md:p-6">
        <div class="px-3 2xl:container 3xl:max-w-screen-2xl py-4">
            <div class="md:flex justify-between px-3 md:px-6">
                <?php if ($order->getCustomerId()): ?>
                    <!--  Login user -->
                <div>
                    <div class="text-xl font-semibold font-inter mb-4">
                        We are getting started on your order right away
                    </div>
                    <div class="w-full md:px-4 space-y-2">
                        <!-- Item -->
                        <div class="flex space-x-2">
                            <svg class="w-5 h-5" xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 20 20" fill="none">
                                <path d="M4.16797 9.99984H15.8346M15.8346 9.99984L10.0013 4.1665M15.8346 9.99984L10.0013 15.8332" stroke="#1E1E1E" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                            </svg>
                            <a href="<?= $escaper->escapeUrl($block->getUrl('sales/order/history')) ?>"
                               class="text-base font-inter hover:underline">
                                <?= $escaper->escapeHtml(__('My Orders')) ?>
                            </a>
                        </div>
                        <!-- Item -->
                        <div class="flex space-x-2">
                            <svg class="w-5 h-5" xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 20 20" fill="none">
                                <path d="M4.16797 9.99984H15.8346M15.8346 9.99984L10.0013 4.1665M15.8346 9.99984L10.0013 15.8332" stroke="#1E1E1E" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                            </svg>
                            <a href="<?= $escaper->escapeUrl($block->getUrl('customer/account')) ?>"
                               class="text-base font-inter hover:underline">
                                <?= $escaper->escapeHtml(__('My Account')) ?>
                            </a>
                        </div>

                        <!-- Item -->
                        <div class="flex space-x-2">
                            <svg class="w-5 h-5  text-green-600" fill="currentColor" viewBox="0 0 20 20">
                                <path fill-rule="evenodd"
                                      d="M16.707 5.293a1 1 0 00-1.414 0L8 12.586 4.707 9.293a1 1 0 10-1.414 1.414l4 4a1 1 0 001.414 0l8-8a1 1 0 000-1.414z"
                                      clip-rule="evenodd"/>
                            </svg>
                            <div class="text-base font-inter ">Easily track, cancel and return your orders</div>
                        </div>
                        <!-- Item -->
                        <div class="flex space-x-2">
                            <svg class="w-5 h-5  text-green-600" fill="currentColor" viewBox="0 0 20 20">
                                <path fill-rule="evenodd"
                                      d="M16.707 5.293a1 1 0 00-1.414 0L8 12.586 4.707 9.293a1 1 0 10-1.414 1.414l4 4a1 1 0 001.414 0l8-8a1 1 0 000-1.414z"
                                      clip-rule="evenodd"/>
                            </svg>
                            <div class="text-base font-inter ">Save your favourite items to your Wishlist.</div>
                        </div>
                    </div>
                </div>
                <?php else: ?>
            <!--  New user registered-->
                    <div>
                        <div class="text-xl font-semibold font-inter mb-4">
                            Create your account now with just one click!
                        </div>
                        <div class="w-full md:px-4 space-y-2">
                            <!-- Item -->
                            <div class="flex space-x-2">
                                <svg class="w-5 h-5  text-green-600" fill="currentColor" viewBox="0 0 20 20">
                                    <path fill-rule="evenodd"
                                          d="M16.707 5.293a1 1 0 00-1.414 0L8 12.586 4.707 9.293a1 1 0 10-1.414 1.414l4 4a1 1 0 001.414 0l8-8a1 1 0 000-1.414z"
                                          clip-rule="evenodd"/>
                                </svg>
                                <div class="text-base font-inter ">Retrieve your saved basket from any device</div>
                            </div>

                            <!-- Item -->
                            <div class="flex space-x-2">
                                <svg class="w-5 h-5  text-green-600" fill="currentColor" viewBox="0 0 20 20">
                                    <path fill-rule="evenodd"
                                          d="M16.707 5.293a1 1 0 00-1.414 0L8 12.586 4.707 9.293a1 1 0 10-1.414 1.414l4 4a1 1 0 001.414 0l8-8a1 1 0 000-1.414z"
                                          clip-rule="evenodd"/>
                                </svg>
                                <div class="text-base font-inter ">Checkout faster with saved payment and shipping
                                    details
                                </div>
                            </div>

                            <!-- Item -->
                            <div class="flex space-x-2">
                                <svg class="w-5 h-5  text-green-600" fill="currentColor" viewBox="0 0 20 20">
                                    <path fill-rule="evenodd"
                                          d="M16.707 5.293a1 1 0 00-1.414 0L8 12.586 4.707 9.293a1 1 0 10-1.414 1.414l4 4a1 1 0 001.414 0l8-8a1 1 0 000-1.414z"
                                          clip-rule="evenodd"/>
                                </svg>
                                <div class="text-base font-inter ">Easily track, cancel and return your orders</div>
                            </div>

                            <!-- Item -->
                            <div class="flex space-x-2">
                                <svg class="w-5 h-5  text-green-600" fill="currentColor" viewBox="0 0 20 20">
                                    <path fill-rule="evenodd"
                                          d="M16.707 5.293a1 1 0 00-1.414 0L8 12.586 4.707 9.293a1 1 0 10-1.414 1.414l4 4a1 1 0 001.414 0l8-8a1 1 0 000-1.414z"
                                          clip-rule="evenodd"/>
                                </svg>
                                <div class="text-base font-inter ">Save your favourite items to your
                                    Wishlist.
                                </div>
                            </div>
                        </div>

                    </div>
                <?php endif; ?>
                <div class="mt-4 md:mt-0">
                    <?php if ($order->getCustomerEmail()): ?>
                        <div class="mb-4">
                            <div class="mb-1">
                                <strong><?= $escaper->escapeHtml(__('Email address for your Banyo account:')) ?></strong>
                            </div>
                            <div><?= $escaper->escapeHtml($order->getCustomerEmail()) ?></div>
                            <?php if ($order->getCustomerId()): ?>
                                <div class="mt-2">
                                    <?php if ($block->getCanViewOrder()): ?>
                                        <div class="btn btn-primary">
                                            <a href="<?= $escaper->escapeUrl($block->getViewOrderUrl()) ?>"
                                               class="order-number">
                                                <strong><?= $escaper->escapeHtml(__('View Order Confirmation')) ?></strong>
                                            </a>
                                        </div>
                                    <?php endif; ?>
                                </div>
                            <?php else: ?>
                                <div class="mt-2">
                                    <a href="<?= $escaper->escapeUrl($block->getUrl('customer/account/create')) ?>"
                                       class="btn btn-primary inline-flex">
                                        <span><?= $escaper->escapeHtml(__('Create Account')) ?></span>
                                    </a>
                                </div>
                            <?php endif; ?>


                        </div>
                    <?php endif; ?>
                </div>
            </div>

        </div>

    </div>
    <div class="px-3 2xl:container 3xl:max-w-screen-2xl py-4">
        <div class="border-2 rounded-md">

            <?php if ($block->getOrderId()): ?>
                <div class="mb-4 text-gray-700 border-b-2 p-4 text-xl md:text-3xl font-semibold flex items-center ">
                    <div class="mb-1">
                        <?php if ($block->getCanViewOrder()): ?>
                            <a href="<?= $escaper->escapeUrl($block->getViewOrderUrl()) ?>" class="order-number">
                                <strong><?= $escaper->escapeHtml(__('Order Number:')) ?> <?= $escaper->escapeHtml($block->getOrderId()) ?></strong>
                            </a>
                        <?php else: ?>
                            <strong><?= $escaper->escapeHtml(__('Order Number:')) ?> <?= $escaper->escapeHtml($block->getOrderId()) ?></strong>
                        <?php endif; ?>
                    </div>
                </div>
            <?php endif; ?>
            <div class="grid grid-cols-2 px-3 md:grid-cols-3 md:px-6">

                <?php if ($shippingAddress): ?>
                    <div class="mb-4">
                        <h3 class="text-lg font-semibold mb-2"><?= $escaper->escapeHtml(__('Shipping Address')) ?></h3>
                        <div>
                            <?= $escaper->escapeHtml($shippingAddress->getName()) ?><br>
                            <?php if ($shippingAddress->getStreetLine(1)): ?>
                                <?= $escaper->escapeHtml($shippingAddress->getStreetLine(1)) ?>
                                <br>
                            <?php endif; ?>
                            <?php if ($shippingAddress->getStreetLine(2)): ?>
                                <?= $escaper->escapeHtml($shippingAddress->getStreetLine(2)) ?>
                                <br>
                            <?php endif; ?>
                            <?php if ($shippingAddress->getCity()): ?>
                                <?= $escaper->escapeHtml($shippingAddress->getCity()) ?>,
                            <?php endif; ?>
                            <?php if ($shippingAddress->getRegion()): ?>
                                <?= $escaper->escapeHtml($shippingAddress->getRegion()) ?>
                            <?php endif; ?>
                            <?php if ($shippingAddress->getPostcode()): ?>
                                <?= $escaper->escapeHtml($shippingAddress->getPostcode()) ?>
                                <br>
                            <?php endif; ?>
                            <?php if ($countryName): ?>
                                <?= $escaper->escapeHtml($countryName) ?><br>
                                <br>
                            <?php endif; ?>
                            <?php if ($shippingAddress->getTelephone()): ?>
                                <?= $escaper->escapeHtml(__('Phone:')) ?> <?= $escaper->escapeHtml($shippingAddress->getTelephone()) ?>
                            <?php endif; ?>
                        </div>
                    </div>
                <?php endif; ?>

                <?php if ($billingAddress): ?>
                    <div class="mb-4">
                        <h3 class="text-lg font-semibold mb-2"><?= $escaper->escapeHtml(__('Invoice Address')) ?></h3>
                        <div>
                            <?= $escaper->escapeHtml($billingAddress->getName()) ?><br>
                            <?php if ($billingAddress->getStreetLine(1)): ?>
                                <?= $escaper->escapeHtml($billingAddress->getStreetLine(1)) ?>
                                <br>
                            <?php endif; ?>
                            <?php if ($billingAddress->getStreetLine(2)): ?>
                                <?= $escaper->escapeHtml($billingAddress->getStreetLine(2)) ?>
                                <br>
                            <?php endif; ?>
                            <?php if ($billingAddress->getCity()): ?>
                                <?= $escaper->escapeHtml($billingAddress->getCity()) ?>,
                            <?php endif; ?>
                            <?php if ($billingAddress->getRegion()): ?>
                                <?= $escaper->escapeHtml($billingAddress->getRegion()) ?>
                            <?php endif; ?>
                            <?php if ($billingAddress->getPostcode()): ?>
                                <?= $escaper->escapeHtml($billingAddress->getPostcode()) ?>
                                <br>
                            <?php endif; ?>
                            <?php if ($countryName): ?>
                                <?= $escaper->escapeHtml($countryName) ?><br>
                                <br>
                            <?php endif; ?>
                            <?php if ($billingAddress->getTelephone()): ?>
                                <?= $escaper->escapeHtml(__('Phone:')) ?> <?= $escaper->escapeHtml($billingAddress->getTelephone()) ?>
                            <?php endif; ?>
                        </div>
                    </div>
                <?php endif; ?>

               <!-- <div class="mb-4">
                    <h3 class="text-lg font-semibold  mb-2"><?php /*= $escaper->escapeHtml(__('Payment Method')) */?></h3>
                    <div>
                        <strong><?php /*= $escaper->escapeHtml($paymentMethod->getTitle()) */?></strong><br>
                        <?php /*= $escaper->escapeHtml(__('Amount:')) */?> <?php /*= $viewModel->formatPrice($order->getGrandTotal()) */?>
                    </div>
                </div>-->

            </div>


            <?php if (count($orderItems) > 0): ?>
            <div class="px-2 mb-4 md:px-6">
                <div class="mb-2 text-base font-semibold">
                    <?php
                    $itemCount = count($orderItems);
                    $itemLabel = $itemCount === 1 ? __('Item') : __('Items');
                    ?>
                    <?= $itemCount . ' ' . $escaper->escapeHtml($itemLabel) ?>
                    <div class="border rounded overflow-hidden">
                        <div class="w-full">
                            <div>
                                <?php foreach ($orderItems as $item): ?>
                                    <div class="flex items-center justify-between px-3 shadow">
                                        <div class="flex gap-2 py-2">
                                            <div class="w-24 flex-shrink-0">
                                                <?php
                                                $imageUrl = $viewModel->getItemImageUrl($item);
                                    ?>
                                                <?php if ($imageUrl): ?>
                                                    <img src="<?= $escaper->escapeUrl($imageUrl) ?>" alt="<?= $escaper->escapeHtml($item->getName()) ?>"
                                                         class="h-32 rounded">
                                                <?php else: ?>
                                                    <span><?= $escaper->escapeHtml(__('No image')) ?></span>
                                                <?php endif; ?>
                                            </div>
                                            <div class="flex flex-col py-1">
                                        <span class="font-medium text-base font-inter">
                                           <?= $escaper->escapeHtml($item->getName()) ?>
                                       </span>
                                                <span class="font-medium text-base font-inter">
                                           <?php if ($options = $item->getProductOptions()): ?>
                                               <?php if (isset($options['options'])): ?>
                                                   <div class="item-options">
                                                    <?php foreach ($options['options'] as $option): ?>
                                                        <div class="text-sm font-inter "><?= $escaper->escapeHtml($option['label']) ?>
                                                            :
                                                            <?php if (is_array($option['value'])): ?>
                                                                <?= nl2br(implode("\n", $option['value'])) ?>
                                                            <?php else: ?>
                                                                <?= $escaper->escapeHtml($option['value']) ?>
                                                            <?php endif; ?>
                                                        </div>
                                                    <?php endforeach; ?>
                                                </div>
                                               <?php endif; ?>
                                           <?php endif; ?>
                                       </span>
                                                <span class="font-medium text-sm font-inter">
                                                    QTY <?= $escaper->escapeHtml((int)$item->getQtyOrdered()) ?>
                                                </span>
                                            </div>
                                        </div>

                                        <div class="p-2 text-right font-semibold">
                                            <?= $viewModel->formatPrice($item->getRowTotal()) ?>
                                        </div>
                                        <div class="p-2 text-right hidden">
                                            <?= $viewModel->formatPrice($item->getPrice()) ?>
                                        </div>

                                    </div>
                                <?php endforeach; ?>
                            </div>
                        </div>
                    </div>
                </div>
                <?php endif; ?>
            </div>

            <?= $block->getAdditionalInfoHtml() ?>

            <div class="my-4 hidden">
                <a class="btn btn-primary inline-flex"
                   href="<?= $escaper->escapeUrl($block->getContinueUrl()) ?>">
                    <span><?= $escaper->escapeHtml(__('Continue Shopping')) ?></span>
                </a>
            </div>
        </div>
    </div>
