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

    <!-- Override portal quotation sidebar buttons for rental orders -->
    <template id="sale_order_portal_template_rental_override"
              inherit_id="sale.sale_order_portal_template"
              name="Rental Portal Override">

        <!-- Sidebar: replace Sign & Pay / Accept & Sign with rental agreement button -->
        <xpath expr="//div[@id='sale_order_sidebar_button']" position="before">
            <t t-if="sale_order.is_rental_order and not sale_order.rental_agreement_signed">
                <div class="d-flex flex-column gap-2 mb-3" id="rental_sidebar_buttons">
                    <a t-if="sale_order.state in ('draft', 'sent')"
                       role="button"
                       class="btn btn-primary"
                       t-attf-href="/rental/confirm-and-sign/#{sale_order.id}?access_token=#{sale_order.access_token}"
                    >
                        <i class="fa fa-check me-1"/>Confirm &amp; Sign Agreement
                    </a>
                    <a t-elif="sale_order.state == 'sale' and sale_order.rental_agreement_token"
                       role="button"
                       class="btn btn-primary"
                       t-attf-href="/rental/agreement/#{sale_order.id}/#{sale_order.rental_agreement_token}"
                    >
                        <i class="fa fa-pencil me-1"/>Sign Rental Agreement
                    </a>
                </div>
            </t>
            <t t-if="sale_order.is_rental_order and sale_order.rental_agreement_signed">
                <div class="d-flex flex-column gap-2 mb-3">
                    <span class="btn btn-success disabled">
                        <i class="fa fa-check me-1"/>Agreement Signed
                    </span>
                </div>
            </t>
        </xpath>

        <!-- Hide standard Sign & Pay sidebar button for rental orders -->
        <xpath expr="//div[@id='sale_order_sidebar_button']" position="attributes">
            <attribute name="t-if">not sale_order.is_rental_order</attribute>
        </xpath>

        <!-- Bottom actions: replace for rental orders -->
        <xpath expr="//div[@name='sale_order_actions']" position="before">
            <div t-if="sale_order.is_rental_order and not sale_order.rental_agreement_signed"
                 class="d-flex justify-content-center gap-1 d-print-none">
                <div class="col-sm-auto mt8">
                    <a t-if="sale_order.state in ('draft', 'sent')"
                       role="button"
                       class="btn btn-primary"
                       t-attf-href="/rental/confirm-and-sign/#{sale_order.id}?access_token=#{sale_order.access_token}">
                        <i class="fa fa-check me-1"/>Confirm &amp; Sign Agreement
                    </a>
                    <a t-elif="sale_order.state == 'sale' and sale_order.rental_agreement_token"
                       role="button"
                       class="btn btn-primary"
                       t-attf-href="/rental/agreement/#{sale_order.id}/#{sale_order.rental_agreement_token}">
                        <i class="fa fa-pencil me-1"/>Sign Rental Agreement
                    </a>
                </div>
                <div class="col-sm-auto mt8">
                    <a role="button" class="btn btn-light" href="#discussion">
                        <i class="fa fa-comment me-1"/>Feedback
                    </a>
                </div>
            </div>
        </xpath>

        <!-- Hide standard bottom actions for rental orders -->
        <xpath expr="//div[@name='sale_order_actions']" position="attributes">
            <attribute name="t-if">(sale_order._has_to_be_signed() or sale_order._has_to_be_paid()) and not sale_order.is_rental_order</attribute>
        </xpath>

    </template>

</odoo>
