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

        <!-- Tree View specifically for Online Orders -->
        <record id="view_backend_online_order_tree" model="ir.ui.view">
            <field name="name">sale.order.online.tree</field>
            <field name="model">sale.order</field>
            <field name="arch" type="xml">
                <tree string="Online Orders" decoration-info="online_order_status == 'pending'" decoration-success="online_order_status == 'confirmed'" decoration-muted="online_order_status == 'rejected'" create="false">
                    <field name="name" string="Order Number"/>
                    <field name="date_order" string="Order Date"/>
                    <field name="partner_id" string="Customer"/>
                    <field name="amount_total" string="Total"/>
                    <field name="online_order_status" string="Status" widget="badge" decoration-info="online_order_status == 'pending'" decoration-success="online_order_status == 'confirmed'" decoration-danger="online_order_status == 'rejected'"/>
                    <button name="action_accept_online_order" string="Accept" type="object" class="btn-success" invisible="online_order_status != 'pending'"/>
                    <button name="action_reject_online_order" string="Reject" type="object" class="btn-danger" invisible="online_order_status != 'pending'"/>
                    <field name="state" column_invisible="1"/>
                </tree>
            </field>
        </record>

        <!-- Form View override for Online Orders buttons -->
        <record id="view_order_form_online_orders" model="ir.ui.view">
            <field name="name">sale.order.form.online</field>
            <field name="model">sale.order</field>
            <field name="inherit_id" ref="sale.view_order_form"/>
            <field name="arch" type="xml">
                <xpath expr="//header" position="inside">
                    <field name="order_source" invisible="1"/>
                    <button name="action_accept_online_order" string="Accept Online Order" type="object" class="btn-success" invisible="order_source != 'online' or online_order_status != 'pending'"/>
                    <button name="action_reject_online_order" string="Reject Online Order" type="object" class="btn-danger" invisible="order_source != 'online' or online_order_status != 'pending'"/>
                </xpath>
                <xpath expr="//field[@name='payment_term_id']" position="after">
                    <field name="online_order_status" invisible="order_source != 'online'" readonly="1" widget="badge" decoration-info="online_order_status == 'pending'" decoration-success="online_order_status == 'confirmed'" decoration-danger="online_order_status == 'rejected'"/>
                </xpath>
            </field>
        </record>

        <!-- Action to open Online Orders (Sale Orders from website) -->
        <record id="action_backend_online_orders" model="ir.actions.act_window">
            <field name="name">Online Orders</field>
            <field name="res_model">sale.order</field>
            <field name="view_mode">tree,kanban,form,calendar,pivot,graph,activity</field>
            <field name="view_id" ref="view_backend_online_order_tree"/>
            <field name="domain">[('order_source', '=', 'online')]</field>
            <field name="context">{'default_order_source': 'online'}</field>
            <field name="help" type="html">
                <p class="o_view_nocontent_smiling_face">
                    No online orders found.
                </p>
                <p>
                    Orders placed on the website will appear here.
                </p>
            </field>
        </record>

        <!-- Menu Item at the Root Level -->
        <menuitem id="menu_backend_online_orders_root"
                  name="Online Orders"
                  web_icon="dine360_dashboard,static/src/img/icons/website.svg"
                  sequence="20"
                  action="action_backend_online_orders"/>

    </data>
</odoo>
