{% extends "pyfarm/user_interface/layout.html" %}
{% block title %}Agents{% endblock %}
{% block agents_nb_class %}active{% endblock %}
{% block additional_scripts %}
<script src="{{ url_for('static', filename='js/agents.js') }}" type="text/javascript"></script>
{% endblock %}
{% block content %}

<ul class="nav nav-tabs">
  <li role="presentation" class="active"><a href="#">Filters</a></li>
  <li role="presentation" class="dropdown">
    <a class="dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-expanded="false">
      Selected Agents <span class="caret"/>
    </a>
    <ul class="dropdown-menu" role="menu">
      <li role="presentation"><a href="#" id="selected-restart">Restart</a></li>
    </ul>
  </li>
</ul>

<form method="GET" action="{{ url_for('agents_index_ui') }}" class="form-inline" role="form" style="margin-top: 15px;">
  <input type="hidden" name="order_by" value="{{ order_by }}"/>
  <input type="hidden" name="order_dir" value="{{ order_dir }}"/>
  <label for="tags">Tags</label>
  <input type="text" class="form-control" id="tags" name="tags" value="{{ filters.tags }}" placeholder="Tags separated by commas"/>
  <label for="hostname">Hostname</label>
  <input type="text" class="form-control" id="hostname" name="hostname" value="{{ filters.hostname }}" placeholder="Part of hostname"/>
  <div class="btn-group">
    <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" id="state-dropdown-menu" aria-expanded="true">
      State <span class="caret"></span>
    </button>
    <ul class="dropdown-menu dropdown-menu-form" role="menu" aria-labelledby="state-dropdown-menu">
      <li>
        <label class="checkbox">
          {% if filters.state_online or no_state_filters %}
          <input type="checkbox" name="state_online" value="true" checked/>
          {% else %}
          <input type="checkbox" name="state_online" value="true"/>
          {% endif %}
          Online
        </label>
      </li>
      <li>
        <label class="checkbox">
          {% if filters.state_offline or no_state_filters %}
          <input type="checkbox" name="state_offline" value="true" checked/>
          {% else %}
          <input type="checkbox" name="state_offline" value="true"/>
          {% endif %}
          Offline
        </label>
      </li>
      <li>
        <label class="checkbox">
          {% if filters.state_running or no_state_filters %}
          <input type="checkbox" name="state_running" value="true" checked/>
          {% else %}
          <input type="checkbox" name="state_running" value="true"/>
          {% endif %}
          Running
        </label>
      </li>
      <li>
        <label class="checkbox">
          {% if filters.state_disabled or no_state_filters %}
          <input type="checkbox" name="state_disabled" value="true" checked/>
          {% else %}
          <input type="checkbox" name="state_disabled" value="true"/>
          {% endif %}
          Disabled
        </label>
      </li>
    </ul>
  </div>
  <label for="per_page" title="Zero or negative values for no pagination">Per page</label>
  <input type="text" class="form-control" id="per_page" name="per_page" value="{{ filters.per_page }}" placeholder="Number" size="4" style="width: 5em;"/>
  <input type="submit" class="btn" value="Filter">
</form>

<nav>
  <ul class="pagination">
    <li class="{{ 'disabled' if filters.page == 1 else '' }}">
      <a href="{{ url_for('agents_index_ui', per_page=filters.per_page, page=filters.page - 1, **filters_and_order_wo_pagination) }}" aria-label="Previous">
        <span aria-hidden="true">&laquo;</span>
      </a>
    </li>
    {% for page in all_pages %}
    <li class="{{ 'active' if filters.page == page + 1 else '' }}">
      <a href="{{ url_for('agents_index_ui', per_page=filters.per_page, page=page + 1, **filters_and_order_wo_pagination) }}">{{ page + 1}}</a>
    </li>
    {% endfor %}
    <li class="{{ 'disabled' if filters.page == num_pages else '' }}">
      <a href="{{ url_for('agents_index_ui', per_page=filters.per_page, page=filters.page + 1, **filters_and_order_wo_pagination) }}" aria-label="Next">
        <span aria-hidden="true">&raquo;</span>
      </a>
    </li>
  </ul>
</nav>

<div>Agenst: {{ agents_count }} (Online: {{ online_agents_count }}, Running: {{ running_agents_count }}, Offline {{ offline_agents_count }}, Disabled {{ disabled_agents_count }})</div>
<table class="table table-striped table-bordered" style="margin-top:20px;">
  <tr>
    <th><input type="checkbox" title="Select all rows" class="all-agents-selector"/></th>
    <th></th>
    <th>
      <a href="{{ url_for('agents_index_ui', order_by='state', order_dir='asc' if order_dir == 'desc' else 'desc', **filters) }}">
        State
        {% if order_by == 'state' and order_dir == 'desc' %}
        <span class="glyphicon glyphicon-chevron-down"></span>
        {% elif order_by == 'state' %}
        <span class="glyphicon glyphicon-chevron-up"></span>
        {% endif %}
      </a> /
      <a href="{{ url_for('agents_index_ui', order_by='hostname', order_dir='asc' if order_dir == 'desc' else 'desc', **filters) }}">
        Hostname
        {% if order_by == 'hostname' and order_dir == 'desc' %}
        <span class="glyphicon glyphicon-chevron-down"></span>
        {% elif order_by == 'hostname' %}
        <span class="glyphicon glyphicon-chevron-up"></span>
        {% endif %}
      </a>
    </th>
    <th>
      <a href="{{ url_for('agents_index_ui', order_by='remote_ip', order_dir='asc' if order_dir == 'desc' else 'desc', **filters) }}">
        IP-Address
        {% if order_by == 'remote_ip' and order_dir == 'desc' %}
        <span class="glyphicon glyphicon-chevron-down"></span>
        {% elif order_by == 'remote_ip' %}
        <span class="glyphicon glyphicon-chevron-up"></span>
        {% endif %}
      </a>
    </th>
     <th>
      <a href="{{ url_for('agents_index_ui', order_by='cpus', order_dir='asc' if order_dir == 'desc' else 'desc', **filters) }}">
        CPUs
        {% if order_by == 'cpus' and order_dir == 'desc' %}
        <span class="glyphicon glyphicon-chevron-down"></span>
        {% elif order_by == 'cpus' %}
        <span class="glyphicon glyphicon-chevron-up"></span>
        {% endif %}
      </a>
    </th>
    <th>
      <a href="{{ url_for('agents_index_ui', order_by='ram', order_dir='asc' if order_dir == 'desc' else 'desc', **filters) }}">
        RAM
        {% if order_by == 'ram' and order_dir == 'desc' %}
        <span class="glyphicon glyphicon-chevron-down"></span>
        {% elif order_by == 'ram' %}
        <span class="glyphicon glyphicon-chevron-up"></span>
        {% endif %}
      </a>
    </th>
    <th>
      <a href="{{ url_for('agents_index_ui', order_by='version', order_dir='asc' if order_dir == 'desc' else 'desc', **filters) }}">
        Version
        {% if order_by == 'version' and order_dir == 'desc' %}
        <span class="glyphicon glyphicon-chevron-down"></span>
        {% elif order_by == 'version' %}
        <span class="glyphicon glyphicon-chevron-up"></span>
        {% endif %}
      </a>
    </th>
    <th>
      <a href="{{ url_for('agents_index_ui', order_by='last_heard_from', order_dir='asc' if order_dir == 'desc' else 'desc', **filters) }}">
        Last Heard From
        {% if order_by == 'last_heard_from' and order_dir == 'desc' %}
        <span class="glyphicon glyphicon-chevron-down"></span>
        {% elif order_by == 'last_heard_from' %}
        <span class="glyphicon glyphicon-chevron-up"></span>
        {% endif %}
      </a>
    </th>
  </tr>
  {% for agent in agents %}
  <tr>
    <td><input type="checkbox" name="jobid" value="{{ agent.id }}" title="Select agent" class="agent-selector"></td>
    <td>
      <form style="display: inline;" role="form" method="POST" action="{{ url_for('delete_single_agent_ui', agent_id=agent.id) }}">
        <label for="delete-agent-{{agent.id}}-submit" class="clickable-icon" title="Delete agent"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span></label>
        <input id="delete-agent-{{agent.id}}-submit" type="submit" class="hidden" onclick="return confirm('Are you sure you want to delete this agent?');"/>
      </form>
      {% if not agent.is_offline() %}
      <form style="display: inline;" role="form" method="POST" action="{{ url_for('restart_single_agent_ui', agent_id=agent.id) }}">
        <label for="restart-agent-{{agent.id}}-submit" class="clickable-icon" title="Restart agent"><span class="glyphicon glyphicon-repeat" aria-hidden="true"></span></label>
        <input id="restart-agent-{{agent.id}}-submit" type="submit" class="hidden" onclick="return confirm('Are you sure you want to restart this agent?');"/>
      </form>
      {% endif %}
    </td>
    <td>
      {% if agent.state == "online" %}
      <span style="color:#5CB85C" class="glyphicon glyphicon-ok" title="online"></span>
      {% endif %}
      {% if agent.state == "running" %}
      <span style="color:#337AB7" class="glyphicon glyphicon-play" title="running"></span>
      {% endif %}
      {% if agent.state == "offline" %}
      <span style="color:#D9534F" class="glyphicon glyphicon-remove" title="offline"></span>
      {% endif %}
      {% if agent.state == "disabled" %}
      <span class="glyphicon glyphicon-pause" title="disabled"></span>
      {% endif %}
      <a href="{{ url_for('single_agent_ui', agent_id=agent.id) }}">
        {{ agent.hostname }}
      </a>
      {% for tag in agent.tags %}
      {% if tag.tag in filters.tags %}
      <span class="label label-default">
        {{ tag.tag }}
      </span>
      {% else %}
      <a class="label label-info" style="margin:3px;" href="{{ url_for('agents_index_ui', tags=(filters.tags or '') + ' ' + tag.tag, **order) }}">
        {{ tag.tag }}
      </a>
      {% endif %}
      {% endfor %}
    </td>
    <td>{{ agent.remote_ip }}</td>
    <td>{{ agent.cpus }}</td>
    <td>{{ agent.ram }} MiB</td>
    <td>{{ agent.version }}</td>
    <td class="timestamp">{{ agent.last_heard_from }}</td>
  </tr>
  {% endfor %}
</table>

<nav>
  <ul class="pagination">
    <li class="{{ 'disabled' if filters.page == 1 else '' }}">
      <a href="{{ url_for('agents_index_ui', per_page=filters.per_page, page=filters.page - 1, **filters_and_order_wo_pagination) }}" aria-label="Previous">
        <span aria-hidden="true">&laquo;</span>
      </a>
    </li>
    {% for page in all_pages %}
    <li class="{{ 'active' if filters.page == page + 1 else '' }}">
      <a href="{{ url_for('agents_index_ui', per_page=filters.per_page, page=page + 1, **filters_and_order_wo_pagination) }}">{{ page + 1}}</a>
    </li>
    {% endfor %}
    <li class="{{ 'disabled' if filters.page == num_pages else '' }}">
      <a href="{{ url_for('agents_index_ui', per_page=filters.per_page, page=filters.page + 1, **filters_and_order_wo_pagination) }}" aria-label="Next">
        <span aria-hidden="true">&raquo;</span>
      </a>
    </li>
  </ul>
</nav>

<form method="post" action="{{ url_for('restart_multiple_agents_ui', next=url_for('agents_index_ui', **filters_and_order)) }}" id="restart_multiple_form"></form>

{% endblock %}
