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

        <!-- Tree view for Group Order -->
        <record id="view_group_order_tree" model="ir.ui.view">
            <field name="name">group.order.tree</field>
            <field name="model">group.order</field>
            <field name="arch" type="xml">
                <list string="Group Orders">
                    <field name="sequence" widget="handle"/>
                    <field name="company_id" optional="hide"/>
                    <field name="name"/>
                    <field name="slug" optional="hide"/>
                    <field name="group_ids" widget="many2many_tags" options="{'color_field': 'color'}"/>
                    <field name="type" optional="show"/>
                    <field name="start_date" optional="show"/>
                    <field name="end_date" optional="show"/>
                    <field name="home_delivery" optional="hide"/>
                    <field name="delivery_product_id" optional="hide"/>
                    <field name="state" optional="show"/>
                </list>
            </field>
        </record>

        <!-- Form view for Group Order -->
        <record id="view_group_order_form" model="ir.ui.view">
            <field name="name">group.order.form</field>
            <field name="model">group.order</field>
            <field name="arch" type="xml">
                <form string="Group Order">
                    <header>
                        <button name="action_open" type="object" string="Open" invisible="state != 'draft'" class="oe_highlight"/>
                        <button name="action_close" type="object" string="Close" invisible="state != 'open'"/>
                        <button name="action_cancel" type="object" string="Cancel" invisible="state in ('closed', 'cancelled')"/>
                        <button name="action_reset_to_draft" type="object" string="Reset to Draft" invisible="state != 'closed'" class="oe_highlight"/>
                        <field name="state" widget="statusbar" statusbar_visible="draft,open,closed"/>
                    </header>
                    <sheet>
                        <div class="oe_title">
                            <div class="row">
                                <div class="col-md-1">
                                    <field name="image" widget="image" class="oe_avatar"/>
                                </div>
                                <div class="col-md-11">
                                    <h1>
                                        <field name="name" placeholder="Order Name"/>
                                    </h1>
                                    <div class="text-muted d-flex align-items-center">
                                        <span class="me-1">/eskaera/</span>
                                        <field name="slug" placeholder="weekly-order" class="oe_inline" help="Readable identifier used in the public URL of this order. Empty it to generate it again from the name; changing it breaks the links already shared with members."/>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <group>
                            <group>
                                <field name="company_id" help="Company that owns this group order"/>
                                <field name="sequence" help="Sequence for ordering in website list"/>
                                <field name="group_ids" widget="many2many_tags" help="Groups that can participate in this order"/>
                                <field name="type" help="Type of group order: Regular, Special, or Promotional"/>
                                <field name="start_date" help="Day when the order opens for purchases (anchors biweekly/monthly cycles)"/>
                                <field name="end_date" help="Day when the order closes (empty = permanent). One-time orders are confirmed and closed once it has passed"/>
                            </group>
                            <group>
                                <field name="period" help="How often this order repeats"/>
                                <field name="pickup_day" help="Day when members pick up orders"/>
                                <field name="cutoff_day" help="Day when purchases stop"/>
                            </group>
                        </group>
                        <notebook>
                            <page string="Calculated Dates" name="calculated_dates">
                            <group>
                                <field name="cutoff_date" readonly="1" help="Automatically calculated cutoff date"/>
                                <field name="pickup_date" readonly="1" help="Automatically calculated pickup date"/>
                                <field name="delivery_date" readonly="1" help="Automatically calculated delivery date (pickup + 1 day)"/>
                            </group>
                            </page>
                            <page string="Description" name="description">
                                <field name="description" placeholder="Free text description..." nolabel="1"/>
                            </page>
                        <page string="Delivery">
                            <field name="home_delivery" readonly="1" help="Automatically enabled when a delivery product is configured"/>
                            <field name="delivery_product_id" help="Product to use for home delivery. Setting this enables home delivery."/>
                            <field name="delivery_notice" placeholder="Information about home delivery..." nolabel="1"/>
                        </page>
                        <page string="Product Catalog">
                            <group string="Included Products" col="2">
                                <field name="supplier_ids" widget="many2many_tags" help="All products from these suppliers will be included"/>
                                <field name="category_ids" widget="many2many_tags" help="All products in these categories (including subcategories) will be included"/>
                                <field name="product_ids" widget="many2many_tags" help="Specific products to include directly"/>
                            </group>
                            <group string="Excluded Products" col="2">
                                <field name="excluded_supplier_ids" widget="many2many_tags" help="Suppliers excluded from this order. Products with these suppliers as main seller will not be available (blacklist has absolute priority)"/>
                                <field name="excluded_category_ids" widget="many2many_tags" help="Categories excluded from this order. Products in these categories and all their subcategories will not be available (blacklist has absolute priority)"/>
                                <field name="excluded_product_ids" widget="many2many_tags" help="Products explicitly excluded from this order (blacklist has absolute priority over inclusions)"/>
                            </group>
                        </page>
                        <page string="Pickup Slots" name="pickup_slots">
                                <field name="pickup_slot_ids">
                                    <list editable="bottom">
                                        <field name="sequence" widget="handle"/>
                                        <field name="weekday"/>
                                        <field name="label" placeholder="e.g. Morning slot"/>
                                        <field name="start_hour" widget="float_time"/>
                                        <field name="end_hour" widget="float_time"/>
                                        <field name="active"/>
                                    </list>
                                </field>
                            </page>
                        </notebook>
                    </sheet>
                </form>
            </field>
        </record>

        <!-- Search view for Group Order -->
        <record id="view_group_order_search" model="ir.ui.view">
            <field name="name">group.order.search</field>
            <field name="model">group.order</field>
            <field name="arch" type="xml">
                <search string="Group Orders">
                    <field name="name"/>
                    <field name="slug"/>
                    <field name="group_ids"/>
                    <field name="type"/>
                    <field name="state"/>
                    <separator/>
                    <filter name="draft" string="Draft" domain="[('state', '=', 'draft')]"/>
                    <filter name="open" string="Open" domain="[('state', '=', 'open')]"/>
                    <filter name="closed" string="Closed" domain="[('state', '=', 'closed')]"/>

                </search>
            </field>
        </record>

        <!-- Acción para Group Order -->
        <record id="action_group_order" model="ir.actions.act_window">
            <field name="name">Group Orders</field>
            <field name="res_model">group.order</field>
            <field name="view_mode">list,form</field>
            <field name="help" type="html">
                <p class="o_view_nocontent_smiling_face">
                    Create a new group order
                </p>
            </field>
        </record>

        <!-- Menú para acceder a Group Order -->
        <menuitem id="menu_group_order" name="Consumer Group Management" parent="website_sale.menu_ecommerce" sequence="50"/>
        <menuitem id="menu_group_order_list" name="Consumer Group Orders" parent="menu_group_order" action="action_group_order" sequence="1"/>

        <!-- Consumer Groups Views -->
        <record id="view_consumer_group_tree" model="ir.ui.view">
            <field name="name">consumer.group.tree</field>
            <field name="model">res.partner</field>
            <field name="arch" type="xml">
                <list string="Consumer Groups">
                    <field name="name"/>
                    <field name="email"/>
                    <field name="phone"/>
                    <field name="city"/>
                    <field name="member_ids" widget="many2many_tags"/>
                </list>
            </field>
        </record>

        <record id="view_consumer_group_form" model="ir.ui.view">
            <field name="name">consumer.group.form</field>
            <field name="model">res.partner</field>
            <field name="arch" type="xml">
                <form string="Consumer Group">
                    <sheet>
                        <div class="oe_button_box" name="button_box"/>
                        <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': 'avatar_128'}"/>
                        <div class="oe_title">
                            <h1>
                                <field name="name" placeholder="Group Name"/>
                            </h1>
                        </div>
                        <group>
                            <group>
                                <field name="email"/>
                                <field name="phone"/>
                                <field name="mobile"/>
                            </group>
                            <group>
                                <field name="street"/>
                                <field name="city"/>
                                <field name="zip"/>
                                <field name="country_id"/>
                            </group>
                        </group>
                        <notebook>
                            <page string="Members" name="members">
                                <field name="member_ids">
                                    <list>
                                        <field name="name"/>
                                        <field name="email"/>
                                    </list>
                                </field>
                            </page>
                            <page string="Internal Notes" name="notes">
                                <field name="comment" placeholder="Internal notes..."/>
                            </page>
                        </notebook>
                    </sheet>
                    <chatter/>
                </form>
            </field>
        </record>

        <record id="action_consumer_groups" model="ir.actions.act_window">
            <field name="name">Consumer Groups</field>
            <field name="res_model">res.partner</field>
            <field name="view_mode">list,form</field>
            <field name="domain">[('is_group', '=', True)]</field>
            <field name="context">{'default_is_group': True, 'default_is_company': True}</field>
            <field name="view_ids" eval="[(5, 0, 0),
                (0, 0, {'view_mode': 'list', 'view_id': ref('view_consumer_group_tree')}),
                (0, 0, {'view_mode': 'form', 'view_id': ref('view_consumer_group_form')})]"/>
            <field name="help" type="html">
                <p class="o_view_nocontent_smiling_face">
                    Create a new consumer group
                </p>
                <p>
                    Consumer groups are organizations that can place group orders together.
                </p>
            </field>
        </record>

        <menuitem id="menu_consumer_groups" name="Consumer Groups" parent="menu_group_order" action="action_consumer_groups" sequence="10"/>

    </data>
</odoo>
