{% set VUE_SCRIPT_NAME = 'profile' %}
{% extends './layout/default' %}

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

{% block content %}

    <div class="row">
        <div class="col-lg-8 col-lg-offset-2 col-md-12 col-sm-12">

            <!-- Nav tabs -->
            <ul class="nav nav-tabs" role="tablist">
                <li role="presentation" class="active">
                    <a href="{{ route('profile.index') }}#options" aria-controls="home" role="tab"
                       data-toggle="tab">{{ 'options'|_ }}</a>
                </li>
                <li role="presentation">
                    <a href="#cmd" aria-controls="profile" role="tab" data-toggle="tab">{{ 'command_line_token'|_ }}</a>
                </li>
                <li role="presentation">
                    <a href="#delete" aria-controls="settings" role="tab"
                       data-toggle="tab">{{ 'delete_stuff_header'|_ }}</a>
                </li>
            </ul>
            <div class="tab-content">

                <!-- main options -->
                <div role="tabpanel" class="tab-pane active" id="options">
                    <div class="box box-default">
                        <div class="box-body">
                            <p>
                                {{ trans('firefly.user_id_is',{user: userId})|raw }}
                            </p>
                            <div class="row">
                                <div class="col-lg-6">
                                    <ul>
                                        {% if true == isInternalAuth %}
                                            <li>
                                                <a href="{{ route('profile.change-email') }}">{{ 'change_your_email'|_ }}</a>
                                            </li>
                                            <li>
                                                <a href="{{ route('profile.change-password') }}">{{ 'change_your_password'|_ }}</a>
                                            </li>
                                        {% if enabled2FA == true %}
                                            <li>
                                                <a href="{{ route('profile.mfa.index') }}">{{ 'manage_mfa_settings'|_ }}</a>
                                            </li>
                                            {% endif %}
                                            {% if enabled2FA == false %}
                                                <li>
                                                    <a href="{{ route('profile.mfa.index') }}">{{ 'enable_mfa'|_ }}</a>
                                                </li>
                                            {% endif %}
                                        {% endif %}

                                        <li><a href="{{ route('logout') }}" class="logout-link">{{ 'logout'|_ }}</a>
                                        </li>

                                        {% if true == isInternalAuth %}
                                            <li>
                                                <a href="{{ route('profile.logout-others') }}">{{ 'logout_other_sessions'|_ }}</a>
                                            </li>
                                            <li><a class="text-danger"
                                                   href="{{ route('profile.delete-account') }}">{{ 'delete_account'|_ }}</a>
                                            </li>
                                        {% endif %}
                                    </ul>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- command line options -->
                <div role="tabpanel" class="tab-pane" id="cmd">
                    <div class="box box-default">
                        <div class="box-body">
                            <p>
                                {{ 'explain_command_line_token'|_ }}
                            </p>
                            <p>
                                <input id="token" type="text" class="form-control" name="token"
                                       value="{{ accessToken.data }}"
                                       size="32" maxlength="32" readonly/>

                            </p>
                            <form action="{{ route('profile.regenerate') }}" method="post">
                                <p>

                                    <input type="hidden" name="_token" value="{{ csrf_token() }}"/>
                                    <button type="submit" class="btn btn-danger btn-xs"><span
                                                class="fa fa-refresh"></span> {{ 'regenerate_command_line_token'|_ }}
                                    </button>
                                </p>
                            </form>
                        </div>
                    </div>
                </div>

                <!-- purge stuff -->
                <div role="tabpanel" class="tab-pane" id="delete">
                    <div class="box box-default">
                        <div class="box-header with-border">
                            <h3 class="box-title">{{ 'purge_data_title'|_ }}</h3>
                        </div>
                        <div class="box-body with-border">
                            <div class="col-lg-12">
                                <p class="text-info">
                                    {{ 'purge_data_expl'|_ }}
                                </p>
                                <p>
                                    <button type="button"
                                            data-success="{{ trans('firefly.purged_all_records')|escape('html') }}"
                                            data-type="purge" class="confirm btn btn-warning btn-sm"><span
                                                class="fa fa-trash"></span> {{ 'purge_all_data'|_ }}</button>

                                </p>

                            </div>
                        </div>
                    </div>
                    <!-- delete stuff -->
                    <div class="box box-default">
                        <div class="box-header with-border">
                            <h3 class="box-title">{{ 'delete_data_title'|_ }}</h3>
                        </div>
                        <div class="box-body with-border">
                            <div class="col-lg-12">
                                <p class="text-info">
                                    {{ 'permanent_delete_stuff'|_ }}
                                </p>
                                <h4>{{ 'financial_control'|_ }}</h4>
                                <div class="btn-group">
                                    <button type="button"
                                            data-success="{{ trans('firefly.deleted_all_budgets')|escape('html') }}"
                                            data-type="budgets" class="confirm btn btn-danger btn-sm"><span
                                                class="fa fa-pie-chart"></span> {{ 'delete_all_budgets'|_ }}</button>

                                    <button type="button"
                                            data-success="{{ trans('firefly.deleted_all_bills')|escape('html') }}"
                                            data-type="bills" class="confirm btn btn-danger btn-sm"><span
                                                class="fa fa-calendar-o"></span> {{ 'delete_all_bills'|_ }}</button>

                                    <button type="button"
                                            data-success="{{ trans('firefly.deleted_all_piggy_banks')|escape('html') }}"
                                            data-type="piggy_banks" class="confirm btn btn-danger btn-sm"><span
                                                class="fa fa-bullseye"></span> {{ 'delete_all_piggy_banks'|_ }}</button>
                                </div>
                                <h4>{{ 'automation'|_ }}</h4>
                                <div class="btn-group">
                                    <button type="button"
                                            data-success="{{ trans('firefly.deleted_all_rules')|escape('html') }}"
                                            data-type="rules" class="confirm btn btn-danger btn-sm"><span
                                                class="fa fa-random"></span> {{ 'delete_all_rules'|_ }}</button>

                                    <button type="button"
                                            data-success="{{ trans('firefly.deleted_all_recurring')|escape('html') }}"
                                            data-type="recurring" class="confirm btn btn-danger btn-sm"><span
                                                class="fa fa-paint-brush"></span> {{ 'delete_all_recurring'|_ }}
                                    </button>
                                </div>

                                <h4>{{ 'classification'|_ }}</h4>
                                <div class="btn-group">
                                    <button type="button"
                                            data-success="{{ trans('firefly.deleted_all_categories')|escape('html') }}"
                                            data-type="categories" class="confirm btn btn-danger btn-sm"><span
                                                class="fa fa-bookmark"></span> {{ 'delete_all_categories'|_ }}</button>

                                    <button type="button"
                                            data-success="{{ trans('firefly.deleted_all_tags')|escape('html') }}"
                                            data-type="tags" class="confirm btn btn-danger btn-sm"><span
                                                class="fa fa-tag"></span> {{ 'delete_all_tags'|_ }}</button>

                                    <button type="button"
                                            data-success="{{ trans('firefly.deleted_all_object_groups')|escape('html') }}"
                                            data-type="object_groups" class="confirm btn btn-danger btn-sm"><span
                                                class="fa fa-envelope-o"></span> {{ 'delete_all_object_groups'|_ }}
                                    </button>
                                </div>

                                <h4>{{ 'accounts'|_ }}</h4>
                                <p>
                                    <em class="text-danger">{{ 'also_delete_transactions'|_ }}</em>
                                </p>
                                <div class="btn-group">
                                    <button type="button"
                                            data-success="{{ trans('firefly.deleted_all_accounts')|escape('html') }}"
                                            data-type="accounts" class="confirm btn btn-danger btn-sm"><span
                                                class="fa fa-credit-card"></span> {{ 'delete_all_accounts'|_ }}</button>

                                    <button type="button"
                                            data-success="{{ trans('firefly.deleted_all_asset_accounts')|escape('html') }}"
                                            data-type="asset_accounts" class="confirm btn btn-danger btn-sm"><span
                                                class="fa fa-money"></span> {{ 'delete_all_asset_accounts'|_ }}</button>

                                    <button type="button"
                                            data-success="{{ trans('firefly.deleted_all_expense_accounts')|escape('html') }}"
                                            data-type="expense_accounts" class="confirm btn btn-danger btn-sm"><span
                                                class="fa fa-shopping-cart"></span> {{ 'delete_all_expense_accounts'|_ }}
                                    </button>
                                    <button type="button"
                                            data-success="{{ trans('firefly.deleted_all_revenue_accounts')|escape('html') }}"
                                            data-type="revenue_accounts" class="confirm btn btn-danger btn-sm"><span
                                                class="fa fa-download"></span> {{ 'delete_all_revenue_accounts'|_ }}
                                    </button>

                                    <button type="button"
                                            data-success="{{ trans('firefly.deleted_all_liabilities')|escape('html') }}"
                                            data-type="liabilities" class="confirm btn btn-danger btn-sm"><span
                                                class="fa fa-ticket"></span> {{ 'delete_all_liabilities'|_ }}</button>
                                </div>
                                <h4>{{ 'accounts'|_ }}</h4>
                                <p>
                                    <em class="text-info">
                                        {{ 'delete_unused_accounts'|_ }}
                                    </em>
                                </p>
                                <div class="btn-group">
                                    <button type="button"
                                            data-success="{{ trans('firefly.deleted_all_unused_accounts')|escape('html') }}"
                                            data-type="unused_accounts" class="confirm btn btn-warning btn-sm"><span
                                                class="fa fa-credit-card"></span> {{ 'delete_all_unused_accounts'|_ }}
                                    </button>
                                </div>


                                <h4> {{ 'transactions'|_ }}</h4>
                                <div class="btn-group">
                                    <button type="button"
                                            data-success="{{ trans('firefly.deleted_all_transactions')|escape('html') }}"
                                            data-type="transactions" class="confirm btn btn-danger btn-sm"><span
                                                class="fa fa-exchange"></span> {{ 'delete_all_transactions'|_ }}
                                    </button>

                                    <button type="button"
                                            data-success="{{ trans('firefly.deleted_all_withdrawals')|escape('html') }}"
                                            data-type="withdrawals" class="confirm btn btn-danger btn-sm"><span
                                                class="fa fa-long-arrow-left"></span> {{ 'delete_all_withdrawals'|_ }}
                                    </button>

                                    <button type="button"
                                            data-success="{{ trans('firefly.deleted_all_deposits')|escape('html') }}"
                                            data-type="deposits" class="confirm btn btn-danger btn-sm"><span
                                                class="fa fa-long-arrow-right"></span> {{ 'delete_all_deposits'|_ }}
                                    </button>
                                    <button type="button"
                                            data-success="{{ trans('firefly.deleted_all_transfers')|escape('html') }}"
                                            data-type="transfers" class="confirm btn btn-danger btn-sm"><span
                                                class="fa fa-exchange"></span> {{ 'delete_all_transfers'|_ }}</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

            </div>

        </div>
    </div>
{% endblock %}
{% block scripts %}
    <script type="text/javascript" nonce="{{ JS_NONCE }}">
        var deleteAPIRoute = '{{ route('api.v1.data.destroy') }}';
        var confirmText = '{{ trans('firefly.are_you_sure')|escape('js') }}';
        $(document).ready(function () {
            $('.confirm').on('click', function (e) {
                if(!confirm(confirmText)) {
                    return false;
                }
                var link = $(e.currentTarget);
                var classes = link.find('i').attr('class');
                var url = deleteAPIRoute + '?objects=' + link.data('type');
                // different URL for purge route:
                if (link.data('type') === 'purge') {
                    url = '{{ route('api.v1.data.purge') }}';
                }
                if (link.data('type') === 'unused_accounts') {
                    url = deleteAPIRoute + '?objects=not_assets_liabilities&unused=true';
                }

                // replace icon with loading thing
                link.prop('disabled', true);
                link.find('i').removeClass().addClass('fa fa-spin fa-spinner');

                // call API:
                $.ajax({
                    method: 'DELETE',
                    url: url,
                }).done(
                    function () {
                        // enable button again:
                        link.prop('disabled', false);
                        link.find('i').removeClass().addClass(classes);
                        alert(link.data('success'));
                    }
                ).fail(function () {
                    link.find('i').removeClass().addClass('fa fa-exclamation-triangle');
                    alert('Could not delete. Sorry.');
                });
                return false;
            });
        });
    </script>
{% endblock %}
