<h2>{{ 'Workflow history'|trans }}</h2>

<div class="flex-table">
    {% for step in entity_workflow.stepsChained %}
        {% set place_labels = workflow_metadata(entity_workflow, 'label', step.currentStep, entity_workflow.workflowName) %}
        {% set place_label = place_labels is null ? step.currentStep : place_labels|localize_translatable_string %}

        <div class="item-bloc {{ 'bloc' ~ step.id }} {% if loop.first %}initial{% endif %}">
            <div class="item-row">

                {% if loop.first and step.next is null %}
                    <div class="item-col">
                        {{ 'workflow.No transitions'|trans }}
                    </div>
                {% else %}
                    <div class="item-col">
                        {% if step.previous is not null and step.previous.freezeAfter == true %}
                            <i class="fa fa-snowflake-o fa-sm me-1" title="{{ 'workflow.Freezed'|trans }}"></i>
                        {% endif %}
                        {% if loop.last %}
                            {% if entity_workflow.isOnHoldAtCurrentStep %}
                                {% for hold in step.holdsOnStep %}
                                    <span class="badge bg-success rounded-pill" title="{{ 'workflow.On hold by'|trans({'by': hold.byUser|chill_entity_render_string})|escape('html_attr') }}">{{ 'workflow.On hold by'|trans({'by': hold.byUser|chill_entity_render_string}) }}</span>
                                {% endfor %}
                            {% endif %}
                        {% endif %}
                    </div>
                    <div class="item-col flex-column align-items-end">
                        <div class="decided">
                            {{ place_label }}
                        </div>
                    </div>
                {% endif %}

            </div>
            {% if step.next is not null %}
                {% set transition = chill_workflow_transition_by_string(step.entityWorkflow, step.transitionAfter) %}
                {% set transition_labels = workflow_metadata(step.entityWorkflow, 'label', transition, step.entityWorkflow.workflowName) %}
                {% set transition_label = transition_labels is null ? step.transitionAfter : transition_labels|localize_translatable_string %}
                {% set forward = workflow_metadata(step.entityWorkflow, 'isForward', transition, step.entityWorkflow.workflowName) %}
                <div class="item-row separator">
                    <div class="item-col" style="width: inherit;">
                            <div>
                                {%- if step.transitionBy is not null -%}
                                    <span class="badge-user">{{ step.transitionBy|chill_entity_render_box({'at_date': step.transitionAt}) }}</span>
                                {% else %}
                                    <span class="chill-no-data-statement">{{ 'workflow.Automated transition'|trans }}</span>
                                {%- endif -%}
                            </div>
                        <div>
                            <span>{{ step.transitionAt|format_datetime('long', 'medium') }}</span>
                        </div>
                    </div>
                    <div class="item-col flex-column align-items-end">
                        <div class="to-decision">
                            <i class="fa fa-share fa-fw text-secondary" title="transféré"></i>
                            {% if forward %}
                                <i class="fa fa-check fa-fw text-success"></i>
                            {% else %}
                                <i class="fa fa-times fa-fw text-danger"></i>
                            {% endif %}
                            {{ transition_label }}
                        </div>
                    </div>
                </div>
            {% endif %}
            {% if step.comment is not empty %}
                <div class="item-row separator">
                    <blockquote class="chill-user-quote col">
                        {{ step.comment|chill_markdown_to_html }}
                    </blockquote>
                </div>
            {% endif %}
            {% if not loop.last and step.signatures|length > 0 %}
                <div class="separator">
                    <div>
                        <p><b>{{ 'workflow.signatures_title'|trans({'nb_signatures': step.signatures|length}) }}&nbsp;:</b></p>
                        <div>
                            {{ include('@ChillMain/Workflow/_signature_list.html.twig', {'signatures': step.signatures, is_small: true }) }}
                        </div>
                    </div>
                </div>
            {% endif %}
            {% if loop.last and not step.isFinal %}
                <div class="item-row separator">
                    <div>
                        {% if step.destUser|length > 0 or step.destUserGroups|length > 0 %}
                            <p><b>{{ 'workflow.Users allowed to apply transition'|trans }}&nbsp;: </b></p>
                            <ul>
                                {% for u in step.destUser %}
                                    <li>
                                        <span class="badge-user">{{ u|chill_entity_render_box({'at_date': step.previous.transitionAt}) }}</span>
                                        {% if step.isOnHoldByUser(u) %}
                                            <span class="badge bg-success rounded-pill" title="{{ 'workflow.On hold'|trans|escape('html_attr') }}">{{ 'workflow.On hold'|trans }}</span>
                                        {% endif %}
                                    </li>
                                {% endfor %}
                                {% for u in step.destUserGroups %}
                                    <li>{{ u|chill_entity_render_box({'at_date': step.previous.transitionAt}) }}</li>
                                {% endfor %}
                            </ul>
                        {% endif %}

                        {% if step.ccUser|length > 0 %}
                            <p><b>{{ 'workflow.Users put in Cc'|trans }}&nbsp;: </b></p>
                            <ul>
                                {% for u in step.ccUser %}
                                    <li><span class="badge-user">{{ u|chill_entity_render_box({'at_date': step.previous.transitionAt}) }}</span></li>
                                {% endfor %}
                            </ul>
                        {% endif %}

                        {% if entity_workflow.currentStep.destEmail|length > 0 %}
                            <p><b>{{ 'workflow.An access key was also sent to those addresses'|trans }}&nbsp;:</b></p>
                            <ul>
                                {% for e in entity_workflow.currentStep.destEmail -%}
                                    <li><a href="mailto:{{ e|escape('html_attr') }}">{{ e }}</a></li>
                                {%- endfor %}
                            </ul>
                        {% endif %}

                        {% if step.destUserByAccessKey|length > 0 %}
                            <p><b>{{ 'workflow.Those users are also granted to apply a transition by using an access key'|trans }}&nbsp;:</b></p>
                            <ul>
                                {% for u in step.destUserByAccessKey %}
                                    <li><span class="badge-user">{{ u|chill_entity_render_box({'at_date': step.previous.transitionAt}) }}</span></li>
                                {% endfor %}
                            </ul>
                        {% endif %}
                    </div>
                </div>
                {% if step.signatures|length > 0 %}
                    <p><b>{{ 'workflow.signatures_title'|trans({'nb_signatures': step.signatures|length}) }}&nbsp;:</b></p>
                    {{ include('@ChillMain/Workflow/_signature_list.html.twig', {'signatures': step.signatures, is_small: true }) }}
                {% endif %}
            {% endif %}
            {% if step.sends|length > 0 %}
                <div>
                    <p><b>{{ 'workflow.sent_through_secured_link'|trans }}</b></p>

                    {{ include('@ChillMain/Workflow/_send_views_list.html.twig', {'sends': step.sends}) }}
                </div>
            {% endif %}
        </div>

    {% endfor %}

</div>
