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

{% block bodyHeaderOptions %}
    {{ parent() }}
    {% set pageData = fsc.getPageData() %}
    {% set firstView = fsc.views | first %}
    <div class="container-fluid border-bottom d-print-none mt-2 mb-2">
        <div class="row">
            {# -- Top left buttons -- #}
            <div class="col-md-8 col-8 mb-2">
                <div class="btn-group">
                    <a href="{{ firstView.model.url('list') }}" class="btn btn-sm btn-phoenix-secondary">
                        <i class="fas 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-phoenix-secondary"
                       title="{{ trans('refresh') }}">
                        <i class="fas 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-phoenix-secondary">
                        <i class="fas 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 %}
            </div>
            {# -- Top right text -- #}
            <div class="col-md col-4 text-end">
                <h1 class="h4">
                    {{ trans(pageData.title) }}
                    <i class="{{ pageData.icon }}" aria-hidden="true"></i>
                </h1>
            </div>
        </div>
    </div>
    <ul class="nav nav-tabs d-print-none" 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="btn btn-phoenix-secondary me-1 ps-2 pe-2{{ active }}{{ disable }}" data-bs-toggle="tab" role="tab"
                   aria-controls="{{ viewName }}">
                    <i class="{{ view.icon }}" aria-hidden="true"></i>
                    <span class="d-none d-sm-inline-block">{{ view.title }}</span>
                    {% if view.count > 0 %}
                        <span class="badge badge-secondary">{{ number(view.count, 0) }}</span>
                    {% endif %}
                </a>
            </li>
        {% endfor %}
    </ul>
{% 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 javascripts %}
    {{ parent() }}
    <script>
        $(document).ready(function () {
            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();
            }
        });
    </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-phoenix-secondary mr-3 dropdown-toggle" type="button" data-bs-toggle="dropdown"
                        aria-haspopup="true" aria-expanded="false">
                    <i class="fas fa-wrench fa-fw" aria-hidden="true"></i>
                    <span class="d-none d-lg-inline-block">{{ trans('options') }}</span>
                </button>
                <div class="dropdown-menu">
                    {% 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" aria-hidden="true"></i> {{ view.title }}
                                {% if view.settings.customized %}
                                    <i class="fas fa-user-pen ml-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-phoenix-secondary">
            <i class="fas 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-phoenix-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-right">
            {% 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" 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="fas fa-tools fa-fw" 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" aria-hidden="true"></i>
                        {{ trans(tool.description) }}
                    </a>
                {% endfor %}
            {% endif %}
        </div>
    </div>
    <form action="{{ firstView.model.url() }}" method="post" 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="fas fa-tools fa-fw" aria-hidden="true"></i>
                            {{ trans('advanced') }}
                        </h5>
                        <button type="button" class="close btn" data-bs-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <div class="modal-body text-left">
                        <div class="form-group">
                            <select name="option" class="form-control">
                                {% for key, option in fsc.exportManager.options() %}
                                    <option value="{{ key }}">{{ trans(option.description) }}</option>
                                {% endfor %}
                            </select>
                        </div>
                        <div class="form-group">
                            {{ trans('format') }}
                            <select name="idformat" class="form-control">
                                <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="form-group">
                            {{ trans('language') }}
                            <select name="langcode" class="form-control">
                                <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 %}
