<?php
/**
 * فروشگاه — جزئیات یک سفارش (بارگذاری جزئی در همان تب).
 *
 * Expects $order (WC_Order) set by zerti_render_shop_order_detail().
 */
if (!defined('ABSPATH')) {
    exit;
}

if (!isset($order) || !is_a($order, 'WC_Order')) {
    return;
}

$oid = (int) $order->get_id();
$created = $order->get_date_created();
$ts = ($created && is_a($created, 'WC_DateTime')) ? (int) $created->getTimestamp() : 0;
$date_label = $ts > 0 && function_exists('zerti_format_datetime')
    ? zerti_format_datetime($ts)
    : '—';

$status_slug = $order->get_status();
$status_label = function_exists('zerti_wc_order_status_label_fa')
    ? zerti_wc_order_status_label_fa((string) $status_slug)
    : (string) $status_slug;
$status_display = function_exists('zerti_localize_digits')
    ? zerti_localize_digits($status_label)
    : (function_exists('zerti_to_persian_digits') ? zerti_to_persian_digits($status_label) : $status_label);
$status_ui_dir = function_exists('zerti_get_text_direction') ? zerti_get_text_direction() : 'rtl';

$status_badge_class = 'bg-secondary';
switch ($status_slug) {
    case 'completed':
    case 'processing':
        $status_badge_class = 'bg-success';
        break;
    case 'pending':
    case 'on-hold':
    case 'draft':
    case 'checkout-draft':
        $status_badge_class = 'bg-warning text-dark';
        break;
    case 'cancelled':
    case 'failed':
        $status_badge_class = 'bg-danger';
        break;
    case 'refunded':
        $status_badge_class = 'bg-info text-dark';
        break;
    default:
        break;
}

$gateway_title = trim((string) $order->get_payment_method_title());
$gateway_id = trim((string) $order->get_payment_method());
if ($gateway_title === '' && $gateway_id !== '' && function_exists('WC')) {
    $wc = WC();
    if ($wc && $wc->payment_gateways()) {
        $registered = $wc->payment_gateways()->payment_gateways();
        if (is_array($registered) && isset($registered[$gateway_id]) && is_object($registered[$gateway_id])) {
            $gw_obj = $registered[$gateway_id];
            if (method_exists($gw_obj, 'get_title')) {
                $gateway_title = trim((string) $gw_obj->get_title());
            }
        }
    }
}
if ($gateway_title === '' && $gateway_id !== '') {
    $gateway_title = $gateway_id;
}

$customer_note = trim((string) $order->get_customer_note());
$tx_id = trim((string) $order->get_transaction_id());
$display_customer_fields = function_exists('zerti_wc_order_collect_display_customer_fields')
    ? zerti_wc_order_collect_display_customer_fields($order)
    : [];
$is_print = !empty($zerti_order_detail_print);
$is_invoice_view = !$is_print && !empty($zerti_shop_order_invoice_view);
$is_detail_view = !$is_print && !$is_invoice_view;
if ($is_invoice_view && function_exists('zerti_wc_order_invoice_display_customer_fields')) {
    $display_customer_fields = zerti_wc_order_invoice_display_customer_fields($order);
} elseif (function_exists('zerti_wc_order_sort_customer_fields_address_priority')) {
    $display_customer_fields = zerti_wc_order_sort_customer_fields_address_priority($display_customer_fields);
}
$display_customer_title = function_exists('zerti_wc_order_display_customer_section_title')
    ? zerti_wc_order_display_customer_section_title($order)
    : __('Customer information', 'zerti');

$shop_name = function_exists('zerti_site_display_name') ? zerti_site_display_name() : '';
$invoice_brand_label = $shop_name;
$invoice_brand_name = $shop_name;
$invoice_brand_slogan = '';
$logo_url = function_exists('zerti_site_logo_url') ? zerti_site_logo_url() : '';
$invoice_custom_text = '';
$show_invoice_custom_text = false;
$show_invoice_logo = true;
$show_site_identity_in_invoice = true;
$show_invoice_brand_label = true;
$show_custom_field_in_invoice = true;
$seller_info_fields = [];
$zerti_invoice_settings = function_exists('zerti_get_plugin_settings') ? zerti_get_plugin_settings() : [];
$zerti_invoice_identity = isset($zerti_invoice_settings['identity']) && is_array($zerti_invoice_settings['identity'])
    ? $zerti_invoice_settings['identity']
    : [];
$zerti_seller_name = '';
$zerti_seller_slogan = '';
if ($zerti_invoice_identity !== []) {
    $zerti_invoice_logo_id = absint($zerti_invoice_identity['logo_id'] ?? 0);
    if ($zerti_invoice_logo_id > 0) {
        $zerti_invoice_logo_url = wp_get_attachment_image_url($zerti_invoice_logo_id, 'medium');
        if (is_string($zerti_invoice_logo_url) && $zerti_invoice_logo_url !== '') {
            $logo_url = $zerti_invoice_logo_url;
        }
    }
    $zerti_seller_name = trim((string) ($zerti_invoice_identity['name'] ?? ''));
    $zerti_seller_slogan = trim((string) ($zerti_invoice_identity['slogan'] ?? ''));
    $invoice_brand_name = $zerti_seller_name;
    $invoice_brand_slogan = $zerti_seller_slogan;
    if ($zerti_seller_name !== '' && $zerti_seller_slogan !== '') {
        $invoice_brand_label = sprintf('%s: %s', $zerti_seller_name, $zerti_seller_slogan);
    } elseif ($zerti_seller_name !== '') {
        $invoice_brand_label = $zerti_seller_name;
    } elseif ($zerti_seller_slogan !== '') {
        $invoice_brand_label = $zerti_seller_slogan;
    }
}
if (($is_invoice_view || $is_print) && $zerti_invoice_settings !== []) {
    $show_invoice_logo = !empty($zerti_invoice_settings['general']['show_invoice_logo']);
    $show_site_identity_in_invoice = !empty($zerti_invoice_settings['general']['show_site_identity_in_invoice']);
    $show_invoice_brand_label = !empty($zerti_invoice_settings['general']['show_invoice_brand_label']);
    $show_custom_field_in_invoice = !empty($zerti_invoice_settings['general']['show_custom_field_in_invoice']);
    if ($zerti_seller_name !== '') {
        $seller_info_fields[] = [
            'label' => __('Seller', 'zerti'),
            'value' => zerti_to_persian_digits($zerti_seller_name),
            'key' => 'company',
        ];
    }
    $zerti_seller_custom_field_label = trim((string) ($zerti_invoice_identity['custom_field_label'] ?? ''));
    $zerti_seller_custom_field_value = trim((string) ($zerti_invoice_identity['custom_field_value'] ?? ''));
    if ($show_custom_field_in_invoice && $zerti_seller_custom_field_label !== '' && $zerti_seller_custom_field_value !== '') {
        $seller_info_fields[] = [
            'label' => zerti_to_persian_digits($zerti_seller_custom_field_label),
            'value' => zerti_to_persian_digits($zerti_seller_custom_field_value),
            'key' => 'seller_custom_field',
        ];
    }
    $zerti_seller_registration_number = trim((string) ($zerti_invoice_identity['registration_number'] ?? ''));
    if ($zerti_seller_registration_number !== '') {
        $seller_info_fields[] = [
            'label' => __('Registration number', 'zerti'),
            'value' => zerti_to_persian_digits($zerti_seller_registration_number),
            'key' => 'seller_registration_number',
        ];
    }
    $zerti_seller_economic_code = trim((string) ($zerti_invoice_identity['economic_code'] ?? ''));
    if ($zerti_seller_economic_code !== '') {
        $seller_info_fields[] = [
            'label' => __('Economic code', 'zerti'),
            'value' => zerti_to_persian_digits($zerti_seller_economic_code),
            'key' => 'seller_economic_code',
        ];
    }
    $zerti_seller_national_id = trim((string) ($zerti_invoice_identity['national_id'] ?? ''));
    if ($zerti_seller_national_id !== '') {
        $seller_info_fields[] = [
            'label' => __('National ID', 'zerti'),
            'value' => zerti_to_persian_digits($zerti_seller_national_id),
            'key' => 'seller_national_id',
        ];
    }
    $zerti_seller_postal_address = trim((string) ($zerti_invoice_identity['postal_address'] ?? ''));
    if ($zerti_seller_postal_address !== '') {
        $seller_info_fields[] = [
            'label' => __('Address', 'zerti'),
            'value' => zerti_to_persian_digits($zerti_seller_postal_address),
            'key' => 'address_1',
        ];
    }
    $zerti_seller_po_box = trim((string) ($zerti_invoice_identity['po_box'] ?? ''));
    if ($zerti_seller_po_box !== '') {
        $seller_info_fields[] = [
            'label' => __('Postal code', 'zerti'),
            'value' => zerti_to_persian_digits($zerti_seller_po_box),
            'key' => 'seller_po_box',
        ];
    }
    $zerti_seller_website = trim((string) ($zerti_invoice_identity['website'] ?? ''));
    if ($zerti_seller_website !== '') {
        $seller_info_fields[] = [
            'label' => __('Website URL', 'zerti'),
            'value' => $zerti_seller_website,
            'key' => 'seller_website',
        ];
    }
    $zerti_seller_email = trim((string) ($zerti_invoice_identity['email'] ?? ''));
    if ($zerti_seller_email !== '') {
        $seller_info_fields[] = [
            'label' => __('Email', 'zerti'),
            'value' => $zerti_seller_email,
            'key' => 'email',
        ];
    }
    $zerti_seller_phone = trim((string) ($zerti_invoice_identity['phone'] ?? ''));
    if ($zerti_seller_phone !== '') {
        $seller_info_fields[] = [
            'label' => __('Phone number', 'zerti'),
            'value' => zerti_to_persian_digits($zerti_seller_phone),
            'key' => 'seller_phone',
        ];
    }
    if ($status_slug === 'pending') {
        $show_invoice_custom_text = !empty($zerti_invoice_settings['general']['show_proforma_invoice_custom_text']);
        $invoice_custom_text = trim((string) ($zerti_invoice_identity['proforma_invoice_custom_text'] ?? ''));
    } elseif (in_array($status_slug, ['completed', 'processing'], true)) {
        $show_invoice_custom_text = !empty($zerti_invoice_settings['general']['show_invoice_custom_text']);
        $invoice_custom_text = trim((string) ($zerti_invoice_identity['invoice_custom_text'] ?? ''));
    }
}
$can_edit_order = !$is_print && current_user_can('edit_shop_order', $oid);
$status_choices = ($is_detail_view && function_exists('zerti_wc_order_status_choices'))
    ? zerti_wc_order_status_choices()
    : [];
$can_open_invoice = function_exists('zerti_shop_order_can_open_invoice')
    ? zerti_shop_order_can_open_invoice($status_slug)
    : in_array($status_slug, ['completed', 'processing', 'pending'], true);
$invoice_title = __('Order details', 'zerti');
$invoice_status_label = __('Paid', 'zerti');
$invoice_status_badge_class = 'bg-success';
if ($is_invoice_view) {
    $invoice_title = $status_slug === 'pending'
        ? __('Proforma invoice', 'zerti')
        : __('Sales invoice', 'zerti');
    $invoice_status_label = $status_slug === 'pending'
        ? __('Unpaid', 'zerti')
        : __('Paid', 'zerti');
    $invoice_status_badge_class = $status_slug === 'pending' ? 'bg-danger' : 'bg-success';
}
?>
<div
    class="zerti-component zerti-component-shop zerti-shop-order-detail<?php echo $is_invoice_view ? ' zerti-shop-order-invoice' : ''; ?>"
    data-zerti-order-id="<?php echo esc_attr((string) $oid); ?>"
    <?php if ($is_invoice_view) : ?>
        data-zerti-shop-view="invoice"
    <?php endif; ?>
>
    <div class="zerti-shop-order-detail__main zerti-scrollbar-thin">
    <?php if (!$is_print) : ?>
    <div class="zerti-shop-order-detail__actions mb-3" role="toolbar" aria-label="<?php echo $is_invoice_view ? esc_attr__('Invoice actions', 'zerti') : esc_attr__('Order actions', 'zerti'); ?>">
        <div class="zerti-shop-order-detail__actions-inner">
            <?php if ($is_invoice_view) : ?>
                <a
                    href="<?php echo esc_url(zerti_admin_route_url(['tab' => 'shop', 'order' => $oid])); ?>"
                    class="btn btn-outline-secondary btn-sm zerti-shop-order-back-detail d-inline-flex align-items-center gap-1"
                    data-zerti-order-id="<?php echo esc_attr((string) $oid); ?>"
                >
                    <i class="fa-solid fa-arrow-right" aria-hidden="true"></i>
                    <?php esc_html_e('Back to order details', 'zerti'); ?>
                </a>
                <?php if ($can_open_invoice && function_exists('zerti_shop_order_pdf_document_url') && function_exists('zerti_shop_order_pdf_is_enabled') && zerti_shop_order_pdf_is_enabled()) : ?>
                    <a
                        href="<?php echo esc_url(zerti_shop_order_pdf_document_url($oid, 'invoice')); ?>"
                        class="btn btn-outline-secondary btn-sm d-inline-flex align-items-center gap-1"
                        target="_blank"
                        rel="noopener"
                    >
                        <i class="<?php echo esc_attr(zerti_shop_order_pdf_doc_icon_classes('invoice')); ?>" aria-hidden="true"></i>
                        <?php
                        if ($status_slug === 'pending') {
                            esc_html_e('Download proforma invoice', 'zerti');
                        } else {
                            esc_html_e('Download sales invoice', 'zerti');
                        }
                        ?>
                    </a>
                    <?php if ($status_slug !== 'pending') : ?>
                    <a
                        href="<?php echo esc_url(zerti_shop_order_pdf_document_url($oid, 'postal_label')); ?>"
                        class="btn btn-outline-secondary btn-sm d-inline-flex align-items-center gap-1"
                        target="_blank"
                        rel="noopener"
                    >
                        <i class="<?php echo esc_attr(zerti_shop_order_pdf_doc_icon_classes('postal_label')); ?>" aria-hidden="true"></i>
                        <?php esc_html_e('Download postal label', 'zerti'); ?>
                    </a>
                    <a
                        href="<?php echo esc_url(zerti_shop_order_pdf_document_url($oid, 'warehouse_sheet')); ?>"
                        class="btn btn-outline-secondary btn-sm d-inline-flex align-items-center gap-1"
                        target="_blank"
                        rel="noopener"
                    >
                        <i class="<?php echo esc_attr(zerti_shop_order_pdf_doc_icon_classes('warehouse_sheet')); ?>" aria-hidden="true"></i>
                        <?php esc_html_e('Download warehouse sheet', 'zerti'); ?>
                    </a>
                    <?php endif; ?>
                <?php endif; ?>
            <?php else : ?>
                <a
                    href="<?php echo esc_url(function_exists('zerti_admin_shop_list_return_url') ? zerti_admin_shop_list_return_url() : zerti_admin_route_url(['tab' => 'shop'])); ?>"
                    class="btn btn-outline-secondary btn-sm zerti-shop-order-back d-inline-flex align-items-center gap-1"
                >
                    <i class="fa-solid fa-arrow-right" aria-hidden="true"></i>
                    <?php esc_html_e('Back to orders list', 'zerti'); ?>
                </a>
                <a
                    href="<?php echo esc_url(zerti_admin_route_url(['tab' => 'shop', 'order' => $oid, 'shop_view' => 'invoice'])); ?>"
                    class="btn btn-outline-secondary btn-sm zerti-shop-order-invoice-btn d-inline-flex align-items-center gap-1<?php echo $can_open_invoice ? '' : ' disabled'; ?>"
                    data-zerti-order-id="<?php echo esc_attr((string) $oid); ?>"
                    <?php if (!$can_open_invoice) : ?>
                        aria-disabled="true"
                        tabindex="-1"
                        title="<?php esc_attr_e('Invoice is only available for completed, processing, or pending payment orders.', 'zerti'); ?>"
                    <?php endif; ?>
                >
                    <i class="fa-solid fa-file-invoice" aria-hidden="true"></i>
                    <?php esc_html_e('View invoice', 'zerti'); ?>
                </a>
            <?php endif; ?>
        </div>
    </div>
    <?php endif; ?>

    <div class="zerti-shop-order-detail__stack">
    <article class="zerti-shop-invoice zerti-shop-order-detail__card" aria-label="<?php esc_attr_e('Order details', 'zerti'); ?>">
        <header class="zerti-shop-invoice__header">
            <div class="zerti-shop-invoice__brand">
                <?php if ($logo_url !== '' && (!$is_invoice_view && !$is_print || $show_invoice_logo)) : ?>
                    <img
                        class="zerti-shop-invoice__logo"
                        src="<?php echo esc_url($logo_url); ?>"
                        alt="<?php echo $invoice_brand_label !== '' ? esc_attr($invoice_brand_label) : esc_attr__('Store logo', 'zerti'); ?>"
                        width="48"
                        height="48"
                        decoding="async"
                    />
                <?php endif; ?>
                <div class="zerti-shop-invoice__brand-text">
                    <?php if ($show_invoice_brand_label && $invoice_brand_label !== '') : ?>
                        <p class="zerti-shop-invoice__seller mb-0">
                            <?php if ($invoice_brand_name !== '') : ?>
                                <span class="zerti-shop-invoice__seller-name"><?php echo esc_html(zerti_to_persian_digits($invoice_brand_name)); ?></span>
                            <?php endif; ?>
                            <?php if ($invoice_brand_slogan !== '') : ?>
                                <span class="zerti-shop-invoice__seller-slogan"><?php echo esc_html(zerti_to_persian_digits($invoice_brand_slogan)); ?></span>
                            <?php elseif ($invoice_brand_name === '') : ?>
                                <span class="zerti-shop-invoice__seller-name"><?php echo esc_html(zerti_to_persian_digits($invoice_brand_label)); ?></span>
                            <?php endif; ?>
                        </p>
                    <?php endif; ?>
                    <h2 class="zerti-shop-invoice__title mb-0">
                        <?php echo esc_html($invoice_title); ?>
                    </h2>
                </div>
            </div>

            <dl class="zerti-shop-invoice__meta mb-0">
                <div class="zerti-shop-invoice__meta-row">
                    <dt><?php esc_html_e('Invoice number', 'zerti'); ?></dt>
                    <dd dir="rtl"><?php echo esc_html(zerti_to_persian_digits((string) $oid)); ?></dd>
                </div>
                <div class="zerti-shop-invoice__meta-row">
                    <dt><?php esc_html_e('Issue date', 'zerti'); ?></dt>
                    <dd><?php echo esc_html(zerti_to_persian_digits($date_label)); ?></dd>
                </div>
                <div class="zerti-shop-invoice__meta-row zerti-shop-invoice__meta-row--status">
                    <dt><?php esc_html_e('Status', 'zerti'); ?></dt>
                    <dd class="zerti-shop-order-detail__meta-status">
                        <?php if ($is_detail_view && $can_edit_order && $status_choices !== []) : ?>
                            <div class="zerti-shop-order-detail__action zerti-shop-order-detail__action--status">
                                <select
                                    id="zerti-shop-order-status-<?php echo esc_attr((string) $oid); ?>"
                                    class="form-select zerti-admin-select zerti-shop-order-status-select"
                                    data-zerti-order-id="<?php echo esc_attr((string) $oid); ?>"
                                    data-zerti-current-status="<?php echo esc_attr((string) $status_slug); ?>"
                                    aria-label="<?php esc_attr_e('Order status', 'zerti'); ?>"
                                >
                                    <?php foreach ($status_choices as $choice_slug => $choice_label) : ?>
                                        <option value="<?php echo esc_attr((string) $choice_slug); ?>" <?php selected($status_slug, $choice_slug); ?>>
                                            <?php echo esc_html(function_exists('zerti_localize_digits') ? zerti_localize_digits((string) $choice_label) : (string) $choice_label); ?>
                                        </option>
                                    <?php endforeach; ?>
                                </select>
                                <button
                                    type="button"
                                    class="btn btn-sm btn-primary zerti-shop-order-status-apply-btn"
                                    data-zerti-order-id="<?php echo esc_attr((string) $oid); ?>"
                                >
                                    <?php esc_html_e('Apply status', 'zerti'); ?>
                                </button>
                            </div>
                        <?php elseif ($is_invoice_view) : ?>
                            <span class="badge <?php echo esc_attr($invoice_status_badge_class); ?>" dir="<?php echo esc_attr($status_ui_dir); ?>">
                                <?php echo esc_html($invoice_status_label); ?>
                            </span>
                        <?php else : ?>
                            <span class="badge <?php echo esc_attr($status_badge_class); ?>" dir="<?php echo esc_attr($status_ui_dir); ?>">
                                <?php echo esc_html($status_display); ?>
                            </span>
                        <?php endif; ?>
                    </dd>
                </div>
                <div class="zerti-shop-invoice__meta-row">
                    <dt><?php esc_html_e('Payment method', 'zerti'); ?></dt>
                    <dd><?php echo esc_html(zerti_wc_payment_gateway_display_label($gateway_id, $gateway_title)); ?></dd>
                </div>
                <?php if ($tx_id !== '') : ?>
                    <div class="zerti-shop-invoice__meta-row">
                        <dt><?php esc_html_e('Transaction ID', 'zerti'); ?></dt>
                        <dd dir="ltr"><?php echo esc_html(zerti_to_persian_digits($tx_id)); ?></dd>
                    </div>
                <?php endif; ?>
            </dl>
        </header>

        <div class="zerti-shop-invoice__divider" role="presentation"></div>

        <?php if (($is_invoice_view || $is_print) && $show_site_identity_in_invoice && $seller_info_fields !== []) : ?>
            <section class="zerti-shop-invoice__party" aria-labelledby="zerti-invoice-seller-heading">
                <h3 id="zerti-invoice-seller-heading" class="zerti-shop-invoice__section-title">
                    <?php esc_html_e('Seller information', 'zerti'); ?>
                </h3>
                <div class="zerti-shop-invoice__party-body">
                    <?php
                    if (function_exists('zerti_wc_render_order_customer_field_rows')) {
                        zerti_wc_render_order_customer_field_rows($seller_info_fields);
                    } else {
                        echo '<p class="text-body-secondary mb-0">—</p>';
                    }
                    ?>
                </div>
            </section>
        <?php endif; ?>

        <section class="zerti-shop-invoice__party" aria-labelledby="zerti-invoice-party-heading">
            <h3 id="zerti-invoice-party-heading" class="zerti-shop-invoice__section-title">
                <?php echo esc_html($display_customer_title); ?>
            </h3>
            <div class="zerti-shop-invoice__party-body">
                <?php
                if (function_exists('zerti_wc_render_order_customer_field_rows')) {
                    zerti_wc_render_order_customer_field_rows($display_customer_fields);
                } else {
                    echo '<p class="text-body-secondary mb-0">—</p>';
                }
                ?>
            </div>
        </section>

        <?php if ($customer_note !== '') : ?>
            <section class="zerti-shop-invoice__note" aria-labelledby="zerti-invoice-note-heading">
                <h3 id="zerti-invoice-note-heading" class="zerti-shop-invoice__section-title">
                    <?php esc_html_e('Customer note', 'zerti'); ?>
                </h3>
                <p class="zerti-shop-invoice__note-text mb-0"><?php echo esc_html($customer_note); ?></p>
            </section>
        <?php endif; ?>

        <section class="zerti-shop-invoice__items" aria-labelledby="zerti-invoice-items-heading">
            <h3 id="zerti-invoice-items-heading" class="zerti-shop-invoice__section-title visually-hidden">
                <?php esc_html_e('Invoice items', 'zerti'); ?>
            </h3>
            <?php
            $zerti_items_lbl_row = __('Row', 'zerti');
            $zerti_items_lbl_id = __('ID', 'zerti');
            $zerti_items_lbl_sku = __('Inventory ID', 'zerti');
            $zerti_items_lbl_product = __('Product', 'zerti');
            $zerti_items_lbl_price = __('Price', 'zerti');
            $zerti_items_lbl_discount = __('Discount amount', 'zerti');
            $zerti_items_lbl_tax = __('Tax amount', 'zerti');
            $zerti_items_lbl_qty = __('Quantity', 'zerti');
            $zerti_items_lbl_total = __('Total amount', 'zerti');
            $zerti_items_lbl_desc = __('Description', 'zerti');
            ?>
            <div class="table-responsive zerti-shop-order-items-wrap zerti-shop-order-items-wrap--stacked-md">
                <table class="table table-sm align-middle mb-0 zerti-shop-order-items-table zerti-shop-invoice__table">
                    <thead>
                        <tr>
                            <th scope="col" class="text-center"><?php echo esc_html($zerti_items_lbl_row); ?></th>
                            <th scope="col" class="text-center"><?php echo esc_html($zerti_items_lbl_id); ?></th>
                            <th scope="col" class="text-center"><?php echo esc_html($zerti_items_lbl_sku); ?></th>
                            <th scope="col"><?php echo esc_html($zerti_items_lbl_product); ?></th>
                            <th scope="col" class="text-end"><?php echo esc_html($zerti_items_lbl_price); ?></th>
                            <th scope="col" class="text-end"><?php echo esc_html($zerti_items_lbl_discount); ?></th>
                            <th scope="col" class="text-end"><?php echo esc_html($zerti_items_lbl_tax); ?></th>
                            <th scope="col" class="text-center"><?php echo esc_html($zerti_items_lbl_qty); ?></th>
                            <th scope="col" class="text-end"><?php echo esc_html($zerti_items_lbl_total); ?></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $zerti_line_row = 0;
                        foreach ($order->get_items('line_item') as $item) :
                            $zerti_line_row++;
                            $line = function_exists('zerti_wc_order_parse_line_item_row')
                                ? zerti_wc_order_parse_line_item_row($item, $zerti_line_row)
                                : null;
                            if ($line === null) {
                                continue;
                            }
                            $zerti_line_product = method_exists($item, 'get_product') ? $item->get_product() : null;
                            $zerti_line_sku = is_object($zerti_line_product) && method_exists($zerti_line_product, 'get_sku')
                                ? trim((string) $zerti_line_product->get_sku())
                                : '';
                            ?>
                            <tr class="zerti-shop-order-items-row">
                                <td class="text-center" data-label="<?php echo esc_attr($zerti_items_lbl_row); ?>"><?php echo esc_html(zerti_to_persian_digits((string) $line['row'])); ?></td>
                                <td class="text-center" data-label="<?php echo esc_attr($zerti_items_lbl_id); ?>" dir="ltr"><?php echo esc_html(zerti_to_persian_digits((string) $line['product_id'])); ?></td>
                                <td class="text-center" data-label="<?php echo esc_attr($zerti_items_lbl_sku); ?>" dir="ltr"><?php echo $zerti_line_sku !== '' ? esc_html(zerti_to_persian_digits($zerti_line_sku)) : '—'; ?></td>
                                <td class="zerti-shop-order-item-name" data-label="<?php echo esc_attr($zerti_items_lbl_product); ?>"><?php echo esc_html($line['name']); ?></td>
                                <td class="text-end" data-label="<?php echo esc_attr($zerti_items_lbl_price); ?>"><?php zerti_wc_render_order_item_price_cell((float) $line['unit_price']); ?></td>
                                <td class="text-end" data-label="<?php echo esc_attr($zerti_items_lbl_discount); ?>"><?php zerti_wc_render_order_item_price_cell((float) $line['discount']); ?></td>
                                <td class="text-end" data-label="<?php echo esc_attr($zerti_items_lbl_tax); ?>"><?php zerti_wc_render_order_item_price_cell((float) $line['tax']); ?></td>
                                <td class="text-center" data-label="<?php echo esc_attr($zerti_items_lbl_qty); ?>"><?php
                                $qty_label = function_exists('wc_format_localized_decimal')
                                    ? wc_format_localized_decimal($line['qty'])
                                    : (string) $line['qty'];
                                echo esc_html(zerti_to_persian_digits($qty_label));
                                ?></td>
                                <td class="text-end" data-label="<?php echo esc_attr($zerti_items_lbl_total); ?>"><?php zerti_wc_render_order_item_price_cell((float) $line['line_total']); ?></td>
                            </tr>
                        <?php endforeach; ?>
                        <?php foreach ($order->get_items('fee') as $fee_item) : ?>
                            <?php
                            if (!is_object($fee_item) || !method_exists($fee_item, 'get_name')) {
                                continue;
                            }
                            $fee_name = (string) $fee_item->get_name();
                            $fee_total = function_exists('zerti_wc_order_line_item_line_total')
                                ? zerti_wc_order_line_item_line_total($fee_item)
                                : (method_exists($fee_item, 'get_total') ? (float) $fee_item->get_total() : 0.0);
                            ?>
                            <tr class="zerti-shop-invoice__table-extra zerti-shop-order-items-row zerti-shop-order-items-row--extra">
                                <td class="text-center text-body-secondary zerti-shop-order-items-cell--skip-stacked" aria-hidden="true">—</td>
                                <td class="text-center text-body-secondary zerti-shop-order-items-cell--skip-stacked" aria-hidden="true">—</td>
                                <td class="text-center text-body-secondary zerti-shop-order-items-cell--skip-stacked" aria-hidden="true">—</td>
                                <td colspan="5" class="zerti-shop-order-item-name" data-label="<?php echo esc_attr($zerti_items_lbl_desc); ?>"><?php echo esc_html($fee_name); ?></td>
                                <td class="text-end" data-label="<?php echo esc_attr($zerti_items_lbl_total); ?>"><?php zerti_wc_render_order_item_price_cell($fee_total); ?></td>
                            </tr>
                        <?php endforeach; ?>
                        <?php foreach ($order->get_items('shipping') as $ship_item) : ?>
                            <?php
                            if (!is_object($ship_item) || !method_exists($ship_item, 'get_name')) {
                                continue;
                            }
                            $ship_name = (string) $ship_item->get_name();
                            $ship_total = function_exists('zerti_wc_order_line_item_line_total')
                                ? zerti_wc_order_line_item_line_total($ship_item)
                                : (method_exists($ship_item, 'get_total') ? (float) $ship_item->get_total() : 0.0);
                            ?>
                            <tr class="zerti-shop-invoice__table-extra zerti-shop-order-items-row zerti-shop-order-items-row--extra">
                                <td class="text-center text-body-secondary zerti-shop-order-items-cell--skip-stacked" aria-hidden="true">—</td>
                                <td class="text-center text-body-secondary zerti-shop-order-items-cell--skip-stacked" aria-hidden="true">—</td>
                                <td class="text-center text-body-secondary zerti-shop-order-items-cell--skip-stacked" aria-hidden="true">—</td>
                                <td colspan="5" class="zerti-shop-order-item-name" data-label="<?php echo esc_attr($zerti_items_lbl_desc); ?>"><?php echo esc_html($ship_name); ?></td>
                                <td class="text-end" data-label="<?php echo esc_attr($zerti_items_lbl_total); ?>"><?php zerti_wc_render_order_item_price_cell($ship_total); ?></td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </section>

        <footer class="zerti-shop-invoice__footer">
            <dl class="zerti-shop-invoice__totals mb-0">
                <div class="zerti-shop-invoice__totals-row">
                    <dt><?php esc_html_e('Subtotal', 'zerti'); ?></dt>
                    <dd><?php echo wp_kses_post(zerti_wc_price_html_persian((float) $order->get_subtotal())); ?></dd>
                </div>
                <?php
                $disc = method_exists($order, 'get_discount_total') ? (float) $order->get_discount_total() : 0.0;
                ?>
                <?php if ($disc > 0) : ?>
                    <div class="zerti-shop-invoice__totals-row">
                        <dt><?php esc_html_e('Discount', 'zerti'); ?></dt>
                        <dd class="zerti-shop-invoice__amount-discount">−<?php echo wp_kses_post(zerti_wc_price_html_persian($disc)); ?></dd>
                    </div>
                <?php endif; ?>
                <?php if ((float) $order->get_shipping_total() > 0 || (float) $order->get_shipping_tax() > 0) : ?>
                    <div class="zerti-shop-invoice__totals-row">
                        <dt><?php esc_html_e('Shipping', 'zerti'); ?></dt>
                        <dd><?php echo wp_kses_post(zerti_wc_price_html_persian((float) $order->get_shipping_total() + (float) $order->get_shipping_tax())); ?></dd>
                    </div>
                <?php endif; ?>
                <?php if ((float) $order->get_total_tax() > 0) : ?>
                    <div class="zerti-shop-invoice__totals-row">
                        <dt><?php esc_html_e('Tax', 'zerti'); ?></dt>
                        <dd><?php echo wp_kses_post(zerti_wc_price_html_persian((float) $order->get_total_tax())); ?></dd>
                    </div>
                <?php endif; ?>
                <div class="zerti-shop-invoice__totals-row zerti-shop-invoice__totals-row--grand">
                    <dt><?php esc_html_e('Amount due', 'zerti'); ?></dt>
                    <dd><?php echo wp_kses_post(zerti_wc_price_html_persian((float) $order->get_total())); ?></dd>
                </div>
            </dl>
        </footer>
        <?php if (($is_invoice_view || $is_print) && $show_invoice_custom_text && $invoice_custom_text !== '') : ?>
            <p class="zerti-shop-invoice__custom-text mb-0">
                <?php echo esc_html(zerti_to_persian_digits($invoice_custom_text)); ?>
            </p>
        <?php endif; ?>
    </article>

    <?php if ($is_detail_view && function_exists('zerti_shop_order_detail_render_notes_section')) : ?>
        <?php zerti_shop_order_detail_render_notes_section($order, $can_edit_order); ?>
    <?php endif; ?>
    </div>

    <?php if (!$is_print) : ?>
    <div class="zerti-shop-order-detail__status-overlay" hidden aria-hidden="true">
        <div class="zerti-shop-order-detail__feedback" role="status" aria-live="polite" hidden></div>
    </div>
    <?php endif; ?>
</div>
