{% extends app.request.xmlHttpRequest?"BackendBaseBundle:Layout:bootstrapModalLayout.html.twig":"BackendBaseBundle:Layout:bootstrapFrameLayout.html.twig" %}
{% import "BackendBaseBundle:Backend/Base:_typeRender.html.twig" as renderer %}

{% block content %}
    <div class="ibox">
        <div class="ibox-content">
            {% include 'BackendBaseBundle:Backend/Base:_search.html.twig' with {searchForm: _config['searchForm']}%}

            {%- include "BackendBaseBundle:Backend/Common:_pager.html.twig" with {index_route: _config['route']['index']|guess_route, index_route_param: {slug: app.session.get('Site').slug}|merge(app.request.attributes.get('_route_params'))}-%}
            {# include出抽離的twig，with後面接的是include樣板帶進來的變數，並在這邊重新命名 #}
            {%- block batch_button_row -%}
            {%- include "BackendBaseBundle:Backend/Base:_indexActionButton.html.twig" with {batchConfig: _config['batch']|default }-%}
            {%- endblock batch_button_row  -%}
            {%- if _config['route']['new']|default(false) and has_role_or_superadmin(_config['security']['new']|default('IS_AUTHENTICATED_FULLY')) -%}
                <a class="btn btn-primary " href="{{ path("new"|guess_route, {slug: app.session.get('Site').slug}|merge(app.request.attributes.get('_route_params'))) }}"><i class="fa fa-plus"></i> {{ 'form.button.add_new_row'|trans({}, 'forms') }}</a>
            {%- endif -%}
            {# 批次刪除用form表單可一次送出 #}
            <form method="post" id="batchForm">
                <table class="footable table table-stripped toggle-arrow-tinys">
                <thead>
                    {% block index_table_head %}
                    <tr>
                        <th><input type="checkbox" data-listAction-all/></th>
                        {%- include 'BackendBaseBundle:Backend/Base:_column.html.twig' with {config: _config.index, with_sort: _config.route.sort|default(false)} -%}
                        {% if _config['action']|length > 0 %}
                        <th>{{ 'index.action'|trans({}, 'forms') }}</th>
                        {% endif %}
                        {%- block thead_custom_action -%}
                        {%- endblock thead_custom_action -%}
                    </tr>
                    {% endblock index_table_head %}
                </thead>
                <tbody>
                    {% for row in pager.results %}
                    <tr>
                        <th><input type="checkbox" name="ids[]" value="{{ row.id }}" data-listAction/></th>
                        {% for columnDisplayName, columnName in _config.index %}
                            <th>
                                {% block index_table_body_row %}
                                {% if columnName is  iterable %}
                                    {% include columnName.template with {data: renderer.render(fetch_relation(row, columnName.field|filter_prefix, has_prefix(columnName.field, '@')), _config.columnMap[columnName.field|filter_prefix]|default)} %}
                                {% else %}
                                    {{ renderer.render(fetch_relation(row, columnName|filter_prefix, has_prefix(columnName, '@')), _config.columnMap[columnName|filter_prefix]|default)}}
                                {% endif %}
                                {% endblock index_table_body_row %}
                            </th>
                        {% endfor %}
                        {% if _config['action']|length > 0 %}
                            <th>
                            {%- for name, route in _config['action'] -%}
                                {%- if route is iterable -%}
                                    {% if (_config['route'][route.action]|default(false)) and has_role_or_superadmin(_config['security'][route.action]|default('IS_AUTHENTICATED_FULLY')) %}
                                    <a class="btn btn-primary"
                                        {%- if route.type|default('link') == 'ajax' %} data-toggle="modal" data-target="#modal"{% endif -%}
                                        {%- if route.confirm is defined %} onclick="return confirm('{{route.confirm|trans({}, 'forms')}}');"{% endif -%}
                                        href="{{ path(route.action|guess_route, {slug: app.session.get('Site').slug, id: row.id, _token: (route.confirm|default(false))?csrf_token(route.action):null }) }}">
                                        <i class="{{ route.class }}"></i> {{ name|trans({}, 'forms') }}
                                    </a>
                                    {% endif %}
                                {%- else -%}
                                    {% if (_config['route'][route]|default(false)) and has_role_or_superadmin(_config['security'][route]|default('IS_AUTHENTICATED_FULLY')) %}
                                    <a class="btn btn-link" href="{{ path(route|guess_route, {slug: app.session.get('Site').slug, id: row.id}) }}">{{- name|trans({}, 'forms') -}}</a>
                                    {% endif %}
                                {%- endif -%}
                            {%- endfor -%}
                            </th>
                        {% endif %}
                        {%- block row_custom_action -%}
                        {%- endblock row_custom_action -%}
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
            </form>
            {%- include "BackendBaseBundle:Backend/Common:_pager.html.twig" with {index_route: _config['route']['index']|guess_route, index_route_param: {slug: app.session.get('Site').slug}|merge(app.request.attributes.get('_route_params'))}-%}
        </div>
    </div>
    <div id="modal" class="modal inmodal fade in" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-lg"><div class="modal-content"></div></div>
    </div>
{% endblock %}
