<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <!-- Specialized Form View for Bulk Result Entry -->
    <record id="view_lims_test_result_entry_form" model="ir.ui.view">
        <field name="name">lims.test.result.entry.form</field>
        <field name="model">lims.test</field>
        <field name="priority">20</field>
        <field name="arch" type="xml">
            <form string="Ingreso Rápido de Resultados">
                <header>
                    <field name="state" widget="statusbar" statusbar_visible="draft,in_process,result_entered,validated"/>
                    <button name="action_start_process" string="Iniciar Análisis" 
                            type="object" class="oe_highlight"
                            invisible="state != 'draft'"/>
                    <button name="action_enter_results" string="Guardar Resultados" 
                            type="object" class="oe_highlight"
                            invisible="state != 'in_process'"/>
                    <button name="action_validate" string="Validar Resultados" 
                            type="object" class="oe_highlight"
                            invisible="state != 'result_entered'"
                            groups="lims_management.group_lims_admin"/>
                </header>
                <sheet>
                    <div class="oe_title">
                        <h1>
                            <field name="name" readonly="1"/>
                        </h1>
                        <h2>
                            <field name="patient_id" readonly="1"/>
                        </h2>
                        <h3>
                            <field name="product_id" readonly="1"/>
                        </h3>
                    </div>
                    
                    <group>
                        <group>
                            <field name="sample_id" readonly="1"/>
                            <field name="technician_id" readonly="state != 'in_process'"/>
                        </group>
                        <group>
                            <field name="create_date" readonly="1"/>
                            <field name="validation_date" readonly="1"/>
                        </group>
                    </group>
                    
                    <separator string="Ingreso de Resultados"/>
                    <field name="result_ids" 
                           readonly="state in ['validated', 'cancelled']"
                           context="{'form_view_ref': 'lims_management.view_lims_result_form'}">
                        <list string="Resultados" editable="bottom" create="0" delete="0">
                            <field name="sequence" invisible="1"/>
                            <field name="parameter_id" readonly="1" force_save="1"/>
                            <field name="parameter_code" readonly="1"/>
                            <field name="parameter_value_type" invisible="1"/>
                            
                            <!-- Entrada rápida de valores -->
                            <field name="value_numeric" 
                                   invisible="parameter_value_type != 'numeric'"
                                   widget="float" 
                                   options="{'digits': [16, 4]}"
                                   decoration-danger="is_critical"
                                   decoration-warning="is_out_of_range and not 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"/>
                            
                            <!-- Información de referencia -->
                            <field name="parameter_unit" 
                                   invisible="parameter_value_type != 'numeric'"
                                   readonly="1"/>
                            <field name="applicable_range_id" 
                                   widget="many2one_tags"
                                   readonly="1"
                                   options="{'no_open': True}"/>
                            
                            <!-- Indicadores -->
                            <field name="result_status" 
                                   widget="badge" 
                                   decoration-success="result_status == 'normal'"
                                   decoration-warning="result_status == 'abnormal'"
                                   decoration-danger="result_status == 'critical'"/>
                            
                            <!-- Campos ocultos -->
                            <field name="is_out_of_range" invisible="1"/>
                            <field name="is_critical" invisible="1"/>
                            
                            <!-- Notas rápidas -->
                            <field name="notes" optional="show"/>
                        </list>
                    </field>
                    
                    <group string="Observaciones Generales" invisible="state == 'draft'">
                        <field name="notes" nolabel="1" 
                               placeholder="Ingrese observaciones generales sobre la prueba..."/>
                    </group>
                </sheet>
            </form>
        </field>
    </record>

    <!-- Action for Quick Result Entry -->
    <record id="action_lims_result_entry" model="ir.actions.act_window">
        <field name="name">Ingreso Rápido de Resultados</field>
        <field name="res_model">lims.test</field>
        <field name="view_mode">list,form</field>
        <field name="view_id" ref="view_lims_test_result_entry_form"/>
        <field name="search_view_id" ref="view_lims_test_search"/>
        <field name="domain">[('state', 'in', ['in_process', 'result_entered'])]</field>
        <field name="context">{'search_default_my_tests': 1, 'search_default_in_process': 1}</field>
        <field name="help" type="html">
            <p class="o_view_nocontent_smiling_face">
                No hay pruebas pendientes de resultados
            </p>
            <p>
                Las pruebas aparecerán aquí cuando estén listas para 
                el ingreso de resultados.
            </p>
        </field>
    </record>

    <!-- Result Summary Dashboard -->
    <record id="view_lims_result_pivot" model="ir.ui.view">
        <field name="name">lims.result.pivot</field>
        <field name="model">lims.result</field>
        <field name="arch" type="xml">
            <pivot string="Análisis de Resultados">
                <field name="parameter_id" type="row"/>
                <field name="result_status" type="col"/>
                <field name="test_id" type="measure"/>
            </pivot>
        </field>
    </record>

    <record id="view_lims_result_graph" model="ir.ui.view">
        <field name="name">lims.result.graph</field>
        <field name="model">lims.result</field>
        <field name="arch" type="xml">
            <graph string="Distribución de Resultados" type="pie">
                <field name="result_status"/>
                <field name="test_id" type="measure"/>
            </graph>
        </field>
    </record>

    <!-- Action for Result Analysis -->
    <record id="action_lims_result_analysis" model="ir.actions.act_window">
        <field name="name">Análisis de Resultados</field>
        <field name="res_model">lims.result</field>
        <field name="view_mode">pivot,graph,list</field>
        <field name="help" type="html">
            <p>
                Análisis estadístico de los resultados de laboratorio.
            </p>
        </field>
    </record>

    <!-- Menu items -->
    <menuitem id="menu_lims_result_entry"
              name="Ingreso de Resultados"
              parent="lims_management.lims_menu_laboratory"
              action="action_lims_result_entry"
              sequence="25"/>
              
    <menuitem id="menu_lims_result_analysis"
              name="Análisis de Resultados"
              parent="lims_management.lims_menu_reports"
              action="action_lims_result_analysis"
              sequence="20"/>
</odoo>