<?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 all_sample_ids"
                            icon="fa-print"/>
                    <button name="action_print_lab_results" 
                            string="Imprimir Informe de Resultados" 
                            type="object" 
                            class="btn-success"
                            invisible="not can_print_results or not is_lab_request"
                            icon="fa-file-pdf-o"/>
                </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" name="all_samples" invisible="not is_lab_request">
                        <group string="Todas las Muestras (incluyendo Re-muestras)">
                            <field name="all_sample_ids" nolabel="1" readonly="1"
                                   context="{'form_view_ref': 'lims_management.view_lab_sample_form', 
                                            'tree_view_ref': 'lims_management.view_lab_sample_list'}">
                                <list string="Todas las Muestras" create="false" edit="false" delete="false">
                                    <field name="name" string="Código de Muestra"/>
                                    <field name="barcode" string="Código de Barras" optional="show"/>
                                    <field name="sample_type_product_id" string="Tipo de Muestra"/>
                                    <field name="volume_ml" string="Volumen (ml)" optional="show"/>
                                    <field name="analysis_names" string="Análisis" optional="show"/>
                                    <field name="is_resample" string="Es Re-muestra" widget="boolean_toggle"/>
                                    <field name="parent_sample_id" string="Muestra Original" optional="show"/>
                                    <field name="state" string="Estado" widget="badge" 
                                           decoration-success="state == 'analyzed'" 
                                           decoration-info="state == 'in_process'" 
                                           decoration-danger="state == 'rejected'"
                                           decoration-warning="state == 'pending_collection'"/>
                                    <field name="rejection_reason_id" string="Motivo Rechazo" optional="show"/>
                                    <button name="action_collect" string="Recolectar" type="object" 
                                            class="btn-sm btn-primary" invisible="state != 'pending_collection'"/>
                                </list>
                            </field>
                        </group>
                        <group string="Resumen" col="4">
                            <field name="generated_sample_ids" invisible="1"/>
                            <group>
                                <label for="generated_sample_ids" string="Muestras Originales:"/>
                                <div>
                                    <span class="badge badge-primary"><field name="generated_sample_ids" readonly="1" widget="many2many_tags"/></span>
                                </div>
                            </group>
                            <group>
                                <div class="alert alert-info" role="alert">
                                    <p><i class="fa fa-info-circle"/> Las muestras han sido generadas automáticamente basándose en los análisis solicitados.</p>
                                    <p>Las re-muestras se generan cuando una muestra es rechazada.</p>
                                </div>
                            </group>
                        </group>
                    </page>
                    <page string="Observaciones Lab" name="lab_notes" invisible="not is_lab_request">
                        <group>
                            <field name="lab_notes" nolabel="1" placeholder="Ingrese observaciones generales sobre la orden o los resultados..."/>
                        </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>
