{#
    /**
     * This file is part of FacturaScripts
     * Copyright (C) 2017-2020 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/MenuTemplate.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="#">{{ i18n.trans(pageData.menu) }}</a>
                        </li>
                        {% if pageData.submenu %}
                            <li class="breadcrumb-item">
                                <a href="#">{{ i18n.trans(pageData.submenu) }}</a>
                            </li>
                        {% endif %}
                        <li class="breadcrumb-item">
                            <a href="{{ firstView.model.url('list') }}">{{ i18n.trans(pageData.title) }}</a>
                        </li>
                        <li class="breadcrumb-item active" aria-current="page">
                            {{ firstView.model.primaryDescription() | raw }}
                        </li>
                    </ol>
                </nav>
            </div>
        </div>
        <div class="row">
            {# -- Top left buttons -- #}
            <div class="col-md col-6 mb-2">
                <div class="btn-group bg-white">
                    <a href="{{ firstView.model.url('list') }}" class="btn btn-sm btn-outline-secondary">
                        <i class="fas fa-list fa-fw" aria-hidden="true"></i>
                        <span class="d-none d-lg-inline-block">{{ i18n.trans('all') }}</span>
                    </a>
                    <a href="{{ firstView.model.url('edit') }}" class="btn btn-sm btn-outline-secondary" title="{{ i18n.trans('refresh') }}">
                        <i class="fas fa-redo" aria-hidden="true"></i>
                    </a>
                </div>
                {# -- Options button -- #}
                {{ _self.optionsButton(fsc, firstView, i18n) }}
            </div>
            {# -- Top center buttons -- #}
            <div class="col-md col-6 text-right">
                {% if fsc.hasData and firstView.settings.btnNew %}
                    <a href="{{ firstView.model.url('new') }}" class="btn btn-sm btn-success">
                        <i class="fas fa-plus fa-fw" aria-hidden="true"></i>
                        <span class="d-none d-lg-inline-block">{{ i18n.trans('new') }}</span>
                    </a>
                {% endif %}
                {% if fsc.hasData and firstView.settings.btnPrint %}
                    {{ _self.printButton(fsc, firstView, i18n) }}
                {% endif %}
            </div>
            {# -- Top right text -- #}
            <div class="col-md d-none d-md-inline-block text-right">
                <h1 class="h4">
                    {{ i18n.trans(pageData.title) }}<br/>
                    {% if fsc.hasData %}
                        <small class="text-info">{{ firstView.model.primaryDescription() | raw }}</small>
                    {% else %}
                        <small class="text-info">{{ i18n.trans('new') }}</small>
                    {% endif %}
                </h1>
            </div>
            {# -- Top right icon -- #}
            <div class="col-lg-1 d-none d-lg-inline-block text-center">
                {% set image = fsc.getImageUrl() %}
                {% if image is empty %}
                    <i class="{{ pageData.icon }} fa-3x" aria-hidden="true"></i>
                {% else %}
                    <img src="{{ image }}" alt="{{ fsc.title }}" class="img-thumbnail" style="margin-bottom: 10px;"/>
                {% endif %}
            </div>
        </div>
    </div>
{% endblock %}

{% block body %}
    {% set firstView = fsc.views | first %}
    <div class="container-fluid">
        <div class="row">
            {# -- Left Panel -- #}
            {% if fsc.views | length > 1 %}
                <div class="col-lg-2 d-none d-lg-inline-block">
                    <div class="nav flex-column nav-pills" id="mainTabs" role="tablist">
                        {% for viewName, view in fsc.views %}
                            {% set active = (viewName == fsc.active) ? ' active' : '' %}
                            {% set disable = view.settings.active ? '' : ' disabled' %}
                            <a class="nav-link{{ active }}{{ disable }}" id="{{ viewName }}-tab" data-toggle="pill" href="#{{ viewName }}"
                               role="tab" aria-controls="{{ viewName }}" aria-expanded="true">
                                {% if view.count >= 1 and viewName != firstView.getViewName() %}
                                    <span class='badge badge-secondary float-right'>{{ view.count }}</span>
                                {% endif %}
                                <i class="{{ view.icon }} fa-fw d-none d-xl-inline-block" aria-hidden="true"></i> {{ view.title }}
                            </a>
                        {% endfor %}
                    </div>
                </div>
            {% endif %}
            {# -- Right Panel -- #}
            {% set rightPanelClass = (fsc.views | length > 1) ? 'col-lg-10' : 'col' %}
            <div class="{{ rightPanelClass }}">
                {# -- Tabs on top when screen is small -- #}
                {% if fsc.views | length > 1 %}
                    <div class="nav nav-tabs d-lg-none" role="tablist">
                        {% for viewName, view in fsc.views %}
                            {% set active = (viewName == fsc.active) ? ' active' : '' %}
                            {% set disable = view.settings.active ? '' : ' disabled' %}
                            <a class="nav-link{{ active }}{{ disable }}" id="{{ viewName }}-tab2" data-toggle="tab" href="#{{ viewName }}"
                               role="tab" aria-controls="{{ viewName }}" aria-expanded="true">
                                {% if view.count > 1 %}<span class='badge badge-secondary float-right'>{{ view.count }}</span>{% endif %}
                                <i class="{{ view.icon }} fa-fw" aria-hidden="true"></i>
                            </a>
                        {% endfor %}
                    </div>
                {% endif %}
                {# -- Tab content -- #}
                <div class="tab-content" id="mainTabsContent">
                    {% for viewName, view in fsc.views %}
                        {% set active = (viewName == fsc.active) ? ' show active' : '' %}
                        <div class="tab-pane fade{{ active }}" id="{{ viewName }}" role="tabpanel" aria-labelledby="{{ viewName }}-tab">
                            {% do fsc.setCurrentView(viewName) %}
                            {{ include(view.template) }}
                        </div>
                    {% endfor %}
                </div>
            </div>
        </div>
    </div>
{% endblock %}

{% block css %}
    {{ parent() }}
    <style>
        body {
            background-color: #FAFBFC;
        }
    </style>
{% endblock %}

{% macro optionsButton(fsc, firstView, i18n) %}
    <div class="btn-group">
        <div class="dropdown">
            <button class="btn btn-sm btn-secondary dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                <i class="fas fa-wrench fa-fw" aria-hidden="true"></i>
                <span class="d-none d-md-inline-block">{{ i18n.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 }}
                        </a>
                    {% endif %}
                {% endfor %}
            </div>
        </div>
    </div>
{% endmacro %}

{% macro printButton(fsc, firstView, i18n) %}
    <div class="btn-group bg-white">
        <a href="{{ firstView.model.url() }}&action=export&option={{ fsc.exportManager.defaultOption() }}" target="_blank" class="btn btn-sm btn-outline-secondary">
            <i class="fas fa-print fa-fw" aria-hidden="true"></i>
            <span class="d-none d-lg-inline-block">{{ i18n.trans('print') }}</span>
        </a>
        <button type="button" class="btn btn-sm btn-outline-secondary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
            <span class="sr-only">{{ i18n.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>
                        {{ i18n.trans(option.description) }}
                    </a>
                {% endif %}
            {% endfor %}
            {% if fsc.exportManager.tools is not empty %}
                <div class="dropdown-divider"></div>
                {% for key, tool in fsc.exportManager.tools() %}
                    <a href="{{ tool.link }}" target="_blank" class="dropdown-item">
                        <i class="{{ tool.icon }} fa-fw" aria-hidden="true"></i>
                        {{ i18n.trans(tool.description) }}
                    </a>
                {% endfor %}
            {% endif %}
        </div>
    </div>
{% endmacro %}