<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <data>
        <!-- Formato de papel para etiquetas - DEBE IR PRIMERO -->
        <record id="paperformat_sample_label" model="report.paperformat">
            <field name="name">Formato Etiqueta Muestra</field>
            <field name="default" eval="False"/>
            <field name="format">custom</field>
            <field name="page_height">50</field>
            <field name="page_width">100</field>
            <field name="orientation">Landscape</field>
            <field name="margin_top">2</field>
            <field name="margin_bottom">2</field>
            <field name="margin_left">2</field>
            <field name="margin_right">2</field>
            <field name="header_line" eval="False"/>
            <field name="header_spacing">0</field>
            <field name="dpi">200</field>
        </record>

        <!-- Definir el reporte - DESPUÉS del paperformat -->
        <record id="action_report_sample_label" model="ir.actions.report">
            <field name="name">Etiquetas de Muestras</field>
            <field name="model">stock.lot</field>
            <field name="report_type">qweb-pdf</field>
            <field name="report_name">lims_management.report_sample_label</field>
            <field name="report_file">lims_management.report_sample_label</field>
            <field name="print_report_name">'Etiquetas - ' + object.name</field>
            <field name="binding_type">report</field>
            <field name="paperformat_id" ref="lims_management.paperformat_sample_label"/>
            <field name="attachment_use" eval="False"/>
        </record>

        <!-- Template del reporte -->
        <template id="report_sample_label">
            <t t-call="web.basic_layout">
                <t t-set="body_classname">o_report_qweb_pdf</t>
                <div class="page">
                    <t t-foreach="docs" t-as="o">
                        <div style="width: 96mm; height: 46mm; border: 1px solid #ccc; padding: 2mm; margin: 2mm; font-family: 'DejaVu Sans', Arial, sans-serif; display: inline-block; vertical-align: top; page-break-inside: avoid; overflow: hidden;">
                            <!-- Encabezado -->
                            <div style="text-align: center; margin-bottom: 2mm;">
                                <h4 style="margin: 0; font-size: 14px; font-family: 'DejaVu Sans', Arial, sans-serif;">LABORATORIO CL&#205;NICO</h4>
                            </div>
                            
                            <!-- Información del paciente -->
                            <div style="font-size: 11px; margin-bottom: 2mm; font-family: 'DejaVu Sans', Arial, sans-serif;">
                                <div><strong>Paciente:</strong> <span t-field="o.patient_id.name"/></div>
                                <div><strong>ID:</strong> <span t-field="o.patient_id.vat" t-if="o.patient_id.vat"/>
                                     <span t-else="">Sin ID</span>
                                </div>
                            </div>
                            
                            <!-- Información de la muestra -->
                            <div style="font-size: 10px; margin-bottom: 3mm; font-family: 'DejaVu Sans', Arial, sans-serif;">
                                <div><strong>Orden:</strong> <span t-field="o.origin"/></div>
                                <div><strong>Tipo:</strong> <span t-esc="o.get_container_name()"/></div>
                                <div><strong>Fecha:</strong> <span t-field="o.collection_date" t-options='{"widget": "date"}'/></div>
                            </div>
                            
                            <!-- Código de barras -->
                            <div style="text-align: center; margin-top: 2mm;">
                                <t t-set="barcode_value" t-value="o.barcode if o.barcode else o.name"/>
                                <t t-if="barcode_value">
                                    <!-- Usar sintaxis específica de Odoo para código de barras -->
                                    <div style="overflow: hidden; height: 55px;">
                                        <span t-field="o.barcode" 
                                              t-options="{'widget': 'barcode', 'type': 'Code128', 'width': 220, 'height': 45, 'humanreadable': 1}"
                                              style="display: block;"/>
                                    </div>
                                </t>
                                <t t-else="">
                                    <div style="border: 1px solid #ccc; width: 220px; height: 45px; margin: 0 auto; display: flex; align-items: center; justify-content: center;">
                                        <span style="color: #666;">Sin código de barras</span>
                                    </div>
                                </t>
                            </div>
                            
                            <!-- Análisis a realizar (si caben) -->
                            <div style="font-size: 9px; margin-top: 1mm; font-family: 'DejaVu Sans', Arial, sans-serif;" t-if="o.analysis_names">
                                <div><strong>An&#225;lisis:</strong> <span t-field="o.analysis_names"/></div>
                            </div>
                        </div>
                    </t>
                </div>
            </t>
        </template>
    </data>
</odoo>