{#
    /**
     * This file is part of FacturaScripts
     * Copyright (C) 2013-2019 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/.
     */
#}

{# IMPORTANT: This template is only executed if the master view has data. #}
{% set currentView = fsc.getCurrentView() %}
{% set currentViewName = currentView.getViewName() %}
{% set detailView = currentView.detailView %}
{% set detailMasterColumns = currentView.getDetailColumns('master') %}
{% set detailViewName = detailView.getViewName() %}
{% set formName = 'formGrid' ~ currentViewName %}
{# -- Scripts -- #}
<script type="text/javascript">
    function gridViewDelete(formViewName) {
        bootbox.confirm({
            title: "{{ i18n.trans('confirm-delete') }}",
            message: "{{ i18n.trans('are-you-sure') }}",
            closeButton: false,
            buttons: {
                cancel: {
                    label: '<i class="fas fa-times"></i> {{ i18n.trans('cancel') }}'
                },
                confirm: {
                    label: '<i class="fas fa-check"></i> {{ i18n.trans('confirm') }}',
                    className: "btn-danger"
                }
            },
            callback: function (result) {
                if (result) {
                    $("#" + formViewName + " :input[name=\"action\"]").val("delete");
                    $("#" + formViewName).submit();
                }
            }
        });

        return false;
    }
</script>
{# -- Main -- #}
<div class="row">
    <div class="col-12">
        {# -- Tabs: When detail view has a master card -- #}
        {% if detailMasterColumns is not empty %}
            {{ _self.tabRender(currentView, i18n) }}
        {% endif %}
        <div class="tab-content">
            <div class="tab-pane show active" id="tab{{ detailViewName }}" role="tabpanel">
                {# -- Row header -- #}
                {% if detailMasterColumns is not empty %}
                    {{ _self.rowRender(fsc, currentView, currentViewName, 'header') }}
                {% endif %}
                {{ _self.rowRender(fsc, detailView, currentViewName, 'header') }}
                {# -- Master data -- #}
                <form id="{{ formName }}" method="post" enctype="multipart/form-data">
                    <input type="hidden" name="action" value="{{ action }}"/>
                    <input type="hidden" name="activetab" value="{{ currentViewName }}"/>
                    <input type="hidden" name="code" value="{{ currentView.model.primaryColumnValue() }}"/>
                    <div class="card shadow">
                        <div class="card-body">
                            <div class="row">
                                <div class="col-12 text-right">
                                    {# -- Row statistics -- #}
                                    {% if detailMasterColumns is not empty %}
                                        {% set row = currentView.getRow('statistics') %}
                                        {{ row.render(fsc) | raw }}
                                    {% endif %}
                                    {% set row = detailView.getRow('statistics') %}
                                    {{ row.render(fsc) | raw }}
                                </div>
                            </div>
                            {% block detailmastercard %}
                                <div class="row">
                                    {% set columns = detailMasterColumns is empty ? currentView.getColumns() : detailMasterColumns %}
                                    {% for group in columns %}
                                        {{ group.edit(currentView.model) | raw }}
                                    {% endfor %}
                                </div>
                            {% endblock %}
                            {% if currentView.model.editable is same as(false) %}
                                <div class="row">
                                    <script type="text/javascript">
                                        documentReadOnly = true;
                                    </script>
                                    <div class="col">
                                        <div class="alert alert-danger text-center mb-0">
                                            <i class="fas fa-lock fa-fw"></i> {{ i18n.trans('non-editable-document') }}
                                        </div>
                                    </div>
                                </div>
                            {% endif %}
                        </div>
                    </div>
                    {# Grid zone #}
                    <div class="row no-gutters mt-2">
                        <script type="text/javascript">
                            documentLineData = {{ currentView.getGridData() | raw }};
                        </script>
                        {% block gridcard %}
                            <div class="col">
                                {# Grid data panel #}
                                <div class="card shadow">
                                    <div class="card-body p-0">
                                        <div id="document-lines"></div>
                                    </div>
                                </div>
                            </div>
                        {% endblock %}
                    </div>
                    {% set maxLines = currentView.getMaxLines() %}
                    {% if maxLines < 200 %}
                    <div class="row">
                        <div class="col-12">
                            <p class="alert alert-warning" role="alert">
                                {{ i18n.trans('max-input-vars-warning', {'%numlines%': maxLines}) }}
                            </p>
                        </div>
                    </div>
                    {% endif %}
                    {# Buttons actions #}
                    <div class="row no-gutters mt-2">
                        <div class="card shadow col-12">
                            <div class="card-body d-flex justify-content-between" style="padding: 1rem 0rem;">
                                {# -- Delete action -- #}
                                <div class="col">
                                    {% if currentView.settings.btnDelete %}
                                        <div class="col">
                                            <button type="button" class="btn btn-sm btn-danger" onclick="gridViewDelete('{{ formName }}');">
                                                <i class="fas fa-trash-alt fa-fw" aria-hidden="true"></i>
                                                <span class="d-none d-sm-inline-block">{{ i18n.trans('delete') }}</span>
                                            </button>
                                        </div>
                                    {% endif %}
                                </div>
                                {# -- Row actions -- #}
                                {% set row = detailView.getRow('actions') %}
                                {% if row is not empty %}
                                    <div class="col d-flex justify-content-center">
                                        {{ row.render(false, currentView.getViewName()) | raw }}
                                    </div>
                                {% endif %}
                                {# -- Save actions -- #}
                                <div class="col text-right">
                                    {% if currentView.settings.btnSave %}
                                        <button class="btn btn-sm btn-primary" type="button" id="save-document" onclick="saveDocument('{{ formName }}');">
                                            <i class="fas fa-save" aria-hidden="true"></i>&nbsp; {{ i18n.trans('save') }}
                                        </button>
                                    {% endif %}
                                </div>
                            </div>
                        </div>
                    </div>
                </form>
                <br/>
                {# -- Row footer -- #}
                {{ _self.rowRender(fsc, detailView, currentViewName, 'footer') }}
                {% if detailMasterColumns is not empty %}
                    {{ _self.rowRender(fsc, currentView, currentViewName, 'footer') }}
                {% endif %}
                {# -- Modals -- #}
                {{ _self.modalRender(detailView, currentViewName) }}
                {% if detailMasterColumns is not empty %}
                    {{ _self.modalRender(currentView, currentViewName) }}
                {% endif %}
            </div>
            {% if detailMasterColumns is not empty %}
                <div class="tab-pane" id="tab{{ currentViewName }}" role="tabpanel">
                    {{ include(currentView.editTemplate) }}
                </div>
            {% endif %}
        </div>
    </div>
</div>

{# -- Macros -- #}
{% macro tabRender(currentView, i18n) %}
    {% set currentViewName = currentView.getViewName() %}
    {% set detailView = currentView.detailView %}
    {% set detailViewName = detailView.getViewName() %}
    <ul class="nav nav-pills mb-3 d-print-none" role="tablist">
        {# Grid View (detail) #}
        <li class="nav-item">
            <a href="#tab{{ detailViewName }}" class="nav-link active" data-toggle="tab" role="tab" aria-controls="tab{{ detailViewName }}">
                <i class="{{ detailView.icon }}" aria-hidden="true"></i>
                <span class="d-none d-sm-inline-block">&nbsp;{{ detailView.title }}</span>
            </a>
        </li>
        {# Edit View (master) #}
        <li class="nav-item">
            <a href="#tab{{ currentViewName }}" class="nav-link" data-toggle="tab" role="tab" aria-controls="tab{{ currentViewName }}">
                <i class="fas fa-edit" aria-hidden="true"></i>
                <span class="d-none d-sm-inline-block">&nbsp;{{ i18n.trans('detail') }}</span>
            </a>
        </li>
    </ul>
{% endmacro %}

{% macro rowRender(fsc, view, viewName, rowName) %}
    <div class="row">
        {% set row = view.getRow(rowName) %}
        {{ row.render(viewName, '', fsc) | raw }}
    </div>
{% endmacro %}

{% macro modalRender(view, viewName) %}
    {% for group in view.getModals() %}
        {{ group.modal(view.model, viewName) | raw }}
    {% endfor %}
{% endmacro %}