<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <!-- Form View -->
    <record id="view_lims_analysis_parameter_form" model="ir.ui.view">
        <field name="name">lims.analysis.parameter.form</field>
        <field name="model">lims.analysis.parameter</field>
        <field name="arch" type="xml">
            <form string="Parámetro de Análisis">
                <sheet>
                    <div class="oe_button_box" name="button_box">
                        <button name="%(lims_management.action_product_template_parameter)d" 
                                type="action" 
                                class="oe_stat_button" 
                                icon="fa-flask"
                                context="{'search_default_parameter_id': id}">
                            <field name="analysis_count" widget="statinfo" string="Análisis"/>
                        </button>
                        <button name="toggle_active" 
                                type="object" 
                                class="oe_stat_button" 
                                icon="fa-archive">
                            <field name="active" widget="boolean_button" 
                                   options="{'terminology': 'archive'}"/>
                        </button>
                    </div>
                    <widget name="web_ribbon" title="Archivado" bg_color="bg-danger" invisible="active"/>
                    <div class="oe_title">
                        <h1>
                            <field name="code" placeholder="Código" class="oe_inline"/>
                        </h1>
                        <h2>
                            <field name="name" placeholder="Nombre del parámetro" class="oe_inline"/>
                        </h2>
                    </div>
                    <group>
                        <group string="Información General">
                            <field name="value_type"/>
                            <field name="unit" invisible="value_type != 'numeric'"/>
                            <field name="selection_values" 
                                   invisible="value_type != 'selection'"
                                   placeholder="Positivo, Negativo, No concluyente"/>
                            <field name="active" invisible="1"/>
                        </group>
                        <group string="Detalles">
                            <field name="description" widget="text" nolabel="1" colspan="2"/>
                        </group>
                    </group>
                    <notebook>
                        <page string="Rangos de Referencia" name="ranges">
                            <field name="range_ids" context="{'default_parameter_id': id}">
                                <list editable="bottom">
                                    <field name="name"/>
                                    <field name="gender"/>
                                    <field name="age_min"/>
                                    <field name="age_max"/>
                                    <field name="pregnant" optional="show"/>
                                    <field name="normal_min"/>
                                    <field name="normal_max"/>
                                    <field name="critical_min" optional="show"/>
                                    <field name="critical_max" optional="show"/>
                                </list>
                            </field>
                        </page>
                        <page string="Análisis Configurados" name="analysis">
                            <field name="template_parameter_ids">
                                <list>
                                    <field name="product_tmpl_id"/>
                                    <field name="sequence"/>
                                    <field name="required"/>
                                </list>
                            </field>
                        </page>
                    </notebook>
                </sheet>
            </form>
        </field>
    </record>

    <!-- List View -->
    <record id="view_lims_analysis_parameter_list" model="ir.ui.view">
        <field name="name">lims.analysis.parameter.list</field>
        <field name="model">lims.analysis.parameter</field>
        <field name="arch" type="xml">
            <list string="Parámetros de Análisis">
                <field name="code"/>
                <field name="name"/>
                <field name="value_type"/>
                <field name="unit" optional="show"/>
                <field name="analysis_count" optional="show"/>
                <field name="active" invisible="1"/>
            </list>
        </field>
    </record>

    <!-- Search View -->
    <record id="view_lims_analysis_parameter_search" model="ir.ui.view">
        <field name="name">lims.analysis.parameter.search</field>
        <field name="model">lims.analysis.parameter</field>
        <field name="arch" type="xml">
            <search string="Buscar Parámetros">
                <field name="name" string="Parámetro" 
                       filter_domain="['|', ('name', 'ilike', self), ('code', 'ilike', self)]"/>
                <field name="code"/>
                <filter string="Numéricos" name="numeric" domain="[('value_type', '=', 'numeric')]"/>
                <filter string="Texto" name="text" domain="[('value_type', '=', 'text')]"/>
                <filter string="Sí/No" name="boolean" domain="[('value_type', '=', 'boolean')]"/>
                <filter string="Selección" name="selection" domain="[('value_type', '=', 'selection')]"/>
                <separator/>
                <filter string="Activos" name="active" domain="[('active', '=', True)]"/>
                <filter string="Archivados" name="archived" domain="[('active', '=', False)]"/>
                <group expand="0" string="Agrupar por">
                    <filter string="Tipo de Valor" name="group_value_type" context="{'group_by': 'value_type'}"/>
                    <filter string="Estado" name="group_active" context="{'group_by': 'active'}"/>
                </group>
            </search>
        </field>
    </record>

    <!-- Action -->
    <record id="action_lims_analysis_parameter" model="ir.actions.act_window">
        <field name="name">Parámetros de Análisis</field>
        <field name="res_model">lims.analysis.parameter</field>
        <field name="view_mode">list,form</field>
        <field name="search_view_id" ref="view_lims_analysis_parameter_search"/>
        <field name="context">{'search_default_active': 1}</field>
        <field name="help" type="html">
            <p class="o_view_nocontent_smiling_face">
                Crear nuevo parámetro
            </p>
            <p>
                Los parámetros definen qué valores se pueden registrar en los análisis de laboratorio.
                Cada parámetro tiene un tipo de dato, unidad de medida y rangos de referencia.
            </p>
        </field>
    </record>
</odoo>