<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <data>

        <!-- Inherit Form View to Modify Sale Order for Lab Requests -->
        <record id="view_order_form_inherit_lims" model="ir.ui.view">
            <field name="name">sale.order.form.inherit.lims</field>
            <field name="model">sale.order</field>
            <field name="inherit_id" ref="sale.view_order_form"/>
            <field name="arch" type="xml">
                <!-- Agregar botón de imprimir etiquetas en el header -->
                <xpath expr="//header" position="inside">
                    <button name="action_print_sample_labels" 
                            string="Imprimir Etiquetas" 
                            type="object" 
                            class="btn-primary"
                            invisible="not is_lab_request or state != 'sale' or not generated_sample_ids"
                            icon="fa-print"/>
                </xpath>
                <xpath expr="//field[@name='partner_id']" position="after">
                    <field name="doctor_id" invisible="not is_lab_request"/>
                </xpath>
                <xpath expr="//field[@name='partner_id']" position="attributes">
                    <attribute name="string">Paciente</attribute>
                    <attribute name="domain">[('is_patient', '=', True)]</attribute>
                </xpath>
                <xpath expr="//notebook/page[@name='order_lines']//field[@name='product_template_id']" position="attributes">
                    <attribute name="domain">[('is_analysis', '=', True)]</attribute>
                </xpath>
                <!-- Add Generated Samples tab -->
                <xpath expr="//notebook" position="inside">
                    <page string="Muestras Generadas" name="generated_samples" invisible="not is_lab_request">
                        <group>
                            <field name="generated_sample_ids" nolabel="1" readonly="1">
                                <list string="Muestras Generadas" create="false" edit="false" delete="false">
                                    <field name="name" string="Código de Muestra"/>
                                    <field name="barcode" string="Código de Barras"/>
                                    <field name="sample_type_product_id" string="Tipo de Muestra"/>
                                    <field name="volume_ml" string="Volumen (ml)"/>
                                    <field name="analysis_names" string="Análisis"/>
                                    <field name="state" string="Estado"/>
                                    <button name="action_collect" string="Recolectar" type="object" 
                                            class="btn-primary" invisible="state != 'pending_collection'"/>
                                </list>
                            </field>
                        </group>
                        <group invisible="not generated_sample_ids">
                            <div class="alert alert-info" role="alert">
                                <p>Las muestras han sido generadas automáticamente basándose en los análisis solicitados. 
                                   Cada muestra agrupa los análisis que requieren el mismo tipo de contenedor.</p>
                            </div>
                        </group>
                    </page>
                </xpath>
            </field>
        </record>

        <!-- Inherit List View to Add Referring Doctor -->
        <record id="view_order_tree_inherit_lims" model="ir.ui.view">
            <field name="name">sale.order.tree.inherit.lims</field>
            <field name="model">sale.order</field>
            <field name="inherit_id" ref="sale.view_order_tree"/>
            <field name="arch" type="xml">
                <xpath expr="//field[@name='partner_id']" position="after">
                    <field name="doctor_id"/>
                </xpath>
                <xpath expr="//field[@name='state']" position="before">
                    <field name="is_lab_request" optional="show" string="Orden Lab"/>
                    <field name="generated_sample_ids" widget="many2many_tags" optional="hide" string="Muestras"/>
                </xpath>
            </field>
        </record>

    </data>
</odoo>
