<script>
    // TODO: select_all_pages_q and id__all_pks_ needs to be namespaced
    function iommi_table_js_select_all(base) {
        var tbody = base.parentNode.parentNode.parentNode.parentNode.querySelector('tbody');
        Array.prototype.forEach.call(tbody.querySelectorAll('.checkbox'), function(el, i) {
            el.click();
        });

        var has_paginator = tbody.parentNode.parentNode.querySelector('.paginator') !== null;

        if (has_paginator) {
            if (tbody.querySelector('.select_all_pages_q') === null) {
                tbody.querySelector('tr').insertAdjacentHTML('beforebegin', '<tr><td colspan="99" style="text-align: center" class="select_all_pages_q">All items on this page are selected. <a onclick="iommi_table_js_select_all_pages(this)" href="#">Select all items</a></td></tr>'
                )
            }
            else {
                tbody.querySelector('.select_all_pages_q').parentNode.parentNode.removeChild(tbody.querySelector('.select_all_pages_q').parentNode);
                var form = base.parentNode.parentNode.parentNode.parentNode.parentNode;
                form.querySelector('#id__all_pks_').value = 0;
            }
        }
    }

    function iommi_table_js_select_all_pages(base) {
        var form = base.parentNode.parentNode.parentNode.parentNode.parentNode;
        var tbody = base.parentNode.parentNode.parentNode.parentNode.querySelector('tbody');
        tbody.querySelector('.select_all_pages_q').textContent = 'All items selected';
        form.querySelector('#id__all_pks_').value = 1;
    }
</script>

<form method="post" action="."><input type="hidden" name="csrfmiddlewaretoken" value="{{ csrf_token }}">

    {% include 'iommi/table/table.html' %}

    {% if bulk_form %}
        {{ bulk_form }}
    {% endif %}

    {{ table.render_actions }}
</form>
