{#
    Licensed to the Apache Software Foundation (ASF) under one
    or more contributor license agreements.  See the NOTICE file
    distributed with this work for additional information
    regarding copyright ownership.  The ASF licenses this file
    to you under the Apache License, Version 2.0 (the
    "License"); you may not use this file except in compliance
    with the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing,
    software distributed under the License is distributed on an
    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    KIND, either express or implied.  See the License for the
    specific language governing permissions and limitations
    under the License.
    #}

    {% extends base_template %}

    {% block title %}
        Edge Worker Hosts
    {% endblock %}

    {% block content %}
      <h2>Edge Worker Hosts</h2>
      {% if hosts|length == 0 %}
        <p>No Edge Workers connected or known currently.</p>
      {% else %}

      <script>
        function showForm(worker_name) {
          var button = document.getElementById("button_" + worker_name);
          var form = document.getElementById("form_" + worker_name);
          form.style.display = "block";
          button.style.display = "none";
        }
        function showEditComment(worker_name) {
          var display = document.getElementById("display_" + worker_name);
          var textarea = document.getElementById("textarea_" + worker_name);
          var button = document.getElementById("update_" + worker_name);
          display.style.display = "none";
          textarea.style.display = "block";
          button.style.display = "inline";
        }
      </script>
      <table class="table table-striped table-bordered">
        <tr>
            <th>Hostname</th>
            <th>State</th>
            <th>Queues</th>
            <th>First Online</th>
            <th>Last Heart Beat</th>
            <th>Active Jobs</th>
            <!-- Stats are not collected (yet) leave the columns out until available
              <th>Jobs Taken</th>
              <th>Jobs Success</th>
              <th>Jobs Failed</th>
            -->
            <th>System Information</th>
            <th>Operations</th>
        </tr>
        {% for host in hosts %}
          <tr>
            <td>{{ host.worker_name }}</a></td>
            <td>
              {%- if host.state in["offline", "offline maintenance"] -%}
                <span class="label" style="color:white; background-color:black;" title="Current State: {{ host.state }}">{{ host.state }}</span>
              {%- elif host.state == "unknown" -%}
                <span class="label" style="color:white; background-color:red;" title="Current State: {{ host.state }}">{{ host.state }}</span>
              {%- elif host.last_update.timestamp() <= five_min_ago.timestamp() -%}
                Reported <span class="label" style="color:white; background-color:red;" title="Current State: {{ host.state }}">{{ host.state }}</span>
                but no heartbeat
              {%- elif host.state in ["starting", "maintenance request", "maintenance exit"] -%}
                <span class="label" style="color:black; background-color:gold;" title="Current State: {{ host.state }}">{{ host.state }}</span>
              {%- elif host.state == "running" -%}
                <span class="label" style="color:white; background-color:green;" title="Current State: {{ host.state }}">{{ host.state }}</span>
              {%- elif host.state == "idle" -%}
                <span class="label" style="color:black; background-color:gray;" title="Current State: {{ host.state }}">{{ host.state }}</span>
              {%- elif host.state == "terminating" -%}
                <span class="label" style="color:black; background-color:violet;" title="Current State: {{ host.state }}">{{ host.state }}</span>
              {%- elif host.state in ["maintenance pending", "maintenance mode"] -%}
                <span class="label" style="color:black; background-color:orange;" title="Current State: {{ host.state }}">{{ host.state }}</span>
              {%- else -%}
                <span class="label" style="color:white; background-color:hotpink;" title="Current State: {{ host.state }}">{{ host.state }}</span>
              {%- endif -%}
            </td>
            <td>
              {% if host.queues %}
                <ul>
                  {% for item in host.queues %}
                    <li>
                      <a href="../taskinstance/list/?_flt_7_state=success&_flt_7_state=failed&_flt_3_queue={{ item }}#"">{{ item }}</a>
                    </li>
                  {% endfor %}
                </ul>
              {% else %}
                (all)
              {% endif %}
            </td>
            <td><time datetime="{{ host.first_online }}">{{ host.first_online }}</time></td>
            <td>{% if host.last_update %}<time datetime="{{ host.last_update }}">{{ host.last_update }}</time>{% endif %}</td>
            <td>
              <a href="../taskinstance/list/?_flt_3_hostname={{ host.worker_name }}&_flt_7_state=success&_flt_7_state=failed#">{{ host.jobs_active }}</a>
            </td>
            <!-- Stats are not collected (yet) leave the columns out until available
              <td>{{ host.jobs_taken }}</td>
              <td>{{ host.jobs_success }}</td>
              <td>{{ host.jobs_failed }}</td>
            -->
            <td>
              <ul>
                {% for item in host.sysinfo_json %}
                  <li>{{ item }}: {{ host.sysinfo_json[item] }}</li>
                {% endfor %}
              </ul>
            </td>
            {%- if host.state in ["idle", "running"] -%}
              <td>
                <button id="button_{{ host.worker_name }}" onclick="showForm('{{ host.worker_name }}')" class="btn btn-sm btn-primary">
                    Enter Maintenance
                </button>
                <form id="form_{{ host.worker_name }}" style="display: none" action="../edgeworkerhosts/status/maintenance/{{ host.worker_name }}/on" method="POST">
                  <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
                  <div style="white-space: pre-line;">
                    <label for="maintenance_comment">Maintenance Comment:</label>
                  </div>
                  <textarea  name="maintenance_comment" rows="3" maxlength="1024" style="width: 100%; margin-bottom: 5px;" required></textarea>
                  <br />
                  <button type="submit" class="btn btn-sm btn-primary">
                      Confirm Maintenance
                  </button>
                </td>
              </form>
            {%- elif host.state in ["maintenance pending", "maintenance mode", "maintenance request"] -%}
              <form action="../edgeworkerhosts/status/maintenance/{{ host.worker_name }}/off" method="POST">
                <td>
                  <div id="display_{{ host.worker_name }}" style="white-space: pre-line;">
                    {{ host.maintenance_comment }}
                    <a onclick="showEditComment('{{ host.worker_name }}')" class="btn btn-sm btn-default" data-toggle="tooltip" rel="tooltip" title="Edit maintenance comment">
                      <span class="sr-only">Edit</span>
                      <i class="fa fa-edit"></i>
                    </a>
                  </div>
                  <textarea id="textarea_{{ host.worker_name }}" name="maintenance_comment" rows="3" maxlength="1024" style="display: none; width:100%;" required>{{ host.maintenance_comment }}</textarea>
                  <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
                  <div style="margin-top: 10px;">
                    <button type="submit" class="btn btn-sm btn-primary">
                        Exit Maintenance
                    </button>
                    <button id="update_{{ host.worker_name }}" type="submit" class="btn btn-sm btn-primary" style="display: none;" formaction="../edgeworkerhosts/status/maintenance/{{ host.worker_name }}/change_comment">
                      Update comment
                    </button>
                  </div>
                </td>
              </form>
            {%- elif host.state in ["offline", "unknown", "offline maintenance"] -%}
              <form action="../edgeworkerhosts/status/maintenance/{{ host.worker_name }}/remove" method="POST">
                <td>
                  <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
                  <button type="submit" class="btn btn-sm btn-primary">
                      Remove
                  </button>
                </td>
              </form>
            {%- else -%}
              <td></td>
            {% endif %}
          </tr>
        {% endfor %}
      </table>
      {% endif %}
    {% endblock %}
<html>
