{% load debusine %}
{% load users %}
<h2>Description</h2>
{% include description_template with data=description_data only %}
<h2>Input artifacts</h2>
{% if input_artifacts_table %}
    {% widget input_artifacts_table %}
{% elif input_artifacts_list %}
    {% include "web/_input-artifact-list.html" with input_artifacts=input_artifacts_list %}
{% else %}
    <p id="no-input-artifacts">No input artifacts.</p>
{% endif %}
{% if work_request.is_workflow or work_request.is_part_of_workflow %}
    <h2>Workflow information</h2>
    {% with children=work_request_ui.workflow_children|lookup:work_request.id %}
        {# Use 'not children' because the defaultdict returns [] when empty #}
        {% if work_request.is_workflow and work_request.status == "running" %}
            {% if not children %}
                <div class="alert alert-info mb-2" id="workflow-orchestrating-message">
                    <h6 class="mb-1">Workflow is being orchestrated</h6>
                    <p class="mb-0">
                        This workflow is still being populated. Large workflows may take a
                        significant amount of time.
                    </p>
                </div>
            {% endif %}
        {% endif %}
    {% endwith %}
    <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">
            <button class="btn btn-secondary btn-sm position-absolute top-0 end-0 m-1 debusine-clipboard-copy"
                    type="button"
                    aria-label="Copy work-request sign command to clipboard"
                    data-copy-target="#work-request-sign-command">
                <i class="bi bi-clipboard-plus" title="Copy to clipboard"></i>
            </button>
            <span>Run <code id="work-request-sign-command">debusine --server {{ DEBUSINE_FQDN }}/{{ scope }} work-request sign {{ work_request.id }}</code> to sign this request.</span>
            <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_ui.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 %}
