(function ($) {
    "use strict";

    KirilKirkovWpInvChartsInit();

    function KirilKirkovWpInvChartsInit() {

        function KirilKirkovWIGetRandomRgb() {
            var num = Math.round(0xffffff * Math.random());
            var r = num >> 16;
            var g = num >> 8 & 255;
            var b = num & 255;
            return 'rgb(' + r + ', ' + g + ', ' + b + ')';
        }
        
        jQuery(document).ready(function () {

            let period_values = kkpdfim_inv_charts_data.charts.by_type.period_values;

            /**
             * Bar chart
             */
            let bar_labels = [];
            let bar_datasets = [];
            jQuery.each(period_values, function( index, value ) {
                jQuery.each(value, function( index1, value1 ) {
                    if(index1 === 0) {
                        bar_labels.push(value1);
                    }
                    if(index1 === 1) {
                        bar_datasets.push(value1);
                    }
                });
            });
            const myChart = new Chart( document.getElementById('kirilkirkov-pdf-invoice-manager-report-num-inv'), {
                type: 'bar',
                    data: {
                        labels: bar_labels,
                        datasets: [{
                            label: kkpdfim_inv_charts_data.charts.by_type.series_name,
                            backgroundColor: 'rgb(255, 99, 132)',
                            borderColor: 'rgb(255, 99, 132)',
                            data: bar_datasets,
                        }]
                      },
                    options: {
                    responsive: true,
                    plugins: {
                        legend: {
                            position: 'top',
                        },
                        title: {
                            display: true,
                            text: kkpdfim_inv_charts_data.charts.by_type.series_name
                        }
                    }
                }
            });
            
            /**
             * Line count chart
             */
             let period_values1 = kkpdfim_inv_charts_data.charts.num_by_month.series;
             let line_dataset1 = [];
             jQuery.each(period_values1, function( index, value ) {
                 line_dataset1.push({
                    label: value.name,
                    backgroundColor: KirilKirkovWIGetRandomRgb(),
                    borderColor: KirilKirkovWIGetRandomRgb(),
                    data: value.data,
                 });
             });
             const myChart1 = new Chart( document.getElementById('kirilkirkov-pdf-invoice-manager-num-invoices-by-month'), {
                 type: 'line',
                     data: {
                         labels: kkpdfim_inv_charts_data.charts.num_by_month.categories,
                         datasets: line_dataset1
                       },
                     options: {
                     responsive: true,
                     plugins: {
                         legend: {
                             position: 'top',
                         },
                         title: {
                             display: true,
                             text: kkpdfim_inv_charts_data.charts.num_by_month.title
                         }
                     }
                 }
             });

             /**
             * Line count chart
             */
            let period_values2 = kkpdfim_inv_charts_data.charts.amount_by_month.series;
            let line_dataset2 = [];
            jQuery.each(period_values2, function( index, value ) {
                line_dataset2.push({
                    label: value.name,
                    backgroundColor: KirilKirkovWIGetRandomRgb(),
                    borderColor: KirilKirkovWIGetRandomRgb(),
                    data: value.data,
                });
            });
            const myChart2 = new Chart( document.getElementById('kirilkirkov-pdf-invoice-manager-amount-invoices-by-month'), {
                type: 'line',
                    data: {
                        labels: kkpdfim_inv_charts_data.charts.amount_by_month.categories,
                        datasets: line_dataset2
                    },
                    options: {
                    responsive: true,
                    plugins: {
                        legend: {
                            position: 'top',
                        },
                        title: {
                            display: true,
                            text: kkpdfim_inv_charts_data.charts.amount_by_month.title
                        }
                    }
                }
            });

        });
    }
})(jQuery);