<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <!-- Rental Line Form View -->
    <record id="view_event_rental_line_form" model="ir.ui.view">
        <field name="name">event.rental.line.form</field>
        <field name="model">event.rental.line</field>
        <field name="arch" type="xml">
            <form>
                <sheet>
                    <group>
                        <field name="product_id"/>
                        <field name="quantity"/>
                        <field name="price_unit"/>
                        <field name="price_subtotal"/>
                    </group>
                </sheet>
            </form>
        </field>
    </record>

    <!-- Rental Request Tree View -->
    <record id="view_event_rental_request_tree" model="ir.ui.view">
        <field name="name">event.rental.request.tree</field>
        <field name="model">event.rental.request</field>
        <field name="arch" type="xml">
            <tree string="Rental Requests" decoration-info="status == 'under_review'" decoration-success="status in ('confirmed', 'completed')" decoration-muted="status == 'rejected'">
                <field name="name"/>
                <field name="customer_name"/>
                <field name="event_date"/>
                <field name="start_date"/>
                <field name="end_date"/>
                <field name="amount_total" sum="Total Amount"/>
                <field name="status" widget="badge" decoration-info="status == 'under_review'" decoration-success="status == 'confirmed'" decoration-warning="status == 'quotation_sent'" decoration-danger="status == 'rejected'"/>
                <field name="delivery_status" widget="badge" decoration-info="delivery_status == 'pending'" decoration-success="delivery_status == 'delivered'" decoration-warning="delivery_status == 'picked_up'"/>
            </tree>
        </field>
    </record>

    <!-- Rental Request Calendar View -->
    <record id="view_event_rental_request_calendar" model="ir.ui.view">
        <field name="name">event.rental.request.calendar</field>
        <field name="model">event.rental.request</field>
        <field name="arch" type="xml">
            <calendar string="Rental Calendar" date_start="start_date" date_stop="end_date" color="status" quick_create="false">
                <field name="customer_name"/>
                <field name="name"/>
                <field name="event_type"/>
            </calendar>
        </field>
    </record>

    <!-- Rental Request Form View -->
    <record id="view_event_rental_request_form" model="ir.ui.view">
        <field name="name">event.rental.request.form</field>
        <field name="model">event.rental.request</field>
        <field name="arch" type="xml">
            <form string="Rental Request">
                <header>
                    <button name="action_approve" string="Approve Request" type="object" class="oe_highlight" invisible="status not in ('draft', 'under_review')"/>
                    <button name="action_reject" string="Reject Request" type="object" class="btn-danger" invisible="status not in ('draft', 'under_review')"/>
                    <button name="action_deliver" string="Mark as Delivered" type="object" class="oe_highlight" invisible="status != 'confirmed'"/>
                    <button name="action_pickup" string="Mark as Picked Up" type="object" invisible="status != 'delivered'"/>
                    <button name="action_return" string="Mark as Returned" type="object" class="oe_highlight" invisible="status != 'returned' or delivery_status == 'returned'"/>
                    <button name="action_complete" string="Complete Booking" type="object" class="oe_highlight" invisible="status not in ('returned', 'confirmed', 'delivered') or delivery_status != 'returned'"/>
                    <button name="action_reset_draft" string="Reset to Draft" type="object" invisible="status != 'rejected'"/>
                    <field name="status" widget="statusbar" statusbar_visible="draft,under_review,quotation_sent,confirmed,delivered,returned,completed"/>
                </header>
                <sheet>
                    <div class="oe_title">
                        <h1>
                            <field name="name" readonly="1"/>
                        </h1>
                    </div>
                    <group>
                        <group string="Customer Details">
                            <field name="partner_id"/>
                            <field name="customer_name" required="1"/>
                            <field name="customer_email" required="1"/>
                            <field name="customer_phone" required="1"/>
                            <field name="company_name"/>
                            <field name="customer_address"/>
                        </group>
                        <group string="Event Details">
                            <field name="event_date" readonly="1"/>
                            <field name="start_date"/>
                            <field name="end_date"/>
                            <field name="location"/>
                            <field name="event_type"/>
                        </group>
                    </group>
                    <notebook>
                        <page string="Requested Products" name="products">
                            <field name="line_ids">
                                <tree editable="bottom">
                                    <field name="product_id"/>
                                    <field name="quantity"/>
                                    <field name="price_unit"/>
                                    <field name="price_subtotal" sum="Subtotal"/>
                                    <field name="is_available" widget="boolean" readonly="1" decoration-danger="not is_available" decoration-success="is_available"/>
                                </tree>
                            </field>
                        </page>
                        <page string="Uploaded Documents" name="documents">
                            <field name="document_ids">
                                <tree editable="bottom">
                                    <field name="doc_type"/>
                                    <field name="attachment_id"/>
                                    <field name="verification_status" widget="badge" decoration-warning="verification_status == 'pending'" decoration-success="verification_status == 'verified'" decoration-danger="verification_status == 'rejected'"/>
                                </tree>
                            </field>
                        </page>
                        <page string="Delivery Management" name="delivery">
                            <group>
                                <group>
                                    <field name="delivery_date"/>
                                    <field name="delivery_staff_id"/>
                                </group>
                                <group>
                                    <field name="delivery_status" widget="radio"/>
                                </group>
                            </group>
                        </page>
                        <page string="Pricing &amp; Sales" name="pricing">
                            <group>
                                <group>
                                    <field name="sale_order_id"/>
                                </group>
                                <group>
                                    <field name="delivery_charge"/>
                                    <field name="setup_charge"/>
                                    <field name="amount_total" widget="monetary" readonly="1" class="oe_subtotal_footer_separator"/>
                                </group>
                            </group>
                        </page>
                    </notebook>
                </sheet>
                <div class="oe_chatter">
                    <field name="message_follower_ids" groups="base.group_user"/>
                    <field name="activity_ids"/>
                    <field name="message_ids"/>
                </div>
            </form>
        </field>
    </record>

    <!-- Rental Request Search View -->
    <record id="view_event_rental_request_search" model="ir.ui.view">
        <field name="name">event.rental.request.search</field>
        <field name="model">event.rental.request</field>
        <field name="arch" type="xml">
            <search string="Search Rental Requests">
                <field name="name"/>
                <field name="customer_name"/>
                <field name="customer_email"/>
                <field name="location"/>
                <filter string="Draft" name="draft" domain="[('status', '=', 'draft')]"/>
                <filter string="Under Review" name="under_review" domain="[('status', '=', 'under_review')]"/>
                <filter string="Approved" name="approved" domain="[('status', '=', 'approved')]"/>
                <filter string="Confirmed" name="confirmed" domain="[('status', '=', 'confirmed')]"/>
                <filter string="Delivered" name="delivered" domain="[('status', '=', 'delivered')]"/>
                <filter string="Returned" name="returned" domain="[('status', '=', 'returned')]"/>
                <filter string="Completed" name="completed" domain="[('status', '=', 'completed')]"/>
                <filter string="Rejected" name="rejected" domain="[('status', '=', 'rejected')]"/>
                <group expand="0" string="Group By">
                    <filter string="Customer" name="customer" context="{'group_by': 'partner_id'}"/>
                    <filter string="Status" name="status" context="{'group_by': 'status'}"/>
                    <filter string="Event Date" name="event_date" context="{'group_by': 'event_date'}"/>
                    <filter string="Event Type" name="event_type" context="{'group_by': 'event_type'}"/>
                </group>
            </search>
        </field>
    </record>

    <!-- Action for Rental Requests -->
    <record id="action_event_rental_request" model="ir.actions.act_window">
        <field name="name">Rental Requests</field>
        <field name="type">ir.actions.act_window</field>
        <field name="res_model">event.rental.request</field>
        <field name="view_mode">tree,calendar,form</field>
        <field name="search_view_id" ref="view_event_rental_request_search"/>
        <field name="help" type="html">
            <p class="o_view_nocontent_smiling_face">
                Create your first Event Rental Request!
            </p>
        </field>
    </record>

    <!-- Action for Rental Products -->
    <record id="action_event_rental_products" model="ir.actions.act_window">
        <field name="name">Rental Products</field>
        <field name="res_model">product.template</field>
        <field name="view_mode">kanban,tree,form</field>
        <field name="domain">[('is_rental', '=', True)]</field>
        <field name="context">{'default_is_rental': True}</field>
    </record>

    <!-- Menus -->
    <menuitem id="menu_event_rental_root" name="Event Rentals" sequence="15"/>
    <menuitem id="menu_event_rental_request" name="Rental Requests" parent="menu_event_rental_root" action="action_event_rental_request" sequence="10"/>
    <menuitem id="menu_event_rental_products" name="Rental Products" parent="menu_event_rental_root" action="action_event_rental_products" sequence="20"/>
</odoo>
