{% extends "templates/home.html" %}

{% block main %}
<div class="container">
    <div class="row">
      <div class="text-center">
        {% if default_server.active %}
          <a id="stop" role="button" class="btn btn-lg btn-danger">
            Stop My Server
          </a>
        {% endif %}
        <a id="start" role="button" class="btn btn-lg btn-primary" href="{{ url }}">
        {% if not default_server.active %}Start{% endif %}
        My Server
        </a>
      </div>
    </div>

    {% if allow_named_servers %}
    
        {% set named_spawners = user.all_spawners(include_default=False)|list %}
        {% set ns = namespace(non_dashboard_count=0) %}

        {% macro spawner_row(spawner) %}
        <tr class="home-server-row" data-server-name="{{ spawner.name }}">
            {# name #}
            <td>{{ spawner.name }}</td>
            {# url #}
            <td>
            <a class="server-link {% if not spawner.ready %}hidden{% endif %}" href="{{ user.server_url(spawner.name) }}">
                {{ user.server_url(spawner.name) }}
            </a>
            </td>

            {% if spawner.orm_spawner.dashboard_final_of %}
            {# dashboard_name #}
            <td><a href="../dashboards/{{ spawner.orm_spawner.dashboard_final_of.urlname }}/edit">{{ spawner.orm_spawner.dashboard_final_of.name }}</a></td>
            {% endif %}

            {# activity #}
            <td class='time-col'>
            {% if spawner.last_activity %}
            {{ spawner.last_activity.isoformat() + 'Z' }}
            {% else %}
            Never
            {% endif %}
            </td>
            {# actions #}
            <td>
            <a role="button" class="stop-server btn btn-xs btn-danger{% if not spawner.active %} hidden{% endif %}" id="stop-{{ spawner.name }}">stop</a>
            <a role="button" class="start-server btn btn-xs btn-primary {% if spawner.active %} hidden{% endif %}" id="start-{{ spawner.name }}"
            href="{{ base_url }}spawn/{{ user.name }}/{{ spawner.name }}"
            >
                start
            </a>
            <a role="button" class="delete-server btn btn-xs btn-danger{% if spawner.active %} hidden{% endif %}" id="delete-{{ spawner.name }}">delete</a>
            </td>
        </tr>
        {% endmacro %}

        {% if not cds_hide_user_named_servers %}

        <h2>
        Named Servers
        </h2>

        <p>
        In addition to your default server,
        you may have additional {% if named_server_limit_per_user > 0 %}{{ named_server_limit_per_user }} {% endif %}server(s) with names.
        This allows you to have more than one server running at the same time.
        </p>
    
        <table class="server-table table table-striped">
        <thead>
            <tr>
            <th>Server name</th>
            <th>URL</th>
            <th>Last activity</th>
            <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            <tr class="home-server-row add-server-row">
            <td colspan="4">
                <input class="new-server-name" placeholder="Name your server">
                <a role="button" class="new-server-btn" class="add-server btn btn-xs btn-primary">
                Add New Server
                </a>
            </td>
            </tr>
            {% for spawner in named_spawners %}
                {% if not spawner.orm_spawner.dashboard_final_of %}
                    {% set ns.non_dashboard_count = ns.non_dashboard_count + 1 %}
                    {{ spawner_row(spawner) }}
                {% endif %}
            {% endfor %}
        </tbody>
        </table>

        {% else %}
            {% for spawner in named_spawners %}
                {% if not spawner.orm_spawner.dashboard_final_of %}
                    {% set ns.non_dashboard_count = ns.non_dashboard_count + 1 %}
                {% endif %}
            {% endfor %}
        {% endif %}

        {% if not cds_hide_user_dashboard_servers
             and ns.non_dashboard_count != named_spawners|length %}

        <h2>
        Dashboard Servers
        </h2>

        <p>
        These are servers running dashboards that you have created.
        </p>
        
        <table class="server-table table table-striped dashboard-server-table">
            <thead>
            <tr>
                <th>Server name</th>
                <th>URL</th>
                <th>Dashboard</th>
                <th>Last activity</th>
                <th>Actions</th>
            </tr>
            </thead>
            <tbody>
            {% for spawner in named_spawners %}
                {% if spawner.orm_spawner.dashboard_final_of %}
                    {{ spawner_row(spawner) }}
                {% endif %}
            {% endfor %}
            </tbody>
        </table>
        {% endif %}

    {% endif %}
 </div>
 {% endblock main %}
  