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

    <record id="sale_order_form_inherit_fusion_rental" model="ir.ui.view">
        <field name="name">sale.order.form.inherit.fusion.rental</field>
        <field name="model">sale.order</field>
        <field name="inherit_id" ref="sale_renting.rental_order_form_view"/>
        <field name="arch" type="xml">

            <!-- Smart buttons in button_box -->
            <div name="button_box" position="inside">
                <button name="action_view_deposit_invoice"
                        type="object"
                        class="oe_stat_button"
                        icon="fa-shield"
                        invisible="not is_rental_order or rental_deposit_invoice_count == 0">
                    <field name="rental_deposit_invoice_count"
                           widget="statinfo"
                           string="Security Deposit"/>
                </button>
                <button name="action_view_rental_charges_invoice"
                        type="object"
                        class="oe_stat_button"
                        icon="fa-file-text-o"
                        invisible="not is_rental_order or rental_charges_invoice_count == 0">
                    <field name="rental_charges_invoice_count"
                           widget="statinfo"
                           string="Rental Invoice"/>
                </button>
                <button name="action_view_renewal_invoices"
                        type="object"
                        class="oe_stat_button"
                        icon="fa-refresh"
                        invisible="not is_rental_order or rental_renewal_invoice_count == 0">
                    <field name="rental_renewal_invoice_count"
                           widget="statinfo"
                           string="Renewals"/>
                </button>
                <button name="action_view_refund_invoices"
                        type="object"
                        class="oe_stat_button"
                        icon="fa-undo"
                        invisible="not is_rental_order or rental_refund_invoice_count == 0">
                    <field name="rental_refund_invoice_count"
                           widget="statinfo"
                           string="Refunds"/>
                </button>
            </div>

            <!-- Header buttons -->
            <button name="action_open_pickup" position="before">
                <button name="action_send_rental_agreement"
                        type="object"
                        class="btn-secondary"
                        string="Send Agreement"
                        data-hotkey="a"
                        invisible="not is_rental_order or state != 'sale' or rental_agreement_signed"
                        icon="fa-file-text-o"/>
                <button name="action_manual_renewal"
                        type="object"
                        class="btn-secondary"
                        string="Renew Rental"
                        data-hotkey="r"
                        invisible="not is_rental_order or state != 'sale' or rental_status not in ('pickup', 'return')"
                        icon="fa-refresh"/>

                <!-- Deposit buttons -->
                <button name="action_create_deposit_invoice"
                        type="object"
                        class="btn-secondary"
                        string="Create Deposit Invoice"
                        invisible="not is_rental_order or state != 'sale' or rental_deposit_invoice_id"
                        icon="fa-file-text"/>
                <button name="action_mark_deposit_collected"
                        type="object"
                        class="btn-secondary"
                        string="Mark Deposit Collected"
                        invisible="not is_rental_order or rental_deposit_status != 'pending'"
                        icon="fa-check-circle"/>
                <button name="action_process_deposit"
                        type="object"
                        class="btn-secondary"
                        string="Process Deposit"
                        invisible="not is_rental_order or rental_deposit_status not in ('collected', 'refund_hold')"
                        icon="fa-credit-card"/>

                <button name="action_close_rental"
                        type="object"
                        class="btn-warning"
                        string="Close Rental"
                        data-hotkey="x"
                        invisible="not is_rental_order or state != 'sale' or rental_closed or rental_deposit_status not in ('refunded', 'deducted', False)"
                        confirm="This will delete the stored card and send a thank-you email. Continue?"
                        icon="fa-power-off"/>
            </button>

            <!-- Hidden fields (must stay in form, outside notebook) -->
            <field name="duration_days" position="after">
                <field name="rental_reminder_sent" invisible="1"/>
                <field name="rental_original_duration" invisible="1"/>
                <field name="rental_agreement_token" invisible="1"/>
                <field name="rental_marketing_email_sent" invisible="1"/>
            </field>

            <!-- Notebook pages -->
            <xpath expr="//notebook" position="inside">

                <!-- Rental Management -->
                <page string="Rental Management"
                      name="rental_management"
                      invisible="not is_rental_order">

                    <!-- Row 1: Agreement + Payment -->
                    <group>
                        <group string="Agreement">
                            <field name="rental_agreement_signed"
                                   widget="boolean_toggle" readonly="1"/>
                            <field name="rental_agreement_signer_name"
                                   invisible="not rental_agreement_signed" readonly="1"/>
                            <field name="rental_agreement_signed_date"
                                   invisible="not rental_agreement_signed" readonly="1"/>
                        </group>
                        <group string="Payment">
                            <field name="rental_payment_token_id"/>
                            <field name="rental_apply_cc_fee"
                                   invisible="not rental_payment_token_id"/>
                            <field name="rental_charges_invoice_id"
                                   invisible="not rental_charges_invoice_id" readonly="1"/>
                            <field name="rental_deposit_invoice_id"
                                   invisible="not rental_deposit_invoice_id" readonly="1"/>
                            <div invisible="not is_rental_order or state != 'sale' or rental_closed"
                                 class="mt-3" colspan="2">
                                <div class="d-grid gap-2" style="max-width:280px;">
                                    <button name="action_open_card_authorization"
                                            type="object"
                                            class="btn btn-primary btn-sm"
                                            string="Authorize Card by Phone"
                                            icon="fa-phone"/>
                                    <button name="action_send_card_reauthorization"
                                            type="object"
                                            class="btn btn-outline-secondary btn-sm"
                                            string="Send Authorization Link to Customer"
                                            icon="fa-envelope"
                                            confirm="This will send a card authorization form to the customer. Continue?"/>
                                </div>
                            </div>
                        </group>
                    </group>

                    <!-- Row 2: Renewal + Status -->
                    <group>
                        <group string="Renewal">
                            <field name="rental_auto_renew"/>
                            <field name="rental_auto_renew_off_reason"
                                   invisible="rental_auto_renew"
                                   required="is_rental_order and not rental_auto_renew"
                                   placeholder="Reason for disabling auto-renewal..."/>
                            <field name="rental_renewal_period"
                                   invisible="not rental_auto_renew"/>
                            <field name="rental_max_renewals"
                                   invisible="not rental_auto_renew"/>
                            <field name="rental_next_renewal_date"
                                   invisible="not rental_auto_renew"/>
                            <field name="rental_renewal_count"
                                   invisible="rental_renewal_count == 0"/>
                        </group>
                        <group string="Status">
                            <field name="rental_deposit_status"
                                   invisible="not rental_deposit_status"
                                   decoration-success="rental_deposit_status == 'refunded'"
                                   decoration-warning="rental_deposit_status in ('pending', 'refund_hold', 'collected')"
                                   decoration-danger="rental_deposit_status == 'deducted'"
                                   widget="badge"/>
                            <field name="rental_inspection_status"
                                   invisible="not rental_inspection_status"
                                   decoration-success="rental_inspection_status == 'passed'"
                                   decoration-danger="rental_inspection_status == 'flagged'"
                                   widget="badge"/>
                            <field name="rental_purchase_interest"
                                   invisible="not rental_purchase_interest"
                                   widget="boolean_toggle" readonly="1"/>
                            <field name="rental_purchase_coupon_id"
                                   invisible="not rental_purchase_coupon_id" readonly="1"/>
                            <field name="rental_closed"
                                   invisible="not rental_closed" readonly="1"/>
                        </group>
                    </group>

                    <!-- Row 3: Document + Billing (visible after agreement signed) -->
                    <group invisible="not rental_agreement_signed">
                        <group string="Signed Agreement">
                            <div invisible="not rental_agreement_document" class="mb-2">
                                <button name="action_preview_rental_agreement" type="object"
                                        class="btn btn-outline-primary"
                                        icon="fa-file-pdf-o">
                                    Preview Signed Agreement
                                </button>
                            </div>
                            <field name="rental_agreement_document"
                                   filename="rental_agreement_document_filename"
                                   widget="binary" readonly="1"/>
                            <field name="rental_agreement_document_filename" invisible="1"/>
                        </group>
                        <group string="Billing Details">
                            <field name="rental_billing_address" readonly="1"/>
                            <field name="rental_billing_postal_code" readonly="1"/>
                        </group>
                    </group>
                </page>

                <!-- Renewal History -->
                <page string="Renewal History"
                      name="renewal_history"
                      invisible="not is_rental_order or rental_renewal_count == 0">
                    <field name="rental_renewal_log_ids" readonly="1">
                        <list>
                            <field name="renewal_number"/>
                            <field name="renewal_type"/>
                            <field name="previous_start_date"/>
                            <field name="previous_return_date"/>
                            <field name="new_start_date"/>
                            <field name="new_return_date"/>
                            <field name="invoice_id"/>
                            <field name="payment_status"
                                   decoration-success="payment_status == 'paid'"
                                   decoration-danger="payment_status == 'failed'"
                                   decoration-warning="payment_status == 'pending'"
                                   widget="badge"/>
                            <field name="state"
                                   decoration-success="state == 'done'"
                                   decoration-danger="state == 'failed'"
                                   widget="badge"/>
                        </list>
                    </field>
                </page>

                <!-- Cancellation Requests -->
                <page string="Cancellation Requests"
                      name="cancellation_requests"
                      invisible="not is_rental_order"
                      badge="rental_cancellation_request_ids">
                    <field name="rental_cancellation_request_ids">
                        <list>
                            <field name="request_date"/>
                            <field name="partner_id"/>
                            <field name="reason"/>
                            <field name="assigned_user_id"/>
                            <field name="state"
                                   decoration-info="state == 'new'"
                                   decoration-success="state in ('confirmed', 'completed')"
                                   decoration-warning="state == 'pickup_scheduled'"
                                   decoration-danger="state == 'rejected'"
                                   widget="badge"/>
                        </list>
                    </field>
                </page>

                <!-- Inspection -->
                <page string="Inspection"
                      name="inspection"
                      invisible="not is_rental_order">
                    <group>
                        <group>
                            <field name="rental_inspection_status"
                                   decoration-success="rental_inspection_status == 'passed'"
                                   decoration-danger="rental_inspection_status == 'flagged'"
                                   decoration-info="rental_inspection_status == 'pending'"
                                   widget="badge"/>
                        </group>
                    </group>
                    <div class="alert alert-secondary" role="alert"
                         invisible="rental_inspection_status">
                        No inspection has been performed yet. Use the
                        <strong>Return</strong> button to process the return
                        and complete the inspection.
                    </div>
                    <group string="Inspection Notes"
                           invisible="not rental_inspection_status">
                        <field name="rental_inspection_notes" nolabel="1"/>
                    </group>
                    <group string="Inspection Photos"
                           invisible="not rental_inspection_status">
                        <field name="rental_inspection_photo_ids"
                               widget="inspection_photos"
                               nolabel="1"
                               class="o_inspection_photos"
                               options="{'accepted_file_extensions': 'image/*'}"/>
                    </group>
                </page>
            </xpath>

        </field>
    </record>

</odoo>
