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

    <record id="product_template_form_view_config_inherited" model="ir.ui.view">
        <field name="name">product.template.common.form</field>
        <field name="model">product.template</field>
        <field name="priority">16</field>
        <field name="inherit_id" ref="product.product_template_form_view" />
        <field name="arch" type="xml">
            <xpath expr="//header" position="inside">
                <field name="config_ok" invisible="1" />
            </xpath>
        </field>
    </record>

    <record id="product_template_only_form_view_inherited" model="ir.ui.view">
        <field name="name">product.configurator.product.template.form</field>
        <field name="model">product.template</field>
        <field name="priority">16</field>
        <field name="inherit_id" ref="product.product_template_only_form_view" />
        <field name="arch" type="xml">
            <xpath expr='//header' position="inside">
                <button
                    name="configure_product"
                    class="oe_highlight"
                    type="object"
                    string="Configure Product"
                    groups="product_configurator.group_product_configurator"
                    attrs="{'invisible': [('config_ok', '=', False)]}"
                />
            </xpath>
            <div name="button_box" position="inside">
                <button
                    class="oe_stat_button"
                    name="toggle_config"
                    string="Configurable"
                    type="object"
                    icon="fa-wrench"
                    groups="product_configurator.group_product_configurator_manager"
                >
                    <field
                        name="config_ok"
                        string="Configurable"
                        options="{&quot;active&quot;: &quot;Configurable&quot;, &quot;inactive&quot;: &quot;Standard&quot;}"
                        widget="boolean_button"
                    />
                </button>
            </div>
            <xpath
                expr="//label[@for='purchase_ok' or @for='sale_ok']"
                position="after"
            >
                <div class="oe_left" name="options" groups="base.group_user">
                    <field name="id" invisible="True" />
                </div>
            </xpath>

            <!-- Product attributes -->
            <xpath expr="//field[@name='attribute_line_ids']" position="attributes">
                <attribute name="context">{
                    'show_attribute': False,
                    'attribute_line_ids': attribute_line_ids,
                }</attribute>
            </xpath>

            <!-- TODO: Implement a method to hide this field for non-configurable product templates -->
            <xpath
                expr="//field[@name='attribute_line_ids']/tree/field[@name='value_ids']"
                position="after"
            >
                <field
                    name="default_val"
                    domain="[('id', 'in', value_ids)]"
                    context="{'show_attribute': False}"
                    options="{'no_create': True, 'no_create_edit': True}"
                    invisible="not context.get('default_config_ok', False)"
                />
                <field
                    name="required"
                    invisible="not context.get('default_config_ok', False)"
                />
                <field
                    name="multi"
                    invisible="not context.get('default_config_ok', False)"
                    attrs="{'readonly': [('custom','=',True)]}"
                    force_save="1"
                />
                <field
                    name="custom"
                    invisible="not context.get('default_config_ok', False)"
                    attrs="{'readonly': [('multi','=',True)]}"
                    force_save="1"
                />
            </xpath>

            <xpath
                expr="//field[@name='attribute_line_ids']/tree/field[@name='value_ids']"
                position="attributes"
            >
                <attribute name="attrs">{'required': [('custom','!=',True)]}</attribute>
            </xpath>

            <xpath
                expr="//field[@name='attribute_line_ids']/tree/field[@name='attribute_id']"
                position="attributes"
            >
                <attribute
                    name="context"
                >{'flag_config_ok': context.get('default_config_ok', False)}</attribute>
            </xpath>

            <xpath
                expr="//field[@name='attribute_line_ids']/tree/field[@name='attribute_id']"
                position="before"
            >
                <field name="sequence" widget="handle" />
            </xpath>

            <!-- TODO: Apply domains so only values from template are available -->
            <xpath expr="//notebook/page[@name='variants']" position="after">
                <page
                    string="Configurator"
                    name="configurator"
                    attrs="{'invisible': [('config_ok','=',False)]}"
                    groups="product_configurator.group_product_configurator"
                >
                <separator
                        colspan="4"
                        string="Configuration Restrictions"
                        name="configurator_restrictions"
                    />
                <field
                        name="config_line_ids"
                        attrs="{'readonly': [('attribute_line_ids','=',[])]}"
                        context="{'show_attribute': False}"
                    >
                    <tree editable="bottom">
                        <field name="sequence" widget="handle" />
                        <field
                                name="attribute_line_id"
                                domain="[('product_tmpl_id','=',parent.id)]"
                                options="{'no_create': True, 'no_create_edit': True}"
                            />
                        <!-- # TODO: Find a more elegant way to restrict the value_ids -->
                        <field
                                name="attr_line_val_ids"
                                widget="many2many_tags"
                                invisible="True"
                            />
                        <field
                                name="value_ids"
                                widget="many2many_tags"
                                attrs="{'readonly': [('attribute_line_id','=',False)]}"
                                domain="[('id','in',attr_line_val_ids)]"
                                options="{'no_create': True, 'no_create_edit': True}"
                                context="{'show_attribute': False}"
                            />
                        <field
                                name="domain_id"
                                context="{'product_tmpl_id': product_tmpl_id or active_id or parent.id, 'product_attribute_ids': template_attribute_ids, 'form_view_ref': 'product_configurator.product_config_domain_form_view_template'}"
                            />
                        <field name="product_tmpl_id" invisible="1" />
                        <field name="template_attribute_ids" invisible="1" />

                    </tree>
                </field>
                <separator
                        colspan="4"
                        string="Configuration Steps"
                        name="configurator_steps"
                    />
                <field
                        name="config_step_line_ids"
                        attrs="{'readonly': [('attribute_line_ids','=',[])]}"
                    >
                    <tree editable="bottom">
                        <field name="sequence" widget="handle" />
                        <field name="config_step_id" />
                        <field
                                name="attribute_line_ids"
                                required="1"
                                domain="[('product_tmpl_id', '=', parent.id)]"
                                options="{'no_create': True, 'no_create_edit': True}"
                                widget="many2many_tags"
                            />
                    </tree>
                </field>
                <separator
                        colspan="4"
                        string="Configuration Images"
                        name="configurator_images"
                    />
                <field name="config_image_ids">
                    <tree editable="bottom">
                        <field name="sequence" widget="handle" />
                        <field name="name" />
                        <field
                                name="value_ids"
                                widget="many2many_tags"
                                context="{'_cfg_product_tmpl_id': parent.id}"
                            />
                        <field
                                name="image_1920"
                                widget="image"
                                class="oe_prod_config_image"
                            />
                    </tree>
                </field>
                <field name="attribute_line_val_ids" invisible="1" />
                <field name="attribute_value_line_ids" invisible="1">
                    <tree
                            editable="bottom"
                            context="{'default_product_tmpl_id': self.id}"
                        >
                        <field name="product_tmpl_id" invisible="1" />
                        <field name="attribute_id" invisible="1" />
                        <field name="sequence" widget="handle" />
                        <field
                                name="value_id"
                                domain="[('id', 'in', parent.attribute_line_val_ids)]"
                            />
                        <field
                                name="value_ids"
                                domain="[
                                ('id', 'in', parent.attribute_line_val_ids),
                                ('attribute_id', '!=', attribute_id)
                               ]"
                                widget="many2many_tags"
                            />
                    </tree>
                </field>
                <separator
                        string="Variant Name"
                        colspan="4"
                        groups="product_configurator.group_product_configurator_manager"
                    />
                    <field
                        name="mako_tmpl_name"
                        groups="product_configurator.group_product_configurator_manager"
                    />
                </page>
            </xpath>

            <xpath expr="//field[@name='default_code']" position="attributes">
                <attribute
                    name="attrs"
                >{'invisible': [('config_ok','=',True)]}</attribute>
            </xpath>

        </field>
    </record>

    <record id="product_template_search_view" model="ir.ui.view">
        <field name="name">product.configurator.product.template.search.view</field>
        <field name="model">product.template</field>
        <field name="inherit_id" ref="product.product_template_search_view" />
        <field name="arch" type="xml">
            <xpath expr="//filter[@name='filter_to_sell']" position="after">
                <separator />
                <filter
                    string="Standard Products"
                    name="filter_standard_products"
                    domain="[('config_ok','=',False)]"
                />
                <filter
                    string="Configurable Products"
                    name="filter_config_ok"
                    domain="[('config_ok','=',True)]"
                />
            </xpath>
        </field>
    </record>

    <record id="template_view_tree_configurable" model="ir.ui.view">
        <field name="name">product.template.product.tree</field>
        <field name="model">product.template</field>
        <field name="inherit_id" ref="product.product_template_tree_view" />
        <field name="arch" type="xml">
            <xpath expr="//field[@name='default_code']" position="attributes">
                <attribute
                    name="invisible"
                >context.get('default_config_ok', 0)</attribute>
            </xpath>
        </field>
    </record>

    <record id="product_template_kanban_view_inherited" model="ir.ui.view">
        <field name="name">Product.template.product.kanban</field>
        <field name="model">product.template</field>
        <field name="inherit_id" ref="product.product_template_kanban_view" />
        <field name="arch" type="xml">
            <xpath expr="//kanban" position="inside">
                <field name="config_ok" />
            </xpath>
            <xpath expr="//field[@name='priority']" position="before">
                <div
                    class="pull-right"
                    groups="product_configurator.group_product_configurator"
                    attrs="{'invisible': [('config_ok', '=', False)]}"
                >
                    <a name="configure_product" type="object">
                        <i class="fa fa-wrench fa-lg" title="Configure Product" />
                    </a>
                </div>
            </xpath>
        </field>
    </record>

    <record id="product_form_view_custom_vals_inherit" model="ir.ui.view">
        <field name="name">product.configurator.form.view.custom.vals</field>
        <field name="model">product.product</field>
        <field name="inherit_id" ref="product.product_normal_form_view" />
        <field name="arch" type="xml">
            <xpath expr='//header' position="inside">
                <button
                    name="reconfigure_product"
                    groups="product_configurator.group_product_configurator"
                    class="oe_highlight"
                    type="object"
                    string="Reconfigure Product"
                    attrs="{'invisible': [('config_ok','=',False)]}"
                />
            </xpath>
            <!-- <xpath expr="//button[@name='toggle_config']" position="attributes">
                <attribute name="invisible">1</attribute>
            </xpath> -->
            <xpath expr="//div[@name='options']" position="inside">
                <field
                    name="config_preset_ok"
                    attrs="{'invisible': [('config_ok', '=', False)]}"
                />
                <label
                    for="config_preset_ok"
                    attrs="{'invisible': [('config_ok', '=', False)]}"
                />
            </xpath>
        </field>
    </record>

    <record id="product_variant_easy_edit_view_inherit" model="ir.ui.view">
        <field name="name">product.product.view.form.easy</field>
        <field name="model">product.product</field>
        <field name="inherit_id" ref="product.product_variant_easy_edit_view" />
        <field name="mode">primary</field>
        <field name="arch" type="xml">
            <xpath expr="//field[@name='active']" position="after">
                <field name="config_ok" invisible="1" />
                <field
                    name="config_preset_ok"
                    attrs="{'invisible': [('config_ok', '=', False)]}"
                />
            </xpath>
            <xpath expr="//field[@name='weight']" position="attributes">
                <attribute
                    name="attrs"
                >{'readonly': [('config_ok', '=', True)]}</attribute>
            </xpath>
        </field>
    </record>

    <record id="product_kanban_view_inherited" model="ir.ui.view">
        <field name="name">Product Kanban</field>
        <field name="model">product.product</field>
        <field name="inherit_id" ref="product.product_kanban_view" />
        <field name="arch" type="xml">
            <xpath expr="//kanban" position="inside">
                <field name="config_ok" />
            </xpath>
            <xpath
                expr="//div/div[2]/strong[hasclass('o_kanban_record_title')]"
                position="after"
            >
                <div
                    class="pull-right"
                    groups="product_configurator.group_product_configurator"
                    attrs="{'invisible': [('config_ok', '=', False)]}"
                >
                    <a
                        name="reconfigure_product"
                        type="object"
                        class="fa fa-repeat fa-lg"
                    >
                    </a>
                </div>
            </xpath>
        </field>
    </record>

</odoo>
