{% macro facts_table(facts, current_env, autofocus=False, condensed=False, show_node=False, show_value=True, link_facts=False, margin_top=20, margin_bottom=20) -%}
<div class="ui fluid icon input hide" style="margin-bottom:20px">
  <input {% if autofocus %} autofocus="autofocus" {% endif %} class="filter-table" placeholder="Type here to filter...">
</div>
<table class="ui very basic {% if condensed %}very{% endif%} compact sortable table" style="table-layout: fixed;">
  <thead>
    <tr>
      {% if show_node %}
      <th>Node</th>
      {% else %}
      <th class="default-sort">Fact</th>
      {% endif %}
      {% if show_value %}
      <th>Value</th>
      {% endif %}
    </tr>
  </thead>
  <tbody class="searchable">
    {% for fact in facts %}
    <tr>
      {% if show_node %}
        <td><a href="{{url_for('node', env=current_env, node_name=fact.node)}}">{{fact.node}}</a></td>
      {% else %}
        <td><a href="{{url_for('fact', env=current_env, fact=fact.name)}}">{{fact.name}}</a></td>
      {% endif %}
      {% if show_value %}
      <td style="word-wrap:break-word">
        {% if link_facts %}
          {% if fact.value is mapping %}
            <a href="{{url_for('fact_value', env=current_env, fact=fact.name, value=fact.value)}}"><pre>{{fact.value|jsonprint}}</pre></a>
          {% else %}
            <a href="{{url_for('fact_value', env=current_env, fact=fact.name, value=fact.value)}}">{{fact.value}}</a>
          {% endif %}
        {% else %}
          {% if fact.value is mapping %}
            <pre>{{fact.value|jsonprint}}</pre>
          {% else %}
            {{fact.value}}
          {% endif %}
        {% endif %}
      </td>
      {% endif %}
    </tr>
    {% endfor %}
  </tbody>
</table>
{%- endmacro %}

{% macro status_counts(caller, status, node_name, events, current_env, unreported_time=False, report_hash=False) -%}
  <a class="ui {{status}} label status" href="{{url_for('report', env=current_env, node_name=node_name, report_id=report_hash)}}">{{ status|upper }}</a>
  {% if status == 'unreported' %}
    <span class="ui label status"> {{ unreported_time|upper }} </span>
  {% else %}
    {% if events['failures'] %}<span class="ui small count label failed">{{events['failures']}}</span>{% else %}<span class="ui small count label">0</span>{% endif%}
    {% if events['successes'] %}<span class="ui small count label changed">{{events['successes']}}</span>{% else %}<span class="ui small count label">0</span>{% endif%}
    {% if events['skips'] %}<span class="ui small count label skipped">{{events['skips']}}</span>{% else %}<span class="ui small count label">0</span>{% endif%}
  {% endif %}
{%- endmacro %}

{% macro datatable_init(table_html_id, ajax_url, default_length, length_selector, extra_options=None) -%}
  // Init datatable
  $.fn.dataTable.ext.errMode = 'throw';
  var table = $('#{{ table_html_id }}').DataTable({
    // Permit flow auto-readjust (responsive)
    "autoWidth": false,
    // Activate "processing" message
    "processing": true,
    // Activate Ajax mode
    "serverSide": true,
    // Responsive
    "responsive": true,
    // Defer rendering out of screen lines (JIT)
    "deferRender": true,
    // Data loading URL
    "ajax": "{{ ajax_url }}",
    // Paging options
    "lengthMenu": {{ length_selector }},
    "pageLength": {{ default_length }},
    // Default sort
    "order": [[ 0, "desc" ]],
    // Custom options
    {% if extra_options %}{% call extra_options() %}Callback to parent defined options{% endcall %}{% endif %}
  });

  table.on('draw.dt', function(){
    $('#{{ table_html_id }} [rel=utctimestamp]').each(
      function(index, timestamp){
        $(this).localise_timestamp();
    });
  });

  // Override Datatables search box events to delay Ajax call while writing
  var searchWait = 0;
  var searchWaitInterval;
  $('.dataTables_filter input')
  .unbind()
  .bind('input', function(e){
    var item = $(this);
    searchWait = 0;
    if(!searchWaitInterval) searchWaitInterval = setInterval(function(){
      if(searchWait>=3){
        clearInterval(searchWaitInterval);
        searchWaitInterval = '';
        searchTerm = $(item).val();
        table.search(searchTerm).draw();
        searchWait = 0;
      }
      searchWait++;
    },80);
  });
{%- endmacro %}
