{% import 'macro_functions.html.twig' as mf %}
{% set nr = invoice.serie ~ "%05s"|format(invoice.nr) %}
{% set seller = invoice.seller %}
{% set buyer = invoice.buyer %}
{% set company = guest.system_company %}

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <link rel="stylesheet" href="{{ 'build/css/vendor.css' | asset_url }}">
    <link rel="stylesheet" href="{{ 'build/css/huraga.css' | asset_url }}">
    <link rel="shortcut icon" href="{{ guest.system_company.favicon_url }}">
</head>
<body>
<div class="container mt-5">
    <div class="row">
        <div class="col-md-10 offset-md-1">
            <div class="text-center">
                <h2 id="invoice-title">{{ 'Invoice'|trans }} {{ nr }}</h2>
            </div>

            <div class="row">
                <div class="col-md-4">
                    <div class="card">
                        <div class="card-body">
                            {% if company.logo_url %}
                                <img src="{{ company.logo_url }}" alt="Logo" class="img-fluid">
                            {% endif %}
                            <dl class="row">
                                <dt class="col-sm-6">{{ 'Invoice Number'|trans }}:</dt>
                                <dd class="col-sm-6">{{ nr }}</dd>
                                <dt class="col-sm-6">{{ 'Invoice Date'|trans }}:</dt>
                                <dd class="col-sm-6">
                                    {% if invoice.created_at %}
                                        {{ invoice.created_at|format_date }}
                                    {% else %}
                                        -----
                                    {% endif %}
                                </dd>
                                <dt class="col-sm-6">{{ 'Due Date'|trans }}:</dt>
                                <dd class="col-sm-6">
                                    {% if invoice.due_at %}
                                        {{ invoice.due_at|format_date }}
                                    {% else %}
                                        -----
                                    {% endif %}
                                </dd>
                                <dt class="col-sm-6">{{ 'Invoice Status'|trans }}:</dt>
                                <dd class="col-sm-6">
                                    {{ mf.status_badge(invoice.status, 'invoice') }}
                                </dd>
                            </dl>
                        </div>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="card">
                        <div class="card-body">
                            <h4>{{ 'Company'|trans }}</h4>
                            <dl class="row">
                                {% if seller.company %}
                                    <dt class="col-sm-6">{{ 'Name'|trans }}:</dt>
                                    <dd class="col-sm-6">{{ seller.company }}</dd>
                                {% endif %}
                                {% if seller.company_vat %}
                                    <dt class="col-sm-6">{{ 'VAT'|trans }}:</dt>
                                    <dd class="col-sm-6">{{ seller.company_vat }}</dd>
                                {% endif %}
                                {% if seller.address %}
                                    <dt class="col-sm-6">{{ 'Address'|trans }}:</dt>
                                    <dd class="col-sm-6">{{ seller.address }}</dd>
                                {% endif %}
                                {% if seller.phone %}
                                    <dt class="col-sm-6">{{ 'Phone'|trans }}:</dt>
                                    <dd class="col-sm-6">{{ seller.phone }}</dd>
                                {% endif %}
                                {% if seller.email %}
                                    <dt class="col-sm-6">{{ 'Email'|trans }}:</dt>
                                    <dd class="col-sm-6">{{ seller.email }}</dd>
                                {% endif %}
                                {% if seller.bank_name %}
                                    <dt class="col-sm-6">{{ 'Bank Name'|trans }}:</dt>
                                    <dd class="col-sm-6">{{ seller.bank_name }}</dd>
                                {% endif %}
                                {% if seller.bic %}
                                    <dt class="col-sm-6">{{ 'BIC / SWIFT Code'|trans }}:</dt>
                                    <dd class="col-sm-6">{{ seller.bic }}</dd>
                                {% endif %}
                                {% if seller.account_number %}
                                    <dt class="col-sm-6">{{ 'Bank Account'|trans }}:</dt>
                                    <dd class="col-sm-6">{{ seller.account_number }}</dd>
                                {% endif %}
                                {% if seller.note %}
                                    <dt class="col-sm-6">{{ 'Note'|trans }}:</dt>
                                    <dd class="col-sm-6">{{ seller.note }}</dd>
                                {% endif %}
                            </dl>
                        </div>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="card">
                        <div class="card-body">
                            <h4>{{ 'Billing & Delivery Address'|trans }}</h4>
                            <dl class="row">
                                {% if buyer.first_name or buyer.last_name %}
                                    <dt class="col-sm-6">{{ 'Name'|trans }}:</dt>
                                    <dd class="col-sm-6">{{ buyer.first_name }} {{ buyer.last_name }}</dd>
                                {% endif %}
                                {% if buyer.company %}
                                    <dt class="col-sm-6">{{ 'Company'|trans }}:</dt>
                                    <dd class="col-sm-6">{{ buyer.company }}</dd>
                                {% endif %}
                                {% if buyer.company_number %}
                                    <dt class="col-sm-6">{{ 'Company Number'|trans }}:</dt>
                                    <dd class="col-sm-6">{{ buyer.company_number }}</dd>
                                {% endif %}
                                {% if buyer.company_vat %}
                                    <dt class="col-sm-6">{{ 'Company VAT'|trans }}:</dt>
                                    <dd class="col-sm-6">{{ buyer.company_vat }}</dd>
                                {% endif %}
                                {% if buyer.address %}
                                    <dt class="col-sm-6">{{ 'Address'|trans }}:</dt>
                                    <dd class="col-sm-6">
                                        {{ buyer.address }}
                                        {% if buyer.city or buyer.state %}
                                            <br>
                                            {% if buyer.city %}{{ buyer.city }}{% endif %}
                                            {% if buyer.city and buyer.state %}, {% endif %}
                                            {% if buyer.state %}{{ buyer.state }}{% endif %}
                                        {% endif %}
                                        {% if buyer.zip or buyer.country %}
                                            <br>
                                            {% if buyer.zip %}{{ buyer.zip }}{% endif %}
                                            {% if buyer.zip and buyer.country %}, {% endif %}
                                            {% if buyer.country %}{{ buyer.country|country_name }}{% endif %}
                                        {% endif %}
                                    </dd>
                                {% endif %}
                                {% if buyer.phone %}
                                    <dt class="col-sm-6">{{ 'Phone'|trans }}:</dt>
                                    <dd class="col-sm-6">{{ buyer.phone }}</dd>
                                {% endif %}
                            </dl>
                        </div>
                    </div>
                </div>
            </div>

            {% if invoice.text_1 %}
                <div class="card mt-3">
                    <div class="card-body">
                        {{ (invoice.text_1 ?? '')|markdown_to_html }}
                    </div>
                </div>
            {% endif %}

            <table class="table mt-3">
                <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>{{ item.title }}</td>
                        <td>
                            {{ item.price|format_currency(invoice.currency ?? default_currency) }}
                            {% if item.quantity > 1 %}
                                x {{ item.quantity }}{% if item.unit is defined and item.unit %} {{ item.unit }}{% endif %}
                            {% endif %}
                        </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">
                        {% 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="card mt-3">
                    <div class="card-body">
                        {{ (invoice.text_2 ?? '')|markdown_to_html }}
                    </div>
                </div>
            {% endif %}
        </div>
    </div>
</div>
</body>
</html>
