<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <template id="variants">
        <t t-set="attribute_exclusions" t-value="product._get_attribute_exclusions(parent_combination)"/>
        <t
            t-set="has_configurable_attribute_line"
            t-value="any(ptal._is_configurable() for ptal in product.attribute_line_ids)"
        />
        <ul
            t-attf-class="o_wsale_product_page_variants d-flex flex-column gap-4 list-unstyled js_add_cart_variants #{ul_class} #{'d-none' if not has_configurable_attribute_line else '' }"
            t-att-data-attribute-exclusions="json.dumps(attribute_exclusions)"
        >
            <t t-foreach="product.valid_product_template_attribute_line_ids" t-as="ptal">
                <t t-set="attribute" t-value="ptal.attribute_id"/>
                <t t-set="ptavs" t-value="ptal.product_template_value_ids._only_active()"/>
                <t t-set="single" t-value="len(ptavs) == 1"/>

                <!-- Attributes selection is hidden if there is only one value available and it's not a custom value -->
                <li name="variant_attribute"
                    t-att-data-attribute-id="attribute.id"
                    t-att-data-attribute-name="attribute.name"
                    t-att-data-attribute-display-type="attribute.display_type"
                    t-attf-class="variant_attribute #{
                        'd-none' if single
                        and not ptavs[0].is_custom
                        and not attribute.display_type == 'multi' else ''
                    }">

                    <!-- Used to customize layout if the only available attribute value is custom -->
                    <t t-set="single_and_custom" t-value="single and ptavs[0].is_custom"/>
                    <h6 class="attribute_name mb-2">
                        <span t-field="ptal.attribute_id.name"/>
                        <span t-if="attribute.display_type in ['color', 'image']" class="text-muted">
                            -
                            <t
                                t-set="active_ptavs"
                                t-value="[ptav for ptav in ptavs if ptav in combination]"
                            />
                            <t t-foreach="active_ptavs" t-as="ptav">
                                <span class="attribute_value" name="attribute_value" t-field="ptav.name"/>
                            </t>
                        </span>
                    </h6>

                    <t t-if="attribute.display_type == 'select'">
                        <select
                            t-att-data-attribute-id="attribute.id"
                            t-attf-class="form-select css_attribute_select o_wsale_product_attribute js_variant_change #{attribute.create_variant} #{'d-none' if single_and_custom else ''}"
                            t-att-name="'ptal-%s' % ptal.id">
                            <t t-foreach="ptavs" t-as="ptav">
                                <option t-att-value="ptav.id"
                                    t-att-data-attribute-value-id="ptav.product_attribute_value_id.id"
                                    t-att-data-value-id="ptav.id"
                                    t-att-data-value-name="ptav.name"
                                    t-att-data-attribute-name="attribute.name"
                                    t-att-data-is-custom="ptav.is_custom"
                                    t-att-selected="ptav in combination">
                                    <span t-field="ptav.name"/>
                                    <t t-call="website_sale.badge_extra_price"/>
                                </option>
                            </t>
                        </select>
                    </t>

                    <t t-elif="attribute.display_type in ('radio', 'multi')">
                        <ul
                            t-att-data-attribute-id="attribute.id"
                            t-attf-class="o_wsale_product_attribute d-flex flex-wrap gap-3 list-unstyled #{'d-none' if single_and_custom else ''}"
                        >
                            <t t-foreach="ptavs" t-as="ptav">
                                <li class="js_attribute_value">
                                    <label class="col-form-label p-0">
                                        <div class="form-check cursor-pointer">
                                            <input t-att-type="'radio' if attribute.display_type == 'radio' else 'checkbox'"
                                                t-attf-class="form-check-input js_variant_change #{attribute.create_variant}"
                                                t-att-checked="ptav in combination"
                                                t-att-title="ptav.name"
                                                t-att-name="'ptal-%s' % ptal.id"
                                                t-att-value="ptav.id"
                                                t-att-data-attribute-value-id="ptav.product_attribute_value_id.id"
                                                t-att-data-value-id="ptav.id"
                                                t-att-data-value-name="ptav.name"
                                                t-att-data-attribute-name="attribute.name"
                                                t-att-data-is-custom="ptav.is_custom"/>
                                            <div class="radio_input_value form-check-label">
                                                <span t-field="ptav.name"/>
                                                <t t-call="website_sale.badge_extra_price"/>
                                            </div>
                                        </div>
                                    </label>
                                </li>
                            </t>
                        </ul>
                    </t>

                    <t t-elif="attribute.display_type == 'pills'">
                        <ul
                            t-att-data-attribute-id="attribute.id"
                            t-attf-class="o_wsale_product_attribute d-flex flex-wrap list-unstyled gap-2 #{'d-none' if single_and_custom else ''}"
                            data-bs-toggle="buttons"
                        >
                            <t t-foreach="ptavs" t-as="ptav">
                                <li t-attf-class="o_variant_pills js_attribute_value border btn m-0 cursor-pointer #{'active border-primary text-primary-emphasis bg-primary-subtle' if ptav in combination else ''}">
                                    <input type="radio"
                                        t-attf-class="js_variant_change position-absolute #{attribute.create_variant} opacity-0 cursor-pointer"
                                        t-att-checked="ptav in combination"
                                        t-att-name="'ptal-%s' % ptal.id"
                                        t-att-value="ptav.id"
                                        t-att-data-value-id="ptav.id"
                                        t-att-id="ptav.id"
                                        t-att-data-attribute-value-id="ptav.product_attribute_value_id.id"
                                        t-att-data-value-name="ptav.name"
                                        t-att-data-attribute-name="attribute.name"
                                        t-att-data-is-custom="ptav.is_custom"
                                        t-att-autocomplete="off"/>
                                    <label class="radio_input_value o_variant_pills_input_value cursor-pointer"
                                           t-att-for="ptav.id">
                                        <span t-field="ptav.name"/>
                                        <t t-call="website_sale.badge_extra_price"/>
                                    </label>
                                </li>
                            </t>
                        </ul>
                    </t>

                    <t t-elif="attribute.display_type == 'color'">
                        <ul
                            t-att-data-attribute-id="attribute.id"
                            t-attf-class="o_wsale_product_attribute d-flex flex-wrap gap-2 list-unstyled #{'d-none' if single_and_custom else ''}"
                        >
                            <li t-foreach="ptavs" t-as="ptav">
                                <t t-set="img_style"
                                   t-value="'background: url(/web/image/product.template.attribute.value/%s/image); background-size: cover; ' % ptav.id if ptav.image else ''"
                                />
                                <t t-set="color_style"
                                   t-value="'background:' + str(ptav.html_color or ptav.name if not ptav.is_custom else '')"
                                />
                                <label t-attf-style="#{img_style or color_style}"
                                       t-attf-class="css_attribute_color cursor-pointer #{'active' if ptav in combination else ''} #{'custom_value' if ptav.is_custom else ''} #{'transparent' if (not ptav.is_custom and not ptav.html_color) else ''}"
                                >
                                      <input type="radio"
                                        t-attf-class="js_variant_change cursor-pointer #{attribute.create_variant}"
                                        t-att-checked="ptav in combination"
                                        t-att-name="'ptal-%s' % ptal.id"
                                        t-att-value="ptav.id"
                                        t-att-title="ptav.name"
                                        t-att-data-attribute-value-id="ptav.product_attribute_value_id.id"
                                        t-att-data-value-id="ptav.id"
                                        t-att-data-value-name="ptav.name"
                                        t-att-data-attribute-name="attribute.name"
                                        t-att-data-is-custom="ptav.is_custom"/>
                                </label>
                            </li>
                        </ul>
                    </t>

                    <t t-elif="attribute.display_type == 'image'">
                        <ul
                            t-att-data-attribute-id="attribute.id"
                            t-attf-class="list-inline o_wsale_product_attribute #{'d-none' if single_and_custom else ''}"
                        >
                            <li t-foreach="ptavs" t-as="ptav" class="list-inline-item me-2 mb-2">
                                <label
                                    name="o_wsale_attribute_image_selector"
                                    t-attf-style="#{'background-image: url(/web/image/product.template.attribute.value/%s/image); background-size:cover;' % ptav.id if ptav.image else ''}"
                                    t-attf-class="css_attribute_image oe_img_bg o_bg_img_center position-relative d-inline-block rounded-3 bg-body text-center cursor-pointer #{'active' if ptav in combination else ''} #{'custom_value' if ptav.is_custom else ''} #{'transparent' if (not ptav.is_custom and not ptav.html_color) else ''}"
                                >
                                    <input
                                        type="radio"
                                        t-attf-class="js_variant_change w-100 h-100 opacity-0 cursor-pointer #{attribute.create_variant}"
                                        t-att-checked="ptav in combination"
                                        t-att-name="'ptal-%s' % ptal.id"
                                        t-att-value="ptav.id"
                                        t-att-title="ptav.name"
                                        t-att-data-attribute-value-id="ptav.product_attribute_value_id.id"
                                        t-att-data-value-id="ptav.id"
                                        t-att-data-value-name="ptav.name"
                                        t-att-data-attribute-name="attribute.name"
                                        t-att-data-is-custom="ptav.is_custom"
                                    />
                                </label>
                            </li>
                        </ul>
                    </t>
                </li>
            </t>
        </ul>
        <ul
            t-if="product._has_multiple_uoms()"
            t-attf-class="o_wsale_product_page_variants d-flex flex-column gap-4 list-unstyled js_add_cart_variants mt-4 #{ul_class}"
            t-att-data-attribute-exclusions="json.dumps(attribute_exclusions)"
        >
            <!-- NOTE: the t-att-data-attribute-exclusions is only there to match existing events selectors -->
            <li>
                <!-- Separate template to reduce views duplication -->
                <t t-call="website_sale.packaging_title"/>
                <ul class="btn-group-toggle list-inline list-unstyled" data-bs-toggle="buttons">
                    <t t-set="product_uoms" t-value="product._get_available_uoms()"/>
                    <t t-foreach="product_uoms" t-as="uom">
                        <t t-set="is_main_uom" t-value="uom.id == product.uom_id.id"/>
                        <li t-attf-class="o_variant_pills border btn m-0 cursor-pointer #{'active border-primary text-primary-emphasis bg-primary-subtle' if is_main_uom else ''}">
                            <input
                                class="position-absolute opacity-0 cursor-pointer"
                                type="radio"
                                t-att-checked="is_main_uom"
                                name="uom_id"
                                t-att-value="uom.id"
                                t-attf-id="uom-{{uom.id}}"
                                t-att-autocomplete="off"/>
                            <label class="radio_input_value cursor-pointer" t-attf-for="uom-{{uom.id}}">
                                <span t-field="uom.name"/>
                            </label>
                        </li>
                    </t>
                </ul>
            </li>
        </ul>
    </template>
    <template id="badge_extra_price" name="Badge Extra Price">
        <t t-set="price_extra" t-value="ptav._get_extra_price(combination_info)"/>
        <span t-if="price_extra" t-attf-class="{{'badge text-bg-primary' if attribute.display_type != 'pills' else 'small text-muted'}}">
            <!--
                price_extra is displayed as catalog price instead of
                price after pricelist because it is impossible to
                compute. Indeed, the pricelist rule might depend on the
                selected variant, so the price_extra will be different
                depending on the selected combination. The price of an
                attribute is therefore variable and it's not very
                accurate to display it.
            -->
        <t t-if="attribute.display_type in ['pills', 'select']">(</t><span class="sign_badge_price_extra"><t t-out="price_extra > 0 and '+' or '-'"/></span>
        <span
            t-out="abs(price_extra)"
            class="variant_price_extra"
            style="white-space: nowrap;"
            t-options='{
                "widget": "monetary",
                "display_currency": website.currency_id
            }'/><t t-if="attribute.display_type in ['pills', 'select']">)</t>
        </span>
    </template>
    <template id="website_sale.packaging_title" name="Packaging title">
        <h6 class="attribute_name mb-2">Packaging</h6>
    </template>
</odoo>
