{% extends request.ajax ? 'layout_blank.phtml' : 'layout_default.phtml' %}

{% import 'macro_functions.phtml' as mf %}

{% block meta_title %}{{ 'Proforma invoice'|trans }}{% endblock %}

{% set nr = invoice.serie ~ '%05s'|format(invoice.nr) %}
{% set seller = invoice.seller %}
{% set buyer = invoice.buyer %}
{% set company = guest.system_company %}

{% block content_before %}
    {% if invoice.status == 'unpaid' %}
        <h1>{{ 'Proforma Invoice'|trans }}</h1>
        <p class="text-secondary">{{ 'Please choose payment type and pay for your chosen products.'|trans }}</p>
        {% include '_order-steps.phtml' with { 'selected': 4 } %}
    {% else %}
        <h1>{{ 'Invoice'|trans }} {{ nr }}</h1>
        <small>{{ 'Invoice is paid'|trans }}</small>
        <ol class="breadcrumb">
            <li class="breadcrumb-item">
                <a href="{{ '/invoice'|link }}">{{ 'Invoices'|trans }}</a>
            </li>
            <li class="breadcrumb-item active">{{ 'Invoice #%05s'|format(invoice.id) }}</li>
        </ol>
    {% endif %}
{% endblock %}

{% block content %}
    {% if invoice.status == 'unpaid' %}
        <div class="alert alert-danger text-center">
            <div class="lead mb-3">{{ 'Unpaid'|trans }}</div>
            {% if invoice.gateway_id and not request.change_payment_method %}
                {% set payment = guest.invoice_payment({ 'gateway_id': invoice.gateway_id, 'hash': invoice.hash, 'auto_redirect': request.auto_redirect }) %}

                {% if payment.type == 'html' %}
                    {{ payment.result|replace({ 'bb-button': 'btn btn-warning' })|raw }}
                {% endif %}

                {% if payment.type == 'form' %}
                    <h2>{{ 'Redirecting to payment gateway...'|trans }}</h2>
                    <form name="payment_form" action="{{ payment.service_url }}" method="post">
                        {% for key, value in payment.result %}
                        <input type="hidden" name="{{ key }}" value="{{ value }}">
                        {% endfor %}
                        <input class="btn btn-warning" type="submit" value="{{ 'Please click here to continue if this page does not redirect automatically in 5 seconds'|trans }}" id="payment_button">
                    </form>

                    <script>
                        $(document).ready(function() {
                            document.getElementById('payment_button').style.display = 'none';
                            document.forms["payment_form"].submit();
                        });
                    </script>
                {% endif %}
            {% endif %}

            {% if request.change_payment_method %}
                <form method="post" action="{{ 'api/guest/invoice/update'|link }}" class="api-form" data-api-redirect="{{ ('invoice/' ~ invoice.hash)|link({ 'auto_redirect': 1 }) }}">
                    <div class="list-group mb-3">
                        {% for gtw in guest.invoice_gateways %}
                            {% if invoice.currency in gtw.accepted_currencies %}
                            <label class="list-group-item list-group-item">
                                <input type="radio" name="gateway_id" value="{{ gtw.id }}" {{ loop.first ? 'checked' : '' }}>
                                {{ 'Pay by'|trans }} {{ gtw.title }}
                                {% if gtw.title == 'Custom' %}
                                    <i class="fa fa-university fa-2x pull-right"></i>
                                {% elseif gtw.title == 'PayPal' %}
                                    <i class="fa fa-paypal fa-2x pull-right"></i>
                                {% elseif gtw.title == 'Stripe' %}
                                    <i class="fa fa-cc-stripe fa-2x pull-right"></i>
                                {% endif %}
                            </label>
                            {% endif %}
                        {% endfor %}
                    </div>
                    <input type="hidden" name="hash" value="{{ invoice.hash }}">
                    <div class="d-grid">
                        <button class="btn btn-warning" type="submit">
                            <i class="fa fa-credit-card me-2"></i>{{ 'Pay now'|trans }} <strong>{{ invoice.total|money(invoice.currency) }}</strong>
                        </button>
                    </div>
                </form>
            {% endif %}
        </div>
    {% endif %}
    
    <div class="row mb-3">
        <div class="col-md-6">
            {% if company.logo_url %}
                <img src="{{ company.logo_url }}" alt="{{ company.name }}" style="max-width: 100%; max-height: 100%">
            {% else %}
                <img src=" {{ 'images/logo.png'|asset_url }}" alt="{{ company.name }}" style="max-width: 100%; max-height: 100%">
            {% endif %}
        </div>
        <div class="col-md-6 text-end">
            <h2>{{ 'Invoice'|trans }} {{ invoice.serie_nr }}</h2>
            <p>
                {% if invoice.paid_at %}
                    {{ invoice.paid_at|date('Y-m-d h:i') }}
                {% else %}
                    {{ invoice.created_at|date('Y-m-d h:i') }}
                {% endif %}
            </p>
            <h2 class="invoice_{{ invoice.status }}">
                {% if invoice.status == 'unpaid' %}
                    {{ 'Unpaid'|trans }}
                {% else %}
                    {{ 'Paid'|trans }}
                {% endif %}
            </h2>
        </div>
        <div class="col-md-12">
            <hr>
        </div>
        <div class="col-md-6">
            <address>
                {% if seller.company %}<strong>{{ seller.company }}</strong><br>{% endif %}
                {% if seller.address %}{{ seller.address }}<br>{% endif %}
                {% if seller.phone %}{{ seller.phone }}<br>{% endif %}
                {% if seller.email %}{{ seller.email }}<br>{% endif %}					
                {% if company.account_number %}{{ 'Account'|trans }}: {{ company.account_number }}<br>{% endif %}
                {% if company.number %}{{ 'Company number'|trans }}: {{ company.number }}<br>{% endif %}
                {% if company.vat_number %}{{ 'VAT'|trans }}: {{ company.vat_number }}<br>{% endif %}
                {# Company note #}{# if company.note %}{{ company.note }}<br>{% endif #}
            </address>
        </div>
        <div class="col-md-6 text-end">
            <address>
                {% if buyer.company %}<strong>{{ buyer.company }}</strong><br>{% endif %}
                {% if buyer.first_name or buyer.last_name %}<strong>{{ buyer.first_name }} {{ buyer.last_name }}</strong><br>{% endif %}
                {% if buyer.email %}{{ buyer.email }}<br>{% endif %}
                {% if buyer.address %}{{ buyer.address }} {{ buyer.city }} {{ buyer.state }} {{ buyer.postcode }} {{ buyer.country }}<br>{% endif %}
                {% if buyer.phone %}{{ buyer.phone }}<br>{% endif %}
            </address>
        </div>
        
        {% if invoice.text_1 %}
        <div class="col-md-12">
            {{ invoice.text_1|markdown }}
        </div>
        {% endif %}

        <div class="col-md-12">
            <div class="panel-heading">{{ 'Invoice'|trans }}</div>
            <div class="panel-body">
                <table class="table table-striped table-hover">
                    <thead>
                        <tr>
                            <th>#</th>
                            <th>{{ 'Title'|trans }}</th>
                            <th class="text-center currency">{{ 'Price'|trans }}</th>
                            <th class="text-end currency">{{ 'Amount'|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 %}
                            </td>
                            <td class="text-center currency">
                                {{ item.price|money(invoice.currency) }}
                                {% if item.quantity > 1 %}
                                x {{ item.quantity }} {# item.unit #}
                                {% endif %}
                            </td>
                            <td class="text-end currency">{{ item.total|money(invoice.currency) }}</td>
                        </tr>
                        {% endfor %}
                    </tbody>

                    <tfoot>
                        {% if invoice.tax > 0 %}
                        <tr>
                            <th colspan="3">{{ invoice.taxname }} {{ invoice.taxrate }}%</th>
                            <th class="currency">{{ invoice.tax|money(invoice.currency) }}</th>
                        </tr>
                        {% endif %}
                    
                        {% if invoice.discount > 0 %}
                        <tr>
                            <th colspan="3">{{ 'Discount'|trans }}</th>
                            <th class="currency">{{ invoice.discount|money(invoice.currency) }}</th>
                        </tr>
                        {% endif %}
                        <tr class="total">
                            <th colspan="3" class="text-end">{{ 'Total'|trans }}</th>
                            <th class="text-end currency">{{ invoice.total|money(invoice.currency) }}</th>
                        </tr>
                    </tfoot>
                </table>
            </div>
        </div>
        
        {% if invoice.text_2 %}
            <div class="col-md-12">
                {{ invoice.text_2|markdown }}
            </div>
        {% endif %}
    </div>

    <div class="mb-3 text-center">
        {% if invoice.status == 'unpaid' %}
            <a class="btn btn-primary" href="{{ ('invoice/'~invoice.hash)|link({ 'change_payment_method': 1 }) }}">
                <i class="fa fa-credit-card me-2"></i>{{ 'Change payment method'|trans }}</a>
        {% endif %}
        <a class="btn btn-info" href="{{ 'invoice/pdf'|link }}/{{ invoice.hash }}">
            <i class="fa fa-download me-2"></i>{{ 'Download PDF'|trans }}</a>
        <a class="btn btn-info" href="{{ 'invoice/print'|link }}/{{ invoice.hash }}">
            <i class="fa fa-print me-2"></i> {{ 'Print'|trans }}</a>
    </div>

    <div class="text-center">
        <a class="btn btn-default" href="{{ '/invoice'|link }}">
            <i class="fa fa-arrow-left me-2"></i>{{ 'List of invoices'|trans }}</a>
    </div>
    {% set transactions = client.invoice_transaction_get_list({ 'invoice_id': invoice.id, 'type': 'payment', 'per_page': 100 }).list %}

    {% if transactions|length > 0 %}
        <div class="well">
            <h2>{{ 'Transactions'|trans }}</h2>
            <table class="table table-striped table-hover">
                <thead>
                    <tr>
                        <th>{{ 'Transaction Date'|trans }}</th>
                        <th>{{ 'Transaction ID'|trans }}</th>
                        <th>{{ 'Gateway'|trans }}</th>
                        <th>{{ 'Amount'|trans }}</th>
                    </tr>
                </thead>

                <tbody>
                    {% for i, tx in transactions %}
                    <tr class="{{ cycle(['odd', 'even'], i) }}">
                        <td>{{ tx.created_at|date('Y-m-d h:i') }}</td>
                        <td>{{ tx.txn_id }}</td>
                        <td>{{ tx.gateway|default('-') }}</td>
                        <td>{{ tx.amount|money(tx.currency) }}</td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    {% endif %}
{% endblock %}
