{% extends "layout.html" %}

{% block content_header %}
  {{ build_breadcrumb(_('Lines'), '', 'exchange', [_('List')]) }}
{% endblock %}

{% block content %}
  {% call build_section_row() %}

    {% call build_form_tabs_box() %}
      {% call build_tabs_navigation() %}
        {{ add_tab_navigation_header(_('Line'), '', 'exchange') }}
        {{ add_tab_navigation_item('line', _('Line'), active=True) }}
        {{ add_tab_navigation_item('aor_section_options', _('AOR')) }}
        {{ add_tab_navigation_item('auth_section_options', _('Authentication')) }}
        {{ add_tab_navigation_item('endpoint_section_options', _('Endpoint')) }}
        {{ add_tab_navigation_item('identify_section_options', _('Identify')) }}
        {{ add_tab_navigation_item('registration_section_options', _('Registration')) }}
        {{ add_tab_navigation_item('registration_outbound_auth_section_options', _('Registration Outbound Auth')) }}
        {{ add_tab_navigation_item('outbound_auth_section_options', _('Outbound Auth')) }}
      {% endcall %}

      {% call build_form(action=url_for('.LineView:post')) %}
        {% call build_tabs_content() %}
          {% call build_tab_content_item('line', active=True) %}
            {% call add_default_fields(form=form, submit_value=_('Add')) %}
              {{ render_field(form.endpoint_sip['label']) }}
              {{ render_field(form.endpoint_sip.transport.uuid,
                              data_listing_href=url_for(listing_urls['transport']),
                              data_allow_clear='true',
                              data_placeholder=_('Choose Transport')) }}
              {{ render_field(form.endpoint_sip.template_uuids,
                              data_listing_href=url_for(listing_urls['sip_template']),
                              data_allow_clear='true',
                              data_placeholder=_('Choose Templates')) }}
              {{ render_field(form.endpoint_sip.templates, with_label=False) }}
              {{ render_field(form.context,
                              data_allow_clear='true',
                              class_='line-context',
                              placeholder=_('Choose Context'),
                              data_listing_href=url_for(listing_urls['context'])) }}
            {% endcall %}
          {% endcall %}

          {{ _generate_section('aor_section_options', form.endpoint_sip.aor_section_options, 'aor') }}
          {{ _generate_section('auth_section_options', form.endpoint_sip.auth_section_options, 'auth') }}
          {{ _generate_section('endpoint_section_options', form.endpoint_sip.endpoint_section_options, 'endpoint') }}
          {{ _generate_section('identify_section_options', form.endpoint_sip.identify_section_options) }}
          {{ _generate_section('registration_section_options', form.endpoint_sip.registration_section_options) }}
          {{ _generate_section('registration_outbound_auth_section_options', form.endpoint_sip.registration_outbound_auth_section_options, 'auth') }}
          {{ _generate_section('outbound_auth_section_options', form.endpoint_sip.outbound_auth_section_options, 'auth') }}

        {% endcall %}
      {% endcall %}
    {% endcall %}

  {% endcall %}
{% endblock %}

{% macro _generate_section(content_item_name, form_options, section=None) %}
  {% call build_tab_content_item(content_item_name) %}
    {% call add_default_fields(form=form, submit_value=_('Add')) %}

      {{ build_add_row_entry_header() }}
      {% call build_table() %}

        {% call build_table_headers() %}
          <th>{{  _('Option Key') }}</th>
          <th>{{  _('Option Value') }}</th>
          <th style="width: 21px"></th>
        {% endcall %}

        {% call build_table_body(class_='dynamic-table') %}
          {% do form_options.append_entry() %}
          {{ _build_option_entry(form_options.pop_entry(), section, template=True) }}

          {% for option in form_options %}
            {{ _build_option_entry(option, section) }}
          {% endfor %}
        {% endcall %}

      {% endcall %}
      {{ build_add_row_entry_header() }}

    {% endcall %}
  {% endcall %}
{% endmacro %}

{% macro _build_option_entry(option, section=None, template=False) %}
  {% if template %}
    {% set tr_class = "row-template hidden" %}
  {% else %}
    {% set tr_class = "row-line" %}
  {% endif %}
  <tr class="{{ tr_class }}">
  {% if section %}
    <td>{{ render_field(
      option.option_key,
      with_label=False,
      inputclass='ui-helper-clearfix',
      data_listing_href=url_for(listing_urls['pjsip_doc'], section=section),
    ) }}</td>
  {% else %}
    <td>{{ render_field(option.option_key, with_label=False, inputclass='ui-helper-clearfix') }}</td>
  {% endif %}
    <td>{{ render_field(option.option_value, with_label=False) }}</td>
    <td>{{ add_delete_entry_button() }}</td>
  </tr>
{% endmacro %}
