<?xml version="1.0" encoding="utf-8"?>
<odoo>

    <record id="view_repair_order_form_relsum" model="ir.ui.view">

        <field name="name">repair.order.form.relsum</field>
        <field name="model">repair.order</field>
        <field name="inherit_id" ref="repair.view_repair_order_form"/>

        <field name="arch" type="xml">

            <!-- ESTADO: ocultar la barra estándar (state) y dejar SOLO las 7 fases RELSUM. -->
            <xpath expr="//header/field[@name='state']" position="attributes">
                <attribute name="invisible">1</attribute>
            </xpath>

            <!-- Barra de 7 fases RELSUM clicable en ambos sentidos -->
            <xpath expr="//header" position="inside">
                <field name="relsum_workflow_stage" widget="statusbar"
                       options="{'clickable': '1'}"
                       statusbar_visible="received,diagnostic,quoted,in_progress,ready,delivered,invoiced"/>
            </xpath>

            <!-- SMART BUTTONS -->
            <xpath expr="//div[@name='button_box']" position="inside">

                <button type="object"
                        name="action_view_sale_orders"
                        class="oe_stat_button"
                        icon="fa-shopping-cart">
                    <field name="sale_order_count" widget="statinfo" string="Presupuestos"/>
                </button>

                <button type="object"
                        name="action_view_purchase_orders"
                        class="oe_stat_button"
                        icon="fa-truck">
                    <field name="purchase_order_count" widget="statinfo" string="Compras"/>
                </button>

                <button type="object"
                        name="action_view_pickings"
                        class="oe_stat_button"
                        icon="fa-exchange">
                    <field name="picking_count" widget="statinfo" string="Albaranes"/>
                </button>

                <button type="object"
                        name="action_view_invoices"
                        class="oe_stat_button"
                        icon="fa-pencil-square-o">
                    <field name="invoice_count" widget="statinfo" string="Facturas"/>
                </button>

                <button type="object"
                        name="action_view_technical_reports"
                        class="oe_stat_button"
                        icon="fa-file-text-o">
                    <field name="technical_report_count" widget="statinfo" string="Informes"/>
                </button>

            </xpath>

            <!-- CABECERA: presupuesto principal + máquina -->
            <xpath expr="//sheet/group/group[1]" position="inside">
                <field name="sale_order_id"/>
                <field name="relsum_machine_id"/>
            </xpath>

            <!-- FASE 1.1: "Demand" → "Cantidad" en piezas -->
            <xpath expr="//field[@name='move_ids']//field[@name='product_uom_qty']" position="attributes">
                <attribute name="string">Cantidad</attribute>
            </xpath>

            <!-- FASE 1.2: Ocultar columna Unidades de medida en piezas -->
            <xpath expr="//field[@name='move_ids']//field[@name='product_uom']" position="attributes">
                <attribute name="column_invisible">1</attribute>
            </xpath>

            <!-- FASE 1.3: Ocultar pestaña Miscellaneous -->
            <xpath expr="//page[@name='page_miscellaneous']" position="attributes">
                <attribute name="invisible">1</attribute>
            </xpath>

            <!-- NOTEBOOK -->
            <xpath expr="//notebook" position="inside">

                <!-- DATOS TÉCNICOS -->
                <page string="Datos Técnicos" name="datos_tecnicos">
                    <div invisible="relsum_machine_id" class="alert alert-info" role="alert">
                        <p>Esta OT aún no tiene máquina asignada. Selecciónala en la cabecera.</p>
                    </div>
                    <group invisible="not relsum_machine_id">
                        <group string="Identificación">
                            <field name="machine_marca"/>
                            <field name="machine_tipo"/>
                            <field name="machine_numero_serie"/>
                            <field name="machine_planta_id"/>
                        </group>
                        <group string="Características eléctricas">
                            <field name="machine_voltios"/>
                            <field name="machine_kva"/>
                            <field name="machine_cos_phi"/>
                            <field name="machine_kw"/>
                            <field name="machine_cv"/>
                            <field name="machine_rpm"/>
                            <field name="machine_amperios"/>
                            <field name="machine_hz"/>
                            <field name="machine_cos_phi"/>
                            <field name="machine_kw"/>
                            <field name="machine_cv"/>
                        </group>
                    </group>
                </page>

                <!-- PARTES DE TRABAJO -->
                <page string="Partes de trabajo" name="partes_trabajo">
                    <group>
                        <group string="Resumen">
                            <field name="timesheet_hours" readonly="1"/>
                        </group>
                        <group string="Contabilidad analítica"
                               groups="analytic.group_analytic_accounting">
                            <field name="analytic_account_id"/>
                        </group>
                    </group>
                    <field name="timesheet_ids" context="{'default_repair_order_id': id}">
                        <list editable="bottom">
                            <field name="date"/>
                            <field name="employee_id" optional="show"/>
                            <field name="name"/>
                            <field name="unit_amount" string="Horas"/>
                        </list>
                    </field>
                </page>

                <!-- DATOS LEGACY MIGRACIÓN (solo admin) -->
                <page string="Datos legacy (migración)" name="legacy" groups="base.group_no_one">
                    <group>
                        <group string="Crosswalk legacy">
                            <field name="x_numero_ot" readonly="1"/>
                            <field name="x_presupuesto" readonly="1"/>
                            <field name="x_numero_pedido" readonly="1"/>
                        </group>
                        <group string="Datos máquina (importados)">
                            <field name="x_marca" readonly="1"/>
                            <field name="x_tipo_maquina" readonly="1"/>
                            <field name="x_numero_serie" readonly="1"/>
                            <field name="x_planta" readonly="1"/>
                            <field name="x_kva" readonly="1"/>
                            <field name="x_voltios" readonly="1"/>
                            <field name="x_rpm" readonly="1"/>
                            <field name="x_amperios" readonly="1"/>
                            <field name="x_hz" readonly="1"/>
                        </group>
                    </group>
                    <group string="Texto libre legacy">
                        <field name="x_albaran" readonly="1"/>
                        <field name="x_albaran_nota" readonly="1"/>
                        <field name="x_pedido_proveedor" readonly="1"/>
                        <field name="x_comentarios" readonly="1"/>
                    </group>
                </page>

            </xpath>

            <!-- FASE 2: Vista search con filtros RELSUM -->

        </field>

    </record>

    <record id="view_repair_order_search_relsum" model="ir.ui.view">
        <field name="name">repair.order.search.relsum</field>
        <field name="model">repair.order</field>
        <field name="inherit_id" ref="repair.view_repair_order_form_filter"/>

        <field name="arch" type="xml">

            <!-- Filtros de fase RELSUM -->
            <xpath expr="//search/filter[@name='filter_draft']" position="before">
                <filter name="filter_pendientes"
                        string="Pendientes"
                        domain="[('relsum_workflow_stage', 'not in', ['delivered', 'invoiced'])]"/>
                <filter name="filter_hechas"
                        string="Hechas"
                        domain="[('relsum_workflow_stage', 'in', ['delivered', 'invoiced'])]"/>
                <separator/>
            </xpath>

            <!-- Búsqueda por planta -->
            <xpath expr="//search/field[@name='partner_id']" position="after">
                <field name="machine_planta_id" string="Planta"/>
            </xpath>

        </field>
    </record>

</odoo>
