<?xml version="1.0" encoding="UTF-8"?>
<odoo>
    <data>
        <!-- Template to load items from history and redirect to group order -->
        <template id="eskaera_load_from_history" name="Load Order from History">
            <html>
                <head>
                    <meta charset="utf-8"/>
                    <title>Loading Order...</title>
                </head>
                <body>
                    <script type="text/javascript">
                        // Items are embedded directly in the script (pre-serialized JSON from controller)
                        var itemsJson = <t t-raw="items_json"/>;  // This is a JSON array/string
                        var groupOrderId = <t t-esc="group_order_id"/>;
                        var saleOrderName = '<t t-esc="sale_order_name"/>';
                        var pickupDay = '<t t-esc="pickup_day or ''"/>';
                        var pickupDate = '<t t-esc="pickup_date or ''"/>';
                        var homeDelivery = <t t-esc="home_delivery and 'true' or 'false'"/>;
                        var sameGroupOrder = <t t-esc="same_group_order and 'true' or 'false'"/>;

                        console.log('load_from_history template: groupOrderId=', groupOrderId);
                        console.log('load_from_history template: saleOrderName=', saleOrderName);
                        console.log('load_from_history template: pickupDay=', pickupDay);
                        console.log('load_from_history template: pickupDate=', pickupDate);
                        console.log('load_from_history template: homeDelivery=', homeDelivery);
                        console.log('load_from_history template: sameGroupOrder=', sameGroupOrder);
                        console.log('load_from_history template: itemsJson type=', typeof itemsJson);
                        console.log('load_from_history template: itemsJson value=', itemsJson);

                        // If itemsJson is already a string, use it directly; if it's an array, stringify it
                        var itemsJsonString = (typeof itemsJson === 'string') ? itemsJson : JSON.stringify(itemsJson);

                        // Store items to sessionStorage
                        sessionStorage['load_from_history_' + groupOrderId] = itemsJsonString;

                        // Store sale order name separately
                        sessionStorage['load_from_history_order_name_' + groupOrderId] = saleOrderName;

                        // Store pickup fields ONLY if from same group order
                        if (sameGroupOrder === 'true') {
                            sessionStorage['load_from_history_pickup_day_' + groupOrderId] = pickupDay;
                            sessionStorage['load_from_history_pickup_date_' + groupOrderId] = pickupDate;
                            sessionStorage['load_from_history_home_delivery_' + groupOrderId] = homeDelivery;
                            console.log('Saved pickup fields (same group order)');
                        } else {
                            console.log('Skipped saving pickup fields (different group order - will use current group order days)');
                        }

                        console.log('Saved to sessionStorage[load_from_history_' + groupOrderId + ']:', itemsJsonString);
                        console.log('Saved order name to sessionStorage[load_from_history_order_name_' + groupOrderId + ']:', saleOrderName);

                        // Redirect to group order page
                        // The JavaScript on that page will detect this and load the items
                        window.location.href = '/eskaera/' + groupOrderId;
                    </script>
                </body>
            </html>
        </template>
    </data>
</odoo>
