<?php if ( ! defined( 'ABSPATH' ) ) exit; ?>
<?php

use FluentCart\App\App;
use FluentCart\Api\StoreSettings;
use FluentCart\App\Helpers\Helper;
use FluentCart\Framework\Support\Arr;
use FluentCart\App\Modules\Tax\TaxModule;
use FluentCart\App\Services\Renderer\Receipt\TaxSummaryHelper;

$settings = new StoreSettings();
$profilePage = $settings->getCustomerProfilePage();

$bgColor = '#d4edda';
$titleColor = '#155724';
$iconBg = '#155724bd';

if ($order->payment_status !== 'paid') {
    $bgColor = '#fff3cd';
    $titleColor = '#856404';
    $iconBg = '#856404bd';
}
?>

<div>
    <div style=" padding: 10px; border-radius: 5px;">
        <div class="email-template-content"
             style="font-family: Arial, Helvetica, sans-serif; max-width: 620px; margin: 0 auto; padding: 0px; width: 100%;">

            <div
                style="margin-bottom: 10px;background: <?php echo esc_attr($bgColor); ?>;padding: 30px 30px;border-radius: 4px;text-align: center;color: white;">
                <?php if ($order->payment_status !== 'paid') : ?>
                    <div
                        style="width: 80px;height: 80px;border-radius: 50%;background: <?php echo esc_attr($iconBg); ?>;display: inline-flex ;align-items: center;justify-content: center;margin-bottom: 20px;">
                        <svg style="width: 40px;height: 40px;" class="w-64 h-64" xmlns="http://www.w3.org/2000/svg"
                             viewBox="0 0 1024 1024" fill="currentColor">
                            <path
                                d="M480 674V192c0-18 14-32 32-32s32 14 32 32v482h-64zm0 63h64v60h-64v-60zM0 512C0 229 229 0 512 0s512 229 512 512-229 512-512 512S0 795 0 512zm961 0c0-247-202-448-449-448S64 265 64 512s201 448 448 448 449-201 449-448z"></path>
                        </svg>
                    </div>
                    <h1 style="font-size: 2em;line-height: 1;margin: 0;font-weight: 700;color:<?php echo esc_attr($titleColor); ?>">
                        <?php echo esc_html__('Payment Pending!', 'fluent-cart'); ?>
                    </h1>

                <?php else: ?>
                    <div
                        style="width: 80px;height: 80px;border-radius: 50%;background: <?php echo esc_attr($iconBg); ?>;display: inline-flex ;align-items: center;justify-content: center;margin-bottom: 20px;">
                        <svg style="width: 40px;height: 40px;" viewBox="0 0 24 24" fill="none" stroke="currentColor"
                             stroke-width="3" stroke-linecap="round" stroke-linejoin="round">
                            <polyline points="20,6 9,17 4,12"></polyline>
                        </svg>
                    </div>
                    <h1 style="font-size: 2em;line-height: 1;margin: 0;font-weight: 700;color:<?php echo esc_attr($titleColor); ?>">
                        <?php echo esc_html__('Purchase Successful!', 'fluent-cart'); ?>
                    </h1>
                <?php endif; ?>
            </div>

            <div style="background-color: #fff; border-radius: 8px; border: 1px solid #e7eaee; ">
                <div style="padding: 20px;">
                    <div style="overflow-x: hidden">
                        <div style="background-color: #ffffff; padding: 0px;">

                            <div class="no-print" style="margin-bottom: 16px;text-align: left; padding: 0;">
                                <p style="font-size: 24px;font-weight: 600;line-height: 1.2; margin: 0 0 4px 0; color: #2F3448;">
                                    <?php
                                    echo sprintf(
                                        /* translators: %s is the customer's full name */
                                        esc_html__('Hello %s!', 'fluent-cart'),
                                        esc_html($order->customer->full_name)
                                    );
                                    ?>
                                </p>
                                <?php if ($order->payment_status === 'paid') : ?>
                                    <p style="font-size: 14px; margin: 0; color: #2F3448;">
                                        <?php
                                        printf(
                                            '%s<strong style="color: #007bff;"><a href="%s">#%s</a></strong>%s',
                                            esc_html__('Your order ', 'fluent-cart'),
                                            esc_url($profilePage . 'order/' . $order->uuid),
                                            esc_html($order->invoice_no),
                                            esc_html__(' has been placed successfully.', 'fluent-cart')
                                        );
                                        ?>
                                    </p>
                                <?php else: ?>
                                    <p style="font-size: 14px; margin: 0; color: #2F3448;">

                                        <?php
                                        printf(
                                            '<strong style="color: #007bff;"><a href="%s">%s</a></strong> %s <a style="color: #007bff;" target="_blank" href="%s">%s</a>.',
                                            esc_url($profilePage . 'order/' . $order->uuid),
                                            esc_html__('Your order', 'fluent-cart'),
                                            esc_html__('has payment due. You can pay from', 'fluent-cart'),
                                            esc_url(\FluentCart\App\Services\Payments\PaymentHelper::getCustomPaymentLink($order->uuid)),
                                            esc_html__('here', 'fluent-cart')
                                        );
                                        ?>

                                    </p>
                                <?php endif; ?>

                            </div>

                            <!-- show tax content  -->
                            <?php 
                             $orderTaxRates = $order->getPrimaryOrderTaxRate();
                            if ($order->tax_total > 0 || $orderTaxRates): ?>
                                <div class="fct_invoice_tax_content" style="text-align: right;">
                                   <?php
                                        if ($orderTaxRates) {
                                            $storeVatNumber = Arr::get($orderTaxRates->meta, 'store_vat_number', '');
                                            $taxcountry = Arr::get($orderTaxRates->meta ?? [], 'tax_country', '');
                                            if ($storeVatNumber !== '') {
                                                echo '<p style="font-weight: 600; font-size: 14px;margin-bottom:12px;">' . esc_html(TaxModule::getCountryTaxTitle($taxcountry)) . ': ' . esc_html($storeVatNumber) . '</p>';
                                            }
                                        }
                                   ?>
                                </div>
                            <?php endif; ?>

                            

                            <?php if ($order->order_items) : ?>
                                <div>
                                    <div
                                        style="display: flex;justify-content: space-between;background: rgb(249,250,251);padding: 0 16px;">
                                        <div
                                            style="font-size: 12px;font-weight: 600;color: rgb(55, 65, 81);text-transform: uppercase;line-height: 24px;margin: 0;text-align: left;">
                                            <?php echo esc_html__('Item', 'fluent-cart'); ?>
                                        </div>
                                        <div
                                            style="width: 200px;text-align:right;font-size: 12px;font-weight: 600;color: rgb(55, 65, 81);text-transform: uppercase;line-height: 24px;margin: 0;">
                                            <?php echo esc_html__('Total', 'fluent-cart'); ?>
                                        </div>
                                    </div>

                                    <div>
                                        <?php
                                        $orderItems = $order->getProductItems()->toArray();
                                        $transaction = $order->getLatestTransaction();

                                        foreach ($orderItems as $item) :
                                            ?>
                                            <div
                                                style="display: flex;align-items: flex-start;justify-content: space-between;padding: 8px 16px;">
                                                <div>
                                                    <p style="font-size: 15px; color: #2F3448; font-weight: 500; overflow: hidden; line-height: 18px; margin-top: 0; margin-bottom: 5px;">
                                                        <?php echo esc_html($item['post_title']); ?>
                                                        <?php if ($item['quantity'] > 1): ?>
                                                            <span
                                                                style="font-size:12px;font-weight:400;color:rgb(75,85,99)">x <?php echo esc_html($item['quantity']); ?></span>
                                                        <?php endif; ?>
                                                    </p>
                                                    <p style="margin: 0; font-size: 14px; color: #758195; font-weight: 400; line-height: 15px;">
                                                        - <?php echo esc_html(!empty($item['variation_display_title']) ? $item['variation_display_title'] : $item['title']); ?>
                                                    </p>
                                                    <?php if ($item['payment_type'] === 'subscription' && !empty($item['payment_info'])): ?>
                                                        <p style="font-size:12px;color:rgb(75,85,99);line-height:20px;margin: 3px 0 0 0;">
                                                            <?php echo wp_kses_post($item['payment_info']) ?>
                                                        </p>
                                                    <?php endif; ?>
                                                </div>
                                                <div style="width: 200px;text-align: right;">
                                                    <div
                                                        style="font-size:14px;font-weight:700;color:rgb(17,24,39);margin:0;line-height:24px;">
                                                        <?php echo esc_html($item['formatted_total']); ?>
                                                    </div>
                                                </div>
                                            </div>
                                        <?php endforeach; ?>

                                        <div
                                            style="margin: 16px 0 0; width: 100%; border-top: 1px solid #e5e7eb; padding-top: 12px;">
                                            <?php if ($order->subtotal != $order->total_amount || $order->tax_total > 0): ?>
                                                <div
                                                    style="display: flex;align-items: center; justify-content: space-between;">
                                                    <div
                                                        style="font-size:14px;color:rgb(55,65,81);line-height:24px;margin: 0;">
                                                        <?php echo esc_html__('Subtotal', 'fluent-cart'); ?>
                                                    </div>
                                                    <div
                                                        style="text-transform:uppercase;font-size:13px;color:rgb(55,65,81);margin:0;line-height:24px;text-align: right"><?php echo esc_html(Helper::toDecimal($order->subtotal)); ?></div>
                                                </div>
                                            <?php endif; ?>
                                            <?php
                                            $prorateCredit = (int) \FluentCart\Framework\Support\Arr::get($order->config, 'prorate_credit', 0);
                                            $upgradeDiscount = $order->manual_discount_total - $prorateCredit;
                                            // When the prorate credit IS the whole discount, label the row directly
                                            // instead of showing "Discount" with a single redundant breakdown row.
                                            $onlyProrateCredit = $prorateCredit > 0 && $upgradeDiscount <= 0 && $order->coupon_discount_total <= 0;
                                            ?>
                                            <?php if ($order->manual_discount_total + $order->coupon_discount_total > 0): ?>
                                                <div
                                                    style="display: flex;align-items: center; justify-content: space-between;">
                                                    <div
                                                        style="font-size:14px;color:rgb(55,65,81);line-height:24px;margin: 0;">
                                                        <?php echo $onlyProrateCredit ? esc_html__('Prorate Credit', 'fluent-cart') : esc_html__('Discount', 'fluent-cart'); ?>
                                                    </div>
                                                    <div
                                                        style="text-transform:uppercase;font-size:13px;color:rgb(55,65,81);margin:0;line-height:24px;text-align: right">
                                                        - <?php echo esc_html(Helper::toDecimal($order->manual_discount_total + $order->coupon_discount_total)); ?>
                                                    </div>
                                                </div>
                                                <?php if ($prorateCredit > 0 && $upgradeDiscount > 0): ?>
                                                    <div style="display: flex;align-items: center; justify-content: space-between; padding-left: 12px;">
                                                        <div style="font-size:12px;color:rgb(107,114,128);line-height:20px;margin: 0;">
                                                            <?php echo esc_html__('Upgrade Discount', 'fluent-cart'); ?>
                                                        </div>
                                                        <div style="font-size:12px;color:rgb(107,114,128);margin:0;line-height:20px;text-align: right">
                                                            <?php echo esc_html(Helper::toDecimal($upgradeDiscount)); ?>
                                                        </div>
                                                    </div>
                                                <?php endif; ?>
                                                <?php if ($prorateCredit > 0 && !$onlyProrateCredit): ?>
                                                    <div style="display: flex;align-items: center; justify-content: space-between; padding-left: 12px;">
                                                        <div style="font-size:12px;color:rgb(107,114,128);line-height:20px;margin: 0;">
                                                            <?php echo esc_html__('Prorate Credit', 'fluent-cart'); ?>
                                                        </div>
                                                        <div style="font-size:12px;color:rgb(107,114,128);margin:0;line-height:20px;text-align: right">
                                                            <?php echo esc_html(Helper::toDecimal($prorateCredit)); ?>
                                                        </div>
                                                    </div>
                                                <?php endif; ?>
                                            <?php endif; ?>
                                            <?php if ($order->shipping_total > 0): ?>
                                                <div
                                                    style="display: flex;align-items: center; justify-content: space-between;">
                                                    <div
                                                        style="font-size:14px;color:rgb(55,65,81);line-height:24px;margin: 0;">
                                                        <?php echo esc_html__('Shipping', 'fluent-cart'); ?>
                                                    </div>
                                                    <div
                                                        style="text-transform:uppercase;font-size:13px;color:rgb(55,65,81);margin:0;line-height:24px;text-align: right"><?php echo esc_html(Helper::toDecimal($order->shipping_total)); ?></div>
                                                </div>
                                            <?php endif; ?>
                                            <?php
                                            $feeItems = $order->feeItems()->get();
                                            foreach ($feeItems as $feeItem): ?>
                                                <div
                                                    style="display: flex;align-items: center; justify-content: space-between;">
                                                    <div
                                                        style="font-size:14px;color:rgb(55,65,81);line-height:24px;margin: 0;">
                                                        <?php echo esc_html($feeItem->title); ?>
                                                    </div>
                                                    <div
                                                        style="text-transform:uppercase;font-size:13px;color:rgb(55,65,81);margin:0;line-height:24px;text-align: right"><?php echo esc_html(Helper::toDecimal($feeItem->subtotal)); ?></div>
                                                </div>
                                            <?php endforeach; ?>
                                        </div><!-- /Zone A: full-width subtotal/shipping/fees -->
                                        <?php if ($order->isReverseChargeTaxOrder() || $order->tax_total > 0): ?>
                                        <div
                                            style="background-color:rgb(249,250,251);padding:16px;border-radius:8px;margin: 12px 0 0 auto; width: 100%;max-width: 400px;">
                                            <?php if ($order->isReverseChargeTaxOrder()): ?>
                                                <?php
                                                    $rcReversedTotal = $order->getReversedTaxTotal();
                                                    $rcChargeLabel = $rcReversedTotal > 0
                                                        ? sprintf(
                                                            /* translators: %1$s: formatted reversed tax amount */
                                                            __('Tax reversed: %1$s', 'fluent-cart'),
                                                            Helper::toDecimal($rcReversedTotal)
                                                        )
                                                        : __('Charge reversed', 'fluent-cart');
                                                ?>
                                                <div
                                                    style="display: flex;align-items: center; justify-content: space-between;">
                                                    <div
                                                        style="font-size:14px;color:rgb(55,65,81);line-height:24px;margin: 0;">
                                                        <?php echo esc_html__('Tax', 'fluent-cart'); ?>
                                                    </div>
                                                    <div
                                                        style="text-transform:uppercase;font-size:13px;color:rgb(55,65,81);margin:0;line-height:24px;text-align: right">
                                                        <?php echo esc_html($rcChargeLabel); ?>
                                                    </div>
                                                </div>
                                            <?php elseif ($order->tax_total > 0): ?>
                                                <?php
                                                    $thankYouTaxSummary     = TaxSummaryHelper::computeTaxSummary($order);
                                                    $tyFoldedRateLines      = Arr::get($thankYouTaxSummary, 'foldedRateLines', []);
                                                    $tyIncludedInPrices     = (int) Arr::get($thankYouTaxSummary, 'includedInPrices', 0);
                                                    $tyPayableTax           = (int) Arr::get($thankYouTaxSummary, 'payableTax', 0);
                                                    $tyTotalOrderTax        = (int) Arr::get($thankYouTaxSummary, 'totalOrderTax', 0);
                                                ?>
                                                <?php if (!empty($tyFoldedRateLines)): ?>
                                                    <div style="font-size:11px;text-transform:uppercase;letter-spacing:0.04em;color:#94a3b8;font-weight:600;margin-bottom:4px;">
                                                        <?php esc_html_e('Tax breakdown by rate', 'fluent-cart'); ?>
                                                    </div>
                                                    <div style="display:grid;grid-template-columns:minmax(0,1fr) 92px 64px;column-gap:12px;align-items:start;margin-bottom:4px;">
                                                        <div style="font-size:11px;text-transform:uppercase;color:rgb(107,114,128);line-height:20px;margin:0;font-weight:600;min-width:0;">
                                                            <?php echo esc_html__('Rate', 'fluent-cart'); ?>
                                                        </div>
                                                        <div style="font-size:11px;text-transform:uppercase;color:rgb(107,114,128);line-height:20px;margin:0;font-weight:600;text-align:right;white-space:nowrap;">
                                                            <?php echo esc_html__('Taxable base', 'fluent-cart'); ?>
                                                        </div>
                                                        <div style="font-size:11px;text-transform:uppercase;color:rgb(107,114,128);line-height:20px;margin:0;font-weight:600;text-align:right;white-space:nowrap;">
                                                            <?php echo esc_html__('Tax', 'fluent-cart'); ?>
                                                        </div>
                                                    </div>
                                                    <?php foreach ($tyFoldedRateLines as $tyFoldedLine): ?>
                                                        <?php $tyMutedStyle = !empty($tyFoldedLine['inclusive']) ? 'opacity:0.7;' : ''; ?>
                                                        <div style="display:grid;grid-template-columns:minmax(0,1fr) 92px 64px;column-gap:12px;align-items:start;<?php echo esc_attr($tyMutedStyle); ?>">
                                                            <div style="font-size:14px;color:rgb(55,65,81);line-height:20px;margin:0;min-width:0;white-space:normal;overflow-wrap:anywhere;word-break:break-word;">
                                                                <?php echo esc_html($tyFoldedLine['label']); ?>
                                                            </div>
                                                            <div style="font-size:13px;color:rgb(107,114,128);line-height:20px;margin:0;text-align:right;white-space:nowrap;">
                                                                <?php echo esc_html(Helper::toDecimal($tyFoldedLine['base'])); ?>
                                                            </div>
                                                            <div style="text-transform:uppercase;font-size:13px;color:rgb(55,65,81);margin:0;line-height:20px;text-align:right;white-space:nowrap;">
                                                                <?php echo esc_html(Helper::toDecimal($tyFoldedLine['tax'])); ?>
                                                            </div>
                                                        </div>
                                                    <?php endforeach; ?>
                                                    <div style="display:flex;align-items:center;justify-content:space-between;margin-top:6px;border-top:1px solid #e2e8f0;padding-top:6px;">
                                                        <div style="font-size:14px;font-weight:700;color:rgb(55,65,81);line-height:24px;margin:0;">
                                                            <?php echo esc_html__('Total tax', 'fluent-cart'); ?>
                                                        </div>
                                                        <div style="text-transform:uppercase;font-size:13px;font-weight:700;color:rgb(55,65,81);margin:0;line-height:24px;text-align:right;">
                                                            <?php echo esc_html(Helper::toDecimal($tyTotalOrderTax)); ?>
                                                        </div>
                                                    </div>
                                                    <?php if ($tyIncludedInPrices > 0): ?>
                                                        <div style="display:flex;align-items:center;justify-content:space-between;">
                                                            <div style="font-size:13px;color:rgb(107,114,128);line-height:24px;margin:0;">
                                                                <?php echo esc_html__('of which included in prices', 'fluent-cart'); ?>
                                                            </div>
                                                            <div style="text-transform:uppercase;font-size:13px;color:rgb(107,114,128);margin:0;line-height:24px;text-align:right;">
                                                                <?php echo esc_html(Helper::toDecimal($tyIncludedInPrices)); ?>
                                                            </div>
                                                        </div>
                                                    <?php endif; ?>
                                                    <?php if ($tyPayableTax > 0 && $tyIncludedInPrices > 0): ?>
                                                        <div style="display:flex;align-items:center;justify-content:space-between;border-top:1px solid #e2e8f0;padding-top:6px;margin-top:6px;">
                                                            <div style="font-size:14px;font-weight:700;color:rgb(55,65,81);line-height:24px;margin:0;">
                                                                <?php echo esc_html__('Payable now (added)', 'fluent-cart'); ?>
                                                            </div>
                                                            <div style="text-transform:uppercase;font-size:13px;font-weight:700;color:rgb(55,65,81);margin:0;line-height:24px;text-align:right;">
                                                                <?php echo esc_html(Helper::toDecimal($tyPayableTax)); ?>
                                                            </div>
                                                        </div>
                                                    <?php endif; ?>
                                                <?php else: ?>
                                                    <?php $thankYouTaxLines = Arr::get($thankYouTaxSummary, 'taxRateLines', []); ?>
                                                    <?php if (!empty($thankYouTaxLines)): ?>
                                                        <?php foreach ($thankYouTaxLines as $thankYouTaxLine): ?>
                                                            <div
                                                                style="display: flex;align-items: center; justify-content: space-between;">
                                                                <div
                                                                    style="font-size:14px;color:rgb(55,65,81);line-height:24px;margin: 0;">
                                                                    <?php echo esc_html($thankYouTaxLine['label']); ?>
                                                                </div>
                                                                <div
                                                                    style="text-transform:uppercase;font-size:13px;color:rgb(55,65,81);margin:0;line-height:24px;text-align: right">
                                                                    <?php echo esc_html(Helper::toDecimal($thankYouTaxLine['order_tax'])); ?>
                                                                </div>
                                                            </div>
                                                        <?php endforeach; ?>
                                                    <?php else: ?>
                                                        <div
                                                            style="display: flex;align-items: center; justify-content: space-between;">
                                                            <div
                                                                style="font-size:14px;color:rgb(55,65,81);line-height:24px;margin: 0;">
                                                                <?php echo esc_html__('Total Tax', 'fluent-cart'); ?>
                                                                <?php echo \FluentCart\App\Helpers\Helper::getOrderTaxLabel($order); ?>
                                                            </div>
                                                            <div
                                                                style="text-transform:uppercase;font-size:13px;color:rgb(55,65,81);margin:0;line-height:24px;text-align: right">
                                                                <?php echo esc_html(Helper::toDecimal($order->tax_total)); ?>
                                                            </div>
                                                        </div>
                                                    <?php endif; ?>
                                                    <?php if ($order->shipping_tax > 0): ?>
                                                        <?php
                                                            $thankYouShippingTaxLines = Arr::get($thankYouTaxSummary, 'shippingTaxLines', []);
                                                        ?>
                                                        <?php if (!empty($thankYouShippingTaxLines)): ?>
                                                            <?php foreach ($thankYouShippingTaxLines as $thankYouShippingTaxLine): ?>
                                                                <div
                                                                    style="display: flex;align-items: center; justify-content: space-between;">
                                                                    <div
                                                                        style="font-size:14px;color:rgb(55,65,81);line-height:24px;margin: 0;">
                                                                        <?php echo esc_html($thankYouShippingTaxLine['label']); ?>
                                                                    </div>
                                                                    <div
                                                                        style="text-transform:uppercase;font-size:13px;color:rgb(55,65,81);margin:0;line-height:24px;text-align: right">
                                                                        <?php echo esc_html(Helper::toDecimal($thankYouShippingTaxLine['shipping_tax'])); ?>
                                                                    </div>
                                                                </div>
                                                            <?php endforeach; ?>
                                                        <?php else: ?>
                                                            <div
                                                                style="display: flex;align-items: center; justify-content: space-between;">
                                                                <div
                                                                    style="font-size:14px;color:rgb(55,65,81);line-height:24px;margin: 0;">
                                                                    <?php echo esc_html__('Shipping Tax', 'fluent-cart'); ?>
                                                                    <?php echo \FluentCart\App\Helpers\Helper::getOrderTaxLabel($order); ?>
                                                                </div>
                                                                <div
                                                                    style="text-transform:uppercase;font-size:13px;color:rgb(55,65,81);margin:0;line-height:24px;text-align: right">
                                                                    <?php echo esc_html(Helper::toDecimal($order->shipping_tax)); ?>
                                                                </div>
                                                            </div>
                                                        <?php endif; ?>
                                                    <?php endif; ?>
                                                <?php endif; ?>
                                            <?php endif; ?>
                                        </div><!-- /tax breakdown box -->
                                        <?php endif; ?>
                                        <div style="width: 100%; margin-top: 12px; border-top: 1px solid #e5e7eb; padding-top: 12px;"><!-- Zone C: full-width total/payment -->

                                            <?php if ($order->total_refund > 0): ?>
                                                <div
                                                    style="display: flex;align-items: center; justify-content: space-between;">
                                                    <div
                                                        style="font-size:14px;color:rgb(55,65,81);line-height:24px;margin: 0;">
                                                        <?php echo esc_html__('Refund', 'fluent-cart'); ?>
                                                    </div>
                                                    <div
                                                        style="text-transform:uppercase;font-size:13px;color:rgb(55,65,81);margin:0;line-height:24px;text-align: right">
                                                        - <?php echo esc_html(Helper::toDecimal($order->total_refund)); ?>
                                                    </div>
                                                </div>
                                            <?php endif; ?>
                                            <div
                                                style="display: flex;align-items: center; justify-content: space-between;">
                                                <div
                                                    style="font-size:16px;font-weight:700;color:rgb(55,65,81);line-height:24px;margin: 0;">
                                                    <?php echo esc_html__('Total', 'fluent-cart'); ?>
                                                </div>
                                                <div
                                                    style="text-transform:uppercase;font-size:14px;font-weight:700;color:rgb(55,65,81);margin:0;line-height:24px;text-align: right">
                                                    <?php echo esc_html(Helper::toDecimal($order->total_amount - $order->total_refund)); ?>
                                                </div>
                                            </div>
                                            <div
                                                style="display: flex;align-items: center; justify-content: space-between;">
                                                <div
                                                    style="font-size:14px;color:rgb(55,65,81);line-height:24px;margin: 0;">
                                                    <?php echo esc_html__('Payment Method', 'fluent-cart'); ?>
                                                </div>
                                                <div
                                                    style="text-transform:uppercase;font-size:13px;color:rgb(55,65,81);margin:0;line-height:24px;text-align: right">
                                                    <?php if ($transaction->card_last_4) :
                                                        echo esc_html($transaction->card_brand) . ' ' . esc_html($transaction->card_last_4);
                                                    else:
                                                        echo esc_html($transaction->payment_method);
                                                    endif; ?>
                                                </div>
                                            </div>
                                        </div>

                                        <!-- tax note -->
                                        <?php
                                         if($order->isReverseChargeTaxOrder()): ?>

                                            <div style="text-align: right; font-size: 14px; margin-top: 10px;">
                                                <?php echo '*' . esc_html__('Tax to be paid on reverse charge basis', 'fluent-cart'); ?>
                                            </div>

                                        <?php endif ?>


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

                            <?php

                            App::make('view')->render('invoice.parts.subscription_items', [
                                'subscriptions' => $order->subscriptions,
                                'order'         => $order
                            ])
                            ?>

                            <?php
                            $downloads = $order->getDownloads();
                            $licenses = $order->getLicenses();
                            if ($downloads) {
                                \FluentCart\App\App::make('view')->render('emails.parts.downloads', [
                                    'order'         => $order,
                                    'heading'       => 'Downloads',
                                    'downloadItems' => $downloads ?: [],
                                    'show_notice'   => false
                                ]);
                            }

                            if ($licenses && $licenses->count() > 0) {
                                \FluentCart\App\App::make('view')->render('emails.parts.licenses', [
                                    'licenses'    => $licenses,
                                    'heading'     => 'Licenses',
                                    'show_notice' => false
                                ]);
                            }
                            ?>

                            <?php if ($order->fulfillment_type === 'physical') : ?>
                                <div
                                    class="fct-thank-you-page-order-items-addresses"
                                    style="display: grid;grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));gap: 20px;margin-top: 20px;">

                                    <div class="fct-thank-you-page-order-items-addresses-bill-to" style="font-size:14px;border-radius: 5px;">
                                        <h5 style="font-weight: bold;font-size: 14px;margin: 0 0 10px 0;color: #495057;border-bottom: 1px solid #dee2e6;padding-bottom: 5px;">
                                            <?php echo esc_html__('Bill To', 'fluent-cart'); ?>
                                        </h5>
                                        <?php
                                        if (!empty($order->billing_address)) :
                                            ?>
                                            <div
                                                class="fct-thank-you-page-order-items-addresses-bill-to-address"
                                                style="margin-bottom: 3px;">
                                                <?php echo esc_html($order->billing_address->getAddressAsText()); ?>
                                            </div>

                                            <div
                                                class="fct-thank-you-page-order-items-addresses-bill-to-email"
                                                style="margin-top: 10px;">
                                                <?php echo esc_html($order->customer->email); ?>
                                            </div>
                                        <?php else: ?>
                                            <div
                                                class="fct-thank-you-page-order-items-addresses-bill-to-name"
                                                style="margin-top: 10px;">
                                                <?php echo esc_html($order->customer->full_name); ?>
                                            </div>
                                            <div
                                                class="fct-thank-you-page-order-items-addresses-bill-to-email"
                                                style="margin-top: 3px;">
                                                <?php echo esc_html($order->customer->email); ?>
                                            </div>
                                        <?php endif; ?>
                                        <?php
                                        $phoneNumber = Arr::get($order->billing_address->meta ?? [], 'other_data.phone', '');
                                        if ($phoneNumber !== ''):
                                        ?>
                                            <div class="fct-thank-you-page-order-items-addresses-bill-to-phone">
                                                <?php echo esc_html($phoneNumber); ?>
                                            </div>
                                        <?php endif; ?>
                                        <div class="fct-thank-you-page-order-items-addresses-bill-to-company-name">
                                            <?php
                                               $companyName = Arr::get($order->billing_address->meta ?? [], 'other_data.company_name', '');

                                               if ($companyName === '') {
                                                    $companyName = $order->getCustomerTaxName();
                                               }
                                                echo esc_html($companyName);
                                            ?>
                                        </div>
                                        <div class="fct-thank-you-page-order-items-addresses-bill-to-vat-number">
                                            <?php
                                               $vatNumber = $order->getCustomerTaxNumber();
                                               if ($vatNumber !== '') {
                                                $vatLabel = __('VAT/Tax ID', 'fluent-cart');
                                                echo esc_html($vatLabel) . ': ' . esc_html($vatNumber);
                                               }
                                            ?>
                                        </div>
                                        <?php
                                        $reverseChargeDeclaration = Arr::get($order->getBusinessInfo(), 'reverse_charge_declaration', '');
                                        if ($reverseChargeDeclaration !== ''):
                                        ?>
                                            <div class="fct-thank-you-page-order-items-addresses-bill-to-vat-number">
                                                <strong><?php echo esc_html__('Reverse Charge Declaration', 'fluent-cart'); ?>:</strong>
                                                <?php echo esc_html($reverseChargeDeclaration); ?>
                                            </div>
                                        <?php endif; ?>
                                    </div>
                                    <div
                                        class="fct-thank-you-page-order-items-addresses-ship-to"
                                         style="font-size:14px;border-radius: 5px;">
                                        <h5 class="fct-thank-you-page-order-items-addresses-ship-to-title" style="font-weight: bold;font-size: 14px;margin: 0 0 10px 0;color: #495057;border-bottom: 1px solid #dee2e6;padding-bottom: 5px;">
                                            <?php echo esc_html__('Ship To', 'fluent-cart'); ?>
                                        </h5>
                                        <?php if (!empty($order->shipping_address)) : ?>
                                            <div
                                                class="fct-thank-you-page-order-items-addresses-ship-to-address"
                                                style="margin-bottom: 3px;">
                                                <?php echo esc_html($order->shipping_address->getAddressAsText()); ?>
                                            </div>
                                            <!-- show phone number -->
                                            <?php
                                            $phone = Arr::get($order->shipping_address->meta ?? [], 'other_data.phone', '');
                                            if ($phone !== ''):?>
                                                <div class="fct-thank-you-page-order-items-addresses-ship-to-phone">
                                                    <?php echo esc_html($phone); ?>
                                                </div>
                                            <?php endif; ?>
                                        <?php else: ?>
                                            <div
                                                class="fct-thank-you-page-order-items-addresses-ship-to-name"
                                                style="margin-top: 10px;">
                                                <?php echo esc_html($order->customer->full_name); ?>
                                            </div>
                                            <div
                                                class="fct-thank-you-page-order-items-addresses-ship-to-email"
                                                style="margin-top: 3px;">
                                                <?php echo esc_html($order->customer->email); ?>
                                            </div>
                                        <?php endif; ?>
                                    </div>
                                </div>
                            <?php endif; ?>

                        </div>
                    </div>
                </div>
            </div>

        </div>
        <?php
        //        include(__DIR__ . '/../emails/components/cta.php')
        ?>
    </div>
</div>

<?php if (!empty($order)): ?>
    <div style="text-align: center;margin-top: 10px;display: flex;align-items: center;justify-content: center;gap: 10px;">
        <a href="<?php echo esc_url($profilePage . 'order/' . $order->uuid); ?>"
           style="background: var(--fluent-cart-primary-color, #253241); color: #fff; padding: 7px 20px; border-radius: 8px; border: none; font-size: 16px; font-weight: 500; text-decoration:none;display:inline-block;">
            <?php echo esc_html__('View Order', 'fluent-cart'); ?>
        </a>
        <a href="<?php echo esc_url(\FluentCart\App\Services\URL::appendQueryParams(
            home_url(),
            [
                'fluent-cart' => 'receipt',
                'order_hash'  => esc_attr($order->uuid),
                'download'    => 1
            ]
        )) ?>"
           style="background: var(--fluent-cart-primary-color, #253241);font-size:14px;color:#000;background: none;font-weight: 600;display:inline-block;">
            <?php echo esc_html__('Download Receipt', 'fluent-cart'); ?>
        </a>
    </div>

    <?php do_action('fluent_cart/after_receipt', [
        'order'           => $order,
        'is_first_time'   => $is_first_time ?? false,
        'order_operation' => $order_operation ?? null
    ]); ?>

    <?php
    if (!empty($is_first_time)) {
        do_action('fluent_cart/after_receipt_first_time', [
            'order'           => $order,
            'order_operation' => $order_operation ?? null
        ]);
    }
    ?>
<?php endif; ?>
