<div class="{{ partial_item.grid_columns }} columns">
    <style>
        /* */
    </style>
    <h4 class="table-partial partial-heading">
        {{ partial_item.title }}
    </h4>
    <div class="table-partial partial" id="{{ partial_item.name }}" 
        {% if partial_item.grid_rows %}
        style="height:{{ partial_item.grid_rows * 50}}px; overflow:scroll;"
        {% endif %}
    >
        <table>
            <thead>
                {% for header in partial_item.headers %}
                <th>{{ header }}</th>
                {% endfor %}
            </thead>
            <tbody >
            </tbody>
        </table>
        <button id="{{ partial_item.name }}_first">&lt;&lt;</button>
        <button id="{{ partial_item.name }}_prev">&lt;</button>
        <div style="display:inline-block; width:100px; height:15px; border: 1px solid grey;" id="{{ partial_item.name }}_outerbar">
            <div id="{{ partial_item.name }}_page_num" style="background-color:grey; width:0%; height:100%;"></div>
        </div>
        <button id="{{ partial_item.name }}_next">&gt;</button>
        <button id="{{ partial_item.name }}_last">&gt;&gt;</button>
        <!-- <button id="{{ partial_item.name }}_sort_order">Toggle Sort Order</button>  -->

    </div>
</div>

<script type="text/javascript">
    $(function () {

        var scrolledDiv = $('#{{ partial_item.name }} > table > tbody')[0];
        var nextButton = $('#{{ partial_item.name }}_next')[0];
        var prevButton = $('#{{ partial_item.name }}_prev')[0];
        var firstButton = $('#{{ partial_item.name }}_first')[0];
        var lastButton = $('#{{ partial_item.name }}_last')[0];
        var pageNum = $('#{{ partial_item.name }}_page_num')[0];
        //var sortButton = $('#{{ partial_item.name }}_sort_order')[0];

        var headers = {{ partial_item.headers|tojson|safe }};

        var startStreamConfig = {
            table_name: {{ partial_item.table_name|tojson|safe }},
            key_field: {{ partial_item.key_field|tojson|safe }},
            restrict_by: {{ partial_item.restrict_by|tojson|safe }},
            start_key: {{ partial_item.start_key|tojson|safe }},
            start_item_number: {{ partial_item.start_item_number|tojson|safe }},
            count: {{ partial_item.count|tojson|safe }},
            end_key: {{ partial_item.end_key|tojson|safe }},
            stream_tag: {{ partial_item.stream_tag|tojson|safe }}
        };

        var record_link = "{{ partial_item.record_link }}";
        var table_rows = {{ partial_item.table_rows|tojson|safe }};
        var totalPages = 0;
        var currentPage = 0;

        var renderTable = function() {
            var htmlString = "";
            var cache = tableStream.cache;
            cache.forEach((item) => {
                htmlString += "<tr>";
                headers.forEach( (header) => {
                    if(header in item) {
                        htmlString += "<td>" + "<a href =" + window.format(record_link, {"key" : item[tableStream.config.key_field] }) + ">" + item[header] + "</a></td>";
                    } else {
                        htmlString += "<td></td>";   
                    }
                });
                htmlString += "</tr>";
                scrolledDiv.innerHTML = htmlString;
            });
            totalPages = Math.ceil(tableStream.config.total_item_count/table_rows);
            currentPage = Math.floor(tableStream.config.start_item_number/table_rows) + 1;
            if (totalPages != 0) {
                pageNum.style.width = currentPage/totalPages*100 + "%";
            } else {
                pageNum.style.width = "0%";
            }
        }

        var onNewMessage = function(data) {
            renderTable();
        }
        
        nextButton.onclick = function() {
            var newConfig = tableStream.config;
            if ((newConfig.start_item_number + newConfig.count - 1) < newConfig.total_item_count) {
                newConfig.start_key = null;
                newConfig.end_key = null;
                newConfig.start_item_number = newConfig.start_item_number + table_rows;
                newConfig.count = table_rows;
                tableStream.updateStream(newConfig);                                
                renderTable();
            }            
        };

        prevButton.onclick = function() {
            var newConfig = tableStream.config;
            if (newConfig.start_item_number > 0) {
                newConfig.start_key = null;
                newConfig.end_key = null;
                newConfig.start_item_number = newConfig.start_item_number - table_rows;
                newConfig.count = table_rows;
                tableStream.updateStream(newConfig);
                renderTable();
            }            
        };

        firstButton.onclick = function() {
            var newConfig = tableStream.config;
            newConfig.start_key = null;
            newConfig.end_key = null;
            newConfig.start_item_number = 0;
            newConfig.count = table_rows;
            console.log("Logging new config");
            console.log(newConfig);
            tableStream.updateStream(newConfig);
            renderTable();
        };

        lastButton.onclick = function() {
            var newConfig = tableStream.config;
            newConfig.start_key = null;
            newConfig.end_key = null;
            newConfig.start_item_number = -1 * table_rows;
            newConfig.count = table_rows;
            tableStream.updateStream(newConfig);
            renderTable();
        };

        var tableStream = new Stream(socket, startStreamConfig, onNewMessage);
        tableStream.startStream();

    });
</script>