{% load taskmonitor %}

{% random_id as data_id %}

{{ data|json_script:data_id }}
<figure class="highcharts-figure">
    {% include "admin/taskmonitor/report/div_chart_partial.html" with data_id=data_id %}
    <script>
        fetch("{% url 'taskmonitor:admin_taskmonitor_report_json' report_name %}")
            .then((response) => {
                if (response.ok) {
                    return response.json();
                }
                throw new Error(`${response.status} ${response.statusText}: ${response.url}`);
            })
            .then((data) => {
                Highcharts.chart('{{ data_id }}-container', {
                    chart: {
                        type: 'bar',
                    },
                    credits: {
                        enabled: false
                    },
                    title: null,
                    legend: false,
                    plotOptions: {
                        bar: {
                            dataLabels: {
                                enabled: true
                            }
                        }
                    },
                    xAxis: {
                        type: 'category',
                    },
                    series: [{
                            name: 'Tasks',
                            dataLabels: {
                                format: '{point.y:,.0f}'
                            },
                            cursor: 'pointer',
                            point: {
                                events: {
                                    click: function () {
                                        location.href = this.options.url;
                                    }
                                }
                            },
                            data: data.data
                        }]
                    });
            })
            .catch((error) => {
                let div = document.getElementById("{{ data_id }}-container");
                div.innerHTML = `<p class="state-failure">${error}</p>`
            });
    </script>
</figure>
