{% macro menu_icon(item) -%}
{% set icon_type = item.get_icon_type() %}
{%- if icon_type %}
  {% set icon_value = item.get_icon_value() %}
  {% if icon_type == 'glyph' %}
    <i class="{{ icon_value }}"></i>
  {% elif icon_type == 'fa' %}
    <i class="fa fa-{{ icon_value }}"></i>
  {% elif icon_type == 'image' %}
    <img src="{{ url_for('static', filename=icon_value) }}" alt="menu image">
  {% elif icon_type == 'image-url' %}
    <img src="item.icon_value" alt="menu image">
  {% endif %}
{% endif %}
{%- endmacro %}

{% macro menu(menu_root=None) %}
  {% if menu_root is none %}{% set menu_root = admin_view.admin.menu() %}{% endif %}
  {%- for item in menu_root %}
    {%- if item.is_category() -%}
      {% set children = item.get_children() %}
      {%- if children %}
        {% set class_name = item.get_class_name() or '' %}
        {%- if item.is_active(admin_view) %}
        <li class="active dropdown{% if class_name %} {{class_name}}{% endif %}">
        {% else -%}
        <li class="dropdown{% if class_name %} {{class_name}}{% endif %}">
        {%- endif %}
          <a class="dropdown-toggle" data-toggle="dropdown" href="javascript:void(0)">
            {% if item.class_name %}<i class="{{ item.class_name }}"></i> {% endif %}
            {{ menu_icon(item) }}{{ item.name }}
            {%- if 'dropdown-submenu' not in class_name -%}<b class="caret"></b>{%- endif -%}
          </a>
          <ul class="dropdown-menu">
          {%- for child in children -%}
            {%- if child.is_category() -%}
              {{ menu(menu_root=[child]) }}
            {% else %}
              {% set class_name = child.get_class_name() %}
              {%- if child.is_active(admin_view) %}
              <li class="active{% if class_name %} {{class_name}}{% endif %}">
              {% else %}
              <li{% if class_name %} class="{{class_name}}"{% endif %}>
              {%- endif %}
              <a href="{{ child.get_url() }}"{% if child.target %}
               target="{{ child.target }}"{% endif %}>
              {{ menu_icon(child) }}{{ child.name }}</a>
              </li>
            {%- endif %}
          {%- endfor %}
          </ul>
        </li>
      {% endif %}
    {%- else %}
      {%- if item.is_accessible() and item.is_visible() -%}
        {% set class_name = item.get_class_name() %}
        {%- if item.is_active(admin_view) %}
        <li class="active{% if class_name %} {{class_name}}{% endif %}">
        {%- else %}
        <li{% if class_name %} class="{{class_name}}"{% endif %}>
        {%- endif %}
          <a href="{{ item.get_url() }}"{% if item.target %} target="{{ item.target }}"{% endif %}>{{ menu_icon(item) }}{{ item.name }}</a>
        </li>
      {%- endif -%}
    {% endif -%}
  {% endfor %}
{% endmacro %}

{% macro menu_links(links=None) %}
  {% if links is none %}{% set links = admin_view.admin.menu_links() %}{% endif %}
  {% for item in links %}
    {% set class_name = item.get_class_name() %}
    {% if item.is_accessible() and item.is_visible() %}
      <li{% if class_name %} class="{{ class_name }}"{% endif %}>
        <a href="{{ item.get_url() }}">{{ menu_icon(item) }}{{ item.name }}</a>
      </li>
    {% endif %}
  {% endfor %}
{% endmacro %}

{% macro messages() %}
  {% with messages = get_flashed_messages(with_categories=True) %}
    {% if messages %}
      {% for category, m in messages %}
        {% if category %}
        <div class="alert alert-{{ category }}">
        {% else %}
        <div class="alert">
        {% endif %}
          <a href="javascript:void(0)" class="close" data-dismiss="alert">x</a>
          {{ m }}
        </div>
      {% endfor %}
    {% endif %}
  {% endwith %}
{% endmacro %}
