{#
    /**
     * This file is part of FacturaScripts
     * Copyright (C) 2017-2020 Carlos Garcia Gomez <carlos@facturascripts.com>
     *
     * This program is free software: you can redistribute it and/or modify
     * it under the terms of the GNU Lesser General Public License as
     * published by the Free Software Foundation, either version 3 of the
     * License, or (at your option) any later version.
     *
     * This program is distributed in the hope that it will be useful,
     * but WITHOUT ANY WARRANTY; without even the implied warranty of
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     * GNU Lesser General Public License for more details.
     *
     * You should have received a copy of the GNU Lesser General Public License
     * along with this program. If not, see http://www.gnu.org/licenses/.
     */
#}
{% set currentView = fsc.getCurrentView() %}
{% set action = currentView.model.exists() ? 'edit' : 'insert' %}

<script type="text/javascript">
    function editViewDelete(viewName) {
        bootbox.confirm({
            title: "{{ i18n.trans('confirm-delete') }}",
            message: "{{ i18n.trans('are-you-sure') }}",
            closeButton: false,
            buttons: {
                cancel: {
                    label: '<i class="fas fa-times"></i> {{ i18n.trans('cancel') }}'
                },
                confirm: {
                    label: '<i class="fas fa-check"></i> {{ i18n.trans('confirm') }}',
                    className: "btn-danger"
                }
            },
            callback: function (result) {
                if (result) {
                    $("#form" + viewName + " :input[name=\"action\"]").val("delete");
                    $("#form" + viewName).submit();
                }
            }
        });

        return false;
    }
</script>

{# -- Row header -- #}
<div class="row">
    {% set row = currentView.getRow('header') %}
    {{ row.render(currentView.getViewName(), '', fsc) | raw }}
</div>

<form id="form{{ currentView.getViewName() }}" method="post" enctype="multipart/form-data">
    <input type="hidden" name="action" value="{{ action }}"/>
    <input type="hidden" name="activetab" value="{{ currentView.getViewName() }}"/>
    <input type="hidden" name="code" value="{{ currentView.model.primaryColumnValue() }}"/>
    <input type="hidden" name="multireqtoken" value="{{ fsc.multiRequestProtection.newToken() }}"/>
    <div class="{{ currentView.settings.card ? 'card shadow' : '' }}">
        <div class="{{ currentView.settings.card ? 'card-body' : 'container-fluid' }}">
            <div class="row">
                <div class="col-12 text-right">
                    {# -- Row statistics -- #}
                    {% set row = currentView.getRow('statistics') %}
                    {{ row.render(fsc) | raw }}
                </div>
            </div>
            <div class="form-row">
                {% for group in currentView.getColumns() %}
                    {{ group.edit(currentView.model) | raw }}
                {% endfor %}
            </div>
            <div class="row">
                {% if fsc.hasData and currentView.settings.btnDelete %}
                    <div class="col">
                        <button type="button" class="btn btn-sm btn-danger" onclick="editViewDelete('{{ currentView.getViewName() }}');">
                            <i class="fas fa-trash-alt fa-fw" aria-hidden="true"></i>
                            <span class="d-none d-sm-inline-block">{{ i18n.trans('delete') }}</span>
                        </button>
                    </div>
                {% endif %}
                {% set extraClass = fsc.hasData and currentView.settings.btnDelete ? 'text-center' : '' %}
                <div class="col {{ extraClass }}">
                    {# -- Row actions -- #}
                    {% set row = currentView.getRow('actions') %}
                    {{ row.render(false, currentView.getViewName()) | raw }}
                </div>
                <div class="col text-right">
                    {% if currentView.settings.btnUndo %}
                        <button class="btn btn-sm btn-secondary" type="reset">
                            <i class="fas fa-undo fa-fw" aria-hidden="true"></i>
                            <span class="d-none d-sm-inline-block">{{ i18n.trans('undo') }}</span>
                        </button>
                    {% endif %}
                    {% if currentView.settings.btnSave %}
                        <button class="btn btn-sm btn-primary" type="submit">
                            <i class="fas fa-save fa-fw" aria-hidden="true"></i>
                            <span class="d-none d-sm-inline-block">{{ i18n.trans('save') }}</span>
                        </button>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</form>

<br/>

{# -- Row footer -- #}
<div class="row">
    {% set row = currentView.getRow('footer') %}
    {{ row.render(currentView.getViewName(), '', fsc) | raw }}
</div>

{# -- Modals -- #}
{% for group in currentView.getModals() %}
    {{ group.modal(currentView.model, currentView.getViewName()) | raw }}
{% endfor %}
