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

{% block breadcrumbs %}
    {{ Breadcrumbs.renderIfExists(Route.getCurrentRoute.getName, journal) }}
{% endblock %}
{% block content %}
    <form method="POST" action="{{ route('transactions.split.update',journal.id) }}" accept-charset="UTF-8" class="form-horizontal" id="update"
          enctype="multipart/form-data">

        <input name="_token" type="hidden" value="{{ csrf_token() }}">
        <input type="hidden" name="id" value="{{ journal.id }}"/>
        <input type="hidden" name="what" value="{{ preFilled.what }}"/>

        {% if errors.all()|length > 0 %}
            <div class="row">
                <div class="col-lg-12 col-md-12 col-sm-12">
                    <div class="box box-danger">
                        <div class="box-header with-border">
                            <h3 class="box-title">{{ 'errors'|_ }}</h3>
                        </div>
                        <div class="box-body">
                            <ul>
                                {% for key, err in errors.all() %}
                                    <li class="text-danger">{{ err }}</li>
                                {% endfor %}
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        {% endif %}
        <div class="row">
            <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
                <div class="box">
                    <div class="box-header with-border">
                        <h3 class="box-title">{{ 'transaction_data'|_ }}</h3>
                    </div>
                    <div class="box-body">

                        {# DESCRIPTION IS ALWAYS AVAILABLE #}
                        {{ ExpandedForm.text('journal_description', journal.description) }}

                        {# show source if withdrawal or transfer #}
                        {% if preFilled.what == 'withdrawal' or preFilled.what == 'transfer' %}
                            {{ ExpandedForm.select('journal_source_account_id', assetAccounts, preFilled.journal_source_account_id) }}
                        {% endif %}

                        {# show destination account id, if deposit (is asset): #}
                        {% if preFilled.what == 'deposit' %}
                            {{ ExpandedForm.select('journal_destination_account_id', assetAccounts, preFilled.journal_destination_account_id) }}
                        {% endif %}

                        {# show static destination if transfer #}
                        {% if preFilled.what == 'transfer' %}
                            {{ ExpandedForm.select('journal_destination_account_id', assetAccounts, preFilled.journal_destination_account_id) }}
                        {% endif %}

                        {# TOTAL AMOUNT IS STATIC TEXT #}
                        {# TODO this does not reflect the actual currency (currencies) #}
                        {{ ExpandedForm.staticText('journal_amount', preFilled.journal_amount|formatAmount ) }}
                        <input type="hidden" name="journal_amount" value="{{ preFilled.journal_amount }}"/>

                        {# DATE #}
                        {{ ExpandedForm.date('date', journal.date) }}
                    </div>
                </div>
            </div>
            <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
                <div class="box">
                    <div class="box-header with-border">
                        <h3 class="box-title">{{ 'optional_field_meta_data'|_ }}</h3>
                    </div>
                    <div class="box-body">
                        {# NO BUDGET #}
                        {# NO CATEGORY #}

                        {# ALWAYS TAGS #}
                        {{ ExpandedForm.text('tags', preFilled.tags) }}

                        {# NO PIGGY BANK #}
                    </div>
                </div>

                {# EXPLANATION IF NECESSARY: #}
                {% if
                not optionalFields.interest_date or
                not optionalFields.book_date or
                not optionalFields.process_date or
                not optionalFields.due_date or
                not optionalFields.payment_date or
                not optionalFields.invoice_date or
                not optionalFields.internal_reference or
                not optionalFields.notes or
                not optionalFields.attachments %}
                    <p class="text-center text-success"><i class="fa fa-info-circle" aria-hidden="true"></i>
                        <em>{{ trans('firefly.hidden_fields_preferences', {link: route('preferences.index')})|raw }}</em></p>
                {% endif %}

                {# BOX FOR DATES #}
                {% if
                optionalFields.interest_date or optionalFields.book_date or optionalFields.process_date
                or optionalFields.due_date or optionalFields.payment_date
                or optionalFields.invoice_date %}
                    <div class="box">
                        <div class="box-header with-border">
                            <h3 class="box-title">{{ 'optional_field_meta_dates'|_ }}</h3>
                        </div>
                        <div class="box-body">

                            {# INTEREST DATE #}
                            {% if optionalFields.interest_date or journal.interest_date %}
                                {{ ExpandedForm.date('interest_date', journal.interest_date) }}
                            {% endif %}

                            {# BOOK DATE #}
                            {% if optionalFields.book_date or journal.book_date %}
                                {{ ExpandedForm.date('book_date', journal.book_date) }}
                            {% endif %}

                            {# PROCESSING DATE #}
                            {% if optionalFields.process_date or journal.process_date %}
                                {{ ExpandedForm.date('process_date', journal.process_date) }}
                            {% endif %}

                            {# DUE DATE #}
                            {% if optionalFields.due_date or journal.due_date %}
                                {{ ExpandedForm.date('due_date', journal.due_date) }}
                            {% endif %}

                            {# PAYMENT DATE #}
                            {% if optionalFields.payment_date or journal.payment_date %}
                                {{ ExpandedForm.date('payment_date', journal.payment_date) }}
                            {% endif %}

                            {# INVOICE DATE #}
                            {% if optionalFields.invoice_date or journal.invoice_date %}
                                {{ ExpandedForm.date('invoice_date', journal.invoice_date) }}
                            {% endif %}
                        </div>
                    </div>
                {% endif %}

                {# BOX FOR BUSINESS FIELDS #}
                {% if optionalFields.internal_reference or optionalFields.notes %}
                    <div class="box">
                        <div class="box-header with-border">
                            <h3 class="box-title">{{ 'optional_field_meta_business'|_ }}</h3>
                        </div>
                        <div class="box-body">

                            {# INTERNAL REFERENCE #}
                            {% if optionalFields.internal_reference or journal.internal_reference %}
                                {{ ExpandedForm.text('internal_reference', journal.internal_reference) }}
                            {% endif %}

                            {# NOTES #}
                            {% if optionalFields.notes  or journal.notes %}
                                {{ ExpandedForm.textarea('notes', journal.notes) }}
                            {% endif %}
                        </div>
                    </div>
                {% endif %}

                {# BOX FOR ATTACHMENTS #}
                {% if optionalFields.attachments %}
                    <div class="box">
                        <div class="box-header with-border">
                            <h3 class="box-title">{{ 'optional_field_attachments'|_ }}</h3>
                        </div>
                        <div class="box-body">
                            {# ATTACHMENTS #}
                            {% if optionalFields.attachments %}
                                {{ ExpandedForm.file('attachments[]', {'multiple': 'multiple','helpText': trans('firefly.upload_max_file_size', {'size': uploadSize|filesize}) }) }}
                            {% endif %}
                        </div>
                    </div>
                {% endif %}

            </div>
        </div>
        <div class="row">
            <div class="col-lg-12 col-md-12 col-sm-12">
                <div class="box">
                    <div class="box-header with-border">
                        <h3 class="box-title">{{ 'splits'|_ }}</h3>
                    </div>
                    <div class="box-body">
                        <div class="container-fluid split_row_holder">
                            <div class="row bg-gray-light" style="padding-bottom:3px;">
                                <div class="col-lg-1 col-md-1 col-sm-6 col-xs-6">&nbsp;</div>
                                <div class="col-lg-1 col-md-1 col-sm-6 col-xs-6"><strong>{{ trans('list.split_number') }}</strong></div>
                                <div class="col-lg-3 col-md-5 col-sm-12 col-xs-12"><strong>{{ trans('list.description') }}</strong></div>
                                {# withdrawal and deposit have a destination. #}
                                {% if preFilled.what == 'withdrawal' %}
                                    <div class="col-lg-2 col-md-5 col-sm-12 col-xs-12"><strong>{{ trans('list.destination') }}</strong></div>
                                {% endif %}
                                {# Deposit has a source #}
                                {% if preFilled.what == 'deposit' %}
                                    <div class="col-lg-2 col-md-5 col-sm-12 col-xs-12"><strong>{{ trans('list.source') }}</strong></div>
                                {% endif %}
                                <div class="col-lg-1 col-md-5 col-sm-12 col-xs-12"><strong>{{ trans('list.amount') }}</strong></div>
                                {% if transaction.foreign_amount != null %}
                                    <div class="col-lg-1 col-md-7 col-sm-12 col-xs-12">&nbsp;</div>
                                {% endif %}
                                {# only withdrawal has budget #}
                                {% if preFilled.what == 'withdrawal' %}
                                    <div class="col-lg-1 col-md-6 col-sm-12 col-xs-12"><strong>{{ trans('list.budget') }}</strong></div>
                                {% endif %}
                                <div class="col-lg-2 col-md-6 col-sm-12 col-xs-12"><strong>{{ trans('list.category') }}</strong></div>
                            </div>

                        {% for index, transaction in preFilled.transactions %}
                            <div class="row {% if loop.index0 % 2 == 1 %}bg-gray-light{% endif %} split_row" data-split="{{ loop.index0 }}">
                                {# button #}
                                <div class="col-lg-1 col-md-1 col-sm-6 col-xs-6">
                                    <a href="#" class="btn btn-xs btn-danger remove-current-split" data-split="{{ loop.index0 }}">
                                        <i class="fa fa-trash" data-split="{{ loop.index0 }}"></i></a>
                                </div>

                                {# index #}
                                <div class="col-lg-1 col-md-1 col-sm-6 col-xs-6 count">#{{ loop.index }}</div>

                                {# description #}
                                <div class="col-lg-3 col-md-5 col-sm-12 col-xs-12">
                                    <input type="text" name="transactions[{{ loop.index0 }}][description]" value="{{ transaction.description }}"
                                           class="form-control"/>
                                </div>

                                {# destination for withdrawals: #}
                                {% if preFilled.what == 'withdrawal' %}
                                    <div class="col-lg-2 col-md-5 col-sm-12 col-xs-12">
                                        <input type="text" name="transactions[{{ loop.index0 }}][destination_account_name]"
                                               value="{{ transaction.destination_account_name }}" class="form-control"/>
                                    </div>
                                {% endif %}

                                {# source for deposits #}
                                {% if preFilled.what == 'deposit' %}
                                    <div class="col-lg-2 col-md-5 col-sm-12 col-xs-12">
                                        <input type="text" name="transactions[{{ loop.index0 }}][source_account_name]"
                                               value="{{ transaction.source_account_name }}" class="form-control"/>
                                    </div>
                                {% endif %}

                                {# amount#}
                                <div class="col-lg-1 col-md-5 col-sm-12 col-xs-12">
                                    <div class="input-group">
                                        <div class="input-group-addon">{{ transaction.transaction_currency_symbol }}</div>
                                        <input type="number" name="transactions[{{ loop.index0 }}][amount]" value="{{ transaction.amount }}"
                                               class="form-control" autocomplete="off" step="any" min="0.01">
                                    </div>
                                    <input type="hidden" name="transactions[{{ loop.index0 }}][transaction_currency_id]"
                                           value="{{ transaction.transaction_currency_id }}">
                                </div>

                                {# foreign amount #}
                                {% if transaction.foreign_amount != null %}
                                    <div class="col-lg-1 col-md-7 col-sm-12 col-xs-12">
                                        <div class="input-group">
                                            <div class="input-group-addon">{{ transaction.foreign_currency_symbol }}</div>
                                            <input type="number" name="transactions[{{ loop.index0 }}][foreign_amount]" value="{{ transaction.foreign_amount }}"
                                                   class="form-control" autocomplete="off" step="any" min="0.01">
                                        </div>
                                        <input type="hidden"
                                               name="transactions[{{ loop.index0 }}][foreign_currency_id]"
                                               value="{{ transaction.foreign_currency_id }}">
                                    </div>
                                {% endif %}

                                {# budget #}
                                {% if preFilled.what == 'withdrawal' %}
                                    <div class="col-lg-1 col-md-6 col-sm-12 col-xs-12">
                                        <select class="form-control" name="transactions[{{ loop.index0 }}][budget_id]">
                                            {% for key, budget in budgets %}
                                                <option label="{{ budget }}" value="{{ key }}"
                                                        {% if transaction.budget_id == key %} selected="selected"{% endif %}>{{ budget }}</option>
                                            {% endfor %}
                                        </select>
                                    </div>
                                {% endif %}

                                {# category #}
                                <div class="col-lg-2 col-md-6 col-sm-12 col-xs-12">
                                    <input type="text" name="transactions[{{ loop.index0 }}][category]" value="{{ transaction.category }}"
                                           class="form-control"/>
                                </div>
                            </div>
                        {% endfor %}
                        </div>
                        <p>
                            <br/>
                            <a href="#" class="btn btn-default btn-do-split"><i class="fa fa-plus-circle"></i> {{ 'add_another_split'|_ }}</a>
                        </p>
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
                <!-- panel for options -->
                <div class="box">
                    <div class="box-header with-border">
                        <h3 class="box-title">{{ 'options'|_ }}</h3>
                    </div>
                    <div class="box-body">
                        {{ ExpandedForm.optionsList('update','split-transaction') }}
                    </div>
                    <div class="box-footer">
                        <button type="submit" class="pull-right btn btn-success">{{ ('update_' ~ preFilled.what)|_ }}</button>
                    </div>
                </div>
            </div>
        </div>
    </form>

{% endblock %}
{% block styles %}
    <link href="css/bootstrap-tagsinput.css" type="text/css" rel="stylesheet" media="all">
    <link href="css/jquery-ui/jquery-ui.structure.min.css" type="text/css" rel="stylesheet" media="all">
    <link href="css/jquery-ui/jquery-ui.theme.min.css" type="text/css" rel="stylesheet" media="all">
{% endblock %}
{% block scripts %}
    <script type="text/javascript">
        var originalSum = {{ preFilled.journal_amount }};
        var what = "{{ preFilled.what }}";
    </script>
    <script type="text/javascript" src="js/lib/bootstrap3-typeahead.min.js"></script>
    <script type="text/javascript" src="js/lib/bootstrap-tagsinput.min.js"></script>
    <script type="text/javascript" src="js/lib/jquery-ui.min.js"></script>
    <script type="text/javascript" src="js/lib/modernizr-custom.js"></script>
    <script type="text/javascript" src="js/ff/transactions/split/edit.js"></script>
{% endblock %}
