<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <data>
        <!-- Extend portal_my_orders to add group order name and pickup day columns -->
        <template id="portal_my_orders_extend" inherit_id="sale.portal_my_orders" name="My Orders - Add Group Order Info">
            <!-- Add column headers for new info -->
            <xpath expr="//tr[hasclass('active')]//th[last()]" position="after">
                <th>Group Order</th>
                <th>Pickup Day</th>
                <th class="text-center">Actions</th>
            </xpath>

            <!-- Add data cells for each order row -->
            <xpath expr="//t[@t-foreach='orders']//tr//td[last()]" position="after">
                <td>
                    <t t-if="order.group_order_id">
                        <t t-esc="order.group_order_id.name"/>
                    </t>
                    <t t-else="">
                        —
                    </t>
                </td>
                <td>
                    <t t-if="order.group_order_id">
                        <t t-set="pickup_day" t-value="order.group_order_id.pickup_day"/>
                        <t t-if="pickup_day is not None and day_names">
                            <t t-esc="day_names[int(pickup_day) % 7]"/>
                        </t>
                    </t>
                    <t t-else="">
                        —
                    </t>
                </td>
                <!-- Action buttons disabled
                <td class="text-center">
                    <t t-if="order.group_order_id">

                        <a t-attf-href="/eskaera/#{order.group_order_id.id}/load-from-history/#{order.id}"
                           class="btn btn-sm btn-primary me-1"
                           t-att-title="'Load order items into cart' if order.state == 'draft' else 'Create new order from this one'">
                            <i class="fa fa-cart-arrow-down"></i>
                        </a>

                        <t t-if="order.state == 'draft'">
                            <a t-attf-href="/eskaera/#{order.group_order_id.id}/checkout"
                               class="btn btn-sm btn-success"
                               title="Go to checkout to review and confirm order">
                                <i class="fa fa-check"></i>
                            </a>
                        </t>
                    </t>
                </td> -->
            </xpath>
        </template>

        <!-- Add Load in Cart button to sidebar -->
        <!-- deshabilitado, da problemas en la recarga con productos no disponibles y cambios de precios
        <template id="portal_order_page_sidebar_button" inherit_id="sale.sale_order_portal_template" name="Order Page - Add Load in Cart Button">
            <xpath expr="//div[@id='sale_order_sidebar_button']" position="inside">
                <t t-if="sale_order.group_order_id">
                    <a t-attf-href="/eskaera/#{sale_order.group_order_id.id}/load-from-history/#{sale_order.id}"
                       class="btn btn-primary" role="button">
                        <i class="fa fa-cart-arrow-down me-1" aria-hidden="true"></i>
                        <span>Load in Cart</span>
                    </a>
                </t>
            </xpath>
        </template> -->
        <!-- Custom portal content template with group order info -->
        <template id="sale_order_portal_content_aplicoop" inherit_id="sale.sale_order_portal_content" name="Sale Order Portal Content - Aplicoop">
            <!-- Insert group order info BEFORE the products table -->
            <xpath expr="//table[@id='sales_order_table']" position="before">
                <t t-if="sale_order.group_order_id">
                    <div class="row mb-4">
                        <!-- Group Order Info -->
                        <div class="col-lg-12">
                            <div class="card">
                                <div class="card-header bg-light">
                                    <strong><t t-esc="sale_order.group_order_id.name"/></strong>
                                    <t t-if="sale_order.home_delivery">
                                        <span class="badge bg-primary">
                                            <t t-set="day_idx" t-value="(int(sale_order.group_order_id.pickup_day) + 1) % 7 if sale_order.group_order_id.pickup_day else 0"/>
                                            <t t-esc="day_names[day_idx] if day_names else ''"/>,
                                            <t t-esc="sale_order.group_order_id.delivery_date.strftime('%d/%m/%Y') if sale_order.group_order_id.delivery_date else ''"/>
                                        </span>
                                        <span class="mt-2 text-muted small">
                                            <t t-esc="sale_order.group_order_id.delivery_notice"/>
                                        </span>
                                    </t>
                                    <t t-else="">
                                        <span class="badge bg-primary">
                                            <t t-set="day_idx" t-value="int(sale_order.group_order_id.pickup_day) % 7 if sale_order.group_order_id.pickup_day else 0"/>
                                            <t t-esc="day_names[day_idx] if day_names else ''"/>,
                                            <t t-esc="sale_order.pickup_date.strftime('%d/%m/%Y')"/>
                                        </span>
                                        <span class="mt-2 small">
                                            <t t-foreach="sale_order.group_order_id.group_ids" t-as="group">
                                                <t t-esc="group.name"/>
                                                <t t-if="group.street">
                                                    <t t-esc="group.street"/>
                                                </t>
                                                <t t-if="group.city">
                                                    <t t-esc="group.zip"/> <t t-esc="group.city"/>
                                                </t>
                                            </t>
                                        </span>
                                    </t>
                                </div>
                            </div>
                        </div>
                    </div>
                </t>
            </xpath>
        </template>
    </data>
</odoo>
