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

{% block breadcrumbs %}
    {{ Breadcrumbs.render(Route.getCurrentRoute.getName, budget, budgetLimit) }}
{% endblock %}

{% block content %}
    <div class="row">
        <div class="{% if attachments.count() == 0 %}col-lg-12 col-md-12 col-sm-12 col-xs-12{% else %}col-lg-8 col-md-6 col-sm-12 col-xs-12{% endif %}">
            <div class="box">
                <div class="box-header with-border">
                    <h3 class="box-title">
                        {% if budgetLimit %}
                            {{ trans('firefly.chart_budget_in_period', {name: budget.name, start: budgetLimit.start_date.isoFormat(monthAndDayFormat), end: budgetLimit.end_date.isoFormat(monthAndDayFormat),currency: budgetLimit.transactionCurrency.name }) }}
                        {% else %}
                            {{ trans('firefly.chart_all_journals_for_budget', {name:budget.name}) }}
                        {% endif %}
                    </h3>
                    <div class="box-tools pull-right">
                        <div class="btn-group">
                            <button class="btn btn-box-tool dropdown-toggle" data-toggle="dropdown"><span class="fa fa-ellipsis-v"></span></button>
                            <ul class="dropdown-menu" role="menu">
                                <li><a href="{{ route('budgets.edit',budget.id) }}"><span class="fa fa-pencil fa-fw"></span> {{ 'edit'|_ }}</a></li>
                                <li><a href="{{ route('budgets.delete',budget.id) }}"><span class="fa fa-trash fa-fw"></span> {{ 'delete'|_ }}</a></li>
                            </ul>
                        </div>
                    </div>
                </div>
                <div class="box-body">
                    <canvas id="budgetOverview" class="wide-chart" height="400" width="100%"></canvas>
                </div>
                {% if budgetLimit %}
                    <div class="box-footer">
                        <p class="text-muted">
                            {{ trans('firefly.chart_budget_in_period_only_currency', {currency: budgetLimit.transactionCurrency.name}) }}
                        </p>
                    </div>
                {% endif %}
            </div>
        </div>
        {% if attachments.count() > 0 %}
            <div class="col-lg-4 col-md-6 col-sm-12 col-xs-12">
                <div class="box">
                    <div class="box-header with-border">
                        <h3 class="box-title">
                            {{ 'attachments'|_ }}
                        </h3>
                    </div>
                    <div class="box-body no-padding">
                        {% include 'list.attachments' %}
                    </div>
                </div>
            </div>
        {% endif %}
    </div>
    <div class="row">
        <div class="col-lg-4 col-md-6 col-sm-12 col-xs-12">
            <div class="box">
                <div class="box-header with-border">
                    <h3 class="box-title">{{ 'expenses_by_category'|_ }}</h3>
                </div>
                <div class="box-body">
                    <div class="center-chart">
                        <canvas id="budget-cat-out" class="medium-chart" height="250"></canvas>
                    </div>
                </div>
                {% if budgetLimit %}
                    <div class="box-footer">
                        <p class="text-muted">
                            {{ trans('firefly.chart_budget_in_period_only_currency', {currency: budgetLimit.transactionCurrency.name}) }}
                        </p>
                    </div>
                {% endif %}
            </div>
        </div>
        <div class="col-lg-4 col-md-6 col-sm-12 col-xs-12">
            <div class="box">
                <div class="box-header with-border">
                    <h3 class="box-title">{{ 'expenses_by_asset_account'|_ }}</h3>
                </div>
                <div class="box-body">
                    <div class="center-chart">
                        <canvas id="budget-asset-out" class="medium-chart" height="250"></canvas>
                    </div>
                </div>
                {% if budgetLimit %}
                    <div class="box-footer">
                        <p class="text-muted">
                            {{ trans('firefly.chart_budget_in_period_only_currency', {currency: budgetLimit.transactionCurrency.name}) }}
                        </p>
                    </div>
                {% endif %}
            </div>
        </div>
        <div class="col-lg-4 col-md-6 col-sm-12 col-xs-12">
            <div class="box">
                <div class="box-header with-border">
                    <h3 class="box-title">{{ 'expenses_by_expense_account'|_ }}</h3>
                </div>
                <div class="box-body">
                    <div class="center-chart">
                        <canvas id="budget-expense-out" class="medium-chart" height="250"></canvas>
                    </div>
                </div>
                {% if budgetLimit %}
                    <div class="box-footer">
                        <p class="text-muted">
                            {{ trans('firefly.chart_budget_in_period_only_currency', {currency: budgetLimit.transactionCurrency.name}) }}
                        </p>
                    </div>
                {% endif %}
            </div>
        </div>
    </div>
    {% if limits|length > 0 %}
        <div class="row">
            <div class="col-lg-offset-9 col-lg-3 col-md-offset-9 col-md-3 col-sm-12 col-xs-12">
                <p class="small text-center"><a href="{{ route('budgets.show',budget.id) }}">{{ 'showEverything'|_ }}</a></p>
            </div>
        </div>
    {% endif %}

    <div class="row">
        <div class="{% if limits|length > 0 %}col-lg-9 col-md-9 col-sm-12 col-xs-12{% else %}col-lg-12 col-md-12 col-sm-12 col-xs-12{% endif %}">

            <div class="box">
                <div class="box-header with-border">
                    <h3 class="box-title">{{ 'transactions'|_ }}</h3>
                </div>
                <div class="box-body">
                    {% include 'list.groups' %}
                    {% if budgetLimit %}
                        <p>
                            <span class="fa fa-calendar"></span>
                            <a href="{{ route('budgets.show', [budget.id]) }}">
                                {{ 'show_all_no_filter'|_ }}
                            </a>
                        </p>
                    {% endif %}
                </div>
            </div>
        </div>
        {% if limits|length > 0 %}
            <div class="col-lg-3 col-md-3 col-sm-12 col-xs-12">
                {% for limit in limits %}
                    <div class="box {% if limit.start_date == budgetLimit.start_date %}box-primary box-solid{% endif %}">
                        <div class="box-header with-border">
                            <h3 class="box-title"><a
                                    href="{{ route('budgets.show.limit',[budget.id,limit.id]) }}">
                                    {{ limit.start_date.isoFormat(monthAndDayFormat) }} &mdash;
                                    {{ limit.end_date.isoFormat(monthAndDayFormat) }}
                                </a>
                            </h3>
                        </div>
                        <div class="box-body no-padding">
                            <table class="table table-hover">
                                <tr>
                                    <td class="third">{{ 'amount'|_ }}</td>
                                    <td>
                                        {{ formatAmountBySymbol(limit.amount, limit.transactionCurrency.symbol, limit.transactionCurrency.decimal_places) }}
                                        {% if convertToPrimary and null != limit.native_amount %}
                                            ({{ formatAmountBySymbol(limit.native_amount, primaryCurrency.symbol, primaryCurrency.decimal_places) }})
                                        {% endif %}
                                    </td>
                                </tr>
                                <tr>
                                    <td class="third">{{ 'spent'|_ }}</td>
                                    <td>
                                        {% if convertToPrimary %}
                                            {{ formatAmountBySymbol(limit.spent, limit.transactionCurrency.symbol, limit.transactionCurrency.decimal_places) }}
                                            {% if limit.pc_spent %}
                                                ({{ formatAmountBySymbol(limit.pc_spent, primaryCurrency.symbol, primaryCurrency.decimal_places) }})
                                            {% endif %}
                                        {% else %}
                                            {{ formatAmountBySymbol(limit.spent, limit.transactionCurrency.symbol, limit.transactionCurrency.decimal_places) }}
                                        {% endif %}
                                    </td>
                                </tr>
                                {% if 0 != bccomp('0', limit.spent) %}
                                    <tr>
                                        <td colspan="2">
                                            {% set overspent = limit.amount + limit.spent < 0 %}

                                            {% if overspent %}
                                                {% set pct = (limit.spent != 0 ? (limit.amount / (limit.spent*-1))*100 : 0) %} {# must have -1 here #}
                                                <div class="progress progress-striped">
                                                    <div class="progress-bar progress-bar-warning width-{{ pct|round }}" role="progressbar" aria-valuenow="{{ pct|round }}"
                                                         aria-valuemin="0"
                                                         aria-valuemax="100"></div>
                                                    <div class="progress-bar progress-bar-danger width-{{ 100-pct|round }}" role="progressbar" aria-valuenow="{{ (100-pct)|round }}"
                                                         aria-valuemin="0" aria-valuemax="100"></div>
                                                </div>
                                            {% else %}
                                                {% set pct = (limit.amount != 0 ? (((limit.spent*-1) / limit.amount)*100) : 0) %} {# must have -1 here #}
                                                <div class="progress progress-striped">
                                                    <div class="progress-bar progress-bar-success width-{{ pct|round }}" role="progressbar" aria-valuenow="{{ pct|round }}"
                                                         aria-valuemin="0"
                                                         aria-valuemax="100"></div>
                                                </div>
                                            {% endif %}
                                        </td>
                                    </tr>
                                {% endif %}
                            </table>
                        </div>
                    </div>
                {% endfor %}
                <p class="small text-center"><a href="{{ route('budgets.show',budget.id) }}">{{ 'showEverything'|_ }}</a></p>
            </div>
        {% endif %}
    </div>

{% endblock %}
{% block scripts %}
    <script type="text/javascript" nonce="{{ JS_NONCE }}">
        var budgetID = {{ budget.id }};
        var budgetLimitID = 0;
        {% if budgetLimit.id %}
        budgetLimitID = {{ budgetLimit.id }};
        var budgetChartUrl = '{{ route('chart.budget.budget-limit', [budget.id, budgetLimit.id] ) }}';
        var currencySymbol = '{{ currencySymbol }}';
        var expenseCategoryUrl = '{{ route('chart.budget.expense-category', [budget.id, budgetLimit.id]) }}';
        var expenseAssetUrl = '{{ route('chart.budget.expense-asset', [budget.id, budgetLimit.id]) }}';
        var expenseExpenseUrl = '{{ route('chart.budget.expense-expense', [budget.id, budgetLimit.id]) }}';
        {% else %}
        var budgetChartUrl = '{{ route('chart.budget.budget', [budget.id] ) }}';
        var expenseCategoryUrl = '{{ route('chart.budget.expense-category', [budget.id]) }}';
        var expenseAssetUrl = '{{ route('chart.budget.expense-asset', [budget.id]) }}';
        var expenseExpenseUrl = '{{ route('chart.budget.expense-expense', [budget.id]) }}';
        {% endif %}
    </script>

    <script type="text/javascript" src="v1/js/lib/Chart.bundle.min.js?v={{ FF_BUILD_TIME }}" nonce="{{ JS_NONCE }}"></script>
    <script type="text/javascript" src="v1/js/ff/charts.defaults.js?v={{ FF_BUILD_TIME }}" nonce="{{ JS_NONCE }}"></script>
    <script type="text/javascript" src="v1/js/ff/charts.js?v={{ FF_BUILD_TIME }}" nonce="{{ JS_NONCE }}"></script>
    <script type="text/javascript" src="v1/js/ff/budgets/show.js?v={{ FF_BUILD_TIME }}" nonce="{{ JS_NONCE }}"></script>
    {# required for groups.twig #}
    <script type="text/javascript" src="v1/js/ff/list/groups.js?v={{ FF_BUILD_TIME }}" nonce="{{ JS_NONCE }}"></script>
{% endblock %}
