
<!doctype html>
<html>
<head>
<title>{{ title }}</title>
</head>
<body>
    <details open="true">
        <summary>Run Info for {{ title }}</summary>
        <pre id="run_info"></pre>
    </details>

<br/>
<label for="fSearch">Search entire report</label>
<input type="text" id="fSearch" name="fSearch">

<div id="example-table"></div>

<script src="https:////ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<link href="https://unpkg.com/tabulator-tables@5.1.1/dist/css/tabulator.min.css" rel="stylesheet">
<script type="text/javascript" src="https://unpkg.com/tabulator-tables@5.1.1/dist/js/tabulator.min.js"></script>

    <script>
             $(document).ready ( function(report) {
                            const True = true, False = false;
                            var report_data = [];
                            report = {{ json_report }};

                            document.getElementById("run_info").textContent = JSON.stringify(report[0], undefined, 5);
                            delete(report[0])
                            for(var i in report){
                                report[i]["id"]=i
                                report_data.push(report [i]);
                            }
                            var customMutator = function(value, data, type, params, component){
                                    if(value)
                                        return value.join("\n");
                                    return value;
                            }
                            var jsonMutator = function(value, data, type, params, component){
                                    if(value)
                                        return JSON.stringify(value,undefined,2);
                                    return value;
                            }
                            const input = document.getElementById("fSearch");
                            input.addEventListener("keyup", function() {
                                table.setFilter(matchAny, { value: input.value });
                                if (input.value == " ") {
                                    table.clearFilter()
                                }
                            });
                            function matchAny(data, filterParams) {
                                var match = false;
                                const regex = RegExp(filterParams.value, 'i');

                                for (var key in data) {
                                    if (regex.test(data[key]) == true) {
                                        match = true;
                                    }
                                }
                                return match;
                            }

                            var table = new Tabulator("#example-table", {
                                data:report_data,               //load row data from array
                                height:"100%",
                                virtualDomHorizontal:true,
                                layout:"fitData",               //fit columns to width of table
                                addRowPos:"top",                //when adding a new row, add it to the top of the table
                                history:true,                   //allow undo and redo actions on the table
                                pagination:"local",             //paginate the data
                                paginationSize:10,              //allow 7 rows per page of data
                                paginationCounter:"rows",       //display count of paginated rows in footer
                                movableColumns:true,            //allow column order to be changed
                                resizableRows:true,             //allow row order to be changed
                                initialSort:[                   //set the initial sort order of the data
                                    {column:"nrn_run_err", dir:"desc"},
                                ],
                                columns:[                 //define the table columns
                                    {title:"Acc. No.", field:"id"},
                                    {title:"Run Logs", field:"nrn_run",formatter:"textarea", mutator:customMutator,},
                                    {title:"MOD error", field:"moderr",formatter:"textarea", mutator:customMutator,},
                                    {title:"Run error", field:"nrn_run_err",formatter:"tickCross",},
                                    {title:"Run time", field:"run_time",},
                                    {title:"Run info", field:"run_info", formatter:"textarea", mutator:jsonMutator,},
                                ],
                                rowFormatter:function(row){
                                    var data = row.getData();
                                    if(data.moderr){
                                        row.getElement().style.backgroundColor = "#FF8C00"; //apply css change to row element
                                    }
                                    else if(data.nrn_run_err == true){
                                        row.getElement().style.backgroundColor = "#FFA500"; //apply css change to row element
                                    }
                                },

                            });
                        })
    </script>
</body>
</html>