{% extends "package/read.html" %}

{%- set schema = h.scheming_get_dataset_schema(dataset_type) -%}

{% block package_notes %}
{% endblock %}

{% block package_resources %}
{% endblock %}

{% block package_additional_info %}
{% endblock %}


{% block content_action %}
{% endblock %}

{% block content_primary_nav %}
{% endblock %}

{% block secondary_content %}
    <section class="module module-narrow module-shallow">
        <h2 class="module-heading"><i
                class="icon-info-sign fa fa-info-circle"></i>{% trans %}This is a list of the executions for the specified service {%
                endtrans %}</h2>
        <div class="module-content">
            <p>
                Monitor the executions of acquired services
            </p>
        </div>
    </section>
{% endblock %}


{% block primary_content_inner %}
    {% link_for _('Create Workflow Execution'), named_route='ids_actions.workflow_configuration', id=agreement, class_="btn btn-outline-primary" %}
    {% if workflows|length > 0  %}
        {% snippet "package/snippets/workflows_table.html", agreement_workflows=workflows, artifacts=artifacts, service_artifact=service_artifact %}
    {% else %}
        There are no execution data available. This table will contain information only when you initiate at least one service execution
    {% endif %}

    <script>
        function triggerWorkflow(triggerArtifactId, agreementId) {
            var data = {
                agreementId: agreementId,
                workflowTriggerArtifactId: triggerArtifactId
            }
            sendTriggerData(data)
        }

        function sendTriggerData( data ) {
            const XHR = new XMLHttpRequest(),
                FD  = new FormData();

            // Push our data into our FormData object
            for( name in data ) {
                FD.append( name, data[ name ] );
            }

            // Define what happens on successful data submission
            XHR.addEventListener( 'load', function( event ) {
                console.debug( 'Workflow was triggered succesfully', event );
                window.location.reload()
            } );

            // Define what happens in case of error
            XHR.addEventListener(' error', function( event ) {
                console.debug( 'Oops! Something went wrong. Workflow execution failed.' );
            } );

            // Set up our request
            XHR.open( 'POST', '/ids/actions/trigger_workflow' );

            // Send our FormData object; HTTP headers are set automatically
            XHR.send( FD );
        }
    </script>

{% endblock %}