{% extends request.ajax ? "layout_blank.html.twig" : "layout_default.html.twig" %}

{% import "macro_functions.html.twig" as mf %}

{% set nr = invoice.serie ~ "%05s"|format(invoice.nr) %}

{% block meta_title %}{{ 'Invoice'|trans }} #{{ nr }}{% endblock %}

{% block body_class %}invoice-invoice{% endblock %}

{% block breadcrumb %}
    <li class="breadcrumb-item"><a href="{{ '/invoice'|link }}">{{ 'Invoices'|trans }}</a></li>
    <li class="breadcrumb-item active" aria-current="page">#{{ nr }}</li>
{% endblock %}

{% block content %}
{% set seller = invoice.seller %}
{% set buyer = invoice.buyer %}
{% set company = guest.system_company %}
    <div class="row mb-3">
        <div class="col-md-12">
            {% if invoice.status == 'unpaid' %}
                <div class="card">
                <div class="card-body">
                    <h5 class="mb-1">{{ 'Payment methods'|trans }}</h5>
                    <p class="small text-muted">{{ 'Please choose a payment type and pay for your chosen products.'|trans }}</p>
                    <form method="post" action="{{ 'api/guest/invoice/payment'|link }}" class="api-form" data-api-redirect="{{ ('invoice/'~invoice.hash)|link({ 'auto_redirect': 1 }) }}">
                        <input type="hidden" name="CSRFToken" value="{{ CSRFToken }}"/>
                        <input type="hidden" name="hash" value="{{ invoice.hash }}"/>
                        <div class="d-flex gap-3">
                            {% for gtw in guest.invoice_gateways %}
                                {% if invoice.currency in gtw.accepted_currencies %}
                                {% set banklink = 'invoice/banklink'|link %}
                                <div class="invoice-gateway rounded-2 p-2">
                                    <input type="radio" class="btn-check border-0" name="gateway_id" gateway_id="{{ gtw.id }}" id="{{ gtw.id }}" autocomplete="off">
                                    <label class="btn" for="{{ gtw.id }}"
                                        style="background: transparent url('{{gtw.logo.logo}}') no-repeat center center; background-size: contain; height:{{gtw.logo.height}}; width:{{gtw.logo.width}};"
                                        onclick="paymentPrompt('{{ gtw.id }}', {{ gtw.allow_recurrent ? 'true' : 'false' }}, `{{ gtw.title }}`)"
                                        data-bs-toggle="tooltip" data-bs-title="{{ gtw.title }}"></label>
                                    </div>
                                {% endif %}
                            {% endfor %}
                        </div>
                        <input type="hidden" name="gateway_id" id="gateway_id">
                    </form>
                </div>
            </div>
            {% endif %}
        </div>
    </div>
    <div class="row">
        <div class="col-md-12">
            <div class="card mb-4">
                <div class="card-header py-3 py-3">
                    <div class="d-flex justify-content-between">
                        <div class="d-flex flex-column">
                            <h5 class="mb-1 d-flex align-items-center">{{ 'Invoice'|trans }}</h5>
                            <span class="small text-muted">{{ 'You can print this invoice or export it to a PDF file by clicking on the corresponding button.'|trans }}</span>
                        </div>
                        <div class="d-flex align-items-center">
                            <div class="d-flex gap-2 align-items-center justify-content-end">
                                <a href="{{ 'invoice/pdf'|link }}/{{ invoice.hash }}" target="_blank" class="btn btn-sm btn-danger">{{ 'PDF'|trans }}</a>
                                <a href="{{ 'invoice/print'|link }}/{{ invoice.hash }}" target="_blank" class="btn btn-sm btn-dark">{{ 'Print'|trans }}</a>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="card-body">
                    <div class="container">
                        <div class="row">
                            <div class="col-md-6 mb-4">
                                <div class="well small">
                                    <h4 class="fw-light mt-2 mb-3">{{ 'Invoice'|trans }} #{{ nr }}</h4>
                                    <dl class="row">
                                        <dt class="col-sm-4 text-muted">{{ 'Invoice Date'|trans }}</dt>
                                        <dd class="col-sm-8">
                                            {% if invoice.paid_at|trim %}
                                                {{ invoice.paid_at|format_date }}
                                            {% else %}
                                                {{ invoice.created_at|format_date }}
                                            {% endif %}
                                        </dd>
                                        <dt class="col-sm-4 text-muted">{{ 'Due Date'|trans }}</dt>
                                        <dd class="col-sm-8">
                                            {% if invoice.due_at|trim %}
                                                {{ invoice.due_at|format_date }}
                                            {% else %}
                                                -----
                                            {% endif %}
                                        </dd>
                                        <dt class="col-sm-4 text-muted">{{ 'Status'|trans }}</dt>
                                        <dd class="col-sm-8">
                                        <span class="badge fs-7 {% if invoice.status == 'paid' %} bg-success{% elseif invoice.status == 'unpaid' %}bg-warning{% endif %}">
                                            {{ mf.status_name(invoice.status) }}
                                        </span>
                                        </dd>
                                    </dl>
                                </div>
                            </div>

                            <div class="col-md-6 mb-4">
                                <div class="well small">
                                    <h5>{{ 'Company'|trans }}</h5>
                                        <dl class="row">
                                            {% if seller.company|trim %}
                                                <dt class="col-sm-3 text-muted">{{ 'Name'|trans }}</dt>
                                                <dd class="col-sm-9">{{ seller.company }}</dd>
                                            {% endif %}

                                            {% if seller.company_vat|trim %}
                                                <dt class="col-sm-3 text-muted">{{ 'VAT'|trans }}</dt>
                                                <dd class="col-sm-9">{{ seller.company_vat }}</dd>
                                            {% endif %}

                                            {% if seller.address|trim %}
                                                <dt class="col-sm-3 text-muted">{{ 'Address'|trans }}</dt>
                                                <dd class="col-sm-9">{{ seller.address }}</dd>
                                            {% endif %}

                                            {% if seller.phone|trim %}
                                                <dt class="col-sm-3 text-muted">{{ 'Phone'|trans }}</dt>
                                                <dd class="col-sm-9">{{ seller.phone }}</dd>
                                            {% endif %}

                                            {% if seller.email|trim %}
                                                <dt class="col-sm-3 text-muted">{{ 'Email'|trans }}</dt>
                                                <dd class="col-sm-9">{{ seller.email }}</dd>
                                            {% endif %}

                                            {% if seller.account_number|trim %}
                                                <dt class="col-sm-3 text-muted">{{ 'Account'|trans }}</dt>
                                                <dd class="col-sm-9">{{ seller.account_number }}</dd>
                                            {% endif %}

                                            {% if seller.note|trim %}
                                                <dt class="col-sm-3 text-muted">{{ 'Note'|trans }}</dt>
                                                <dd class="col-sm-9">{{ seller.note }}</dd>
                                            {% endif %}
                                        </dl>
                                    </div>
                                </div>
                            </div>

                            <div class="row">
                                <div class="col-md-6">
                                    <div class="well small">
                                        <h5>{{ 'Client'|trans }}</h5>
                                        <dl class="row">
                                            {% if buyer.first_name|trim or buyer.last_name|trim %}
                                                <dt class="col-sm-4 text-muted">{{ 'Name'|trans }}</dt>
                                                <dd class="col-sm-8">{{ buyer.first_name }} {{ buyer.last_name }}</dd>
                                            {% endif %}

                                            {% if buyer.company|trim %}
                                                <dt class="col-sm-4 text-muted">{{ 'Company'|trans }}</dt>
                                                <dd class="col-sm-8">{{ buyer.company }}</dd>
                                            {% endif %}

                                            {% if buyer.company_number|trim %}
                                                <dt class="col-sm-4 text-muted">{{ 'Company number'|trans }}</dt>
                                                <dd class="col-sm-8">{{ buyer.company_number }}</dd>
                                            {% endif %}

                                            {% if buyer.company_vat|trim %}
                                                <dt class="col-sm-4 text-muted">{{ 'Company VAT'|trans }}</dt>
                                                <dd class="col-sm-8">{{ buyer.company_vat }}</dd>
                                            {% endif %}

                                            {% if buyer.address|trim %}
                                                <dt class="col-sm-4 text-muted">{{ 'Address'|trans }}</dt>
                                                <dd class="col-sm-8">{{ buyer.address }}</dd>
                                                <dd class="col-sm-4">{{ buyer.city }}, {{ buyer.state }}</dd>
                                                <dd class="col-sm-8">{{ buyer.zip }}, {{ guest.system_countries[buyer.country] }}</dd>
                                            {% endif %}

                                            {% if buyer.phone|trim %}
                                                <dt class="col-sm-4 text-muted">{{ 'Phone'|trans }}</dt>
                                                <dd class="col-sm-8">{{ buyer.phone }}</dd>
                                            {% endif %}
                                        </dl>
                                    </div>
                                </div>
                            </div>
                        </div>

                        {% if invoice.text_1 %}
                            <div class="well mt-3">
                                {{ invoice.text_1|markdown }}
                            </div>
                        {% endif %}

                        <table class="table table-hover mt-3 mb-0">
                            <thead>
                            <tr>
                                <th>{{ '#'|trans }}</th>
                                <th>{{ 'Title'|trans }}</th>
                                <th>{{ 'Price'|trans }}</th>
                                <th class="text-end">{{ 'Total'|trans }}</th>
                            </tr>
                            </thead>
                            <tbody>
                            {% for i, item in invoice.lines %}
                                <tr>
                                    <td>{{ i+1 }}</td>
                                    <td>
                                        {% if item.order_id %}
                                            <a href="{{ '/order/service'|link }}/manage/{{ item.order_id }}">{{ item.title }}</a>
                                        {% else %}
                                            {{ item.title }}
                                        {% endif %}
                                        {% if item.quantity > 1 %}
                                            x {{ item.quantity }} {{ item.unit }}
                                        {% endif %}
                                    </td>
                                    <td>{{ item.price|money(invoice.currency) }}</td>
                                    <td class="text-end">{{ item.total|money(invoice.currency) }}</td>
                                </tr>
                            {% endfor %}
                            </tbody>
                        </table>

                        <div class="row">
                            <div class="col-md-4 offset-md-8">
                                <table class="table table-striped">
                                    {% if invoice.tax > 0 %}
                                        <tr class="text-end">
                                            <td>{{ invoice.taxname }} {{ invoice.taxrate }}%</td>
                                            <td>{{ invoice.tax | money(invoice.currency) }}</td>
                                        </tr>
                                    {% endif %}
                                    {% if invoice.discount > 0 %}
                                        <tr class="text-end">
                                            <td>{{ 'Discount'|trans }}</td>
                                            <td>{{ invoice.discount | money(invoice.currency) }}</td>
                                        </tr>
                                    {% endif %}
                                    <tr class="text-end">
                                        <td><strong>{{ 'Total'|trans }}</strong></td>
                                        <td><strong>{{ invoice.total | money(invoice.currency) }}</strong></td>
                                    </tr>
                                </table>
                            </div>
                        </div>

                        {% if invoice.text_2 %}
                            <div class="well mt-3">
                                {{ invoice.text_2|markdown }}
                            </div>
                        {% endif %}
                    </div>
                </div>
            </div>
        </div>

        <div class="modal modal-lg fade" id="paymentPrompt" tabindex="-1" aria-labelledby="paymentPromptLabel" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <h4 class="modal-title" id="paymentPromptLabel"></h4>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <div class="modal-body">
                        <p id="bodyText"></p>
                        <a href="#" class="btn btn-primary" id="single">{{ "Pay now with a one-time payment"|trans }}</a>
                        <a href="#" class="btn btn-primary" id="subscription" hidden="true">{{ "Pay now and create a subscription"|trans }}</a>
                        <a href="#" class="btn btn-primary" id="pay-nondescript" hidden="true">{{ "Pay now"|trans }}</a>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-primary" data-bs-dismiss="modal">{{ 'Cancel'|trans }}</button>
                    </div>
                </div>
            </div>
        </div>
{% endblock %}
{% block js %}
<script>
    $(function() {
        $(".hover-popover").tooltip({
            placement: 'top'
        });
    });

    function paymentPrompt(id, supportsSubscription, title){
        invoiceHash = "{{ invoice.hash }}";
        backLink    = "{{ 'invoice/banklink'|link }}";
        paymentLink = new URL(backLink.concat('/', invoiceHash, '/', id));
        invoiceSubscribable = {{ invoice.subscribable ? 'true' : 'false' }};

        {% set anyGatewayDoesSubscriptions = false %}
        {% for gtw in guest.invoice_gateways %}
            {% if gtw.allow_recurrent %}
                {% set anyGatewayDoesSubscriptions = true %}
            {% endif %}
        {% endfor %}

        canAnyGatewayDoSubs = {{ anyGatewayDoesSubscriptions ? 'true' : 'false' }};

        {% if settings.prompt_subscription %}
            if(supportsSubscription && invoiceSubscribable){
                document.getElementById('bodyText').textContent = `{{ 'The invoice you are paying and your chosen payment gateway both support subscription payments. What type of payment would you like to continue with?'|trans }}`;
                document.getElementById('subscription').hidden = false;
            } else if (!invoiceSubscribable) {
                document.getElementById('bodyText').textContent = `{{ 'If you are happy with the selected payment gateway, please use the button below to continue with it.'|trans }}`;
                document.getElementById('subscription').hidden = true;
            } else {
                if(canAnyGatewayDoSubs){
                    document.getElementById('bodyText').textContent = `{{ 'If you are happy with the selected payment gateway, please use the button below to continue with it. Optionally, you may choose a different payment gateway if you wish to pay using a subscription.'|trans }}`;
                } else {
                    document.getElementById('bodyText').textContent = `{{ 'If you are happy with the selected payment gateway, please use the button below to continue with it.'|trans }}`;
                }
                document.getElementById('subscription').hidden = true;
            }
        {% else %}
            document.getElementById('bodyText').textContent = `{{ 'If you are happy with the selected payment gateway, please use the button below to continue with it.'|trans }}`;
            document.getElementById('subscription').hidden = true;
            document.getElementById('single').hidden = true;
            document.getElementById('pay-nondescript').hidden = false;
        {% endif %}

        document.getElementById('paymentPromptLabel').textContent = `{{ 'Pay with'|trans }}`.concat(` `, title);
        document.getElementById('subscription').href = paymentLink;
        document.getElementById('pay-nondescript').href = paymentLink;
        paymentLink.searchParams.append('allow_subscription', 0); 
        document.getElementById('single').href = paymentLink;

        modal = new bootstrap.Modal('#paymentPrompt');
        modal.show();
    }
</script>
{% endblock %}
