{% extends "layout.html" %}

{% block content_header %}
  {{ build_breadcrumbs(current_breadcrumbs + [
    { 'name': resource.name, 'link': url_for('.GroupView:get', id=resource.uuid), 'icon': 'users' }
  ]) }}
{% endblock %}

{% block content %}
  {% call build_section_row() %}
    {% call build_form_tabs_box() %}
        {% call build_tabs_navigation() %}
          {{ add_tab_navigation_header(_('Group'), resource.label, 'users') }}
          {{ add_tab_navigation_item('general', _('General'), active=True) }}
          {{ add_tab_navigation_item('members', _('Members')) }}
          {{ add_tab_navigation_item('fallbacks', _('Fallbacks')) }}
          {{ add_tab_navigation_item('schedule', _('Schedule')) }}
          {{ add_tab_navigation_item('callpermissions', _('Call Permissions')) }}
        {% endcall %}

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

          {% call build_tab_content_item('general', active=True) %}
            {% call add_default_fields(form=form, submit_value=_('Update')) %}
              {{ render_field(form.label) }}
              {{ render_field(form.name) }}
              {{ render_field(form.extensions[0].context,
                              label='Exten',
                              inputclass='ui-helper-clearfix',
                              divclass='col-sm-4',
                              class_='group-context',
                              placeholder='Choose Context',
                              data_listing_href=url_for(listing_urls['context_by_type'], type_='internal')) }}
              {{ render_field(form.extensions[0].exten,
                              data_allow_custom_values=True,
                              data_allow_clear='true',
                              divclass='col-sm-2',
                              with_label=False,
                              class_='group-exten',
                              data_listing_href=url_for(listing_urls['available_extension_group'])) }}
              {{ render_field(form.caller_id_mode) }}
              {{ render_field(form.caller_id_name) }}
              {{ render_field(form.enabled) }}
              {{ render_field(form.music_on_hold,
                              data_listing_href=url_for(listing_urls['moh']),
                              data_allow_clear='true',
                              data_placeholder='Choose Music on Hold') }}
              {{ render_field(form.preprocess_subroutine) }}
              {{ render_field(form.retry_delay) }}
              {{ render_field(form.ring_in_use) }}
              {{ render_field(form.ring_strategy) }}
              {{ render_field(form.timeout) }}
              {{ render_field(form.user_timeout) }}
              {{ render_field(form.mark_answered_elsewhere) }}
            {% endcall %}
          {% endcall %}

          {% call build_tab_content_item('members') %}
            {% call add_default_fields(form=form, submit_value=_('Update')) %}
	      <div class=col-xs-6>
		<div class=box-header></div>
                {{ render_field(form.members.user_uuids, data_listing_href=url_for(listing_urls['uuid_user'])) }}
                {{ render_field(form.members.users, with_label=False) }}
              </div>
	      <div class=col-xs-6>
                {{ build_add_row_entry_header() }}
                {% call build_table() %}

                  {% call build_table_headers() %}
                    <th>{{ _('Extension') }}</th>
                    <th>{{ _('Context') }}</th>
                    <th style="width: 21px;"></th>
                  {% endcall %}

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

                    {% for extension in form.extensions_members %}
                      {{ _build_option_entry(extension) }}
                    {% endfor %}
                  {% endcall %}

                {% endcall %}
              </div>
            {% endcall %}
          {% endcall %}

          {% call build_tab_content_item('fallbacks') %}
            {% call add_default_fields(form=form, submit_value=_('Update')) %}
              {{ render_field(form.fallbacks.noanswer_destination) }}
            {% endcall %}
          {% endcall %}

          {% call build_tab_content_item('schedule') %}
            {% call add_default_fields(form=form, submit_value=_('Update')) %}
                {{ render_field(form.schedules[0]['name']) }}
                {{ render_field(form.schedules[0]['id'],
                                data_listing_href=url_for(listing_urls['schedule']),
                                data_allow_clear='true',
                                data_placeholder='Choose Schedules') }}
            {% endcall %}
          {% endcall %}

          {% call build_tab_content_item('callpermissions') %}
            {% call add_default_fields(form=form, submit_value=_('Update')) %}
              {{ render_field(form.call_permission_ids,
                              data_listing_href=url_for(listing_urls['callpermission']),
                              data_allow_clear='true',
                              data_placeholder=_('Choose Call Permissions')) }}
              {{ render_field(form.call_permissions, with_label=False) }}
            {% endcall %}
          {% endcall %}

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

{% macro _build_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.exten, with_label=False) }}</td>
    <td>{{ render_field(option.context, with_label=False, disabled=True) }}</td>
    <td>{{ render_field(option.priority, with_label=False) }}</td>
    <td>{{ add_delete_entry_button() }}</td>
  </tr>
{% endmacro %}

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