{% extends "layout.html" %}

{% block content_header %}
  {{ build_breadcrumbs(current_breadcrumbs + [
    { 'name': resource.endpoint_sip['label'], 'link': url_for('.TrunkView:get', id=resource.id), 'icon': 'server' }
  ]) }}
{% 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(_('Trunk'), form.endpoint_sip.label.data, 'server') }}
          {{ add_tab_navigation_item('trunk', _('Trunk'), 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('.TrunkView:put', id=resource.id)) %}
          {% call build_tabs_content() %}

            {% call build_tab_content_item('trunk', 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 %}

            {% call build_tab_content_item('aor_section_options') %}
              {% 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.endpoint_sip.aor_section_options.append_entry() %}
                    {{ _build_option_entry(form.endpoint_sip.aor_section_options.pop_entry(), 'aor', template=True) }}

                    {% for option in form.endpoint_sip.aor_section_options %}
                      {{ _build_option_entry(option, 'aor') }}
                    {% endfor %}
                  {% endcall %}

                {% endcall %}

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

            {% call build_tab_content_item('auth_section_options') %}
              {% 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.endpoint_sip.auth_section_options.append_entry() %}
                    {{ _build_option_entry(form.endpoint_sip.auth_section_options.pop_entry(), 'auth', template=True) }}

                    {% for option in form.endpoint_sip.auth_section_options %}
                      {{ _build_option_entry(option, 'auth') }}
                    {% endfor %}
                  {% endcall %}

                {% endcall %}

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

            {% call build_tab_content_item('endpoint_section_options') %}
              {% 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.endpoint_sip.endpoint_section_options.append_entry() %}
                    {{ _build_option_entry(form.endpoint_sip.endpoint_section_options.pop_entry(), 'endpoint', template=True) }}

                    {% for option in form.endpoint_sip.endpoint_section_options %}
                      {{ _build_option_entry(option, 'endpoint') }}
                    {% endfor %}
                  {% endcall %}

                {% endcall %}

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

            {% call build_tab_content_item('identify_section_options') %}
              {% 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.endpoint_sip.identify_section_options.append_entry() %}
                    {{ _build_option_entry(form.endpoint_sip.identify_section_options.pop_entry(), template=True) }}

                    {% for option in form.endpoint_sip.identify_section_options %}
                      {{ _build_option_entry(option) }}
                    {% endfor %}
                  {% endcall %}

                {% endcall %}

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

            {% call build_tab_content_item('registration_section_options') %}
              {% 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.endpoint_sip.registration_section_options.append_entry() %}
                    {{ _build_option_entry(form.endpoint_sip.registration_section_options.pop_entry(), template=True) }}

                    {% for option in form.endpoint_sip.registration_section_options %}
                      {{ _build_option_entry(option) }}
                    {% endfor %}
                  {% endcall %}

                {% endcall %}

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

            {% call build_tab_content_item('registration_outbound_auth_section_options') %}
              {% 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.endpoint_sip.registration_outbound_auth_section_options.append_entry() %}
                    {{ _build_option_entry(form.endpoint_sip.registration_outbound_auth_section_options.pop_entry(), 'auth', template=True) }}

                    {% for option in form.endpoint_sip.registration_outbound_auth_section_options %}
                      {{ _build_option_entry(option, 'auth') }}
                    {% endfor %}
                  {% endcall %}

                {% endcall %}

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

            {% call build_tab_content_item('outbound_auth_section_options') %}
              {% 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.endpoint_sip.outbound_auth_section_options.append_entry() %}
                    {{ _build_option_entry(form.endpoint_sip.outbound_auth_section_options.pop_entry(), 'auth', template=True) }}

                    {% for option in form.endpoint_sip.outbound_auth_section_options %}
                      {{ _build_option_entry(option, 'auth') }}
                    {% endfor %}
                  {% endcall %}

                {% endcall %}

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

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

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

      {% call build_form_tabs_box() %}
        {% call build_tabs_navigation() %}
          {{ add_tab_navigation_header(_('Trunk'), form.endpoint_iax.name.data, 'server') }}
          {{ add_tab_navigation_item('trunk', _('Trunk'), active=True) }}
          {{ add_tab_navigation_item('register', _('Register')) }}
          {{ add_tab_navigation_item('options', _('Options')) }}
        {% endcall %}

        {% call build_form(action=url_for('.TrunkView:put', id=resource.id)) %}
          {% call build_tabs_content() %}

            {% call build_tab_content_item('trunk', active=True) %}
              {% call add_default_fields(form=form, submit_value=_('Update')) %}
                {{ render_field(form.endpoint_iax['id']) }}
                {{ render_field(form.endpoint_iax['name']) }}
                {{ render_field(form.endpoint_iax['host']) }}
                {{ render_field(form.endpoint_iax['host_value'], placeholder=_('Set host value')) }}
                {{ render_field(form.endpoint_iax['type']) }}
                {{ render_field(form.context,
                                data_allow_clear='true',
                                class_='trunk-context',
                                placeholder=_('Choose Context'),
                                data_listing_href=url_for(listing_urls['context'])) }}
              {% endcall %}
            {% endcall %}

            {% call build_tab_content_item('register') %}
             {% call add_default_fields(form=form, submit_value=_('Update')) %}
               {{ render_field(form.register_iax['id']) }}
               {{ render_field(form.register_iax['enabled']) }}
               {{ render_field(form.register_iax['auth_username']) }}
               {{ render_field(form.register_iax['auth_password']) }}
               {{ render_field(form.register_iax['callback_context']) }}
               {{ render_field(form.register_iax['callback_extension']) }}
               {{ render_field(form.register_iax['remote_host']) }}
               {{ render_field(form.register_iax['remote_port']) }}
             {% endcall %}
            {% endcall %}

            {% call build_tab_content_item('options') %}
              {% 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.endpoint_iax.options.append_entry() %}
                    {{ _build_option_entry(form.endpoint_iax.options.pop_entry(), template=True) }}

                    {% for option in form.endpoint_iax.options %}
                      {{ _build_option_entry(option) }}
                    {% endfor %}
                  {% endcall %}

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

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

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

      {% call build_form_box(_('Trunk'), form.endpoint_custom.interface.data, 'server') %}
        {% call build_form(action=url_for('.TrunkView: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_='trunk-context',
                            placeholder=_('Choose Context'),
                            data_listing_href=url_for(listing_urls['context'])) }}
          {% endcall %}
        {% endcall %}
      {% endcall %}

    {% endif %}

  {% endcall %}
{% endblock %}


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


{% block additional_js %}
  <script src="{{ url_for('.static', filename='js/trunk.js') }}"></script>
{% endblock %}
