{#
/**
 * 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/.
 */
#}
{% extends "Master/MenuBghTemplate.html.twig" %}

{% block bodyHeaderOptions %}
    {{ parent() }}
    {% set pageData = fsc.getPageData() %}
    {% set firstView = fsc.views | first %}
    <div class="container-fluid d-print-none">
        <div class="row">
            <div class="col-md-12">
                {# -- Hidden alternative page info -- #}
                <nav aria-label="breadcrumb">
                    <ol class="breadcrumb d-md-none">
                        <li class="breadcrumb-item">
                            <a href="#">{{ trans(pageData.menu) }}</a>
                        </li>
                        {% if pageData.submenu %}
                            <li class="breadcrumb-item">
                                <a href="#">{{ trans(pageData.submenu) }}</a>
                            </li>
                        {% endif %}
                        <li class="breadcrumb-item">
                            <a href="{{ firstView.model.url('list') }}">{{ trans(pageData.title) }}</a>
                        </li>
                        <li class="breadcrumb-item active" aria-current="page">
                            {% if fsc.hasData %}
                                {{ firstView.model.primaryDescription() | raw }}
                            {% else %}
                                {{ trans('new') }}
                            {% endif %}
                        </li>
                    </ol>
                </nav>
            </div>
        </div>
        <div class="row">
            {# -- Top left buttons -- #}
            <div class="col-md-8 mb-2">
                <div class="btn-group">
                    <a href="{{ firstView.model.url('list') }}" class="btn btn-sm btn-secondary">
                        <i class="fa-solid fa-list fa-fw" aria-hidden="true"></i>
                        <span class="d-none d-lg-inline-block">{{ trans('all') }}</span>
                    </a>
                    <a href="{{ firstView.model.url('edit') }}" class="btn btn-sm btn-secondary"
                       title="{{ trans('refresh') }}">
                        <i class="fa-solid fa-redo" aria-hidden="true"></i>
                    </a>
                </div>
                {# -- Options button -- #}
                {{ _self.optionsButton(fsc, firstView, i18n) }}
                {# -- New button -- #}
                {% if fsc.hasData and firstView.settings.btnNew %}
                    <a href="{{ firstView.model.url('new') }}" class="btn btn-sm btn-success">
                        <i class="fa-solid fa-plus fa-fw" aria-hidden="true"></i>
                        <span class="d-none d-lg-inline-block">{{ trans('new') }}</span>
                    </a>
                {% endif %}
                {# -- Action buttons -- #}
                {{ firstView.getRow('actions').renderTop() | raw }}
                {# -- Print button -- #}
                {% if fsc.hasData and firstView.settings.btnPrint %}
                    {{ _self.printButton(fsc, firstView, i18n) }}
                {% endif %}
                {% for includeView in getIncludeViews('PanelControllerTop', 'topButtons') %}
                    {% include includeView['path'] %}
                {% endfor %}
            </div>
            {# -- Top right text -- #}
            <div class="col-md d-none d-md-inline-block text-end">
                {% set image = fsc.getImageUrl() %}
                {% if image is empty %}
                    <i class="{{ pageData.icon }} fa-3x ps-3 float-end" aria-hidden="true"></i>
                {% else %}
                    <img src="{{ image }}" alt="{{ fsc.title }}" class="img-thumbnail ms-3 mb-2 float-end"/>
                {% endif %}
                <h1 class="h5 mb-0">{{ trans(pageData.title) }}</h1>
                {% if fsc.hasData %}
                    <p class="text-info mb-3">{{ firstView.model.primaryDescription() | raw }}</p>
                {% else %}
                    <p class="text-info mb-3">{{ trans('new') }}</p>
                {% endif %}
            </div>
        </div>
    </div>
    <div class="nav-tabs-wrapper d-print-none">
        <ul class="nav nav-tabs nav-tabs-scroll" id="mainTabs" role="tablist">
            {% for viewName, view in fsc.views %}
                <li class="nav-item">
                    {% set active = (viewName == fsc.active) ? ' active' : '' %}
                    {% set disable = view.settings.active ? '' : ' disabled' %}
                    <a href="#{{ viewName }}" class="nav-link{{ active }}{{ disable }}" data-bs-toggle="tab" role="tab"
                       aria-controls="{{ viewName }}">
                        <i class="{{ view.icon }} fa-fw me-1 d-none d-sm-inline" aria-hidden="true"></i>
                        <span>{{ view.title }}</span>
                        {% if view.count > 0 %}
                            <span class="badge bg-secondary">{{ number(view.count, 0) }}</span>
                        {% endif %}
                    </a>
                </li>
            {% endfor %}
        </ul>
    </div>
{% endblock %}

{% block body %}
    <div class="tab-content pt-3" id="mainTabsContent">
        {% for viewName, view in fsc.views %}
            {% set active = (viewName == fsc.active) ? 'tab-pane show active' : 'tab-pane' %}
            <div class="{{ active }}" id="{{ viewName }}" role="tabpanel">
                {% do fsc.setCurrentView(viewName) %}
                {{ include(view.template) }}
            </div>
        {% endfor %}
    </div>
{% endblock %}

{% block css %}
    {{ parent() }}
    <style>
        /* Scroll horizontal para pestañas en móviles */
        @media (max-width: 767px) {
            .nav-tabs-wrapper {
                overflow-x: auto;
                overflow-y: hidden;
                -webkit-overflow-scrolling: touch;
                margin-bottom: -1px;
            }

            .nav-tabs-scroll {
                flex-wrap: nowrap;
                white-space: nowrap;
                border-bottom: 1px solid #dee2e6;
                min-width: min-content;
            }

            .nav-tabs-scroll .nav-item {
                flex-shrink: 0;
            }

            .nav-tabs-scroll .nav-link {
                white-space: nowrap;
            }

            /* Indicador visual de scroll */
            .nav-tabs-wrapper::-webkit-scrollbar {
                height: 4px;
            }

            .nav-tabs-wrapper::-webkit-scrollbar-track {
                background: #f1f1f1;
            }

            .nav-tabs-wrapper::-webkit-scrollbar-thumb {
                background: #888;
                border-radius: 2px;
            }

            .nav-tabs-wrapper::-webkit-scrollbar-thumb:hover {
                background: #555;
            }
        }

        /* En pantallas grandes, comportamiento normal */
        @media (min-width: 768px) {
            .nav-tabs-wrapper {
                overflow: visible;
            }

            .nav-tabs-scroll {
                flex-wrap: wrap;
            }
        }
    </style>
{% endblock %}

{% block javascripts %}
    {{ parent() }}
    <script>
        $(document).ready(function () {
            // Función para desplazar la pestaña activa a la vista
            function scrollActiveTabIntoView() {
                if (window.innerWidth <= 767) {
                    var activeTab = $('.nav-tabs-scroll .nav-link.active');
                    var wrapper = $('.nav-tabs-wrapper');
                    if (activeTab.length && wrapper.length) {
                        var tabLeft = activeTab.parent().position().left;
                        var tabWidth = activeTab.parent().outerWidth();
                        var wrapperWidth = wrapper.width();
                        var scrollLeft = wrapper.scrollLeft();

                        // Centrar la pestaña activa si es posible
                        var targetScroll = tabLeft + scrollLeft - (wrapperWidth / 2) + (tabWidth / 2);
                        wrapper.animate({ scrollLeft: targetScroll }, 300);
                    }
                }
            }

            // Desplazar la pestaña activa al cargar la página
            scrollActiveTabIntoView();

            if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) == false) {
                $("input:visible,textarea:visible").filter(":not([readonly='readonly']):not([disabled='disabled']):not([type='hidden'])").first().focus();
            }

            // Desplazar cuando se cambia de pestaña
            $('a[data-bs-toggle="tab"]').on('shown.bs.tab', function (e) {
                scrollActiveTabIntoView();
            });
        });
    </script>
{% endblock %}

{% macro optionsButton(fsc, firstView, i18n) %}
    {% set show = false %}
    {% for viewName, view in fsc.views %}
        {% if view.settings.active and view.columns is not empty %}
            {% set show = true %}
        {% endif %}
    {% endfor %}
    {% if show and firstView.settings.btnOptions %}
        <div class="btn-group">
            <div class="dropdown">
                <button class="btn btn-sm btn-secondary me-3 dropdown-toggle" type="button" data-bs-toggle="dropdown"
                        aria-haspopup="true" aria-expanded="false">
                    <i class="fa-solid fa-wrench fa-fw" aria-hidden="true"></i>
                    <span class="d-none d-lg-inline-block">{{ trans('options') }}</span>
                </button>
                <div class="dropdown-menu">
                    <h6 class="dropdown-header">{{ trans('options-button-header') }}</h6>
                    {% for viewName, view in fsc.views %}
                        {% if view.settings.active and view.columns is not empty %}
                            <a class="dropdown-item"
                               href="EditPageOption?code={{ viewName }}&url={{ firstView.model.url() | url_encode }}">
                                <i class="{{ view.icon }} fa-fw me-1" aria-hidden="true"></i> {{ view.title }}
                                {% if view.settings.customized %}
                                    <i class="fa-solid fa-user-pen ms-2" title="{{ trans('personalized') }}"></i>
                                {% endif %}
                            </a>
                        {% endif %}
                    {% endfor %}
                </div>
            </div>
        </div>
    {% endif %}
{% endmacro %}

{% macro printButton(fsc, firstView, i18n) %}
    <div class="btn-group">
        <a href="{{ firstView.model.url() }}&action=export&option={{ fsc.exportManager.defaultOption() }}"
           target="_blank" class="btn btn-sm btn-secondary">
            <i class="fa-solid fa-print fa-fw" aria-hidden="true"></i>
            <span class="d-none d-lg-inline-block">{{ trans('print') }}</span>
        </a>
        <button type="button" class="btn btn-sm btn-secondary dropdown-toggle dropdown-toggle-split"
                data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
            <span class="sr-only">{{ trans('more') }}</span>
        </button>
        <div class="dropdown-menu dropdown-menu-end">
            {% for key, option in fsc.exportManager.options() %}
                {% if key != fsc.exportManager.defaultOption() %}
                    <a href="{{ firstView.model.url() }}&action=export&option={{ key }}" class="dropdown-item">
                        <i class="{{ option.icon }} fa-fw me-1" aria-hidden="true"></i>
                        {{ trans(option.description) }}
                    </a>
                {% endif %}
            {% endfor %}
            <div class="dropdown-divider"></div>
            <a href="#" class="dropdown-item" data-bs-toggle="modal" data-bs-target="#advancedExportModal">
                <i class="fa-solid fa-tools fa-fw me-1" aria-hidden="true"></i>
                {{ trans('advanced') }}
            </a>
            {% set tools = fsc.exportManager.tools() %}
            {% if tools is not empty %}
                <div class="dropdown-divider"></div>
                {% for key, tool in tools %}
                    <a href="{{ tool.link }}" target="_blank" class="dropdown-item">
                        <i class="{{ tool.icon }} fa-fw me-1" aria-hidden="true"></i>
                        {{ trans(tool.description) }}
                    </a>
                {% endfor %}
            {% endif %}
        </div>
    </div>
    <form action="{{ firstView.model.url() }}" method="post" class="float-start" target="_blank" onsubmit="animateSpinner('add')">
        <input type="hidden" name="action" value="export"/>
        <div class="modal fade" id="advancedExportModal" tabindex="-1" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title">
                            <i class="fa-solid fa-tools fa-fw" aria-hidden="true"></i>
                            {{ trans('advanced') }}
                        </h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <div class="modal-body text-start">
                        <div class="mb-3">
                            <select name="option" class="form-select">
                                {% for key, option in fsc.exportManager.options() %}
                                    <option value="{{ key }}">{{ trans(option.description) }}</option>
                                {% endfor %}
                            </select>
                        </div>
                        <div class="mb-3">
                            {{ trans('format') }}
                            <select name="idformat" class="form-select">
                                <option value="">{{ trans('default') }}</option>
                                <option value="">------</option>
                                {% for format in fsc.exportManager.getFormats(firstView.model) %}
                                    <option value="{{ format.id }}">{{ format.nombre }}</option>
                                {% endfor %}
                            </select>
                        </div>
                        <div class="mb-3">
                            {{ trans('language') }}
                            <select name="langcode" class="form-select">
                                <option value="">{{ trans('default') }}</option>
                                <option value="">------</option>
                                {% for code, lang in i18n.getAvailableLanguages() %}
                                    <option value="{{ code }}">{{ lang }}</option>
                                {% endfor %}
                            </select>
                        </div>
                        <div class="text-end">
                            <button type="submit" class="btn btn-primary">
                                {{ trans('accept') }}
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>
{% endmacro %}
