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

{% block breadcrumbs %}
    {{ Breadcrumbs.renderIfExists(Route.getCurrentRoute.getName, what) }}
{% endblock %}

{% block content %}
    <form method="POST" action="{{ route('transactions.store',what) }}" accept-charset="UTF-8" class="form-horizontal" id="store" enctype="multipart/form-data">
        <input name="_token" type="hidden" value="{{ csrf_token() }}">
        <input type="hidden" name="what" value="{{ what }}"/>

        <div class="row">
            <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
                <div class="box box-primary">
                    <div class="box-header with-border">
                        <h3 class="box-title">{{ 'mandatoryFields'|_ }}</h3>
                    </div>
                    <div class="box-body">
                        <div class="form-group">
                            <label class="col-sm-4 control-label">Quickswitch</label>

                            <div class="col-sm-8">
                                <div class="btn-group btn-group-justified">
                                    <a href="#" data-what="withdrawal" class="switch btn btn-default"> {{ 'withdrawal'|_ }}</a>
                                    <a href="#" data-what="deposit" class="switch btn btn-default"> {{ 'deposit'|_ }}</a>
                                    <a href="#" data-what="transfer" class="switch btn btn-default">{{ 'transfer'|_ }}</a>
                                </div>
                            </div>
                        </div>


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

                        {# SELECTABLE SOURCE ACCOUNT ONLY FOR WITHDRAWALS AND TRANSFERS #}
                        {{ ExpandedForm.select('source_account_id', assetAccounts, null, {label: trans('form.asset_source_account')}) }}

                        {# FREE FORMAT SOURCE ACCOUNT ONLY FOR DEPOSITS #}
                        {{ ExpandedForm.text('source_account_name', null, {label: trans('form.revenue_account')}) }}

                        {# FREE FORMAT DESTINATION ACCOUNT ONLY FOR EXPENSES #}
                        {{ ExpandedForm.text('destination_account_name', null, {label: trans('form.expense_account')}) }}

                        {# SELECTABLE DESTINATION ACCOUNT ONLY FOR TRANSFERS AND DEPOSITS #}
                        {{ ExpandedForm.select('destination_account_id', assetAccounts, null, {label: trans('form.asset_destination_account')} ) }}

                        {# ALWAYS SHOW AMOUNT #}
                        {{ ExpandedForm.amount('amount') }}

                        {# INSTRUCTIONS FOR EXCHANGE RATES #}
                        {{ ExpandedForm.staticText('exchange_rate_instruction','(here be text)') }}

                        {{ ExpandedForm.nonSelectableAmount('native_amount') }}

                        {{ ExpandedForm.nonSelectableAmount('source_amount') }}

                        {{ ExpandedForm.nonSelectableAmount('destination_amount') }}

                        {# ALWAYS SHOW DATE #}
                        {{ ExpandedForm.date('date', preFilled.date|default(phpdate('Y-m-d'))) }}
                    </div>
                    <div class="box-footer">
                        <button type="submit" id="transaction-btn" class="btn btn-success pull-right">
                            {{ trans('form.store_new_'~what) }}
                        </button>
                    </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">
                        {# BUDGET ONLY WHEN CREATING A WITHDRAWAL #}
                        {% if budgets|length > 1 %}
                            {{ ExpandedForm.select('budget_id', budgets, null) }}
                        {% else %}
                            {{ ExpandedForm.select('budget_id', budgets, null, {helpText: trans('firefly.no_budget_pointer')}) }}
                        {% endif %}

                        {# CATEGORY ALWAYS #}
                        {{ ExpandedForm.text('category') }}

                        {# TAGS #}
                        {{ ExpandedForm.text('tags') }}

                        {# RELATE THIS TRANSFER TO A PIGGY BANK #}
                        {{ ExpandedForm.select('piggy_bank_id', piggies, '0') }}

                    </div>
                </div>

                {# explain 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 %}
                                {{ ExpandedForm.date('interest_date') }}
                            {% endif %}

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

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

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

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

                            {# INVOICE DATE #}
                            {% if optionalFields.invoice_date %}
                                {{ ExpandedForm.date('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">

                            {# REFERENCE #}
                            {% if optionalFields.internal_reference %}
                                {{ ExpandedForm.text('internal_reference') }}
                            {% endif %}

                            {# NOTES #}
                            {% if optionalFields.notes %}
                                {{ ExpandedForm.textarea('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 %}

                {# 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('create','transaction') }}
                    </div>
                    <div class="box-footer">
                        <button type="submit" id="transaction-btn" class="btn btn-success pull-right">
                            {{ trans('form.store_new_'~what) }}
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </form>

{% endblock %}
{% block scripts %}
    <script type="text/javascript">
        var what = "{{ what }}";
        var piggiesLength = {{ piggies|length }};

        // some titles and names:
        var txt = [];
        var title = [];
        var breadcrumbs = [];
        var middleCrumbName = [];
        var middleCrumbUrl = [];
        var button = [];
        var exchangeRateInstructions = "{{ 'exchange_rate_instructions'|_|escape('js') }}";
        var transferInstructions = "{{ 'transfer_exchange_rate_instructions'|_|escape('js') }}";

        {% for type in {0:'withdrawal',1:'deposit',2:'transfer'} %}

        txt['{{ type }}'] = '{{ type|_ }}';
        title['{{ type }}'] = '{{ trans('form.add_new_' ~ type) }}';
        breadcrumbs['{{ type }}'] = '{{ trans('breadcrumbs.create_' ~ type) }}';
        middleCrumbName['{{ type }}'] = '{{ trans('breadcrumbs.' ~ type ~ '_list') }}';
        middleCrumbUrl['{{ type }}'] = '{{ route('transactions.index', type) }}';
        button['{{ type }}'] = '{{ trans('form.store_new_' ~ type) }}';
        {% endfor %}

        // some code for the foreign amount logic:
        var useAccountCurrency = {% if preFilled.amount_currency_id_amount > 0 %}false{% else %}true{% endif %};
        var overruleCurrency = {{ preFilled.amount_currency_id_amount|default(0) }};

    </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/modernizr-custom.js"></script>
    <script type="text/javascript" src="js/lib/jquery-ui.min.js"></script>
    <script type="text/javascript" src="javascript/accounts?ext=.js"></script>
    <script type="text/javascript" src="javascript/currencies?ext=.js"></script>
    <script type="text/javascript" src="js/ff/transactions/single/common.js"></script>
    <script type="text/javascript" src="js/ff/transactions/single/create.js"></script>
{% 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 %}
