<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <data>
        <!-- Order Tree View -->
        <record id="view_marketplace_order_tree" model="ir.ui.view">
            <field name="name">marketplace.order.tree</field>
            <field name="model">marketplace.order</field>
            <field name="arch" type="xml">
                <list>
                    <field name="name"/>
                    <field name="team_uuid"/>
                    <field name="source_location_id"/>
                    <field name="destination_location_id"/>
                    <field name="total_amount"/>
                    <field name="created_at"/>
                </list>
            </field>
        </record>

        <!-- Order Form View -->
        <record id="view_marketplace_order_form" model="ir.ui.view">
            <field name="name">marketplace.order.form</field>
            <field name="model">marketplace.order</field>
            <field name="arch" type="xml">
                <form>
                    <header>
                    </header>
                    <sheet>
                        <group>
                            <group>
                                <field name="name"/>
                                <field name="uuid" readonly="1"/>
                                <field name="team_uuid"/>
                                <field name="user_id"/>
                            </group>
                            <group>
                                <field name="source_location_id"/>
                                <field name="destination_location_id"/>
                                <field name="total_amount"/>
                                <field name="currency_id"/>
                            </group>
                        </group>
                        <group>
                            <field name="created_at" readonly="1"/>
                            <field name="updated_at" readonly="1"/>
                        </group>
                        <notebook>
                            <page string="Stages">
                                <!-- Stages Kanban -->
                                <group string="Logistics Stages">
                                    <field name="stage_ids" mode="kanban" nolabel="1" 
                                           context="{'default_order_id': id}">
                                        <kanban>
                                            <field name="name"/>
                                            <field name="stage_type"/>
                                            <field name="transport_type"/>
                                            <field name="source_location_id"/>
                                            <field name="destination_location_id"/>
                                            <field name="location_id"/>
                                            <templates>
                                                <t t-name="kanban-box">
                                                    <div class="oe_kanban_card oe_kanban_global_click">
                                                        <div class="o_kanban_record_top">
                                                            <div class="o_kanban_record_headings">
                                                                <strong class="o_kanban_record_title">
                                                                    <field name="name"/>
                                                                </strong>
                                                                <div class="o_kanban_record_subtitle">
                                                                    <t t-if="record.stage_type.raw_value == 'transport'">
                                                                        <i class="fa fa-truck" title="Transport"/> 
                                                                        <field name="transport_type"/>
                                                                    </t>
                                                                    <t t-else="">
                                                                        <i class="fa fa-cogs" title="Service"/> Service
                                                                    </t>
                                                                </div>
                                                            </div>
                                                        </div>
                                                        
                                                        <div class="o_kanban_record_body">
                                                            <!-- Locations -->
                                                            <div t-if="record.stage_type.raw_value == 'transport'" class="mb-1">
                                                                <small><i class="fa fa-map-marker" title="Route"/> 
                                                                <field name="source_location_id"/> → <field name="destination_location_id"/></small>
                                                            </div>
                                                            <div t-else="" class="mb-1">
                                                                <small><i class="fa fa-map-marker" title="Location"/> 
                                                                <field name="location_id"/></small>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </t>
                                            </templates>
                                        </kanban>
                                    </field>
                                </group>
                            </page>
                            <page string="Order Lines">
                                <field name="order_lines">
                                    <list>
                                        <field name="product_id"/>
                                        <field name="quantity"/>
                                        <field name="unit"/>
                                        <field name="price_unit"/>
                                        <field name="subtotal"/>
                                        <field name="currency_id"/>
                                    </list>
                                </field>
                            </page>
                            <page string="Tracking">
                                <field name="tracking_events">
                                    <list>
                                        <field name="timestamp"/>
                                        <field name="stage_id"/>
                                        <field name="trip_id"/>
                                        <field name="location_id"/>
                                        <field name="description"/>
                                    </list>
                                </field>
                            </page>
                            <page string="Notes">
                                <field name="notes" placeholder="Additional notes..."/>
                            </page>
                        </notebook>
                    </sheet>
                </form>
            </field>
        </record>

        <!-- Order Action -->
        <record id="action_marketplace_order" model="ir.actions.act_window">
            <field name="name">Orders</field>
            <field name="res_model">marketplace.order</field>
            <field name="view_mode">list,form</field>
            <field name="help" type="html">
                <p class="o_view_nocontent_smiling_face">
                    Create your first marketplace order!
                </p>
            </field>
        </record>

        <!-- Stage List View -->
        <record id="view_marketplace_order_stage_list" model="ir.ui.view">
            <field name="name">marketplace.order.stage.list</field>
            <field name="model">marketplace.order.stage</field>
            <field name="arch" type="xml">
                <list string="Order Stages">
                    <field name="sequence"/>
                    <field name="name"/>
                    <field name="order_id"/>
                    <field name="stage_type"/>
                    <field name="transport_type"/>
                    <field name="source_location_id"/>
                    <field name="destination_location_id"/>
                    <field name="location_id"/>
                </list>
            </field>
        </record>

        <!-- Stage Form View -->
        <record id="view_marketplace_order_stage_form" model="ir.ui.view">
            <field name="name">marketplace.order.stage.form</field>
            <field name="model">marketplace.order.stage</field>
            <field name="arch" type="xml">
                <form string="Order Stage">
                    <sheet>
                        <group>
                            <group>
                                <field name="name"/>
                                <field name="order_id"/>
                                <field name="sequence"/>
                                <field name="stage_type"/>
                                <field name="transport_type" invisible="stage_type != 'transport'"/>
                            </group>
                            <group>
                                <field name="source_location_id" invisible="stage_type != 'transport'"/>
                                <field name="destination_location_id" invisible="stage_type != 'transport'"/>
                                <field name="location_id" invisible="stage_type != 'service'"/>
                                <field name="selected_company_id" invisible="stage_type != 'service'"/>
                            </group>
                        </group>


                        <notebook>
                            <page string="Trips" invisible="stage_type != 'transport'">
                                <field name="trip_ids">
                                    <list>
                                        <field name="sequence"/>
                                        <field name="name"/>
                                        <field name="company_id"/>
                                        <field name="planned_weight"/>
                                        <field name="weight_at_loading"/>
                                        <field name="weight_at_unloading"/>
                                    </list>
                                </field>
                            </page>
                        </notebook>
                    </sheet>
                </form>
            </field>
        </record>

        <!-- Stage Kanban View -->
        <record id="view_marketplace_order_stage_kanban" model="ir.ui.view">
            <field name="name">marketplace.order.stage.kanban</field>
            <field name="model">marketplace.order.stage</field>
            <field name="arch" type="xml">
                <kanban>
                    <field name="name"/>
                    <field name="stage_type"/>
                    <field name="transport_type"/>
                    <field name="source_location_id"/>
                    <field name="destination_location_id"/>
                    <field name="location_id"/>
                    <templates>
                        <t t-name="kanban-box">
                            <div class="oe_kanban_card oe_kanban_global_click">
                                <div class="o_kanban_record_top">
                                    <div class="o_kanban_record_headings">
                                        <strong class="o_kanban_record_title">
                                            <field name="name"/>
                                        </strong>
                                        <div class="o_kanban_record_subtitle">
                                            <t t-if="record.stage_type.raw_value == 'transport'">
                                                <i class="fa fa-truck" title="Transport"/> 
                                                <field name="transport_type"/>
                                            </t>
                                            <t t-else="">
                                                <i class="fa fa-cogs" title="Service"/> Service
                                            </t>
                                        </div>
                                    </div>
                                </div>
                                
                                <div class="o_kanban_record_body">
                                    <!-- Locations -->
                                    <div t-if="record.stage_type.raw_value == 'transport'" class="mb-2">
                                        <i class="fa fa-map-marker" title="Route"/> 
                                        <field name="source_location_id"/> → <field name="destination_location_id"/>
                                    </div>
                                    <div t-else="" class="mb-2">
                                        <i class="fa fa-map-marker" title="Location"/> 
                                        <field name="location_id"/>
                                    </div>
                                </div>
                            </div>
                        </t>
                    </templates>
                </kanban>
            </field>
        </record>

        <!-- Stage Action -->
        <record id="action_marketplace_order_stage" model="ir.actions.act_window">
            <field name="name">Order Stages</field>
            <field name="res_model">marketplace.order.stage</field>
            <field name="view_mode">kanban,list,form</field>
            <field name="help" type="html">
                <p class="o_view_nocontent_smiling_face">
                    No stages found
                </p>
                <p>
                    Stages represent logistics steps in order fulfillment.
                </p>
            </field>
        </record>
    </data>
</odoo>