{% extends 'datatables/filter_blocks/base_filter_block.html' %}
{% block header_icons %}
    <a class="filter-header-icon all-check" href="javascript:void(0);" title="Select All">
        <i class="select-all fas fa-check-square"></i></a>
    <a class="filter-header-icon none-check" href="javascript:void(0);">
        <i class="select-none far fa-square"></i></a>
{% endblock %}
{% block content %}
    <table class="small w-100">
        <tr>
            <td></td>
            <td class="text-right text-secondary">Filtered</td>
            <td class="text-right">Total</td>
        </tr>
        <tbody class="filter-content">
        </tbody>
    </table>
{% endblock %}

{% block javascript %}
    <script>
        django_datatables.TotalPivotFilter = function(column_no, html_id, options) {
            django_datatables.PivotFilter.call(this, column_no, html_id, options)
            this.filter_calcs.get_value = function (row) {
                return parseFloat(row[options.sum_column]);
            }
            this.storage_key = 'pivot_totals'
            this.refresh = function () {
                totals = this.filter_calcs.calcs
                $("#" + html_id + " .total").each(function () {
                    var result = totals[decodeURI($(this).attr("data-value"))]
                    $(this).html(django_datatables.utilities.numberWithCommas(result[0]))
                })
                $("#" + html_id + " .overall").each(function () {
                    var result = totals[decodeURI($(this).attr("data-value"))]
                    $(this).html(django_datatables.utilities.numberWithCommas(result[1]))
                })
            }
            this.init_filters = function () {
                this.table.api().rows().data().each(function (row) {
                    this.filters.forEach(function (filter) {
                        filter.filter_calcs.init_calcs(row, row[column_no])
                    })
                })
            }
        }
        django_datatables.TotalPivotFilter.prototype = Object.create(django_datatables.PivotFilter.prototype);
        options = {'htmlcheckbox': `{% include 'datatables/filter_rows/checkbox_total.html' %}`,
                   'sum_column': {{ kwargs.sum_column|default:'column_no' }}}
        var new_filter = new django_datatables.TotalPivotFilter({{ column_no }}, '{{ html_id }}', options)
    </script>
{% endblock %}


