<?xml version="1.0" encoding="UTF-8"?>
<!--
    Copyright 2024-2025 Nexa Systems Inc.
    License OPL-1 (Odoo Proprietary License v1.0)
    Part of the Fusion Claim Assistant product family.
-->
<odoo>
    <!-- Shared Report Header Template -->
    <template id="report_header_fusion_claims">
        <div class="fc-header">
            <div class="row">
                <!-- Company Logo -->
                <div class="col-3">
                    <img t-if="company.logo" t-att-src="image_data_uri(company.logo)" 
                         style="max-height: 80px; max-width: 200px;" alt="Company Logo"/>
                </div>
                <!-- Company Info -->
                <div class="col-9 text-end">
                    <h3 class="mb-0" t-field="company.name"/>
                    <div t-if="company.x_fc_store_address_1" class="small">
                        <t t-esc="company.x_fc_store_address_1"/>
                    </div>
                    <div t-if="company.x_fc_store_address_2" class="small">
                        <t t-esc="company.x_fc_store_address_2"/>
                    </div>
                    <div t-if="company.x_fc_company_tagline" class="small fst-italic text-muted">
                        <t t-esc="company.x_fc_company_tagline"/>
                    </div>
                </div>
            </div>
        </div>
    </template>

    <!-- Address Boxes Template -->
    <template id="report_address_boxes">
        <div class="row mt-4">
            <!-- Billing Address -->
            <div class="col-6">
                <div class="border p-2">
                    <strong>BILLING ADDRESS</strong>
                    <div t-field="doc.partner_id" 
                         t-options='{"widget": "contact", "fields": ["name", "address"], "no_marker": True}'/>
                </div>
            </div>
            <!-- Delivery Address -->
            <div class="col-6">
                <div class="border p-2">
                    <strong>DELIVERY ADDRESS</strong>
                    <div t-if="doc.partner_shipping_id" 
                         t-field="doc.partner_shipping_id" 
                         t-options='{"widget": "contact", "fields": ["name", "address"], "no_marker": True}'/>
                    <div t-else="" t-field="doc.partner_id" 
                         t-options='{"widget": "contact", "fields": ["name", "address"], "no_marker": True}'/>
                </div>
            </div>
        </div>
    </template>

    <!-- Serial Numbers Section Template -->
    <template id="report_serial_numbers">
        <t t-set="serials" t-value="[]"/>
        <!-- Collect serial numbers from order lines -->
        <t t-if="doc._name == 'sale.order'">
            <t t-foreach="doc.order_line" t-as="line">
                <t t-if="line.x_fc_serial_number">
                    <t t-set="serials" t-value="serials + [{'product': line.product_id.name, 'serial': line.x_fc_serial_number}]"/>
                </t>
            </t>
        </t>
        <t t-if="doc._name == 'account.move'">
            <t t-foreach="doc.invoice_line_ids" t-as="line">
                <t t-if="line.x_fc_serial_number">
                    <t t-set="serials" t-value="serials + [{'product': line.product_id.name, 'serial': line.x_fc_serial_number}]"/>
                </t>
            </t>
        </t>
        <div t-if="serials" class="mt-3 border p-2">
            <strong>SERIAL NUMBERS</strong>
            <ul class="mb-0 ps-3">
                <li t-foreach="serials" t-as="s">
                    <t t-esc="s['product']"/>: <strong t-esc="s['serial']"/>
                </li>
            </ul>
        </div>
    </template>

    <!-- Payment Terms Template -->
    <template id="report_payment_terms">
        <div t-if="company.x_fc_payment_terms_html" class="mt-3">
            <strong>Payment Communication: </strong><span t-esc="doc.name"/>
            <div class="mt-2" t-raw="company.x_fc_payment_terms_html"/>
        </div>
    </template>

    <!-- Refund Policy Page Template -->
    <template id="report_refund_policy_page">
        <div t-if="company.x_fc_include_refund_page and company.x_fc_refund_policy_html" 
             class="page" style="page-break-before: always;">
            <t t-call="fusion_claims.report_header_fusion_claims"/>
            <div class="mt-4">
                <t t-raw="company.x_fc_refund_policy_html"/>
            </div>
        </div>
    </template>

    <!-- Footer Template -->
    <template id="report_footer_fusion_claims">
        <div class="fc-footer border-top pt-2 mt-3">
            <div class="row small">
                <div class="col-12 text-center">
                    <span t-if="company.phone">Call &amp; Fax: <t t-esc="company.phone"/></span>
                    <span t-if="company.email"> | <t t-esc="company.email"/></span>
                    <span t-if="company.vat"> | HST # <t t-esc="company.vat"/></span>
                    <span t-if="company.website"> | Shop online: <t t-esc="company.website"/></span>
                </div>
            </div>
        </div>
    </template>

    <!-- Report Styles -->
    <template id="report_styles_fusion_claims">
        <style>
            .fc-header { margin-bottom: 20px; }
            .fc-footer { margin-top: 20px; }
            .fc-table { width: 100%; border-collapse: collapse; }
            .fc-table th { background-color: #0077b6; color: white; padding: 8px; text-align: left; }
            .fc-table td { padding: 6px; border-bottom: 1px solid #ddd; }
            .fc-table .section-header { background-color: #f0f0f0; font-weight: bold; }
            .fc-table .text-end { text-align: right; }
            .fc-totals { margin-top: 20px; }
            .fc-totals table { width: 300px; float: right; }
            .fc-totals td { padding: 4px 8px; }
            .fc-totals .total-row { font-weight: bold; background-color: #0077b6; color: white; }
        </style>
    </template>

    <!-- ================================================================= -->
    <!-- Default Invoice Report: Add SKU column, clean description         -->
    <!-- ================================================================= -->
    <template id="report_invoice_sku_column"
              inherit_id="account.report_invoice_document">

        <!-- 1. Add SKU header before Description -->
        <xpath expr="//th[@name='th_description']" position="before">
            <th name="th_sku" class="text-start" style="width: 12%;"><span>SKU</span></th>
        </xpath>

        <!-- 2. Add SKU cell before description for product lines -->
        <xpath expr="//td[@name='account_invoice_line_name']" position="before">
            <td name="td_sku" class="text-start">
                <span t-esc="line.product_id.default_code or ''"/>
            </td>
        </xpath>

        <!-- 3. Replace description to strip [internal_ref] prefix -->
        <xpath expr="//td[@name='account_invoice_line_name']" position="replace">
            <td name="account_invoice_line_name" t-att-class="padding_class">
                <t t-if="line.name">
                    <t t-set="clean_desc" t-value="line.name"/>
                    <t t-if="'] ' in (line.name or '')">
                        <t t-set="clean_desc" t-value="line.name.split('] ', 1)[1]"/>
                    </t>
                    <span t-out="clean_desc" t-options="{'widget': 'text'}"/>
                </t>
            </td>
        </xpath>

    </template>

    <!-- ================================================================= -->
    <!-- Default Sale Order Report: Add SKU column, clean description      -->
    <!-- ================================================================= -->
    <template id="report_saleorder_sku_column"
              inherit_id="sale.report_saleorder_document">

        <!-- 1. Add SKU header before Description -->
        <xpath expr="//th[@name='th_description']" position="before">
            <th name="th_sku" class="text-start" style="width: 12%;"><span>SKU</span></th>
        </xpath>

        <!-- 2. Add SKU cell before description for product lines -->
        <xpath expr="//td[@name='td_product_name']" position="before">
            <td name="td_sku" class="text-start">
                <span t-esc="line.product_id.default_code or ''"/>
            </td>
        </xpath>

        <!-- 3. Replace description to strip [internal_ref] prefix -->
        <xpath expr="//td[@name='td_product_name']" position="replace">
            <td name="td_product_name" t-att-class="padding_class">
                <t t-if="line.name">
                    <t t-set="clean_desc" t-value="line.name"/>
                    <t t-if="'] ' in (line.name or '')">
                        <t t-set="clean_desc" t-value="line.name.split('] ', 1)[1]"/>
                    </t>
                    <span t-out="clean_desc" t-options="{'widget': 'text'}"/>
                </t>
            </td>
        </xpath>

    </template>
</odoo>

