var statusReport = {
    settings: {
        statusReport_data_url: '',
        statusReport_preview_url: '',
        statusReport_redirect_preview_url: '',
        new_date_from: '',
        new_date_to: '',
    },
    init: function() {
        this.bindAutoComplete();
        this.statusReportData(null, null);
    },
    bindAutoComplete: function() {
      //

    },
    statusReportData: function(dateFrom, dateTo) {
        if(jQ('#status-report-data-container').length) {

            var requestName = 'statusReportData';

            var salesData = [], salesStatusData = {};
            var referralData = [], referralStatusData = {};

            var dataSet = {};
            dataSet['dateFrom'] = dateFrom;
            dataSet['dateTo'] = dateTo;
            //global.ajaxCancel(requestName);

            var canvassSales = document.getElementById('status-report-data').getContext('2d');
            var canvassReferral = document.getElementById('referral-status-report-data').getContext('2d');

            var settings = {
                url: statusReport.settings.statusReport_data_url,
                data: dataSet,
                type: 'POST',
                success: function(json) {
                    if(json.success) {

                        jQ('#status-report-data-loading').addClass('hide');
                        jQ('#status-report-data-title').removeClass('hide');
                        jQ('#status-report-data-container').removeClass('has-loading');

                        jQ('#referral-status-report-data-loading').addClass('hide');
                        jQ('#referral-status-report-data-title').removeClass('hide');
                        jQ('#referral-status-report-data-container').removeClass('has-loading');

                        var i;
                        if(json.data.sales.length) {
                            for(i=0; i < json.data.sales.length; i++) {
                                var colors = '';
                                if(json.data.sales[i].healthFacility == 'QSC EDM Mira') {
                                    colors = 'rgba(255, 99, 132, 0.6)';
                                }

                                if(json.data.sales[i].healthFacility == 'QSC EDM South') {
                                    colors = 'rgba(255, 223, 132, 0.6)';
                                }

                                if(json.data.sales[i].healthFacility == 'DSRC Sherwood Park') {
                                    colors = 'rgba(42, 1, 121, 0.6)';
                                }

                                if(json.data.sales[i].healthFacility == 'DSRC EDM Mira') {
                                    colors = 'rgba(4, 11, 132, 0.6)';
                                }

                                if(json.data.sales[i].healthFacility == 'DSRC Calgary North') {
                                    colors = 'rgba(55, 232, 23, 0.6)';
                                }

                                if(json.data.sales[i].healthFacility == 'DSRC EDM South') {
                                    colors = 'rgba(127, 34, 132, 0.6)';
                                }

                                if(json.data.sales[i].healthFacility == 'DSRC Calgary South') {
                                    colors = 'rgba(12, 34, 54, 0.6)';
                                }

                                salesStatusData = {
                                    label : json.data.sales[i].healthFacility,
                                    data: [json.data.sales[i].referralCount, json.data.sales[i].cpapEndEvalCount, json.data.sales[i].cpapSoldCount, json.data.sales[i].invoiceCount],
                                    backgroundColor: colors,
                                    borderWidth:1,
                                    borderColor:'#777',
                                    hoverBorderWidth:1,
                                    hoverBorderColor:'#000'
                                };

                                salesData.push(salesStatusData);
                            }
                        }

                        if(window.ctxChartSales) window.ctxChartSales.destroy();
                        window.ctxChartSales = new Chart(canvassSales, {
                            type: 'bar',
                            data: {
                                labels: ['Referrals', 'CPAP Evaluation', 'Sold', 'Invoices'],
                                datasets: salesData
                            },
                            options: {
                                responsive: true,
                                maintainAspectRatio: false,
                                scales: {
                                    yAxes: [{
                                        ticks: {
                                            beginAtZero: true
                                        }
                                    }]
                                },
                                title:{
                                    display:true,
                                    text: 'Sales Report',
                                    fontSize:25
                                }
                            }
                        });

                        //Click Events Sales Report

                        var paramData = {};
                        document.getElementById("status-report-data").onclick = function(event) {
                            var activePoint = window.ctxChartSales.getElementAtEvent(event);
                            var activePoints = window.ctxChartSales.getElementsAtEvent(event);

                            // make sure click was on an actual point
                            if (activePoint.length > 0) {

                                if (activePoint.length > 0) {
                                    // Get current Dataset
                                    var dataset = activePoint[0]._datasetIndex;
                                    //get the internal index of slice in pie chart
                                    var clickedElementindex = activePoints[0]["_index"];
                                    //get specific label by index
                                    var labelValue = window.ctxChartSales.data.datasets[dataset].label;
                                    //get specific label by y-index
                                    var yLabel = window.ctxChartSales.data.labels[clickedElementindex];
                                    //get value by index
                                    // var value = window.ctxChartSales.data.datasets[dataset].data[clickedElementindex];
                                    var date_from = jQ('#date_from').val();
                                    var date_to = jQ('#date_to').val();
                                    var new_date_from = date_from.replace(/\//g, '-');
                                    var new_date_to = date_to.replace(/\//g, '-');
                                    paramData['branch'] = labelValue;

                                    switch (yLabel) {
                                        case 'Referrals' :
                                            window.open(
                                                statusReport.settings.statusReport_redirect_preview_url+'/'+new_date_from+'/'+new_date_to+'/'+labelValue,
                                                '_blank' // <- This is what makes it open in a new window.
                                            );

                                            break;
                                        case 'CPAP Evaluation' :
                                        case 'Sold' :
                                            window.open(
                                                statusReport.settings.statusReport_redirect_preview_url+'/'+new_date_from+'/'+new_date_to+'/'+labelValue+'/null/null/'+yLabel,
                                                '_blank' // <- This is what makes it open in a new window.
                                            );
                                            break;
                                        case 'Invoices' :
                                            window.open(
                                                statusReport.settings.statusReport_redirect_preview_url+'/'+new_date_from+'/'+new_date_to+'/null/'+labelValue+'/null/'+yLabel,
                                                '_blank' // <- This is what makes it open in a new window.
                                            );
                                            break;
                                        default:
                                            break;
                                    }
                                }
                            }
                        };

                        var j;
                        if(json.data.referral.length) {
                            for(j=0; j < json.data.referral.length; j++) {
                                var colors = '';
                                if(json.data.referral[j].healthFacility == 'QSC EDM Mira') {
                                    colors = 'rgba(255, 99, 132, 0.6)';
                                }

                                if(json.data.referral[j].healthFacility == 'QSC EDM South') {
                                    colors = 'rgba(255, 223, 132, 0.6)';
                                }

                                if(json.data.referral[j].healthFacility == 'DSRC Sherwood Park') {
                                    colors = 'rgba(42, 1, 121, 0.6)';
                                }

                                if(json.data.referral[j].healthFacility == 'DSRC EDM Mira') {
                                    colors = 'rgba(4, 11, 132, 0.6)';
                                }

                                if(json.data.referral[j].healthFacility == 'DSRC Calgary North') {
                                    colors = 'rgba(55, 232, 23, 0.6)';
                                }

                                if(json.data.referral[j].healthFacility == 'DSRC EDM South') {
                                    colors = 'rgba(127, 34, 132, 0.6)';
                                }

                                if(json.data.referral[j].healthFacility == 'DSRC Calgary South') {
                                    colors = 'rgba(12, 34, 54, 0.6)';
                                }

                                referralStatusData = {
                                    label : json.data.referral[j].healthFacility,
                                    data: [json.data.referral[j].referralCount, json.data.referral[j].declinedCount, json.data.referral[j].noShowCount, json.data.referral[j].cancelledCount],
                                    backgroundColor: colors,
                                    borderWidth:1,
                                    borderColor:'#777',
                                    hoverBorderWidth:1,
                                    hoverBorderColor:'#000'
                                };

                                referralData.push(referralStatusData);
                            }
                        }
                        if(window.referralCtxChart) window.referralCtxChart.destroy();
                        window.referralCtxChart = new Chart(canvassReferral, {
                            type: 'bar',
                            data: {
                                labels: ['Referrals', 'Declined', 'No Show', 'Cancelled'],
                                datasets: referralData
                            },
                            options: {
                                responsive: true,
                                maintainAspectRatio: false,
                                scales: {
                                    yAxes: [{
                                        ticks: {
                                            beginAtZero: true
                                        }
                                    }]
                                },
                                title:{
                                    display:true,
                                    text: 'Referral Status',
                                    fontSize:25
                                }
                            }
                        });

                        //Click Events Referral Report

                        document.getElementById("referral-status-report-data").onclick = function(event) {
                            var activePoint = window.referralCtxChart.getElementAtEvent(event);
                            var activePoints = window.referralCtxChart.getElementsAtEvent(event);

                            // make sure click was on an actual point
                            if (activePoint.length > 0) {

                                if (activePoint.length > 0) {
                                    // Get current Dataset
                                    var dataset = activePoint[0]._datasetIndex;
                                    //get the internal index of slice in pie chart
                                    var clickedElementindex = activePoints[0]["_index"];
                                    //get specific label by index
                                    var labelValue = window.referralCtxChart.data.datasets[dataset].label;
                                    //get specific label by y-index
                                    var yLabel = window.referralCtxChart.data.labels[clickedElementindex];
                                    //get value by index
                                    // var value = window.ctxChartSales.data.datasets[dataset].data[clickedElementindex];
                                    var date_from = jQ('#date_from').val();
                                    var date_to = jQ('#date_to').val();
                                    var new_date_from = date_from.replace(/\//g, '-');
                                    var new_date_to = date_to.replace(/\//g, '-');
                                    paramData['branch'] = labelValue;

                                    switch (yLabel) {
                                        case 'Referrals' :
                                            window.open(
                                                statusReport.settings.statusReport_redirect_preview_url+'/'+new_date_from+'/'+new_date_to+'/'+labelValue,
                                                '_blank' // <- This is what makes it open in a new window.
                                            );

                                            break;
                                        case 'Declined' :
                                        case 'No Show' :
                                        case 'Cancelled' :
                                            window.open(
                                                statusReport.settings.statusReport_redirect_preview_url+'/'+new_date_from+'/'+new_date_to+'/'+labelValue+'/null/null/'+yLabel,
                                                '_blank' // <- This is what makes it open in a new window.
                                            );
                                            break;
                                        default:
                                            break;
                                    }
                                }
                            }
                        };

                    } else {
                        location.reload();
                    }
                }
            };

            global.ajax(settings, requestName);
        }
    },
};