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

    <!-- ===== Tree View ===== -->
    <record id="woo_return_view_tree" model="ir.ui.view">
        <field name="name">woo.return.tree</field>
        <field name="model">woo.return</field>
        <field name="arch" type="xml">
            <tree>
                <field name="instance_id"/>
                <field name="order_id"/>
                <field name="picking_id"/>
                <field name="reason"/>
                <field name="state" widget="badge"
                       decoration-success="state == 'refunded'"
                       decoration-info="state == 'approved'"
                       decoration-warning="state == 'requested'"
                       decoration-danger="state == 'rejected'"/>
            </tree>
        </field>
    </record>

    <!-- ===== Form View ===== -->
    <record id="woo_return_view_form" model="ir.ui.view">
        <field name="name">woo.return.form</field>
        <field name="model">woo.return</field>
        <field name="arch" type="xml">
            <form>
                <header>
                    <button name="action_approve" type="object" string="Approve"
                            class="oe_highlight"
                            invisible="state != 'requested'"/>
                    <button name="action_reject" type="object" string="Reject"
                            invisible="state != 'requested'"/>
                    <button name="action_receive" type="object" string="Receive"
                            class="oe_highlight"
                            invisible="state != 'approved'"/>
                    <button name="action_refund" type="object" string="Refund"
                            class="oe_highlight"
                            invisible="state != 'received'"/>
                    <field name="state" widget="statusbar"
                           statusbar_visible="requested,approved,received,refunded"/>
                </header>
                <sheet>
                    <group>
                        <group>
                            <field name="instance_id"/>
                            <field name="order_id"/>
                            <field name="picking_id"/>
                            <field name="company_id" groups="base.group_multi_company"/>
                        </group>
                        <group>
                            <field name="reason"/>
                        </group>
                    </group>
                    <notebook>
                        <page string="Return Lines" name="lines">
                            <field name="line_ids">
                                <tree editable="bottom">
                                    <field name="product_id"/>
                                    <field name="quantity"/>
                                    <field name="reason"/>
                                </tree>
                            </field>
                        </page>
                    </notebook>
                </sheet>
            </form>
        </field>
    </record>

    <!-- ===== Search View ===== -->
    <record id="woo_return_view_search" model="ir.ui.view">
        <field name="name">woo.return.search</field>
        <field name="model">woo.return</field>
        <field name="arch" type="xml">
            <search>
                <field name="order_id"/>
                <filter name="requested" string="Requested"
                        domain="[('state', '=', 'requested')]"/>
                <filter name="approved" string="Approved"
                        domain="[('state', '=', 'approved')]"/>
                <filter name="refunded" string="Refunded"
                        domain="[('state', '=', 'refunded')]"/>
                <group expand="1" string="Group By">
                    <filter name="group_state" string="State"
                            context="{'group_by': 'state'}"/>
                    <filter name="group_instance" string="Instance"
                            context="{'group_by': 'instance_id'}"/>
                </group>
            </search>
        </field>
    </record>

    <!-- ===== Action ===== -->
    <record id="action_woo_return" model="ir.actions.act_window">
        <field name="name">WooCommerce Returns</field>
        <field name="res_model">woo.return</field>
        <field name="view_mode">tree,form</field>
        <field name="search_view_id" ref="woo_return_view_search"/>
    </record>

</odoo>
