{% extends './layout/default' %}

{% block breadcrumbs %}
    {{ Breadcrumbs.render(Route.getCurrentRoute.getName, group, groupTitle) }}
{% endblock %}

{% block content %}
    <form method="POST" action="{{ route('transactions.convert.index.post', [destinationType.type|lower, group.id]) }}"
          accept-charset="UTF-8"
          class="form-horizontal"
          enctype="multipart/form-data">
        <input name="_token" type="hidden" value="{{ csrf_token() }}">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-12 col-sm-12">
                <div class="box box-primary">
                    <div class="box-header with-border">
                        <h3 class="box-title">{{ ('convert_options_'~sourceType.type~destinationType.type)|_ }}</h3>
                    </div>
                    <div class="box-body">
                        <p>
                            {# ONE: WITHDRAWAL TO DEPOSIT #}
                            {% if sourceType.type == 'Withdrawal' and destinationType.type == 'Deposit' %}
                                {{ trans_choice('firefly.convert_expl_w_d', groupArray.transactions|length) }}
                                {{ trans_choice('firefly.convert_select_sources', groupArray.transactions|length) }}
                            {% endif %}

                            {# TWO: WITHDRAWAL TO TRANSFER #}
                            {% if sourceType.type == 'Withdrawal' and destinationType.type == 'Transfer' %}
                                {{ trans_choice('firefly.convert_expl_w_t', groupArray.transactions|length) }}
                                {{ trans_choice('firefly.convert_select_destinations', groupArray.transactions|length) }}
                            {% endif %}

                            {# THREE: DEPOSIT TO WITHDRAWAL #}
                            {% if sourceType.type == 'Deposit' and destinationType.type == 'Withdrawal' %}
                                {{ trans_choice('firefly.convert_expl_d_w', groupArray.transactions|length) }}
                                {{ trans_choice('firefly.convert_select_destinations', groupArray.transactions|length) }}
                            {% endif %}

                            {# FOUR: DEPOSIT TO TRANSFER #}
                            {% if sourceType.type == 'Deposit' and destinationType.type == 'Transfer' %}
                                {{ trans_choice('firefly.convert_expl_d_t', groupArray.transactions|length) }}
                                {{ trans_choice('firefly.convert_select_sources', groupArray.transactions|length) }}
                            {% endif %}

                            {# FIVE: TRANSFER TO WITHDRAWAL #}
                            {% if sourceType.type == 'Transfer' and destinationType.type == 'Withdrawal' %}
                                {{ trans_choice('firefly.convert_expl_t_w', groupArray.transactions|length) }}
                                {{ trans_choice('firefly.convert_select_destinations', groupArray.transactions|length) }}
                            {% endif %}

                            {# SIX: TRANSFER TO DEPOSIT #}
                            {% if sourceType.type == 'Transfer' and destinationType.type == 'Deposit' %}
                                {{ trans_choice('firefly.convert_expl_t_d', groupArray.transactions|length) }}
                                {{ trans_choice('firefly.convert_select_sources', groupArray.transactions|length) }}
                            {% endif %}
                        </p>

                        <table class="table">
                            <tr>
                                <th class="ten">ID</th>
                                <th class="quarter">Description</th>
                                <th class="quarter">Source account</th>
                                <th class="quarter">Destination account</th>
                                <th class="fifteen">Amount</th>
                            </tr>
                            {% for transaction in groupArray.transactions %}
                                <tr>
                                    <td>#{{ transaction.transaction_journal_id }}</td>
                                    <td>{{ transaction.description }}</td>
                                    <td>
                                        {# ONE: WITHDRAWAL TO DEPOSIT #}
                                        {% if sourceType.type == 'Withdrawal' and destinationType.type == 'Deposit' %}
                                            {# NEW DESTINATION = Asset, SOURCE MUST BE [Revenue, Cash, Loan, Debt, Mortgage] #}
                                            {% if transaction.source_type == 'Asset account' %}
                                                {{ Html.select('source_id['~transaction.transaction_journal_id~']', validDepositSources).class('form-control') }}
                                            {% endif %}

                                            {# NEW DESTINATION = [Loan, Debt, Mortgage], SOURCE MUST BE [Revenue] #}
                                            {% if
                                                transaction.source_type == 'Loan' or
                                                transaction.source_type == 'Debt' or
                                                transaction.source_type == 'Mortgage' %}
                                                <input
                                                    autocomplete="off"
                                                    placeholder="Source account"
                                                    name="source_name[{{ transaction.transaction_journal_id }}]"
                                                    type="text"
                                                    value="{% if transaction.destination_type != "Cash account" %}{{ preFilled.source_name[transaction.transaction_journal_id]|default(transaction.destination_name) }}{% endif %}"
                                                    class="form-control tt-input input-revenue"
                                                    spellcheck="false" dir="auto">
                                            {% endif %}
                                        {% endif %}

                                        {# TWO: WITHDRAWAL TO TRANSFER #}
                                        {% if sourceType.type == 'Withdrawal' and destinationType.type == 'Transfer' %}

                                            <a href="{{ route('accounts.show', [transaction.source_id]) }}"
                                               title="{{ transaction.source_iban|default(transaction.source_name) }}">{{ transaction.source_name }}</a>
                                            {# hide source in hidden input #}
                                            <input type="hidden"
                                                   name="source_id[{{ transaction.transaction_journal_id }}]"
                                                   value="{{ transaction.source_id }}">
                                        {% endif %}

                                        {# THREE: DEPOSIT TO WITHDRAWAL #}
                                        {% if sourceType.type == 'Deposit' and destinationType.type == 'Withdrawal' %}
                                            <a href="{{ route('accounts.show', [transaction.destination_id]) }}"
                                               title="{{ transaction.destination_iban|default(transaction.destination_name) }}">{{ transaction.destination_name }}</a>

                                            {# hide new source in hidden input #}
                                            <input type="hidden"
                                                   name="source_id[{{ transaction.transaction_journal_id }}]"
                                                   value="{{ transaction.destination_id }}">
                                        {% endif %}

                                        {# FOUR: DEPOSIT TO TRANSFER #}
                                        {% if sourceType.type == 'Deposit' and destinationType.type == 'Transfer' %}
                                            {# if new destination is asset, then asset #}
                                            {% if transaction.destination_type == 'Asset account' %}
                                                {{ Html.select('source_id['~transaction.transaction_journal_id~']', assets).class('form-control') }}
                                            {% endif %}
                                            {% if transaction.destination_type == 'Loan' or
                                                transaction.destination_type == 'Debt' or
                                                transaction.destination_type == 'Mortgage' %}
                                                {{ Html.select('source_id['~transaction.transaction_journal_id~']', liabilities).class('form-control') }}
                                            {% endif %}

                                            {# if new destination liability, then liability. #}

                                        {% endif %}

                                        {# FIVE: TRANSFER TO WITHDRAWAL #}
                                        {% if sourceType.type == 'Transfer' and destinationType.type == 'Withdrawal' %}
                                            <a href="{{ route('accounts.show', [transaction.source_id]) }}"
                                               title="{{ transaction.source_iban|default(transaction.source_name) }}">{{ transaction.source_name }}</a>

                                            {# hide source in hidden input #}
                                            <input type="hidden"
                                                   name="source_id[{{ transaction.transaction_journal_id }}]"
                                                   value="{{ transaction.source_id }}">
                                        {% endif %}

                                        {# SIX: TRANSFER TO DEPOSIT #}
                                        {% if sourceType.type == 'Transfer' and destinationType.type == 'Deposit' %}
                                            {# NEW DESTINATION = Asset, SOURCE MUST BE [Revenue, Cash, Loan, Debt, Mortgage] #}
                                            {% if
                                                transaction.source_type == 'Asset account' %}
                                                {{ Html.select('source_id['~transaction.transaction_journal_id~']', validDepositSources).class('form-control') }}
                                            {% endif %}
                                            {# NEW DESTINATION = [Debt, Mortgage, Load], SOURCE MUST BE [Revenue] #}
                                            {% if
                                                transaction.source_type == 'Loan' or
                                                transaction.source_type == 'Debt' or
                                                transaction.source_type == 'Mortgage' %}
                                                <input
                                                    autocomplete="off"
                                                    placeholder="Source account"
                                                    name="source_name[{{ transaction.transaction_journal_id }}]"
                                                    type="text"
                                                    value="{% if transaction.destination_type != "Cash account" %}{{ transaction.source_name }}{% endif %}"
                                                    class="form-control tt-input"
                                                    spellcheck="false" dir="auto">
                                            {% endif %}

                                        {% endif %}

                                    </td>
                                    <td>
                                        {# ONE: WITHDRAWAL TO DEPOSIT #}
                                        {% if sourceType.type == 'Withdrawal' and destinationType.type == 'Deposit' %}
                                            <a href="{{ route('accounts.show', [transaction.source_id]) }}"
                                               title="{{ transaction.source_iban|default(transaction.source_name) }}">{{ transaction.source_name }}</a>

                                            {# hide destination in hidden input #}
                                            <input type="hidden"
                                                   name="destination_id[{{ transaction.transaction_journal_id }}]"
                                                   value="{{ transaction.source_id }}">
                                        {% endif %}

                                        {# TWO: WITHDRAWAL TO TRANSFER #}
                                        {% if sourceType.type == 'Withdrawal' and destinationType.type == 'Transfer' %}
                                            {# if the source is a liability, destination must also be a liability. #}
                                            {% if
                                                transaction.source_type == 'Loan' or
                                                transaction.source_type == 'Debt' or
                                                transaction.source_type == 'Mortgage' %}
                                                {{ Html.select('destination_id['~transaction.transaction_journal_id~']', liabilities).class('form-control') }}
                                            {% endif %}

                                            {# if the source is an asset, destination can only be an asset. #}
                                            {% if transaction.source_type == 'Asset account' %}
                                                {{ Html.select('destination_id['~transaction.transaction_journal_id~']', assets).class('form-control') }}
                                            {% endif %}
                                        {% endif %}

                                        {# THREE: DEPOSIT TO WITHDRAWAL #}
                                        {% if sourceType.type == 'Deposit' and destinationType.type == 'Withdrawal' %}

                                            {# if new source is Asset, destination must be [Expense, Loan, Debt or Mortgage] #}
                                            {% if transaction.destination_type == 'Asset account' %}
                                                {{ Html.select('destination_id['~transaction.transaction_journal_id~']', validWithdrawalDests).class('form-control') }}
                                            {% endif %}
                                            {% if transaction.destination_type == 'Loan' or
                                                transaction.destination_type == 'Debt' or
                                                transaction.destination_type == 'Mortgage' %}
                                                {# if new source is Liability, destination must be expense account. #}

                                                <input
                                                    autocomplete="off"
                                                    placeholder="Destination account"
                                                    name="destination_name[{{ transaction.transaction_journal_id }}]"
                                                    type="text"
                                                    value="{% if transaction.source_type != "Cash account" %}{{ transaction.source_name }}{% endif %}"
                                                    class="form-control tt-input"
                                                    spellcheck="false" dir="auto">
                                            {% endif %}
                                        {% endif %}

                                        {# FOUR: DEPOSIT TO TRANSFER #}
                                        {% if sourceType.type == 'Deposit' and destinationType.type == 'Transfer' %}
                                            <a href="{{ route('accounts.show', [transaction.destination_id]) }}"
                                               title="{{ transaction.destination_iban|default(transaction.destination_name) }}">{{ transaction.destination_name }}</a>

                                            {# hide destination in hidden input #}
                                            <input type="hidden"
                                                   name="destination_id[{{ transaction.transaction_journal_id }}]"
                                                   value="{{ transaction.destination_id }}">
                                        {% endif %}

                                        {# FIVE: TRANSFER TO WITHDRAWAL #}
                                        {% if sourceType.type == 'Transfer' and destinationType.type == 'Withdrawal' %}

                                            {% if transaction.source_type == 'Asset account' %}
                                                {{ Html.select('destination_id['~transaction.transaction_journal_id~']', validWithdrawalDests).class('form-control') }}
                                            {% endif %}
                                            {% if transaction.source_type == 'Loan' or
                                                transaction.source_type == 'Debt' or
                                                transaction.source_type == 'Mortgage' %}
                                                <input
                                                    autocomplete="off"
                                                    placeholder="Destination account"
                                                    name="destination_name[{{ transaction.transaction_journal_id }}]"
                                                    type="text"
                                                    value="{% if transaction.source_type != "Cash account" %}{{ transaction.destination_name }}{% endif %}"
                                                    class="form-control tt-input"
                                                    spellcheck="false" dir="auto">
                                            {% endif %}
                                        {% endif %}

                                        {# SIX: TRANSFER TO DEPOSIT #}
                                        {% if sourceType.type == 'Transfer' and destinationType.type == 'Deposit' %}
                                            <a href="{{ route('accounts.show', [transaction.destination_id]) }}"
                                               title="{{ transaction.destination_iban|default(transaction.destination_name) }}">{{ transaction.destination_name }}</a>

                                            {# hide destination in hidden input #}
                                            <input type="hidden"
                                                   name="destination_id[{{ transaction.transaction_journal_id }}]"
                                                   value="{{ transaction.destination_id }}">
                                        {% endif %}
                                    </td>
                                    <td>
                                        {% if transaction.transaction_type_type == 'Deposit' %}
                                            {{ formatAmountBySymbol(transaction.amount*-1, transaction.currency_symbol, transaction.currency_decimal_places) }}
                                            {% if null != transaction.foreign_amount %}
                                                ({{ formatAmountBySymbol(transaction.foreign_amount*-1, transaction.foreign_currency_symbol, transaction.foreign_currency_decimal_places) }})
                                            {% endif %}
                                        {% elseif transaction.transaction_type_type == 'Transfer' %}
                                            <span class="text-info money-transfer">{{ formatAmountBySymbol(transaction.amount*-1, transaction.currency_symbol, transaction.currency_decimal_places, false) }}
                                                {% if null != transaction.foreign_amount %}
                                                    ({{ formatAmountBySymbol(transaction.foreign_amount*-1, transaction.foreign_currency_symbol, transaction.foreign_currency_decimal_places, false) }})
                                                {% endif %}</span>
                                        {% else %}
                                            {{ formatAmountBySymbol(transaction.amount, transaction.currency_symbol, transaction.currency_decimal_places) }}
                                            {% if null != transaction.foreign_amount %}
                                                ({{ formatAmountBySymbol(transaction.foreign_amount, transaction.foreign_currency_symbol, transaction.foreign_currency_decimal_places) }})
                                            {% endif %}
                                        {% endif %}
                                    </td>
                                </tr>
                            {% endfor %}
                        </table>
                    </div>
                    <div class="box-footer">
                        <a href="{{ route('transactions.show', group.id) }}" class="btn btn-danger">{{ 'cancel'|_ }}</a>
                        <button type="submit" id="transaction-btn" class="btn btn-success pull-right">
                            {{ trans('form.convert_'~sourceType.type) }}
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </form>

{% endblock %}
{% block scripts %}
    <script type="text/javascript" src="v1/js/lib/typeahead/typeahead.bundle.min.js?v={{ FF_BUILD_TIME }}"
            nonce="{{ JS_NONCE }}"></script>
    <script type="text/javascript" src="v1/js/ff/transactions/convert.js?v={{ FF_BUILD_TIME }}"
            nonce="{{ JS_NONCE }}"></script>
{% endblock %}
