{#
/**
 * 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() %}

<script>
    var editListViewDeleteCancel = "{{ trans('cancel') }}";
    var editListViewDeleteConfirm = "{{ trans('confirm') }}";
    var editListViewDeleteMessage = "{{ trans('are-you-sure') }}";
    var editListViewDeleteTitle = "{{ trans('confirm-delete') }}";
</script>

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

    {# -- Row statistics -- #}
    <div>
        {% set row = currentView.getRow('statistics') %}
        {{ row.render(fsc) | raw }}
    </div>

    {# -- New form -- #}
    {% if currentView.settings.btnNew %}
        {% set formName = 'form' ~ currentView.getViewName() ~ 'New' %}
        <form id="{{ formName }}" method="post" enctype="multipart/form-data" onsubmit="animateSpinner('add')">
            {{ formToken() }}
            <input type="hidden" name="action" value="insert"/>
            <input type="hidden" name="activetab" value="{{ currentView.getViewName() }}"/>
            <div class="card border-success shadow mb-2">
                <div class="card-body p-2">
                    <button class="btn btn-spin-action btn-sm btn-outline-success mr-1" type="button" data-toggle="collapse"
                            data-target="#{{ formName }}Collapse" aria-expanded="false">
                        <i class="fas fa-plus-square fa-fw" aria-hidden="true"></i>
                        {{ trans('add') }}
                    </button>
                    {{ currentView.title }}
                </div>
                <div class="collapse" id="{{ formName }}Collapse">
                    <div class="card-body border-top">
                        <div class="form-row">
                            {% for group in currentView.getColumns() %}
                                {{ group.edit(currentView.model) | raw }}
                            {% endfor %}
                        </div>
                    </div>
                    <div class="card-footer text-right p-2">
                        <button class="btn btn-spin-action btn-sm btn-success" 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>
                    </div>
                </div>
            </div>
        </form>

        <script>
            $(document).ready(function () {
                $('#{{ formName }}Collapse').on('shown.bs.collapse', function () {
                    $('#{{ formName }}Collapse input[type="text"]').first().focus();
                });
            });
        </script>
    {% endif %}

    {# -- Forms -- #}
    {% for counter, model in currentView.cursor %}
        {% set formName = 'form' ~ currentView.getViewName() ~ counter %}
        <form id="{{ formName }}" method="post" enctype="multipart/form-data" onsubmit="animateSpinner('add')">
            {{ formToken() }}
            <input type="hidden" name="action" value="edit"/>
            <input type="hidden" name="activetab" value="{{ currentView.getViewName() }}"/>
            <input type="hidden" name="code" value="{{ model.primaryColumnValue() }}"/>
            <div class="card shadow mb-2"{% if currentView.selected == model.primaryColumnValue() %} id="EditListViewSelected"{% endif %}>
                <div class="card-body">
                    <div class="form-row">
                        {% for group in currentView.getColumns() %}
                            {{ group.edit(model) | raw }}
                        {% endfor %}
                    </div>
                </div>
                <div class="card-footer p-2">
                    <div class="form-row">
                        {% if currentView.settings.btnDelete %}
                            <div class="col-auto">
                                <button type="button" class="btn btn-spin-action btn-sm btn-danger"
                                        onclick="editListViewDelete('{{ currentView.getViewName() ~ counter }}');">
                                    <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 %}
                        <div class="col">
                            {# -- Row actions -- #}
                            {% set row = currentView.getRow('actions') %}
                            {{ row.render(false, currentView.getViewName() ~ counter) | raw }}
                        </div>
                        <div class="col-auto">
                            {% if currentView.settings.btnUndo %}
                                <button class="btn btn-spin-action 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-spin-action 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>
    {% endfor %}

    {# -- Pagination -- #}
    {% if currentView.getPagination() | length > 0 %}
        {% set formName = 'form' ~ currentView.getViewName() ~ 'Offset' %}
        <form id="{{ formName }}" method="post" onsubmit="animateSpinner('add')">
            <input type="hidden" name="activetab" value="{{ currentView.getViewName() }}"/>
            <input type="hidden" name="offset" value="{{ currentView.offset }}"/>
            <div class="text-center pt-3">
                <div class="btn-group">
                    {% for page in currentView.getPagination() %}
                        {% set btnClass = page.active ? 'btn btn-outline-dark active' : 'btn btn-outline-dark' %}
                        <button type="button" class="btn-spin-action {{ btnClass }}"
                                onclick="editListViewSetOffset('{{ currentView.getViewName() }}', '{{ page.offset }}');">
                            {{ page.num }}
                        </button>
                    {% endfor %}
                </div>
            </div>
        </form>
    {% endif %}

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

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