<?xml version="1.0" encoding="UTF-8"?>
<odoo>
    <record id="view_fusion_loaner_checkout_list" model="ir.ui.view">
        <field name="name">fusion.loaner.checkout.list</field>
        <field name="model">fusion.loaner.checkout</field>
        <field name="arch" type="xml">
            <list decoration-danger="state == 'overdue'"
                  decoration-warning="state == 'rental_pending'"
                  decoration-muted="state in ('returned', 'lost')"
                  default_order="checkout_date desc, id desc">
                <field name="name"/>
                <field name="partner_id"/>
                <field name="product_id"/>
                <field name="lot_id" optional="show"/>
                <field name="checkout_date"/>
                <field name="expected_return_date"/>
                <field name="actual_return_date" optional="hide"/>
                <field name="days_out"/>
                <field name="days_overdue" optional="hide"/>
                <field name="sales_rep_id" optional="hide"/>
                <field name="checkout_condition" optional="hide"/>
                <field name="return_condition" optional="hide"/>
                <field name="sale_order_id" optional="hide"/>
                <field name="state" widget="badge"
                       decoration-info="state == 'draft'"
                       decoration-success="state == 'checked_out'"
                       decoration-danger="state in ('overdue', 'lost')"
                       decoration-warning="state == 'rental_pending'"
                       decoration-muted="state in ('returned', 'converted_rental')"/>
            </list>
        </field>
    </record>

    <record id="view_fusion_loaner_checkout_form" model="ir.ui.view">
        <field name="name">fusion.loaner.checkout.form</field>
        <field name="model">fusion.loaner.checkout</field>
        <field name="arch" type="xml">
            <form>
                <header>
                    <button name="action_checkout" type="object" string="Confirm Checkout"
                            class="btn-primary" invisible="state != 'draft'"/>
                    <button name="action_return" type="object" string="Return Loaner"
                            class="btn-success" invisible="state not in ('checked_out', 'overdue', 'rental_pending')"/>
                    <field name="state" widget="statusbar"
                           statusbar_visible="draft,checked_out,returned"/>
                </header>
                <sheet>
                    <div class="oe_button_box" name="button_box">
                        <button name="action_view_sale_order" type="object"
                                class="oe_stat_button" icon="fa-file-text-o"
                                invisible="not sale_order_id">
                            <div class="o_field_widget o_stat_info">
                                <span class="o_stat_text">Sale Order</span>
                            </div>
                        </button>
                        <button name="action_view_partner" type="object"
                                class="oe_stat_button" icon="fa-user">
                            <div class="o_field_widget o_stat_info">
                                <span class="o_stat_text">Contact</span>
                            </div>
                        </button>
                    </div>
                    <div class="oe_title">
                        <h1><field name="name" readonly="1"/></h1>
                    </div>
                    <group>
                        <group string="Client Information">
                            <field name="partner_id"/>
                            <field name="sales_rep_id"/>
                            <field name="sale_order_id"/>
                        </group>
                        <group string="Product">
                            <field name="product_id"/>
                            <field name="lot_id" context="{'default_product_id': product_id}"/>
                            <field name="loaner_period_days"/>
                        </group>
                    </group>
                    <group>
                        <group string="Dates">
                            <field name="checkout_date"/>
                            <field name="expected_return_date"/>
                            <field name="actual_return_date"/>
                            <field name="days_out"/>
                        </group>
                        <group string="Condition">
                            <field name="checkout_condition"/>
                            <field name="checkout_notes"/>
                        </group>
                    </group>
                </sheet>
                <chatter/>
            </form>
        </field>
    </record>

    <record id="view_fusion_loaner_checkout_search" model="ir.ui.view">
        <field name="name">fusion.loaner.checkout.search</field>
        <field name="model">fusion.loaner.checkout</field>
        <field name="arch" type="xml">
            <search string="Search Loaners">
                <field name="name"/>
                <field name="partner_id"/>
                <field name="product_id"/>
                <field name="lot_id" string="Serial Number"/>
                <field name="sale_order_id"/>
                <field name="sales_rep_id"/>
                <separator/>
                <filter string="Draft" name="filter_draft" domain="[('state', '=', 'draft')]"/>
                <filter string="Checked Out" name="filter_checked_out" domain="[('state', '=', 'checked_out')]"/>
                <filter string="Overdue" name="filter_overdue" domain="[('state', '=', 'overdue')]"/>
                <filter string="Rental Pending" name="filter_rental_pending" domain="[('state', '=', 'rental_pending')]"/>
                <filter string="Returned" name="filter_returned" domain="[('state', '=', 'returned')]"/>
                <filter string="Converted to Rental" name="filter_converted" domain="[('state', '=', 'converted_rental')]"/>
                <filter string="Lost" name="filter_lost" domain="[('state', '=', 'lost')]"/>
                <separator/>
                <filter string="Active Loaners" name="filter_active" domain="[('state', 'in', ['checked_out', 'overdue', 'rental_pending'])]"/>
                <filter string="Needs Attention" name="filter_attention" domain="[('state', 'in', ['overdue', 'rental_pending'])]"/>
                <filter string="My Loaners" name="filter_my_loaners" domain="[('sales_rep_id', '=', uid)]"/>
                <separator/>
                <filter string="Needs Repair (Checkout)" name="filter_checkout_repair" domain="[('checkout_condition', '=', 'needs_repair')]"/>
                <filter string="Damaged (Return)" name="filter_return_damaged" domain="[('return_condition', 'in', ['needs_repair', 'damaged'])]"/>
                <separator/>
                <filter string="Status" name="group_state" context="{'group_by': 'state'}"/>
                <filter string="Client" name="group_client" context="{'group_by': 'partner_id'}"/>
                <filter string="Product" name="group_product" context="{'group_by': 'product_id'}"/>
                <filter string="Sales Rep" name="group_sales_rep" context="{'group_by': 'sales_rep_id'}"/>
                <filter string="Checkout Month" name="group_checkout_month" context="{'group_by': 'checkout_date:month'}"/>
            </search>
        </field>
    </record>

    <record id="view_fusion_loaner_history_list" model="ir.ui.view">
        <field name="name">fusion.loaner.history.list</field>
        <field name="model">fusion.loaner.history</field>
        <field name="arch" type="xml">
            <list default_order="action_date desc, id desc">
                <field name="action_date"/>
                <field name="checkout_id"/>
                <field name="partner_id" optional="show"/>
                <field name="product_id" optional="show"/>
                <field name="lot_id" optional="hide"/>
                <field name="action" widget="badge"
                       decoration-info="action in ('create', 'note')"
                       decoration-success="action in ('checkout', 'return')"
                       decoration-warning="action in ('reminder_sent', 'overdue', 'rental_pending')"
                       decoration-danger="action in ('lost', 'condition_update')"/>
                <field name="user_id"/>
                <field name="notes" optional="show"/>
            </list>
        </field>
    </record>

    <record id="view_fusion_loaner_history_search" model="ir.ui.view">
        <field name="name">fusion.loaner.history.search</field>
        <field name="model">fusion.loaner.history</field>
        <field name="arch" type="xml">
            <search string="Search Loaner History">
                <field name="checkout_id"/>
                <field name="partner_id"/>
                <field name="product_id"/>
                <field name="lot_id" string="Serial Number"/>
                <field name="user_id"/>
                <separator/>
                <filter string="Checkouts" name="filter_checkout" domain="[('action', '=', 'checkout')]"/>
                <filter string="Returns" name="filter_return" domain="[('action', '=', 'return')]"/>
                <filter string="Reminders" name="filter_reminders" domain="[('action', '=', 'reminder_sent')]"/>
                <filter string="Overdue" name="filter_overdue" domain="[('action', '=', 'overdue')]"/>
                <filter string="Rental Conversions" name="filter_rental" domain="[('action', 'in', ['rental_pending', 'rental_converted'])]"/>
                <filter string="Lost" name="filter_lost" domain="[('action', '=', 'lost')]"/>
                <separator/>
                <filter string="Action Type" name="group_action" context="{'group_by': 'action'}"/>
                <filter string="Client" name="group_client" context="{'group_by': 'partner_id'}"/>
                <filter string="Product" name="group_product" context="{'group_by': 'product_id'}"/>
                <filter string="Month" name="group_month" context="{'group_by': 'action_date:month'}"/>
            </search>
        </field>
    </record>

    <!-- Wizard Views -->
    <record id="view_loaner_checkout_wizard_form" model="ir.ui.view">
        <field name="name">fusion.loaner.checkout.wizard.form</field>
        <field name="model">fusion.loaner.checkout.wizard</field>
        <field name="arch" type="xml">
            <form>
                <group>
                    <field name="partner_id"/>
                    <field name="product_id"/>
                    <field name="lot_id" context="{'default_product_id': product_id}"/>
                    <field name="checkout_date"/>
                    <field name="loaner_period_days"/>
                    <field name="checkout_condition" widget="radio"/>
                    <field name="checkout_notes"/>
                </group>
                <footer>
                    <button name="action_checkout" type="object" string="Checkout Loaner" class="btn-primary"/>
                    <button string="Cancel" class="btn-secondary" special="cancel"/>
                </footer>
            </form>
        </field>
    </record>

    <record id="view_loaner_return_wizard_form" model="ir.ui.view">
        <field name="name">fusion.loaner.return.wizard.form</field>
        <field name="model">fusion.loaner.return.wizard</field>
        <field name="arch" type="xml">
            <form>
                <group>
                    <field name="checkout_id" readonly="1"/>
                    <field name="return_date"/>
                    <field name="return_condition" widget="radio"/>
                    <field name="return_notes"/>
                </group>
                <footer>
                    <button name="action_return" type="object" string="Confirm Return" class="btn-primary"/>
                    <button string="Cancel" class="btn-secondary" special="cancel"/>
                </footer>
            </form>
        </field>
    </record>

    <!-- Loaner Products Views -->
    <record id="view_fusion_loaner_products_list" model="ir.ui.view">
        <field name="name">product.template.loaner.list</field>
        <field name="model">product.template</field>
        <field name="arch" type="xml">
            <list>
                <field name="name"/>
                <field name="default_code" string="Reference" optional="show"/>
                <field name="x_fc_equipment_type" optional="show"/>
                <field name="x_fc_wheelchair_category" optional="show"/>
                <field name="x_fc_seat_width" optional="show"/>
                <field name="x_fc_seat_depth" optional="show"/>
                <field name="x_fc_seat_height" optional="hide"/>
                <field name="x_fc_storage_location" optional="show"/>
                <field name="x_flm_current_location" optional="show"/>
                <field name="x_fc_listing_type" optional="show"/>
                <field name="x_fc_asset_number" optional="hide"/>
                <field name="active" column_invisible="True"/>
            </list>
        </field>
    </record>

    <!-- Dedicated simplified form for loaner products -->
    <record id="view_fusion_loaner_products_form" model="ir.ui.view">
        <field name="name">product.template.loaner.simplified.form</field>
        <field name="model">product.template</field>
        <field name="arch" type="xml">
            <form>
                <sheet>
                    <field name="id" invisible="1"/>
                    <field name="product_variant_count" invisible="1"/>
                    <field name="x_fc_can_be_loaned" invisible="1"/>
                    <div class="oe_button_box" name="button_box">
                        <button name="action_open_quants" type="object"
                                icon="fa-cubes" class="oe_stat_button">
                            <div class="o_field_widget o_stat_info">
                                <span class="o_stat_value">
                                    <field name="qty_available" widget="statinfo" nolabel="1"/>
                                </span>
                                <span class="o_stat_text">On Hand</span>
                            </div>
                        </button>
                        <button name="action_view_serial_numbers" type="object"
                                icon="fa-barcode" class="oe_stat_button"
                                invisible="tracking == 'none'">
                            <div class="o_field_widget o_stat_info">
                                <span class="o_stat_value">
                                    <field name="x_flm_serial_count" widget="statinfo" nolabel="1"/>
                                </span>
                                <span class="o_stat_text">Serial Numbers</span>
                            </div>
                        </button>
                    </div>
                    <widget name="web_ribbon" title="Archived" bg_color="text-bg-danger"
                            invisible="active"/>
                    <field name="image_1920" widget="image" class="oe_avatar"
                           options="{'preview_image': 'image_128'}"/>
                    <div class="oe_title">
                        <label for="name" string="Product"/>
                        <h1>
                            <field name="name" placeholder="Product Name"/>
                        </h1>
                    </div>
                    <group>
                        <group string="Identification">
                            <field name="default_code" string="Reference"/>
                            <field name="barcode"/>
                            <field name="categ_id" string="Category"/>
                            <field name="x_fc_asset_number"/>
                            <field name="company_id" groups="base.group_multi_company"/>
                        </group>
                        <group string="Inventory">
                            <field name="type" string="Product Type"/>
                            <field name="tracking"/>
                            <field name="active" invisible="1"/>
                        </group>
                    </group>
                    <notebook>
                        <page string="Equipment Details" name="equipment_details">
                            <group>
                                <group string="Type &amp; Classification">
                                    <field name="x_fc_equipment_type"/>
                                    <field name="x_fc_wheelchair_category"/>
                                    <field name="x_fc_listing_type"/>
                                </group>
                                <group string="Dimensions">
                                    <field name="x_fc_seat_width"/>
                                    <field name="x_fc_seat_depth"/>
                                    <field name="x_fc_seat_height"/>
                                </group>
                            </group>
                        </page>
                        <page string="Loaner Settings" name="loaner_settings">
                            <group>
                                <group string="Loaner Period">
                                    <field name="x_fc_loaner_period_days"/>
                                </group>
                                <group string="Rental Pricing (if not returned)">
                                    <field name="x_fc_rental_price_weekly"/>
                                    <field name="x_fc_rental_price_monthly"/>
                                </group>
                            </group>
                            <group string="Security Deposit">
                                <group>
                                    <field name="x_fc_security_deposit_type"/>
                                    <field name="x_fc_security_deposit_amount"
                                           invisible="x_fc_security_deposit_type != 'fixed'"/>
                                    <field name="x_fc_security_deposit_percent"
                                           invisible="x_fc_security_deposit_type != 'percentage'"/>
                                </group>
                            </group>
                        </page>
                        <page string="Location" name="location_tracking">
                            <group>
                                <group string="Storage">
                                    <field name="x_fc_storage_location"/>
                                    <field name="x_flm_current_location" readonly="1"/>
                                </group>
                            </group>
                        </page>
                        <page string="Package Info" name="package_info">
                            <group>
                                <field name="x_fc_package_info" nolabel="1"/>
                            </group>
                        </page>
                        <page string="Internal Notes" name="internal_notes">
                            <field name="description_picking" string="Notes"
                                   placeholder="Internal notes about this loaner product..."/>
                        </page>
                    </notebook>
                </sheet>
                <chatter/>
            </form>
        </field>
    </record>

    <record id="view_fusion_loaner_products_search" model="ir.ui.view">
        <field name="name">product.template.loaner.search</field>
        <field name="model">product.template</field>
        <field name="arch" type="xml">
            <search string="Search Loaner Products">
                <field name="name"/>
                <field name="default_code" string="Reference"/>
                <field name="x_fc_equipment_type"/>
                <field name="x_fc_asset_number"/>
                <separator/>
                <filter string="Owned" name="filter_owned" domain="[('x_fc_listing_type', '=', 'owned')]"/>
                <filter string="Borrowed" name="filter_borrowed" domain="[('x_fc_listing_type', '=', 'borrowed')]"/>
                <separator/>
                <filter string="Archived" name="filter_archived" domain="[('active', '=', False)]"/>
                <separator/>
                <filter string="Equipment Type" name="group_equipment_type" context="{'group_by': 'x_fc_equipment_type'}"/>
                <filter string="Wheelchair Category" name="group_wheelchair_category" context="{'group_by': 'x_fc_wheelchair_category'}"/>
                <filter string="Storage Location" name="group_storage_location" context="{'group_by': 'x_fc_storage_location'}"/>
                <filter string="Listing Type" name="group_listing_type" context="{'group_by': 'x_fc_listing_type'}"/>
            </search>
        </field>
    </record>

    <!-- Loaner checkbox on standard product form (global) -->
    <record id="view_product_template_loaner_checkbox" model="ir.ui.view">
        <field name="name">product.template.loaner.checkbox</field>
        <field name="model">product.template</field>
        <field name="inherit_id" ref="product.product_template_form_view"/>
        <field name="priority">50</field>
        <field name="arch" type="xml">
            <xpath expr="//div[@name='options']" position="inside">
                <span class="d-inline-flex">
                    <field name="x_fc_can_be_loaned"/>
                    <label for="x_fc_can_be_loaned" string="Loaner"/>
                </span>
            </xpath>
        </field>
    </record>

    <!-- Loaner Settings tab on standard product form (only visible when loaner checkbox is checked) -->
    <record id="view_product_template_loaner_form" model="ir.ui.view">
        <field name="name">product.template.loaner.form</field>
        <field name="model">product.template</field>
        <field name="inherit_id" ref="product.product_template_form_view"/>
        <field name="arch" type="xml">
            <xpath expr="//page[@name='sales']" position="after">
                <page string="Loaner Settings" name="loaner_settings" invisible="not x_fc_can_be_loaned">
                    <group>
                        <group string="Loaner Period">
                            <field name="x_fc_loaner_period_days"/>
                        </group>
                        <group string="Rental Pricing (if not returned)">
                            <field name="x_fc_rental_price_weekly"/>
                            <field name="x_fc_rental_price_monthly"/>
                        </group>
                    </group>
                    <group>
                        <group string="Equipment Details">
                            <field name="x_fc_equipment_type"/>
                            <field name="x_fc_wheelchair_category"/>
                            <field name="x_fc_listing_type"/>
                            <field name="x_fc_asset_number"/>
                        </group>
                        <group string="Dimensions">
                            <field name="x_fc_seat_width"/>
                            <field name="x_fc_seat_depth"/>
                            <field name="x_fc_seat_height"/>
                        </group>
                    </group>
                    <group>
                        <group string="Location">
                            <field name="x_fc_storage_location"/>
                            <field name="x_flm_current_location" readonly="1"/>
                        </group>
                    </group>
                    <group string="Package Information">
                        <field name="x_fc_package_info" nolabel="1" colspan="2"/>
                    </group>
                    <group string="Security Deposit">
                        <group>
                            <field name="x_fc_security_deposit_type"/>
                            <field name="x_fc_security_deposit_amount" invisible="x_fc_security_deposit_type != 'fixed'"/>
                            <field name="x_fc_security_deposit_percent" invisible="x_fc_security_deposit_type != 'percentage'"/>
                        </group>
                    </group>
                </page>
            </xpath>
        </field>
    </record>

    <!-- Actions -->
    <record id="action_fusion_loaner_checkout" model="ir.actions.act_window">
        <field name="name">Loaner Equipment</field>
        <field name="res_model">fusion.loaner.checkout</field>
        <field name="view_mode">list,form</field>
        <field name="search_view_id" ref="view_fusion_loaner_checkout_search"/>
        <field name="context">{'search_default_filter_active': 1}</field>
        <field name="help" type="html">
            <p class="o_view_nocontent_smiling_face">No loaner checkouts yet</p>
            <p>Track loaner equipment issued to clients during assessments or trials.</p>
        </field>
    </record>

    <record id="action_fusion_loaner_all" model="ir.actions.act_window">
        <field name="name">All Loaners</field>
        <field name="res_model">fusion.loaner.checkout</field>
        <field name="view_mode">list,form</field>
        <field name="search_view_id" ref="view_fusion_loaner_checkout_search"/>
    </record>

    <record id="action_fusion_loaner_overdue" model="ir.actions.act_window">
        <field name="name">Overdue Loaners</field>
        <field name="res_model">fusion.loaner.checkout</field>
        <field name="view_mode">list,form</field>
        <field name="search_view_id" ref="view_fusion_loaner_checkout_search"/>
        <field name="context">{'search_default_filter_attention': 1}</field>
    </record>

    <record id="action_fusion_loaner_returned" model="ir.actions.act_window">
        <field name="name">Returned Loaners</field>
        <field name="res_model">fusion.loaner.checkout</field>
        <field name="view_mode">list,form</field>
        <field name="search_view_id" ref="view_fusion_loaner_checkout_search"/>
        <field name="context">{'search_default_filter_returned': 1}</field>
    </record>

    <record id="action_fusion_loaner_history" model="ir.actions.act_window">
        <field name="name">Loaner History</field>
        <field name="res_model">fusion.loaner.history</field>
        <field name="view_mode">list</field>
        <field name="search_view_id" ref="view_fusion_loaner_history_search"/>
    </record>

    <record id="action_fusion_loaner_products" model="ir.actions.act_window">
        <field name="name">Loaner Products</field>
        <field name="res_model">product.template</field>
        <field name="view_mode">list,form</field>
        <field name="view_id" eval="False"/>
        <field name="search_view_id" ref="view_fusion_loaner_products_search"/>
        <field name="domain">[('x_fc_can_be_loaned', '=', True)]</field>
        <field name="context">{'default_x_fc_can_be_loaned': True, 'default_sale_ok': False, 'default_purchase_ok': False, 'default_tracking': 'serial'}</field>
        <field name="help" type="html">
            <p class="o_view_nocontent_smiling_face">No loaner products configured yet</p>
            <p>Mark products as "Loaner" in the product form to add them here.</p>
        </field>
    </record>

    <record id="action_fusion_loaner_products_view_list" model="ir.actions.act_window.view">
        <field name="sequence" eval="1"/>
        <field name="view_mode">list</field>
        <field name="view_id" ref="view_fusion_loaner_products_list"/>
        <field name="act_window_id" ref="action_fusion_loaner_products"/>
    </record>

    <record id="action_fusion_loaner_products_view_form" model="ir.actions.act_window.view">
        <field name="sequence" eval="2"/>
        <field name="view_mode">form</field>
        <field name="view_id" ref="view_fusion_loaner_products_form"/>
        <field name="act_window_id" ref="action_fusion_loaner_products"/>
    </record>

    <!-- Menus - standalone root -->
    <menuitem id="menu_loaner_root"
              name="Loaners"
              web_icon="fusion_loaners_management,static/description/icon.png"
              sequence="58"/>

    <menuitem id="menu_loaner_active"
              name="Active Loaners"
              parent="menu_loaner_root"
              action="action_fusion_loaner_checkout"
              sequence="10"/>

    <menuitem id="menu_loaner_all"
              name="All Loaners"
              parent="menu_loaner_root"
              action="action_fusion_loaner_all"
              sequence="15"/>

    <menuitem id="menu_loaner_overdue"
              name="Overdue / Attention"
              parent="menu_loaner_root"
              action="action_fusion_loaner_overdue"
              sequence="18"/>

    <menuitem id="menu_loaner_returned"
              name="Returned"
              parent="menu_loaner_root"
              action="action_fusion_loaner_returned"
              sequence="19"/>

    <menuitem id="menu_loaner_history"
              name="Loaner History"
              parent="menu_loaner_root"
              action="action_fusion_loaner_history"
              sequence="20"/>

    <menuitem id="menu_loaner_products"
              name="Loaner Products"
              parent="menu_loaner_root"
              action="action_fusion_loaner_products"
              sequence="30"/>
</odoo>
