<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <data>
        
        <!-- Vista formulario para lims.test -->
        <record id="view_lims_test_form" model="ir.ui.view">
            <field name="name">lims.test.form</field>
            <field name="model">lims.test</field>
            <field name="arch" type="xml">
                <form string="Prueba de Laboratorio">
                    <header>
                        <button name="action_start_process" string="Iniciar Proceso" 
                                type="object" class="oe_highlight" 
                                invisible="state != 'draft'"
                                groups="lims_management.group_lims_technician"/>
                        <button name="action_enter_results" string="Marcar Resultados Ingresados" 
                                type="object" class="oe_highlight" 
                                invisible="state != 'in_process'"
                                groups="lims_management.group_lims_technician"/>
                        <button name="action_validate" string="Validar Resultados" 
                                type="object" class="oe_highlight" 
                                invisible="state != 'result_entered' or not require_validation"
                                groups="lims_management.group_lims_admin"/>
                        <button name="action_cancel" string="Cancelar" 
                                type="object"
                                invisible="state in ['validated', 'cancelled']"
                                groups="lims_management.group_lims_technician"/>
                        <button name="action_draft" string="Volver a Borrador" 
                                type="object"
                                invisible="state != 'cancelled'"
                                groups="lims_management.group_lims_admin"/>
                        <button name="action_regenerate_results" string="Regenerar Resultados" 
                                type="object"
                                invisible="state not in ['draft', 'in_process']"
                                confirm="¿Está seguro de regenerar los resultados? Esto eliminará los resultados actuales."
                                groups="lims_management.group_lims_technician"/>
                        <field name="state" widget="statusbar" 
                               statusbar_visible="draft,in_process,result_entered,validated"/>
                    </header>
                    <sheet>
                        <div class="oe_title">
                            <h1>
                                <field name="name" readonly="1"/>
                            </h1>
                        </div>
                        <group>
                            <group>
                                <field name="sale_order_line_id" invisible="1"/>
                                <field name="patient_id"/>
                                <field name="product_id"/>
                                <field name="sample_id" 
                                       options="{'no_create': True}"
                                       domain="[('is_lab_sample', '=', True), ('patient_id', '=', patient_id)]"/>
                            </group>
                            <group>
                                <field name="technician_id" readonly="state != 'draft'"/>
                                <field name="require_validation" invisible="1"/>
                                <field name="validator_id" readonly="1" invisible="not validator_id"/>
                                <field name="validation_date" readonly="1" invisible="not validation_date"/>
                                <field name="company_id" groups="base.group_multi_company"/>
                            </group>
                        </group>
                        
                        <notebook>
                            <page string="Resultados">
                                <field name="result_ids" 
                                       readonly="state in ['validated', 'cancelled']"
                                       context="{'default_test_id': id, 'default_patient_id': patient_id, 'default_test_date': create_date}"
                                       mode="list">
                                    <list string="Resultados" editable="bottom" 
                                          decoration-danger="is_out_of_range and not is_critical"
                                          decoration-warning="is_critical"
                                          decoration-success="not is_out_of_range and not is_critical and parameter_value_type == 'numeric'">
                                        <field name="sequence" widget="handle" optional="show"/>
                                        <field name="parameter_id" 
                                               options="{'no_create': True, 'no_open': True}"
                                               readonly="1"/>
                                        <field name="parameter_code" optional="show" readonly="1"/>
                                        <field name="parameter_value_type" invisible="1"/>
                                        <!-- Campos de valor con mejores widgets -->
                                        <field name="value_numeric" 
                                               invisible="parameter_value_type != 'numeric'"
                                               widget="float" 
                                               options="{'digits': [16, 4]}"
                                               class="oe_edit_only"/>
                                        <field name="value_text" 
                                               invisible="parameter_value_type != 'text'"
                                               class="oe_edit_only"/>
                                        <field name="value_selection" 
                                               invisible="parameter_value_type != 'selection'"
                                               widget="selection"
                                               class="oe_edit_only"/>
                                        <field name="value_boolean" 
                                               invisible="parameter_value_type != 'boolean'"
                                               widget="boolean_toggle"
                                               class="oe_edit_only"/>
                                        <!-- Unidad y rangos -->
                                        <field name="parameter_unit" 
                                               invisible="parameter_value_type != 'numeric'"
                                               optional="show" 
                                               readonly="1"/>
                                        <field name="applicable_range_id" 
                                               optional="hide" 
                                               readonly="1"/>
                                        <!-- Indicadores de estado -->
                                        <field name="is_out_of_range" invisible="1"/>
                                        <field name="is_critical" invisible="1"/>
                                        <!-- Campo de estado visual -->
                                        <field name="result_status" 
                                               widget="badge" 
                                               optional="show"
                                               decoration-success="result_status == 'normal'"
                                               decoration-warning="result_status == 'abnormal'"
                                               decoration-danger="result_status == 'critical'"/>
                                        <field name="notes" optional="show"/>
                                    </list>
                                </field>
                            </page>
                            <page string="Observaciones">
                                <field name="notes" placeholder="Agregar observaciones generales de la prueba..."/>
                            </page>
                        </notebook>
                    </sheet>
                    <div class="oe_chatter">
                        <field name="message_follower_ids"/>
                        <field name="activity_ids"/>
                        <field name="message_ids"/>
                    </div>
                </form>
            </field>
        </record>
        
        <!-- Vista lista para lims.test -->
        <record id="view_lims_test_tree" model="ir.ui.view">
            <field name="name">lims.test.tree</field>
            <field name="model">lims.test</field>
            <field name="arch" type="xml">
                <list string="Pruebas de Laboratorio">
                    <field name="name"/>
                    <field name="patient_id"/>
                    <field name="product_id"/>
                    <field name="sample_id"/>
                    <field name="technician_id" optional="show"/>
                    <field name="state" widget="badge" 
                           decoration-success="state == 'validated'"
                           decoration-warning="state == 'result_entered'"
                           decoration-info="state == 'in_process'"
                           decoration-muted="state == 'cancelled'"/>
                    <field name="create_date" optional="hide"/>
                    <field name="company_id" groups="base.group_multi_company" optional="hide"/>
                </list>
            </field>
        </record>
        
        <!-- Vista kanban para lims.test -->
        <record id="view_lims_test_kanban" model="ir.ui.view">
            <field name="name">lims.test.kanban</field>
            <field name="model">lims.test</field>
            <field name="arch" type="xml">
                <kanban default_group_by="state" class="o_kanban_small_column">
                    <field name="name"/>
                    <field name="patient_id"/>
                    <field name="product_id"/>
                    <field name="state"/>
                    <field name="technician_id"/>
                    <field name="create_date"/>
                    <templates>
                        <t t-name="kanban-card">
                            <div class="oe_kanban_card oe_kanban_global_click">
                                <div class="oe_kanban_content">
                                    <div class="o_kanban_record_top">
                                        <div class="o_kanban_record_headings">
                                            <strong class="o_kanban_record_title">
                                                <field name="name"/>
                                            </strong>
                                        </div>
                                    </div>
                                    <div class="o_kanban_record_body">
                                        <div>
                                            <i class="fa fa-user" title="Paciente"/> 
                                            <field name="patient_id"/>
                                        </div>
                                        <div>
                                            <i class="fa fa-flask" title="Análisis"/> 
                                            <field name="product_id"/>
                                        </div>
                                        <div t-if="record.technician_id.raw_value">
                                            <i class="fa fa-user-md" title="Técnico"/> 
                                            <field name="technician_id"/>
                                        </div>
                                    </div>
                                    <div class="o_kanban_record_bottom">
                                        <div class="oe_kanban_bottom_left">
                                            <field name="create_date" widget="date"/>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </t>
                    </templates>
                </kanban>
            </field>
        </record>
        
        <!-- Vista búsqueda para lims.test -->
        <record id="view_lims_test_search" model="ir.ui.view">
            <field name="name">lims.test.search</field>
            <field name="model">lims.test</field>
            <field name="arch" type="xml">
                <search string="Buscar Pruebas">
                    <field name="name"/>
                    <field name="patient_id"/>
                    <field name="product_id"/>
                    <field name="sample_id"/>
                    <field name="technician_id"/>
                    <separator/>
                    <filter string="Borrador" name="draft" domain="[('state','=','draft')]"/>
                    <filter string="En Proceso" name="in_process" domain="[('state','=','in_process')]"/>
                    <filter string="Resultado Ingresado" name="result_entered" domain="[('state','=','result_entered')]"/>
                    <filter string="Validado" name="validated" domain="[('state','=','validated')]"/>
                    <separator/>
                    <filter string="Mis Pruebas" name="my_tests" domain="[('technician_id','=',uid)]"/>
                    <separator/>
                    <filter string="Hoy" name="today" domain="[('create_date','&gt;=',(datetime.datetime.now().replace(hour=0, minute=0, second=0)).strftime('%Y-%m-%d %H:%M:%S'))]"/>
                    <group expand="0" string="Agrupar Por">
                        <filter string="Estado" name="group_by_state" context="{'group_by':'state'}"/>
                        <filter string="Paciente" name="group_by_patient" context="{'group_by':'patient_id'}"/>
                        <filter string="Análisis" name="group_by_product" context="{'group_by':'product_id'}"/>
                        <filter string="Técnico" name="group_by_technician" context="{'group_by':'technician_id'}"/>
                        <filter string="Fecha" name="group_by_date" context="{'group_by':'create_date:day'}"/>
                    </group>
                </search>
            </field>
        </record>
        
    </data>
</odoo>