{% extends base_template %}
{% load debusine %}
{% block tail_js %}
    {{ block.super }}
    {% include "web/_tab_selector.html" %}
{% endblock %}
{% block content %}
    {% ui work_request as work_request_ui %}
    <h1 class="mb-4" title="{{ work_request.task_name }}">
        <span class="bi {% icon "work_request" %}" title="Work request"></span>
        {{ title }}
    </h1>
    <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>
            <button class="nav-link"
                    id="nav-output-tab"
                    data-bs-toggle="tab"
                    data-bs-target="#nav-output"
                    type="button"
                    role="tab"
                    aria-controls="nav-work_request"
                    aria-selected="false">Output</button>
            <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 %}
                <p id="no-input-artifacts">No input artifacts.</p>
            {% else %}
                {% include "web/_artifact-table.html" with table_id="input-artifacts" artifacts=input_artifacts only %}
            {% 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 validation_error %}
                <h2>Validation error</h2>
                <pre><code>{{ validation_error }}</code></pre>
            {% endif %}
            {% if work_request.output_data.errors %}
                <h2>Errors</h2>
                <ul id="errors">
                    {% for error in work_request.output_data.errors %}
                        <li>
                            <pre><code>{{ error.message }}</code></pre>
                        </li>
                    {% endfor %}
                </ul>
            {% endif %}
        </div>
        <div class="tab-pane"
             id="nav-output"
             role="tabpanel"
             aria-labelledby="nav-output-tab"
             tabindex="0">
            <h2>Output artifacts</h2>
            {% if not built_artifacts %}
                <p id="no-output-artifacts">No artifacts produced.</p>
            {% else %}
                {% include "web/_artifact-table.html" with table_id="output-artifacts" artifacts=built_artifacts only %}
            {% 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>
        <div class="tab-pane fade"
             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 fade"
             id="nav-internals"
             role="tabpanel"
             aria-labelledby="nav-internals-tab"
             tabindex="0">
            {% with workflow_root=work_request.get_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 is_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">Work request is not configured.</p>
            {% endif %}
            <h2>Dynamic data</h2>
            {% if is_task_data_configured %}
                {% include "web/_card.html" with id="card-dynamic_data" data=task_data_dynamic only %}
            {% else %}
                <p id="dynamic_data_not_configured">Work request is not configured.</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 %}
