{% extends "layout.html" %}

{% block content_header %}
  {{ build_breadcrumb(_('Voicemail'), '', 'envelope', [_('Edit')]) }}
{% endblock %}

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

      {% call build_form_tabs_box() %}
        {% call build_tabs_navigation() %}
          {{ add_tab_navigation_item('general', _('General'), active=True) }}
          {{ add_tab_navigation_item('timezones', _('Timezones')) }}
        {% endcall %}

        {% call build_form(action=url_for('.VoicemailGeneralSettingsView:put')) %}
          {% call build_tabs_content() %}

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

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

                {% endcall %}

              {% endcall %}
            {% endcall %}

            {% call build_tab_content_item('timezones') %}
              {% call add_default_fields(form=form, submit_value=_('Update')) %}

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

                  {% call build_table_headers() %}
                    <th>{{  _('Name') }}</th>
                    <th>{{  _('Timezone') }}</th>
                    <th>{{  _('Message') }}</th>
                    <th style="width: 21px"></th>
                  {% endcall %}

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

                    {% for zonemessage in form.zonemessages %}
                      {{ _build_zonemessages_entry(zonemessage) }}
                    {% endfor %}
                  {% endcall %}

                {% endcall %}

              {% 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.option_key, with_label=False) }}</td>
    <td>{{ render_field(option.option_value, with_label=False) }}</td>
    <td>{{ add_delete_entry_button() }}</td>
  </tr>
{% endmacro %}


{% macro _build_zonemessages_entry(zonemessage, template=False) %}
  {% if template %}
    {% set tr_class = "row-template hidden" %}
  {% else %}
    {% set tr_class = "row-line" %}
  {% endif %}
  <tr class="{{ tr_class }}">
    <td>{{ render_field(zonemessage['name'], with_label=False) }}</td>
    <td>{{ render_field(zonemessage.timezone,
                        with_label=False,
                        data_listing_href=url_for(listing_urls['timezone']),
                        data_placeholder=_('Choose Timezone')) }}</td>
    <td>{{ render_field(zonemessage.message, with_label=False) }}</td>
    <td>{{ add_delete_entry_button() }}</td>
  </tr>
{% endmacro %}
