{#
 # This file is part of SolidInvoice package.
 #
 # (c) Pierre du Plessis <open-source@solidworx.co>
 #
 # This source file is subject to the MIT license that is bundled
 # with this source code in the file LICENSE.
 #}

{% set currency = invoice.client.currency %}

{% set isRecurring = true %}

{% extends "@SolidInvoiceCore/Layout/default.html.twig" %}

{% block page_title %}
    {{ "invoice.title"|trans({"%id%" : invoice.id}) }}
{% endblock page_title %}

{% block content %}
    <div class="float-right no-print mt-3">
        <div class="btn-toolbar" role="toolbar">
            <div class="btn-group mr-2" role="group">
                <a href="{{ path('_invoices_clone_recurring', {'id' : invoice.id}) }}" class="btn btn-secondary">
                    {{ icon('clone') }} {{ "invoice.view.toolbar.clone"|trans }}
                </a>

                {% if workflow_can(invoice, 'activate') %}
                    <a href="{{ path('_action_recurring_invoice', {'action' : 'activate', 'id' : invoice.id}) }}"
                       class="btn btn-success">
                        {{ icon('check') }} {{ "Activate"|trans }}
                    </a>
                {% endif %}

                {% if workflow_can(invoice, 'cancel') %}
                    <a href="{{ path('_action_recurring_invoice', {'action' : 'cancel', 'id' : invoice.id}) }}"
                       class="btn btn-warning">
                        {{ icon('ban') }} {{ "invoice.view.toolbar.cancel"|trans }}
                    </a>
                {% endif %}

                {% if workflow_can(invoice, 'edit') %}
                    <a href="{{ path('_invoices_edit_recurring', {'id' : invoice.id}) }}" class="btn btn-secondary">
                        {{ icon('edit') }} {{ "invoice.view.toolbar.edit"|trans }}
                    </a>
                {% endif %}
            </div>
        </div>
    </div>

    <br class="clear"/>
    <br class="clear"/>

    {% embed "@SolidInvoiceInvoice/invoice_template.html.twig" %}
        {% block title %}
            <h2 class="page-header">
                <span class="badge badge-pill bg-success">{{ 'invoice.recurring.view.label'|trans }}</span>
                {{ invoice_label(invoice.status) }}
            </h2>
        {% endblock %}

        {% block footer %}
            {{ parent() }}

            {% if invoice.notes is not empty %}
                <div class="col-6 no-print">
                    <strong>{{ 'invoice.notes'|trans }}</strong>
                    <p class="text-muted well well-sm no-shadow">
                        {{ invoice.notes }}
                    </p>
                </div>
            {% endif %}

            <div class="row no-print">
                <div class="col-12">
                    <div class="btn-group">
                        <a href="javascript:window.print()" class="btn btn-secondary">{{ icon('print') }} {{ 'invoice.action.print'|trans }}</a>
                    </div>
                </div>
            </div>
        {% endblock footer %}

        {% block header %}
            {{ parent() }}

            <div class="row">
                <div class="col-md-12">
                    <div>
                        {{ icon('refresh') }}
                        {{ recurring_frequency(invoice.recurringOptions) }}
                    </div>
                    <br class="clearfix" />
                </div>
            </div>
        {% endblock %}
    {% endembed %}

    {% if invoice.status is same as('active') %}

        {% set totalCount = 0 %}

        {% set totalCount = totalCount - 10 %}

        {% set occurrences %}
            {% for occurrence in recurring_occurrences(invoice.recurringOptions) %}
                {% if loop.index0 < 10 %}
                    <tr>
                        <td>
                            {{ occurrence|date('l, d F Y') }}
                        </td>
                    </tr>
                {% endif %}

                {% set totalCount = totalCount + 1 %}
            {% endfor %}
            {% if totalCount > 0 %}
                <tr>
                    <td>
                        ... {{ totalCount }} {{ 'more'|trans }}
                    </td>
                </tr>
            {% endif %}
        {% endset %}

        {% if occurrences|trim is not empty %}
            <br class="clear"/>
            <div class="card">
                <div class="card-header">
                    <h3 class="card-title">{{ 'Upcoming Occurrences'|trans }}</h3>
                </div>
                <div class="card-body p-0">
                    <table class="table">
                        <tbody>
                            {{ occurrences }}
                        </tbody>
                    </table>

                </div>
            </div>
        {% endif %}
    {% endif %}
{% endblock content %}
