{#**
 * Copyright since 2025 InPost S.A.
 *
 * For the full license information, please view the LICENSE file bundled with the module.
 *
 * @author InPost S.A.
 * @copyright since 2025 InPost S.A.
 * @license MIT
 *#}
{% use '@PrestaShop/Admin/TwigTemplateForm/bootstrap_4_horizontal_layout.html.twig' %}

{%- block form_rest -%}
  {% for child in form -%}
    {% if not child.rendered and 'button' not in child.vars.block_prefixes %}
      {{ form_row(child) }}
    {% endif %}
  {%- endfor %}
{% endblock %}

{% block form_row -%}
  {% apply spaceless %}
    <div class="{{ block('form_row_class') }} {% if (not compound or force_error|default(false)) and not valid %} has-error{% endif %}">
      {{ form_label(form) }}
      <div class="{{ block('form_group_class') }}">
        {{ form_widget(form) }}
        {{ form_errors(form) }}
        {{ block('form_help') }}
      </div>
    </div>
  {% endapply %}
{%- endblock form_row %}

{%- block form_label -%}
  {% if label is not same as(false) -%}
    {% if label is empty -%}
      {%- if label_format is not empty -%}
        {% set label = label_format|replace({
          '%name%': name,
          '%id%': id,
        }) %}
      {%- else -%}
        {% set label = name|humanize %}
      {%- endif -%}
    {%- endif -%}

    {% set label_attr = label_attr|merge({
      for: id,
      class: (label_attr.class|default('') ~ ' ' ~ block('form_label_class'))|trim,
    }) %}

    {% if label_attr.popover is defined %}
      {% set popover = label_attr.popover %}
      {% set label_attr = label_attr|filter((v, k) => k != 'popover') %}
    {% else %}
      {% set popover = null %}
    {% endif %}

    <label {% with { attr: label_attr } %}{{ block('attributes') }}{% endwith %}>
      {%- if required %}
        <span class="text-danger">*</span>
        {{- label -}}
      {% else -%}
        {{- label -}}
      {% endif -%}
      {%- if popover %}
        <span
            class="help-box"
            data-toggle="popover"
            data-trigger="hover"
            data-html="true"
            data-content="{{ popover }}"
        ></span>
      {% endif -%}
    </label>
  {%- endif -%}
{%- endblock -%}

{% block form_errors %}
  {% import '@PrestaShop/Admin/macros.html.twig' as ps %}
  {{- ps.form_error_with_popover(form) -}}
{% endblock %}

{% block form_help %}
  {%- if help is not empty -%}
    {%- set help_attr = help_attr|merge({class: (help_attr.class|default('') ~ ' form-text')|trim}) -%}
    <small id="{{ id }}_help"{% with { attr: help_attr } %}{{ block('attributes') }}{% endwith %}>
      {%- if help_html is same as(false) -%}
        {{- help -}}
      {%- else -%}
        {{- help|raw -}}
      {%- endif -%}
    </small>
  {%- endif -%}
{% endblock form_help %}

{% block dimensions_widget %}
  {% import '@PrestaShop/Admin/macros.html.twig' as ps %}
  <div class="row">
    {% for child in form %}
      <div class="col-6 col-md-3">
        {{- ps.form_group_row(child, child.vars, {
          label: child.vars.label,
          label_on_top: true,
          required: false
        }) -}}
      </div>
    {% endfor %}
  </div>
{% endblock %}

{% block weight_widget %}
  {% import '@PrestaShop/Admin/macros.html.twig' as ps %}
  <div class="row">
    <div class="col-6 col-md-9">
      {{- ps.form_widget_with_error(form.amount, form.amount.vars) -}}
    </div>
    <div class="col-6 col-md-3">
      {{- ps.form_widget_with_error(form.unit, form.unit.vars) -}}
    </div>
  </div>
{% endblock %}

{% block phone_widget %}
  {% import '@PrestaShop/Admin/macros.html.twig' as ps %}
  <div class="row">
    <div class="col-4 col-md-3">
      {{- ps.form_widget_with_error(form.prefix, form.prefix.vars) -}}
    </div>
    <div class="col-8 col-md-9">
      {{- ps.form_widget_with_error(form.number, form.number.vars) -}}
    </div>
  </div>
{% endblock %}

{% block reference_row %}
  {% import '@PrestaShop/Admin/macros.html.twig' as ps %}
  <div class="row">
    {% for child in form %}
      <div class="col-6">
        {{- ps.form_group_row(child, child.vars, {
          label: child.vars.label,
          label_on_top: true,
          required: false
        }) -}}
      </div>
    {% endfor %}
  </div>
{% endblock %}

{% block environment_configuration_row %}
  {{ block('fieldset_row') }}
{% endblock %}

{% block contact_details_row %}
  {{ block('fieldset_row') }}
{% endblock %}

{% block contact_person_row %}
  {{ block('fieldset_row') }}
{% endblock %}

{% block address_row %}
  {{ block('fieldset_row') }}
{% endblock %}

{% block parcel_row %}
  {{ block('fieldset_row') }}
{% endblock %}

{% block fieldset_row %}
  <fieldset {% with {attr: row_attr|merge({class: (row_attr.class|default('') ~ ' p-3 my-4 border')|trim})} %}{{ block('attributes') }}{% endwith %}>
    <legend {% with {attr: label_attr|merge({class: (label_attr.class|default('') ~ ' h4 w-auto px-3')|trim})} %}{{ block('attributes') }}{% endwith %}>
      {{ form.vars.label }}
      {{ form_errors(form) }}
    </legend>

    {{ form_rest(form) }}
  </fieldset>
{% endblock %}

{% block dynamic_collection_widget %}
  {% with {'child': prototype} %}
    <div {{ block('widget_container_attributes') }} data-prototype="{{ block('collection_item_widget')|e('html_attr') }}">
      <div class="js-inpost-intl-collection-items">
        {% for child in form %}
          {{ block('collection_item_widget') }}
        {% endfor %}
      </div>

      {% if allow_add %}
        <button type="button" class="js-inpost-intl-add-collection-item btn btn-outline-primary">
          <i class="material-icons">add_circle</i>
          {{ 'Add another'|trans({}, 'Modules.Inpostinternational.Admin') }}
        </button>
      {% endif %}
    </div>
  {% endwith %}
{% endblock %}

{% block collection_item_widget %}
  <div class="js-inpost-intl-collection-item">
    {% if allow_delete %}
      <div class="input-group row">
        <div class="col">
          {{ form_row(child) }}
        </div>

        <div class="input-group-btn col-auto m-auto">
          <button type="button" class="js-inpost-intl-remove-collection-item btn btn-outline-secondary float-right btn-sm" title="{{ 'Remove'|trans({}, 'Admin.Actions') }}">
            <i class="material-icons icon-delete">delete</i>
          </button>
        </div>
      </div>
    {% else %}
      {{ form_row(child) }}
    {% endif %}
  </div>
{% endblock %}

{% block pickup_time_widget %}
  {% import '@PrestaShop/Admin/macros.html.twig' as ps %}
  <div class="row">
    <div class="col-auto">
      {{- ps.form_widget_with_error(form.from, form.from.vars) -}}
    </div>
    <div class="col-1 text-center my-auto">—</div>
    <div class="col">
      {{- ps.form_widget_with_error(form.to, form.to.vars) -}}
    </div>
  </div>
{% endblock %}

{% block inpost_intl_point_id_widget %}
  <div class="input-group">
    {{ block('form_widget_simple') }}

    <div class="input-group-append">
      <button class="btn btn-outline-secondary js-inpost-intl-geowidget-button" type="button" data-target="#{{ form.vars.id }}">
        {{ 'Open map'|trans({}, 'Modules.Inpostinternational.Shipment') }}
      </button>
    </div>
  </div>
{% endblock %}
