<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <!-- Form View for lims.result -->
    <record id="view_lims_result_form" model="ir.ui.view">
        <field name="name">lims.result.form</field>
        <field name="model">lims.result</field>
        <field name="arch" type="xml">
            <form string="Resultado de Análisis">
                <sheet>
                    <group>
                        <group string="Información del Test">
                            <field name="test_id" readonly="1"/>
                            <field name="patient_id" readonly="1"/>
                            <field name="test_date" readonly="1"/>
                        </group>
                        <group string="Parámetro">
                            <field name="parameter_id" readonly="1"/>
                            <field name="parameter_code" readonly="1"/>
                            <field name="parameter_value_type" invisible="1"/>
                            <field name="parameter_unit" invisible="parameter_value_type != 'numeric'"/>
                        </group>
                    </group>
                    <group string="Valor del Resultado">
                        <group>
                            <field name="value_numeric" 
                                   invisible="parameter_value_type != 'numeric'"
                                   widget="float" 
                                   options="{'digits': [16, 4]}"
                                   decoration-danger="is_out_of_range"
                                   decoration-warning="is_critical"/>
                            <field name="value_text" 
                                   invisible="parameter_value_type != 'text'"/>
                            <field name="value_selection" 
                                   invisible="parameter_value_type != 'selection'"
                                   widget="selection"/>
                            <field name="value_boolean" 
                                   invisible="parameter_value_type != 'boolean'"
                                   widget="boolean_toggle"/>
                        </group>
                        <group>
                            <field name="is_out_of_range" readonly="1"/>
                            <field name="is_critical" readonly="1"/>
                        </group>
                    </group>
                    <group string="Rango de Referencia" invisible="parameter_value_type != 'numeric'">
                        <field name="applicable_range_id" readonly="1">
                            <form>
                                <group>
                                    <field name="normal_min"/>
                                    <field name="normal_max"/>
                                    <field name="critical_min"/>
                                    <field name="critical_max"/>
                                </group>
                            </form>
                        </field>
                    </group>
                    <group string="Observaciones">
                        <field name="notes" nolabel="1"/>
                    </group>
                </sheet>
            </form>
        </field>
    </record>

    <!-- List View for lims.result -->
    <record id="view_lims_result_list" model="ir.ui.view">
        <field name="name">lims.result.list</field>
        <field name="model">lims.result</field>
        <field name="arch" type="xml">
            <list string="Resultados de Análisis" editable="bottom">
                <field name="sequence" widget="handle"/>
                <field name="parameter_id" options="{'no_create': True, 'no_open': True}"/>
                <field name="parameter_code" optional="show"/>
                <field name="parameter_value_type" invisible="1"/>
                <field name="value_numeric" 
                       invisible="parameter_value_type != 'numeric'"
                       decoration-danger="is_out_of_range"
                       decoration-warning="is_critical"/>
                <field name="value_text" 
                       invisible="parameter_value_type != 'text'"/>
                <field name="value_selection" 
                       invisible="parameter_value_type != 'selection'"/>
                <field name="value_boolean" 
                       invisible="parameter_value_type != 'boolean'"
                       widget="boolean_toggle"/>
                <field name="parameter_unit" 
                       invisible="parameter_value_type != 'numeric'"
                       optional="show"/>
                <field name="is_out_of_range" invisible="1"/>
                <field name="is_critical" invisible="1"/>
                <field name="applicable_range_id" optional="hide"/>
                <field name="notes" optional="show"/>
            </list>
        </field>
    </record>

    <!-- Search View for lims.result -->
    <record id="view_lims_result_search" model="ir.ui.view">
        <field name="name">lims.result.search</field>
        <field name="model">lims.result</field>
        <field name="arch" type="xml">
            <search string="Buscar Resultados">
                <field name="test_id"/>
                <field name="parameter_id"/>
                <field name="parameter_name"/>
                <field name="patient_id"/>
                <separator/>
                <filter string="Fuera de Rango" name="out_of_range" 
                        domain="[('is_out_of_range', '=', True)]"/>
                <filter string="Críticos" name="critical" 
                        domain="[('is_critical', '=', True)]"/>
                <separator/>
                <filter string="Numéricos" name="numeric" 
                        domain="[('parameter_value_type', '=', 'numeric')]"/>
                <filter string="Texto" name="text" 
                        domain="[('parameter_value_type', '=', 'text')]"/>
                <filter string="Selección" name="selection" 
                        domain="[('parameter_value_type', '=', 'selection')]"/>
                <filter string="Sí/No" name="boolean" 
                        domain="[('parameter_value_type', '=', 'boolean')]"/>
                <group expand="0" string="Agrupar por">
                    <filter string="Test" name="group_test" context="{'group_by': 'test_id'}"/>
                    <filter string="Parámetro" name="group_parameter" context="{'group_by': 'parameter_id'}"/>
                    <filter string="Paciente" name="group_patient" context="{'group_by': 'patient_id'}"/>
                    <filter string="Tipo de Valor" name="group_value_type" context="{'group_by': 'parameter_value_type'}"/>
                </group>
            </search>
        </field>
    </record>

    <!-- Action for lims.result -->
    <record id="action_lims_result" model="ir.actions.act_window">
        <field name="name">Resultados de Análisis</field>
        <field name="res_model">lims.result</field>
        <field name="view_mode">list,form</field>
        <field name="search_view_id" ref="view_lims_result_search"/>
        <field name="context">{'search_default_out_of_range': 1}</field>
        <field name="help" type="html">
            <p class="o_view_nocontent_smiling_face">
                No hay resultados registrados
            </p>
            <p>
                Los resultados se crean automáticamente al generar las pruebas
                de laboratorio basándose en los parámetros configurados.
            </p>
        </field>
    </record>
</odoo>