{# user_avater 已废弃，请勿使用 #}
{%- macro user_avatar(user, class, imgClass, card = true) -%}
  {% if user %}
    <a class="{{ class }} {% if card %}js-user-card{% endif %}" href="{{ path('user_show', {id:user.id}) }}" data-card-url="{{ path('user_card_show', {userId:user.id}) }}" data-user-id="{{ user.id }}">
      {% if imgClass == 'avatar-md' %}
        {% set avatarType = 'medium' %}
      {% elseif imgClass == 'avatar-lg' %}
        {% set avatarType = 'large' %}
      {% else %}
        {% set avatarType = 'small' %}
      {% endif %}
    <img class="{{ imgClass|default('avatar-sm') }}" src="{{ avatar_path(user, avatarType) }}">

  </a>
  {% else %}
    <a class="{{ class }}" href="javascript:;">
      <img class="{{ imgClass|default('avatar-sm') }}"  src="{{ avatar_path('', 'small') }}">
    </a>
  {% endif %}
{%- endmacro -%}

{# options 可以传card(显示卡片), _blank(新开窗口), null_link(空链接) #}
{%- macro user_avatar_link(user, size, options = []) -%}
  {% set size = size|default('small') %}
  {% if user %}
    <a
      class="avatar-link-{{ size }} {% if 'card' in options %}js-user-card{% endif %}"
      {% if '_blank' in options %}
        target="_blank"
      {% endif %}
      href="{% if 'null_link' in options %}javascript:;{% else %}{{ path('user_show', {id:user.id}) }}{% endif %}"
      data-card-url="{{ path('user_card_show', {userId:user.id}) }}"
      data-user-id="{{ user.id }}">
      {{ _self.user_avater_img(user, size) }}
    </a>
  {% else %}
    <a class="avatar-link-{{ size }}" href="javascript:;">
      {{ _self.user_avater_img(user, size) }}
    </a>
  {% endif %}
{%- endmacro -%}

{%- macro user_avater_img(user, size) -%}
  <img class="avatar-{{ size }}"  src="{{ avatar_path(user, size) }}">
{%- endmacro -%}


{# {%- macro user_link(user, class, options = []) -%}
  {% if user %}
    <a
      class="{{ class }} {% if 'card' in options %}js-user-card{% endif %}" {% if '_blank' in options %} target="_blank" {% endif %}href="{{ path('user_show', {id:user.id}) }}">{{ user.nickname }}
    </a>
  {% else %}
    <a class="{{ class }}" href="javascript:;"><del>用户不存在</del></a>
  {% endif %}
{%- endmacro -%} #}

{%- macro user_link(user, class, card = true) -%}
  {% if user %}
    <a class="link-dark {{ class }}" href="{{ path('user_show', {id:user.id}) }}" target="_blank">{{ user.nickname }}</a>
  {% else %}
    <a class="link-dark {{ class }}" href="javascript:;"><del>{{'user.not_exist'|trans}}</del></a>
  {% endif %}
{%- endmacro -%}

{% macro flash_messages(isUseNew = false) %}
  {% for type, flashMessages in app.session.flashbag.all() %}
    {% if 'currentThrowedException' != type %}
      {% for flashMessage in flashMessages %}
        {% if isUseNew %}
          <div class="alert cd-alert cd-alert-{{ type }}">{{ flashMessage|trans|raw }}</div>
        {% else %}
          <div class="alert alert-{{ type }}">{{ flashMessage|trans|raw }}</div>
        {% endif %}
      {% endfor %}
    {% endif %}
  {% endfor %}
{% endmacro %}

{% macro bytesToSize(bytes) %}
  {% spaceless %}

      {% set kilobyte = 1024 %}
      {% set megabyte = kilobyte * 1024 %}
      {% set gigabyte = megabyte * 1024 %}
      {% set terabyte = gigabyte * 1024 %}

      {% if bytes < kilobyte %}
          {{ bytes ~ ' B' }}
      {% elseif bytes < megabyte %}
          {{ (bytes / kilobyte)|number_format(2, '.') ~ ' KB' }}
      {% elseif bytes < gigabyte %}
          {{ (bytes / megabyte)|number_format(2, '.') ~ ' MB' }}
      {% elseif bytes < terabyte %}
          {{ (bytes / gigabyte)|number_format(2, '.') ~ ' GB' }}
      {% else %}
          {{ (bytes / terabyte)|number_format(2, '.') ~ ' TB' }}
      {% endif %}

  {% endspaceless %}
{% endmacro %}

{% macro paginator(paginator, class) %}
  {% if paginator|default() and paginator.lastPage > 1 %}
    <nav class=" {{ class|default('text-center') }}">
      <ul class="pagination cd-pagination">
        {% if paginator.currentPage != paginator.firstPage %}
          <li><a  href="{{ paginator.getPageUrl(paginator.firstPage) }}"><i class="cd-icon cd-icon-first-page"></i></a></li>
          <li><a  href="{{ paginator.getPageUrl(paginator.previousPage) }}"><i class="cd-icon cd-icon-arrow-left"></i></a></li>
        {% endif %}
        {% for page in paginator.pages %}
          <li {% if page == paginator.currentPage %}class="active"{% endif %}><a href="{{ paginator.getPageUrl(page) }}">{{ page }}</a></li>
        {% endfor %}

        {% if paginator.currentPage != paginator.lastPage %}
          <li><a  href="{{ paginator.getPageUrl(paginator.nextPage) }}"><i class="cd-icon cd-icon-arrow-right"></i></a></li>
          <li><a  href="{{ paginator.getPageUrl(paginator.getLastPage()) }}"><i class="cd-icon cd-icon-last-page"></i></a></li>
        {% endif %}
      </ul>
    </nav>
  {% endif %}
{% endmacro %}

{% macro star(score) %}
  {% set floorScore = score|round(0, 'floor') %}
  {% set emptyNum = 5 - floorScore %}

  {% if floorScore > 0 %}
    {% for i in range(1, floorScore) %}
      <i class="es-icon es-icon-star color-warning"></i>
    {% endfor %}
  {% endif %}

  {% if (score - floorScore) >= 0.5 %}
    {% set emptyNum = emptyNum - 1 %}
    <i class="es-icon es-icon-starhalf color-warning"></i>
  {% endif %}

  {% if emptyNum > 0 %}
    {% for i in range(1, emptyNum) %}
      <i class="es-icon es-icon-staroutline"></i>
    {% endfor %}
  {% endif %}
{% endmacro %}

{% macro ajax_paginator(paginator, class)  %}
  <input class="js-page" type="hidden" name="page" value="{{ paginator.currentPage|default(1) }}">

  {% if paginator.lastPage > 1 %}
    <nav class="{{ class|default('text-center') }}">
      <ul class="pagination cd-pagination">
        {% if paginator.currentPage != paginator.firstPage %}
          <li data-url="{{ paginator.getPageUrl(paginator.firstPage) }}" data-page="{{paginator.firstPage}}"><a href="javascript:;"><i class="cd-icon cd-icon-first-page"></i></a></li>
          <li data-url="{{ paginator.getPageUrl(paginator.previousPage) }}" data-page="{{paginator.previousPage}}"><a  href="javascript:;"><i class="cd-icon cd-icon-arrow-left"></i></a></li>
        {% endif %}
        {% for page in paginator.pages %}
          <li {% if page == paginator.currentPage %}class="active"{% endif %} data-url="{{ paginator.getPageUrl(page) }}" data-page="{{page}}"><a href="javascript:;">{{ page }}</a></li>
        {% endfor %}

        {% if paginator.currentPage != paginator.lastPage %}
          <li data-url="{{ paginator.getPageUrl(paginator.nextPage) }}" data-page="{{paginator.nextPage}}"><a  href="javascript:;"><i class="cd-icon cd-icon-arrow-right"></i></a></li>
          <li data-url="{{ paginator.getPageUrl(paginator.getLastPage) }}" data-page="{{paginator.getLastPage}}"><a  href="javascript:;"><i class="cd-icon cd-icon-last-page"></i></a></li>
        {% endif %}
      </ul>
    </nav>
  {% endif %}
{% endmacro %}

{% macro link(name, url, title = '', target = '_blnak') %}
  <a href="{{ url }}" title= "{{ title }}" target="{{ target }}"><strong>{{ name }}</strong></a>
{% endmacro %}

{% macro table_filter(header, choices, key, dataTarget, selectedChoice, default = '') %}
  <div class="cd-dropdown cd-dropdown-filter" data-toggle="cd-dropdown">
    {{ header }}
    <a href="javascript:;" class="{% if selectedChoice == '' %}cd-link-assist{% else %}cd-link-primary{% endif %}">
      <i class="cd-icon cd-icon-filter cd-text-sm"></i>
    </a>
    <ul class="dropdown-menu">
      {% if default %}
        <li class="{% if selectedChoice == '' %}active{% endif %}"><a href="javascript:;" data-toggle="cd-table-filter" data-target="{{ dataTarget }}" data-filter-key="{{ key }}" data-filter-value="">{{ default }}</a></li>
      {% endif %}
      {% for choiceKey,choice in choices %}
        <li class="{% if selectedChoice == choiceKey %}active{% endif %}"><a href="javascript:;" data-toggle="cd-table-filter" data-target="{{ dataTarget }}" data-filter-key="{{ key }}"  data-filter-value="{{ choiceKey }}">{{ choice }}</a></li>
      {% endfor %}
    </ul>
  </div>
{% endmacro %}

{% macro cd_select(options, checkedOption, name, className = '', id = '') %}
  <div class="cd-select {{ className }}" id="{{ id }}">
    <input type="hidden" value="{{ checkedOption }}" name="{{name}}" />
    <div class="select-value">{{ options[checkedOption]|default('') }}</div>
    <ul class="select-options">
      {% for optionKey, option in options %}
        <li class="{% if optionKey == checkedOption %}checked{% endif %}" data-value="{{optionKey}}">{{ option }}</li>
      {% endfor %}
    </ul>
  </div>
{% endmacro %}