{# Container list for a stack on a single host #}
{% from "partials/icons.html" import terminal, rotate_ccw, scroll_text, square, play, cloud_download %}
{% macro container_row(stack, container, host) %}
<div class="flex items-center gap-2 mb-2">
    {% if container.State == "running" %}
        <span class="badge badge-success">running</span>
    {% elif container.State == "unknown" %}
        <span class="badge badge-ghost"><span class="loading loading-spinner loading-xs"></span></span>
    {% elif container.State == "exited" %}
        {% if container.ExitCode == 0 %}
            <span class="badge badge-neutral">exited (0)</span>
        {% else %}
            <span class="badge badge-error">exited ({{ container.ExitCode }})</span>
        {% endif %}
    {% elif container.State == "created" %}
        <span class="badge badge-neutral">created</span>
    {% else %}
        <span class="badge badge-warning">{{ container.State }}</span>
    {% endif %}
    <code class="text-sm flex-1">{{ container.Name }}</code>
    <div class="join">
        <div class="tooltip tooltip-top" data-tip="View logs">
            <button class="btn btn-sm btn-outline join-item"
                    hx-post="/api/stack/{{ stack }}/service/{{ container.Service }}/logs"
                    hx-swap="none">
                {{ scroll_text() }}
            </button>
        </div>
        <div class="tooltip tooltip-top" data-tip="Restart service">
            <button class="btn btn-sm btn-outline join-item"
                    hx-post="/api/stack/{{ stack }}/service/{{ container.Service }}/restart"
                    hx-swap="none">
                {{ rotate_ccw() }}
            </button>
        </div>
        <div class="tooltip tooltip-top" data-tip="Pull image">
            <button class="btn btn-sm btn-outline join-item"
                    hx-post="/api/stack/{{ stack }}/service/{{ container.Service }}/pull"
                    hx-swap="none">
                {{ cloud_download() }}
            </button>
        </div>
        <div class="tooltip tooltip-top" data-tip="Start service">
            <button class="btn btn-sm btn-outline join-item"
                    hx-post="/api/stack/{{ stack }}/service/{{ container.Service }}/up"
                    hx-swap="none">
                {{ play() }}
            </button>
        </div>
        <div class="tooltip tooltip-top" data-tip="Stop service">
            <button class="btn btn-sm btn-outline join-item"
                    hx-post="/api/stack/{{ stack }}/service/{{ container.Service }}/stop"
                    hx-swap="none">
                {{ square() }}
            </button>
        </div>
        <div class="tooltip tooltip-top" data-tip="Open shell">
            <button class="btn btn-sm btn-outline join-item"
                    onclick="initExecTerminal('{{ stack }}', '{{ container.Name }}', '{{ host }}')">
                {{ terminal() }}
            </button>
        </div>
    </div>
</div>
{% endmacro %}

{% macro host_containers(stack, host, containers, show_header=False) %}
{% if show_header %}
<div class="font-semibold text-sm mt-3 mb-1">{{ host }}</div>
{% endif %}
{% for container in containers %}
{{ container_row(stack, container, host) }}
{% endfor %}
{% endmacro %}
