{% extends "base.html" %}
{% block content %}
    <table class="table">
        <thead>
        <tr>
            <th scope="col">Name</th>
            <th scope="col">Queue</th>
            <th scope="col">Deferred</th>
            <th scope="col">Queued</th>
            <th scope="col">InProgress</th>
            <th scope="col">Success</th>
            <th scope="col">Failed</th>
            <th scope="col">Expired</th>
            <th scope="col">LastAlive</th>
            <th scope="col">Information</th>
            <th scope="col">Actions</th>
        </tr>
        </thead>
        <tbody>
        {% for worker in workers %}
            <tr>
                <td>{{ worker.name }}</td>
                <td>{{ worker.queue }}</td>
                <td>{{ worker.job_stat.deferred or 0 }}</td>
                <td>{{ worker.job_stat.queued or 0 }}</td>
                <td>{{ worker.job_stat.in_progress or 0 }}</td>
                <td>{{ worker.job_stat.success or 0 }}</td>
                <td>{{ worker.job_stat.failed or 0 }}</td>
                <td>{{ worker.job_stat.expired or 0 }}</td>
                <td class="time-format">{{ worker.time }}</td>
                <td>
                    {% if worker.is_offline %}
                        <span class="badge bg-danger">offline</span>
                    {% else %}
                        <span class="badge bg-success">online</span>
                    {% endif %}
                    {% if worker.is_timer %}
                        <span class="badge bg-info">timer</span>
                    {% else %}
                        <span class="badge bg-primary">worker</span>
                    {% endif %}
                </td>
                <td>
                    <button class="btn btn-danger btn-sm" onclick="delete_worker('{{ worker.name }}')">Remove</button>
                    <button class="btn btn-dark btn-sm" data-bs-toggle="modal" data-bs-target="#logs-{{ loop.index }}"
                            onclick="show_logs('{{ worker.name }}',$(this))">Log
                    </button>
                    <div class="modal fade" id="logs-{{ loop.index }}" tabindex="-1" aria-hidden="true">
                        <div class="modal-dialog modal-dialog-scrollable modal-xl">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <h5 class="modal-title" id="exampleModalLabel">Worker {{ worker.name }} Logs</h5>
                                    <button type="button" class="btn-close" data-bs-dismiss="modal"
                                            aria-label="Close"></button>
                                </div>
                                <div class="modal-body bg-dark text-white">
                                </div>
                                <div class="modal-footer">
                                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </td>
            </tr>
        {% endfor %}
        </tbody>
    </table>
{% endblock %}
{% block body_end %}
    <script>
        function show_logs(name, btn) {
            let modal = btn.parent().find('.modal-body');
            $.ajax({
                url: "/worker/log?name=" + name,
                method: "GET",
                success: res => {
                    modal.html(res.replace(/\n/g, '<br/>'));
                }
            });
        }

        function delete_worker(name) {
            $.ajax({
                url: "/worker?name=" + name,
                method: "DELETE",
                success: res => {
                    location.reload();
                }
            });
        }
    </script>
{% endblock %}
