{#
/**
 * This file is part of FacturaScripts
 * Copyright (C) 2017-2025 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 formName = 'form' ~ currentView.getViewName() %}

<script>
    var listViewDeleteCancel = "{{ trans('cancel') }}";
    var listViewDeleteConfirm = "{{ trans('confirm') }}";
    var listViewDeleteMessage = "{{ trans('are-you-sure') }}";
    var listViewDeleteTitle = "{{ trans('confirm-delete') }}";
</script>

<form id="{{ formName }}" method="post" onsubmit="animateSpinner('add')">
    {{ formToken() }}
    <input type="hidden" name="action"/>
    <input type="hidden" name="activetab" value="{{ currentView.getViewName() }}"/>
    <input type="hidden" name="loadfilter" value="{{ currentView.pageFilterKey }}"/>
    <input type="hidden" name="offset" value="{{ currentView.offset }}"/>
    <input type="hidden" name="order" value="{{ currentView.orderKey }}"/>
    <div class="{{ currentView.settings.card ? 'card shadow' : '' }}">
        <div class="{{ currentView.settings.card ? 'container-fluid pt-3' : 'container-fluid' }}">
            <div class="row">
                {# -- Left buttons -- #}
                <div class="col-md-auto mb-2">
                    {% if currentView.settings.btnNew %}
                        {% if currentView.settings.modalInsert %}
                            <button type="button" class="btn btn-success" title="{{ trans('new') }}"
                                    data-bs-toggle="modal" data-bs-target="#modal{{ currentView.settings.modalInsert }}">
                                <i class="fa-solid fa-plus fa-fw" aria-hidden="true"></i>
                                {% if currentView.settings.card == false %}
                                    <span class="d-none d-xl-inline-block">{{ trans('new') }}</span>
                                {% endif %}
                            </button>
                        {% else %}
                            <a href="{{ asset(currentView.btnNewUrl()) }}" class="btn btn-success"
                               title="{{ trans('new') }}"><i class="fa-solid fa-plus fa-fw" aria-hidden="true"></i>
                                {% if currentView.settings.card == false %}
                                    <span class="d-none d-xl-inline-block">{{ trans('new') }}</span>
                                {% endif %}
                            </a>
                        {% endif %}
                    {% endif %}
                    {% if currentView.settings.btnDelete %}
                        <button type="button" class="btn btn-danger"
                                onclick="listViewDelete('{{ currentView.getViewName() }}');"
                                title="{{ trans('delete') }}">
                            <i class="fa-solid fa-trash-alt fa-fw" aria-hidden="true"></i>
                        </button>
                    {% endif %}
                    {% if currentView.settings.btnPrint %}
                        <div class="btn-group">
                            {{ _self.printButton(fsc, currentView) }}
                        </div>
                    {% endif %}
                    {% if currentView.settings.clickable %}
                        <button type="button" class="btn btn-light"
                                onclick="listViewOpenTab('{{ currentView.getViewName() }}');"
                                title="{{ trans('open-tab') }}">
                            <i class="fa-solid fa-external-link-alt fa-fw" aria-hidden="true"></i>
                        </button>
                    {% endif %}
                    {# -- Row actions -- #}
                    {{ currentView.getRow('actions').render(true, currentView.getViewName()) | raw }}
                    {# -- Row statistics -- #}
                    {{ currentView.getRow('statistics').render(fsc) | raw }}
                </div>
                <div class="col-sm mb-2">
                    {# -- Search field -- #}
                    {{ _self.searchControl(currentView) }}
                </div>
                <div class="col-sm-auto text-end mb-2">
                    {# -- Filters button -- #}
                    {{ _self.filterButton(currentView, fsc) }}
                    {# -- OrderBy button -- #}
                    {{ _self.sortButton(currentView) }}
                    {# -- Colors button -- #}
                    {{ _self.colorsButton(currentView) }}
                </div>
            </div>
            {# -- Filters -- #}
            {% set divFiltersStyle = currentView.showFilters ? '' : 'display: none;' %}
            <div id="form{{ currentView.getViewName() }}Filters" class="row g-2 align-items-center border-bottom mb-3"
                 style="{{ divFiltersStyle }}">
                {% for filterName, filter in currentView.filters %}
                    {{ filter.render() | raw }}
                {% endfor %}
            </div>
            {# -- Row header -- #}
            <div class="row">
                {{ currentView.getRow('header').render(currentView.getViewName(), 'listViewSetAction', fsc) | raw }}
            </div>
        </div>
        {# -- Pagination -- #}
        {% set pages = currentView.getPagination() %}
        {% if pages | length > 4 %}
            <div class="text-center pb-2">
                <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="{{ btnClass }}"
                                onclick="listViewSetOffset('{{ currentView.getViewName() }}', '{{ page.offset }}');">
                            {{ page.num }}
                        </button>
                    {% endfor %}
                </div>
            </div>
        {% endif %}
        {# -- Table -- #}
        <div class="table-responsive">
            {% set tableClass = settings('default', 'tablesize') == 'small' ? 'table-sm' : '' %}
            <table class="table table-hover mb-0 {{ tableClass }}">
                <thead>
                <tr>
                    {% if (currentView.cursor | length > 0) and (currentView.settings.checkBoxes or currentView.settings.clickable) %}
                        <th width="50" class="text-center">
                            {% if currentView.settings.checkBoxes %}
                                <div class="form-check form-check-inline m-0 toggle-ext-link">
                                    <input class="form-check-input listActionCB" type="checkbox"
                                           onclick="listViewCheckboxes('{{ currentView.getViewName() }}');"/>
                                </div>
                            {% endif %}
                            {% if currentView.settings.clickable %}
                                <i class="fa-solid fa-external-link-alt toggle-ext-link d-none"
                                   title="{{ trans('open-tab') }}"></i>
                            {% endif %}
                        </th>
                    {% endif %}
                    {% for column in currentView.getColumns() %}
                        {{ column.tableHeader(currentView) | raw }}
                    {% endfor %}
                </tr>
                </thead>
                <tbody>
                {% set rowStatus = currentView.getRow('status') %}
                {% for model in currentView.cursor %}
                    {% set trClass = currentView.settings.clickable ? 'clickableListRow ' ~ rowStatus.trClass(model) : rowStatus.trClass(model) %}
                    {% set trTitle = rowStatus.trTitle(model) %}
                    <tr class="{{ trClass }}" title="{{ trTitle }}" data-href="{{ asset(model.url()) }}">
                        {% if currentView.settings.checkBoxes or currentView.settings.clickable %}
                            <td class="cancelClickable p-0 text-center align-middle">
                                {% if currentView.settings.checkBoxes %}
                                    <div class="form-check form-check-inline m-0 toggle-ext-link">
                                        <input class="form-check-input listAction" type="checkbox" name="codes[]"
                                               value="{{ model.primaryColumnValue() }}"/>
                                    </div>
                                {% endif %}
                                {% if currentView.settings.clickable %}
                                    <a href="{{ asset(model.url()) }}" target="_blank" class="toggle-ext-link d-none"
                                       onauxclick="$(this).addClass('text-dark');" title="{{ trans('open-tab') }}">
                                        <i class="fa-solid fa-external-link-alt"></i>
                                    </a>
                                {% endif %}
                            </td>
                        {% endif %}
                        {% for column in currentView.getColumns() %}
                            {{ column.tableCell(model) | raw }}
                        {% endfor %}
                    </tr>
                {% else %}
                    <tr class="table-warning">
                        <td colspan="{{ 1 + currentView.getColumns() | length }}">{{ trans('no-data') }}</td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
        {# -- Totals -- #}
        {% if currentView.totalAmounts is not empty %}
            <div class="table-responsive">
                <table class="table table-sm table-hover mt-4 mb-0">
                    <thead>
                    <tr>
                        <th></th>
                        {% for item in currentView.totalAmounts %}
                            <th class="text-end text-capitalize">{{ trans(item.title) }}</th>
                        {% endfor %}
                    </tr>
                    </thead>
                    <tbody>
                    <tr>
                        <td class="text-end">{{ trans('page-total-amount') }}</td>
                        {% for item in currentView.totalAmounts %}
                            <td class="text-end">{{ number(item.page) }}</td>
                        {% endfor %}
                    </tr>
                    <tr>
                        <td class="text-end">{{ trans('total-amount') }}</td>
                        {% for item in currentView.totalAmounts %}
                            <td class="text-end">{{ number(item.total) }}</td>
                        {% endfor %}
                    </tr>
                    </tbody>
                </table>
            </div>
        {% endif %}
        {# -- Pagination -- #}
        {% if pages | length > 0 %}
            <div class="{{ currentView.settings.card ? 'card-footer text-center' : 'pt-3 text-center' }}">
                <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="{{ btnClass }}"
                                onclick="listViewSetOffset('{{ currentView.getViewName() }}', '{{ page.offset }}');">
                            {{ page.num }}
                        </button>
                    {% endfor %}
                </div>
            </div>
        {% endif %}
    </div>
    <br/>
    {# -- Row footer -- #}
    <div class="container-fluid">
        <div class="row">
            {{ currentView.getRow('footer').render(currentView.getViewName(), 'listViewSetAction', fsc) | raw }}
        </div>
    </div>
    {# -- Save Filter modal window -- #}
    {% if currentView.showFilters %}
        {{ _self.filterSaveModal(currentView) }}
    {% endif %}
</form>

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

{# -- Macros -- #}
{% macro filterButton(currentView, fsc) %}
    {% if currentView.filters is not empty %}
        {% set viewName = currentView.getViewName() %}
        {% if currentView.pageFilterKey %}
            {# -- Disable user filters -- #}
            <a href="{{ fsc.url() }}?activetab={{ viewName }}" class="btn btn-light" title="{{ trans('all') }}">
                <i class="fa-solid fa-filter fa-fw"></i> {{ trans('all') }}
            </a>
        {% else %}
            <button type="button" class="btn btn-light" onclick="listViewShowFilters('{{ viewName }}');">
                <i class="fa-solid fa-filter fa-fw"></i>
                <span class="d-none d-lg-inline">{{ trans('filters') }}</span>
            </button>
            {% if currentView.showFilters and currentView.settings.saveFilters %}
                {# -- Save user filters -- #}
                <button type="button" class="btn btn-success" data-bs-toggle="modal"
                        data-bs-target="#savefilter{{ viewName }}" title="{{ trans('save-filter') }}">
                    <i class="fa-solid fa-save fa-fw"></i>
                    <span class="d-none d-lg-inline">{{ trans('save') }}</span>
                </button>
                <button type="button" class="btn btn-outline-danger" title="{{ trans('clear-filters') }}"
                        onclick="listViewSetAction('{{ viewName }}', 'clear-filters');">
                    <i class="fa-solid fa-filter-circle-xmark fa-fw"></i>
                </button>
            {% endif %}
        {% endif %}
        {% set saveFilters = currentView.pageFilters %}
        {% if saveFilters is not empty %}
            {% for pageFilter in saveFilters %}
                {% set icon = (pageFilter.nick is empty) ? 'fa-users' : 'fa-user' %}
                {% set filterDesc = pageFilter.description|length > 10 ? pageFilter.description|slice(0,10) ~ '...' : pageFilter.description %}
                {% if pageFilter.id == currentView.pageFilterKey %}
                    <div class="btn-group">
                        {# -- Selected user filter -- #}
                        <button title="{{ pageFilter.description }}" class="btn btn-light active"
                                onclick="listViewSetLoadFilter('{{ viewName }}', '{{ pageFilter.id }}');">
                            <i class="fa-solid fa-filter fa-fw"></i> {{ filterDesc }}
                        </button>
                        {# -- Delete user filter -- #}
                        <button type="button" class="btn btn-light text-danger"
                                title="{{ trans('remove-filter') }}"
                                onclick="listViewSetAction('{{ viewName }}', 'delete-filter');">
                            <i class="fa-solid fa-trash-alt fa-fw"></i>
                        </button>
                    </div>
                {% else %}
                    {# -- Select user filters -- #}
                    <button title="{{ pageFilter.description }}" class="btn btn-light"
                            onclick="listViewSetLoadFilter('{{ viewName }}', '{{ pageFilter.id }}');">
                        <i class="fa-solid fa-filter fa-fw"></i> {{ filterDesc }}
                    </button>
                {% endif %}
            {% endfor %}
        {% endif %}
    {% endif %}
{% endmacro %}

{% macro filterSaveModal(currentView) %}
    {% set viewName = currentView.getViewName() %}
    <div class="modal" id="savefilter{{ viewName }}" tabindex="-1" role="dialog">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">
                        <i class="fa-solid fa-filter fa-fw"></i> {{ trans('save-filter') }}
                    </h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                </div>
                <div class="modal-body">
                    <div class="row">
                        <div class="col">
                            <div class="mb-3">
                                <label>{{ trans('description') }}</label>
                                <input type="text" name="filter-description" class="form-control noEnterKey"
                                       autofocus/>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">
                        {{ trans('cancel') }}
                    </button>
                    <button type="button" class="btn btn-primary"
                            onclick="listViewSetAction('{{ viewName }}', 'save-filter');">
                        {{ trans('accept') }}
                    </button>
                </div>
            </div>
        </div>
    </div>
{% endmacro %}

{% macro colorsButton(currentView) %}
    {% set legend = currentView.getRow('status').legend() %}
    {% if legend %}
        {% set viewName = currentView.getViewName() %}
        <div class="btn-group">
            <div class="dropdown">
                <button class="btn btn-light dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-haspopup="true"
                        aria-expanded="false">
                    <i class="fa-solid fa-fill-drip fa-fw" aria-hidden="true"></i>
                </button>
                <div class="dropdown-menu dropdown-menu-end pb-0">
                    <h6 class="dropdown-header">
                        {{ trans('colors') }}
                    </h6>
                    {{ legend | raw }}
                </div>
            </div>
        </div>
    {% endif %}
{% endmacro %}

{% macro printButton(fsc, currentView) %}
    <div class="dropdown">
        <button class="btn btn-light dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-haspopup="true"
                aria-expanded="false">
            <i class="fa-solid fa-print fa-fw" aria-hidden="true"></i>
        </button>
        <div class="dropdown-menu">
            {% for key, option in fsc.exportManager.options() %}
                <a href="#" class="dropdown-item"
                   onclick="listViewPrintAction('{{ currentView.getViewName() }}', '{{ key }}');">
                    <i class="{{ option.icon }} fa-fw" aria-hidden="true"></i>
                    {{ trans(option.description) }}
                </a>
            {% endfor %}
            {% if fsc.exportManager.tools is not empty %}
                <div class="dropdown-divider"></div>
                {% for key, tool in fsc.exportManager.tools() %}
                    <a href="{{ asset(tool.link) }}" class="dropdown-item">
                        <i class="{{ tool.icon }} fa-fw" aria-hidden="true"></i>
                        {{ trans(tool.description) }}
                    </a>
                {% endfor %}
            {% endif %}
        </div>
    </div>
{% endmacro %}

{% macro searchControl(currentView) %}
    {% if currentView.searchFields is not empty %}
        <div class="mb-3">
            <div class="input-group">
                <input class="form-control" type="text" name="query" value="{{ currentView.query }}" autocomplete="off"
                       placeholder="{{ trans('search') }}"/>
                <button type="submit" class="btn btn-secondary">
                    <i class="fa-solid fa-search" aria-hidden="true"></i>
                </button>
            </div>
        </div>
    {% endif %}
{% endmacro %}

{% macro sortButton(currentView) %}
    {% if currentView.orderOptions is not empty %}
        <div class="btn-group">
            <button class="btn btn-light dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-haspopup="true"
                    aria-expanded="true">
                {% set icon = (currentView.orderOptions[currentView.orderKey]['type'] == 'ASC') ? 'fa-solid fa-angles-up' : 'fa-solid fa-angles-down' %}
                <i class="{{ icon }} fa-fw" aria-hidden="true"></i>
                <span class="d-none d-lg-inline">{{ currentView.orderOptions[currentView.orderKey]['label'] }}</span>
                <span class="caret"></span>
            </button>
            <div class="dropdown-menu dropdown-menu-end">
                {% for key, orderby in currentView.orderOptions %}
                    {% set activeClass = (currentView.orderKey == key) ? ' active' : '' %}
                    {% set icon = (orderby.type == 'ASC') ? 'fa-solid fa-angles-up' : 'fa-solid fa-angles-down' %}
                    <a class="dropdown-item{{ activeClass }}" href="#"
                       onclick="listViewSetOrder('{{ currentView.getViewName() }}', '{{ key }}');">
                        <i class="{{ icon }} fa-fw" aria-hidden="true"></i> {{ orderby.label }}
                    </a>
                {% endfor %}
            </div>
        </div>
    {% endif %}
{% endmacro %}
