{% extends "layout.html" %}

{% block content_header %}
  {% if form.protocol.data == 'sip' %}
    {{ build_breadcrumbs(current_breadcrumbs + [
      { 'name': resource.endpoint_sip['label'], 'link': url_for('.LineView:get', id=resource.id), 'icon': 'exchange' }
    ]) }}
  {% elif form.protocol.data == 'sccp' %}
    {{ build_breadcrumbs(current_breadcrumbs + [
      { 'name': resource.name, 'link': url_for('.LineView:get', id=resource.id), 'icon': 'exchange' }
    ]) }}
  {% endif %}
{% endblock %}

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

    {% if form.protocol.data == 'sip' %}

      {% call build_form_tabs_box() %}
        {% call build_tabs_navigation() %}
          {{ add_tab_navigation_header(_('Line'), form.endpoint_sip.label.data, '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:put', id=resource.id)) %}
          {% call build_tabs_content() %}

            {% call build_tab_content_item('line', active=True) %}
              {% call add_default_fields(form=form, submit_value=_('Update')) %}
                {{ render_field(form.endpoint_sip.uuid) }}
                {{ render_field(form.endpoint_sip['label']) }}
                {{ render_field(form.endpoint_sip['name']) }}
                {{ 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 %}

    {% elif form.protocol.data == 'sccp' %}

      {% call build_form_box(_('Line'), form.endpoint_sccp['id'], 'server') %}
        {% call build_form(action=url_for('.LineView:put', id=resource.id)) %}
          {% call add_default_fields(form=form, submit_value=_('Update')) %}
            {{ render_field(form.endpoint_sccp['id']) }}
            {{ render_field(form.context,
                            data_allow_clear='true',
                            class_='line-context',
                            placeholder=_('Choose Context'),
                            data_listing_href=url_for(listing_urls['context'])) }}
            {{ 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.endpoint_sccp.options.append_entry() %}
                {{ _build_sccp_option_entry(form.endpoint_sccp.options.pop_entry(), template=True) }}

                {% for option in form.endpoint_sccp.options %}
                  {{ _build_sccp_option_entry(option) }}
                {% endfor %}
              {% endcall %}

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

    {% elif form.protocol.data == 'custom' %}

      {% call build_form_box(_('Line'), form.endpoint_custom.interface.data, 'server') %}
        {% call build_form(action=url_for('.LineView:put', id=resource.id)) %}
          {% call add_default_fields(form=form, submit_value=_('Update')) %}
            {{ render_field(form.endpoint_custom['id']) }}
            {{ render_field(form.endpoint_custom['interface']) }}
            {{ render_field(form.endpoint_custom['interface_suffix']) }}
            {{ render_field(form.context,
                            data_allow_clear='true',
                            class_='line-context',
                            placeholder=_('Choose Context'),
                            data_listing_href=url_for(listing_urls['context'])) }}
          {% endcall %}
        {% endcall %}
      {% endcall %}

    {% endif %}

  {% 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=_('Update')) %}

      {{ 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_pjsip_option_entry(form_options.pop_entry(), section, template=True) }}

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

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

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

{% macro _build_pjsip_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 %}

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