{% extends "layout.html" %}

{% block content_header %}
  {{ build_breadcrumbs([
    { 'name': _('Call Permissions'), 'link': url_for('.CallPermissionView:index'), 'icon': 'ban' }
  ]) }}
{% endblock %}

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

      {% call build_tabs_navigation() %}
        {{ add_tab_navigation_header(_('Call Permissions'), '', 'ban') }}
        {{ add_tab_navigation_item('general', _('General'), active=True) }}
        {{ add_tab_navigation_item('extensions', _('Extensions')) }}
        {{ add_tab_navigation_item('users', _('Users')) }}
        {{ add_tab_navigation_item('groups', _('Groups')) }}
        {{ add_tab_navigation_item('outcalls', _('Outcalls')) }}
      {% endcall %}

      {% call build_form(action=url_for('.CallPermissionView:post')) %}
        {% call build_tabs_content() %}

          {% call build_tab_content_item('general', active=True) %}
            {% call add_default_fields(form=form, submit_value=_('Add')) %}
              {{ render_field(form.name) }}
              {{ render_field(form.password, autocomplete="off") }}
              {{ render_field(form.mode) }}
              {{ render_field(form.description) }}
            {% endcall %}
          {% endcall %}

          {% call build_tab_content_item('extensions') %}
            {% call add_default_fields(form=form, submit_value=_('Add')) %}

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

                  {% call build_table_headers() %}
                    <th>{{  _('Extensions') }}</th>
                    <th style="width: 21px"></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 %}

          {% call build_tab_content_item('users') %}
            {% call add_default_fields(form=form, submit_value=_('Add')) %}
              {{ render_field(form.user_uuids, data_listing_href=url_for(listing_urls['uuid_user'])) }}
            {% endcall %}
          {% endcall %}

          {% call build_tab_content_item('groups') %}
            {% call add_default_fields(form=form, submit_value=_('Add')) %}
              {{ render_field(form.group_ids, data_listing_href=url_for(listing_urls['group'])) }}
            {% endcall %}
          {% endcall %}

          {% call build_tab_content_item('outcalls') %}
            {% call add_default_fields(form=form, submit_value=_('Add')) %}
              {{ render_field(form.outcall_ids, data_listing_href=url_for(listing_urls['outcall'])) }}
            {% endcall %}
          {% endcall %}

        {% endcall %}

      {% endcall %}
    {% endcall %}
  {% endcall %}
{% 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.exten, with_label=False) }}</td>
    <td>{{ add_delete_entry_button() }}</td>
  </tr>
{% endmacro %}
