{% extends "layout.html" %}

{% block content_header %}
  {{ build_breadcrumbs([
    { 'name': _('Outcall'), 'link': url_for('.OutcallView:index'), 'icon': 'long-arrow-left' }
  ]) }}
{% endblock %}

{% block content %}
  <section class="content">
    {% call build_list_containers(_('Outcalls'), 'long-arrow-left') %}
      {% call build_list_table() %}
        {% call build_list_table_headers(get=url_for('.OutcallView:get', id=''), delete=url_for('.OutcallView:delete', id='')) %}
          <th>{{ _('Name') }}</th>
          <th>{{ _('Description') }}</th>
        {% endcall %}
        {% call(outcall) build_list_table_rows(resource_list['items']) %}
          <td>{{ outcall.name }}</td>
          <td>{{ outcall.description }}</td>
        {% endcall %}
      {% endcall %}
    {% endcall %}

    {% call build_hidden_add_containers(_('Add Outcall')) %}
      {% call build_form() %}
        {% call add_default_fields(form=form, submit_value=_('Add')) %}
          {{ render_field(form.name) }}
          {{ render_field(form.description) }}
          {{ render_field(form.trunks_ids, data_listing_href=url_for(listing_urls['trunk'])) }}
          {{ render_field(form.trunks, with_label=False) }}

              {{ build_add_row_entry_header() }}
              {% call build_table() %}
                {% call build_table_headers() %}
                  <th>{{ _('Context') }}</th>
                  <th>{{ _('Extension') }}</th>
                  <th>{{ _('Caller ID') }}</th>
                  <th>{{ _('External prefix') }}</th>
                  <th>{{ _('Prefix') }}</th>
                  <th>{{ _('Strip digits') }}</th>
                {% endcall %}

                {% call build_table_body(class_='dynamic-table') %}
                  {% do form.extensions.append_entry() %}
                  {{ _build_extension_entry(form.extensions.pop_entry(), template=True) }}

                  {% for extension in form.extensions %}
                    {{ _build_extension_entry(extension) }}
                  {% endfor %}
                {% endcall %}
              {% endcall %}

              {{ build_add_row_entry_header() }}
        {% endcall %}
      {% endcall %}
    {% endcall %}
  </section>
{% endblock %}

{% macro _build_extension_entry(extension, template=False) %}
  {% if template %}
    {% set tr_class = "row-template hidden" %}
  {% else %}
    {% set tr_class = "row-line" %}
  {% endif %}
  <tr class="{{ tr_class }}">
    <td>{{ render_field(extension.context,
                        with_label=False,
                        class_='outcall-context',
                        placeholder=_('Choose Context'),
                        data_listing_href=url_for(listing_urls['context_by_type'], type_='outcall')) }}</td>
    <td>{{ render_field(extension.exten, with_label=False) }}</td>
    <td>{{ render_field(extension.caller_id, with_label=False) }}</td>
    <td>{{ render_field(extension.external_prefix, with_label=False) }}</td>
    <td>{{ render_field(extension.prefix_, with_label=False) }}</td>
    <td>{{ render_field(extension.strip_digits, with_label=False) }}</td>
    <td>{{ add_delete_entry_button() }}</td>
  </tr>
{% endmacro %}
