{#
/**
 * This file is part of FacturaScripts
 * Copyright (C) 2017-2023 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' %}
{% set fieldCount = 0 %}
{% for group in currentView.getColumns() %}
    {% set fieldCount = fieldCount + group.columns | length %}
{% endfor %}

<script>
    function editViewDelete(viewName) {
        bootbox.confirm({
            title: "{{ trans('confirm-delete') }}",
            message: "{{ trans('are-you-sure') }}",
            closeButton: false,
            buttons: {
                cancel: {
                    label: '<i class="fas fa-times"></i> {{ trans('cancel') }}'
                },
                confirm: {
                    label: '<i class="fas fa-check"></i> {{ 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" onsubmit="animateSpinner('add')">
    {{ formToken() }}
    <input type="hidden" name="action" value="{{ action }}"/>
    <input type="hidden" name="activetab" value="{{ currentView.getViewName() }}"/>
    <input type="hidden" name="code" value="{{ currentView.model.primaryColumnValue() }}"/>
    <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 }}
                    {% if fieldCount > 30 and 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">{{ trans('save') }}</span>
                        </button>
                    {% endif %}
                </div>
            </div>
            <div class="form-row">
                {% for group in currentView.getColumns() %}
                    {{ group.edit(currentView.model) | raw }}
                {% endfor %}
            </div>
        </div>
        <div class="{{ currentView.settings.card ? 'card-footer p-2' : 'container-fluid' }}">
            <div class="form-row">
                {% if fsc.hasData and currentView.settings.btnDelete %}
                    <div class="col-auto">
                        <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">{{ 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-auto">
                    {% 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">{{ 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">{{ 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 %}
