{% extends base_template %}
{% load debusine %}
{% load users %}
{% block tail_js %}
    {{ block.super }}
    <script type="module">
    import { DebusineClipboardCopy } from "debusine";

    document.addEventListener("DOMContentLoaded", () => {
        DebusineClipboardCopy.setup_all();
    });
    </script>
    {% include "web/_tab_selector.html" %}
{% endblock %}
{% block content %}
    {% ui work_request as work_request_ui %}
    <nav>
        <div class="nav nav-tabs mb-3" id="nav-tab" role="tablist">
            {% if specialized_tab %}
                <button class="nav-link active"
                        id="nav-{{ specialized_tab.slug }}-tab"
                        data-bs-toggle="tab"
                        data-bs-target="#nav-{{ specialized_tab.slug }}"
                        type="button"
                        role="tab"
                        aria-controls="nav-{{ specialized_tab.slug }}"
                        aria-selected="false">{{ specialized_tab.label }}</button>
            {% endif %}
            <button class="nav-link {% if not specialized_tab %}active{% endif %}"
                    id="nav-work_request-tab"
                    data-bs-toggle="tab"
                    data-bs-target="#nav-work_request"
                    type="button"
                    role="tab"
                    aria-controls="nav-work_request"
                    aria-selected="false">Work request</button>
            {% if not work_request.is_workflow or work_request_output_data %}
                <button class="nav-link"
                        id="nav-output-tab"
                        data-bs-toggle="tab"
                        data-bs-target="#nav-output"
                        type="button"
                        role="tab"
                        aria-controls="nav-output"
                        aria-selected="false">Output</button>
            {% endif %}
            <button class="nav-link"
                    id="nav-dependencies-tab"
                    data-bs-toggle="tab"
                    data-bs-target="#nav-dependencies"
                    type="button"
                    role="tab"
                    aria-controls="nav-dependencies"
                    aria-selected="false">Dependencies</button>
            <button class="nav-link"
                    id="nav-internals-tab"
                    data-bs-toggle="tab"
                    data-bs-target="#nav-internals"
                    type="button"
                    role="tab"
                    aria-controls="nav-internals"
                    aria-selected="false">Internals</button>
        </div>
    </nav>
    <div class="tab-content" id="nav-content">
        {% if specialized_tab %}
            <div class="tab-pane show active"
                 id="nav-{{ specialized_tab.slug }}"
                 role="tabpanel"
                 aria-labelledby="nav-{{ specialized_tab.slug }}-tab"
                 tabindex="0">{% include specialized_tab.template %}</div>
        {% endif %}
        <div class="tab-pane {% if not specialized_tab %}show active{% endif %}"
             id="nav-work_request"
             role="tabpanel"
             aria-labelledby="nav-work_request-tab"
             tabindex="0">
            <h2>Description</h2>
            {% include description_template with data=description_data only %}
            <h2>Input artifacts</h2>
            {% if not input_artifacts_table %}
                <p id="no-input-artifacts">No input artifacts.</p>
            {% else %}
                {% widget input_artifacts_table %}
            {% endif %}
            {% if work_request.is_workflow or work_request.is_part_of_workflow %}
                <h2>Workflow information</h2>
                <ul>
                    {% for workflow in workflow_parents %}
                        {% if workflow.is_workflow_root %}
                            <li class="workflow-ancestor">
                                <span title="Root workflow">⬆️</span>
                                {% include "web/_work_request-workflow.html" with work_request=workflow only %}
                            </li>
                        {% else %}
                            <li class="workflow-ancestor">
                                <span title="Ancestor workflow">⬆️</span>
                                {% include "web/_work_request-workflow.html" with work_request=workflow only %}
                            </li>
                        {% endif %}
                    {% endfor %}
                    {% if work_request_ui.workflow_children|lookup:work_request.id %}
                        <li>{% include "web/_work_request-descendants.html" %}</li>
                    {% endif %}
                </ul>
                {% if perms.db.change_workrequest and work_request.can_be_unblocked and work_request.unblock_strategy == "manual" %}
                    <div class="card mb-2">
                        <div class="card-header">Review blocked workflow step</div>
                        <div class="card-body">
                            {% if manual_unblock_log_entries %}
                                {% include "web/_manual_unblock_log.html" with manual_unblock_log_entries=manual_unblock_log_entries only %}
                            {% endif %}
                            <form id="manual-unblock-form"
                                  method="post"
                                  action="{{ work_request.get_absolute_url_unblock }}">
                                {% csrf_token %}
                                {{ manual_unblock_form.as_p }}
                                <input class="btn btn-success btn-sm"
                                       type="submit"
                                       name="action"
                                       value="Accept">
                                <input class="btn btn-danger btn-sm"
                                       type="submit"
                                       name="action"
                                       value="Reject">
                                <input class="btn btn-info btn-sm"
                                       type="submit"
                                       name="action"
                                       value="Record notes only">
                            </form>
                        </div>
                    </div>
                {% endif %}
            {% endif %}
            {% if work_request.requires_signature and user == work_request.created_by %}
                <div id="requires-signature" class="card mb-2">
                    <div class="card-header">Waiting for signature</div>
                    <div class="card-body">
                        Run <code>debusine --server {{ DEBUSINE_FQDN }}/{{ scope }} provide-signature {{ work_request.id }}</code> to sign this request.
                        <form method="post" action="{{ work_request.get_absolute_url_abort }}">
                            {% csrf_token %}
                            If you do not want to sign it, you can
                            <button class="btn btn-link in-running-text" type="submit">abort</button>
                            it instead.
                        </form>
                    </div>
                </div>
            {% endif %}
            {% if work_request.requires_confirmation and work_request|has_perm:"can_confirm" %}
                <div id="confirm" class="card mb-2">
                    <div class="card-header">Confirm</div>
                    <div class="card-body">
                        <form id="confirm-form"
                              method="post"
                              action="{{ work_request.get_absolute_url_confirm }}">
                            {% csrf_token %}
                            {{ confirm_form.as_p }}
                            <button class="btn btn-success btn-sm"
                                    type="submit"
                                    name="action"
                                    value="confirm">{{ confirm_label }}</button>
                            <button class="btn btn-danger btn-sm"
                                    type="submit"
                                    name="action"
                                    value="deny">{{ deny_label }}</button>
                        </form>
                    </div>
                </div>
            {% endif %}
            {% with confirmation=work_request.output_data.confirmation confirmed_by=work_request.completed_by|default:work_request.aborted_by %}
                {% if work_request.completed_at and confirmation %}
                    <div id="confirmation">
                        <h2>Confirmation</h2>
                        {% if confirmation.confirmed %}
                            <p>Confirmed at {{ work_request.completed_at|date:"Y-m-d H:i" }} by {{ confirmed_by|user_link }}.</p>
                        {% else %}
                            <p>
                                Denied at {{ work_request.completed_at|date:"Y-m-d H:i" }} by {{ confirmed_by|user_link }}.
                                {% if work_request.task_data.deny_action == "fail" %}
                                    Failed the work request.
                                {% elif work_request.task_data.deny_action == "abort" %}
                                    Aborted the work request.
                                {% elif work_request.task_data.deny_action == "abort-workflow" %}
                                    Aborted the workflow.
                                {% endif %}
                            </p>
                        {% endif %}
                        {% if confirmation.comment %}
                            <p>Comment left by {{ confirmed_by|user_link }}:</p>
                            {{ confirmation.comment|linebreaks }}
                        {% endif %}
                    </div>
                {% endif %}
            {% endwith %}
            {% if validation_error %}
                <h2>Validation error</h2>
                <div id="validation-error"
                     class="border rounded p-2 mb-2 position-relative">
                    <button class="btn btn-secondary btn-sm position-absolute top-0 end-0 m-1 debusine-clipboard-copy"
                            type="button"
                            aria-label="Copy validation error to clipboard"
                            data-copy-target="#validation-error-content">
                        <i class="bi bi-clipboard-plus" title="Copy to clipboard"></i>
                    </button>
                    <pre class="pre-wrap mb-0"><code id="validation-error-content">{{ validation_error }}</code></pre>
                </div>
            {% endif %}
            {% if work_request.output_data.errors %}
                <h2>Errors</h2>
                <ul id="errors" class="error-list">
                    {% for error in work_request.output_data.errors %}
                        <li>
                            <div id="error-{{ forloop.counter }}"
                                 class="border rounded p-2 mb-2 position-relative">
                                {#Floating copy button#}
                                <button class="btn btn-secondary btn-sm position-absolute top-0 end-0 m-1 debusine-clipboard-copy"
                                        type="button"
                                        aria-label="Copy error message to clipboard"
                                        data-copy-target="#error-message-{{ forloop.counter }}-content">
                                    <i class="bi bi-clipboard-plus" title="Copy to clipboard"></i>
                                </button>
                                <pre class="pre-wrap mb-0"><code id="error-message-{{ forloop.counter }}-content">{{ error.message }}</code></pre>
                            </div>
                        </li>
                    {% endfor %}
                </ul>
            {% endif %}
        </div>
        {% if not work_request.is_workflow or work_request_output_data %}
            <div class="tab-pane"
                 id="nav-output"
                 role="tabpanel"
                 aria-labelledby="nav-output-tab"
                 tabindex="0">
                <h2>Output artifacts</h2>
                {% if not built_artifacts_table %}
                    <p id="no-output-artifacts">No artifacts produced.</p>
                {% else %}
                    {% widget built_artifacts_table %}
                {% endif %}
                <h2>Output data</h2>
                {% if work_request_output_data %}
                    {% include "web/_dict_to_ul.html" with dict=work_request_output_data only %}
                {% else %}
                    <p id="output-no-output-data">No output data.</p>
                {% endif %}
            </div>
        {% endif %}
        <div class="tab-pane"
             id="nav-dependencies"
             role="tabpanel"
             aria-labelledby="nav-dependencies-tab"
             tabindex="0">
            <h2>
                Dependencies
                {% help "dependencies" %}
            </h2>
            {% if work_request_dependencies %}
                <p>Work requests that must be completed before this one can run.</p>
                {% widget work_request_dependencies %}
            {% else %}
                <p>This work request does not depend on any other one.</p>
            {% endif %}
            <h2>
                Reverse dependencies
                {% help "reverse_dependencies" %}
            </h2>
            {% if work_request_reverse_dependencies %}
                <p>Work requests that require this one to complete successfully before they can run.</p>
                {% widget work_request_reverse_dependencies %}
            {% else %}
                <p>This work request is not required by any other one.</p>
            {% endif %}
        </div>
        <div class="tab-pane"
             id="nav-internals"
             role="tabpanel"
             aria-labelledby="nav-internals-tab"
             tabindex="0">
            {% with workflow_root=work_request.workflow_root %}
                {% if workflow_root %}
                    {% with collection=workflow_root.internal_collection %}
                        <p id="internal-collection">
                            Internal collection: <a href="{{ collection.get_absolute_url }}">{{ collection.name }}</a>
                        </p>
                    {% endwith %}
                {% endif %}
            {% endwith %}
            <h2>Task data</h2>
            {% include "web/_card.html" with id="card-task_data" data=task_data only %}
            <h2>Task data after applying task configuration</h2>
            {% if show_task_data_configured %}
                {% include "web/_card.html" with id="card-configured_task_data" data=task_data_configured only %}
            {% else %}
                <p id="task_data_not_configured">Task has not yet been configured.</p>
            {% endif %}
            <h2>Dynamic data</h2>
            {% if show_task_data_dynamic %}
                {% include "web/_card.html" with id="card-dynamic_data" data=task_data_dynamic only %}
            {% else %}
                <p id="dynamic_data_not_available">Dynamic task data has not yet been computed.</p>
            {% endif %}
            <h2>Workflow data</h2>
            {% include "web/_card.html" with id="card-workflow_data" data=work_request_workflow_data_json only %}
            <h2>Event reactions</h2>
            {% include "web/_card.html" with id="card-event_reactions" data=work_request_event_reactions_json only %}
            <h2>Output data</h2>
            {% if work_request_output_data %}
                {% include "web/_card.html" with id="card-output_data" data=work_request_output_data_json only %}
            {% else %}
                <p id="internals-no-output-data">No output data.</p>
            {% endif %}
        </div>
    </div>
{% endblock %}
