{% extends request.ajax ? "layout_blank.html.twig" : "layout_default.html.twig" %}
{% set active_menu = 'invoices' %}

{% 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'|url }}">{{ '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="{{ 'invoice/payment'|api_url(role: 'guest') }}"
                        {{ fb_api_form({ redirect: ("invoice/#{invoice.hash}")|url(query: { 'auto_redirect': 1 }) }) }}>
                        <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 and (gtw.allow_single or (invoice.subscribable and gtw.allow_recurrent)) %}
                                {% set banklink = 'invoice/banklink'|url %}
                                <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.allow_single ? '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/#{invoice.hash}"|url }}' target="_blank" class="btn btn-sm btn-danger">{{ 'PDF'|trans }}</a>
                                <a href='{{ "invoice/print/#{invoice.hash}"|url }}' 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">
                                        {{ mf.status_badge(invoice.status, 'invoice') }}
                                        </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 }}, {{ buyer.country|country_name }}</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_to_html }}
                            </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/manage/#{item.order_id}"|url }}'>{{ item.title }}</a>
                                        {% else %}
                                            {{ item.title }}
                                        {% endif %}
                                        {% if item.quantity > 1 %}
                                            x {{ item.quantity }} {{ item.unit }}
                                        {% endif %}
                                    </td>
                                    <td>{{ item.price|format_currency(invoice.currency ?? default_currency) }}</td>
                                    <td class="text-end">{{ item.total|format_currency(invoice.currency ?? default_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|format_currency(invoice.currency ?? default_currency) }}</td>
                                        </tr>
                                    {% endif %}
                                    {% if invoice.discount > 0 %}
                                        <tr class="text-end">
                                            <td>{{ 'Discount'|trans }}</td>
                                            <td>{{ invoice.discount|format_currency(invoice.currency ?? default_currency) }}</td>
                                        </tr>
                                    {% endif %}
                                    <tr class="text-end">
                                        <td><strong>{{ 'Total'|trans }}</strong></td>
                                        <td><strong>{{ invoice.total|format_currency(invoice.currency ?? default_currency) }}</strong></td>
                                    </tr>
                                </table>
                            </div>
                        </div>

                        {% if invoice.text_2 %}
                            <div class="well mt-3">
                                {{ (invoice.text_2 ?? '')|markdown_to_html }}
                            </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'|trans }}"></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>
    document.addEventListener("DOMContentLoaded", function() {
        document.querySelectorAll('.hover-popover').forEach(element => {
            const tooltip = new bootstrap.Tooltip(element, {
                placement: 'top'
            });
        });
    });

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

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

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

        const singleBtn = document.getElementById('single');
        const subscriptionBtn = document.getElementById('subscription');
        const genericBtn = document.getElementById('pay-nondescript');
        singleBtn.hidden = true;
        subscriptionBtn.hidden = true;
        genericBtn.hidden = true;

        {% if settings.prompt_subscription %}
            if(supportsSubscription && supportsOneTime && 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 }}`;
                subscriptionBtn.hidden = false;
                singleBtn.hidden = false;
            } else 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 }}`;
                subscriptionBtn.hidden = false;
            } else if (supportsOneTime) {
                if(invoiceSubscribable && 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 }}`;
                }
                singleBtn.hidden = false;
            } else {
                document.getElementById('bodyText').textContent = `{{ 'This payment gateway does not support any of the payment methods available for this invoice. Please choose a different payment gateway.'|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 }}`;
            genericBtn.hidden = false;
        {% endif %}

        document.getElementById('paymentPromptLabel').textContent = `{{ 'Pay with'|trans }}`.concat(` `, title);
        subscriptionBtn.href = paymentLink;
        genericBtn.href = paymentLink;
        const singlePaymentLink = new URL(paymentLink);
        singlePaymentLink.searchParams.append('allow_subscription', 0);
        singleBtn.href = singlePaymentLink;

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