{#
 # For the full copyright and license information, please view the
 # docs/licenses/LICENSE.txt file that was distributed with this source code.
 #}

<div class="product-header-details">
{% block header_summary %}
  {# It is important to use vars.data and not vars.value because value contains the localized value which may
   # not be written with numeric characters anymore depending on the language but data is the initial float data.
   #}
  {% set productData = productForm.vars.data %}
  <div class="product-header-summary">
    <div class="product-field-preview" data-role="price-tax-excluded">
      <div class="product-field-value">
        {% if productData.pricing.retail_price.ecotax_tax_excluded is defined %}
          {% set final_price_tax_excluded = productData.pricing.retail_price.price_tax_excluded + productData.pricing.retail_price.ecotax_tax_excluded %}
          {{ final_price_tax_excluded|price_format }}
        {% else %}
          {{ productData.pricing.retail_price.price_tax_excluded|price_format }}
        {% endif %}
      </div>
      <div class="product-field-label">
        {{ 'tax excl.'|trans({}, 'Admin.Global') }}
      </div>
    </div>

    <div class="product-field-preview" data-role="price-tax-included">
      <div class="product-field-value">
        {{ productData.pricing.retail_price.price_tax_included|price_format }}
      </div>
      <div class="product-field-label">
        {# If tax feature is enabled display it in the label #}
        {% if taxEnabled %}
          {# First find the selected tax rate #}
          {% set taxRate = 0 %}
          {% for group_label, choice in productForm.pricing.retail_price.tax_rules_group_id.vars.choices %}
            {% if choice.data == productData.pricing.retail_price.tax_rules_group_id %}
              {% set taxRate = choice.attr['data-tax-rate'] %}
            {% endif %}
          {% endfor %}

          {{ 'tax incl. (tax rule: %taxRate%%)'|trans({'%taxRate%': taxRate|round(2)}, 'Admin.Catalog.Feature') }}
        {% else %}
          {{ 'tax incl.'|trans({}, 'Admin.Catalog.Feature') }}
        {% endif %}
      </div>
    </div>

    {# Only display stock when Stock management is enabled #}
    {% if stockEnabled %}
      {% if productData.header.type == 'pack' and productData.stock.quantities.pack_quantity is not null %}
        {% set quantityData = productData.stock.quantities.pack_quantity %}
      {% else %}
        {% set quantityData = productData.stock.quantities.delta_quantity.quantity %}
      {% endif %}

      {% if productData.header.type != 'combinations' %}
        {% set lowStock = productData.stock.options.low_stock_threshold %}
      {% else %}
        {% set lowStock = 0 %}
      {% endif %}

      {% set outOfStockLabel = 'out of stock'|trans({}, 'Admin.Catalog.Feature') %}
      {% set lowStockLabel = 'low stock'|trans({}, 'Admin.Catalog.Feature') %}
      {% set inStockLabel = 'in stock'|trans({}, 'Admin.Catalog.Feature') %}

      {% if quantityData <= 0 %}
        {% set stockClass = 'danger' %}
        {% set stockLabel = outOfStockLabel %}
      {% elseif lowStock > 0 and quantityData <= lowStock %}
        {% set stockClass = 'warning' %}
        {% set stockLabel = lowStockLabel %}
      {% else %}
        {% set stockClass = 'success' %}
        {% set stockLabel = inStockLabel %}
      {% endif %}

      <div
        class="product-field-preview"
        data-role="quantity"
        {# these data attributes are needed for dynamic quantity update in javascript side #}
        data-low-stock-threshold="{{ lowStock }}"
        data-out-of-stock-label="{{ outOfStockLabel }}"
        data-low-stock-label="{{ lowStockLabel }}"
        data-in-stock-label="{{ inStockLabel }}"
      >
        <div class="product-total-quantity product-field-value {{ stockClass }}">
          {{ quantityData }}
        </div>
        <div class="product-field-label product-total-quantity-label">
          {{ stockLabel }}
        </div>
      </div>
    {% endif %}
  </div>
{% endblock %}

{% block header_references %}
  {% set referencesForm = productForm.details.references %}
  <div class="product-header-references">
    {% for keyChild, child in referencesForm.children %}
      {% if child.vars.value is not empty %}
        <div class="product-reference" data-reference-type="{{ keyChild }}">
          <label>{{ child.vars.label }}</label>
          <span>{{ child.vars.value }}</span>
        </div>
      {% endif %}
    {% endfor %}
  </div>
{% endblock %}
</div>
