var monthlyCountRequest = null;
var encounterRequest = null;
var conversionRequest = null;
var cycleRequest = null;

var managementDashboard = {
    settings: {
        ajaxUrl: '',
        // dailyDashboardUrl: '',
        monthlyDashboardUrl: '',
        chartTypeCyclePatientProgressUrl: '',
        encounterDashboardUrl: '',
        conversionDashboardUrl: '',
        referralManagementDashboardReportUrl: '',
        conversionRedirect_url: '',

        company_autocomplete_url: '',
        branch_autocomplete_url: '',
        provider_autocomplete_url: '',
    },
    init: function (doInit) {
        // this.initDailyCount(null, null, null, null);
        this.initEvents();
        if (doInit) {
            let company = jQ('#prod-company-id').val();
            let branch = jQ('#prod-branch-id').val();
            let clinician = jQ('#prod-clinician-id').val();

            let encounter_date_from = jQ('#encounter_date_from').val().replace(/\//g, '-');
            let encounter_date_to = jQ('#encounter_date_to').val().replace(/\//g, '-');

            let monthly_date_from = jQ('#monthly_date_from').val().replace(/\//g, '-');
            let monthly_date_to = jQ('#monthly_date_to').val().replace(/\//g, '-');

            managementDashboard.initMonthlyCount(monthly_date_from, monthly_date_to, company, branch, clinician, function () {
                    managementDashboard.initEncounter(encounter_date_from, encounter_date_to, company, branch, clinician, function () {
                        /*managementDashboard.initConversion(null, null, null, null, null, function () {
                            managementDashboard.initCycle(null, null, null, null, null);
                        });*/
                    });
                }
            );
            // this.initMonthlyCount(null, null, null, null, null);
            // this.initCycle(null, null, null, null, null);
            // this.initEncounter(null, null, null, null, null);
            // this.initConversion(null, null, null, null, null);
        }
        this.initAutocomplete();
    },
    initAutocomplete: function () {

        global.autocomplete.bind(managementDashboard.settings.company_autocomplete_url, '#prod-company-desc', '#prod-company-id');
        global.autocomplete.bind(managementDashboard.settings.branch_autocomplete_url, '#prod-branch-desc', '#prod-branch-id');
        global.autocomplete.bind(managementDashboard.settings.provider_autocomplete_url, '#prod-clinician-desc', '#prod-clinician-id');
    },
    // initDailyCount : function (daily_dateFrom, company, branch, clinician) {
    //
    //     if(jQ('#daily-management-dashboard-data-container').length) {
    //         var i, j, x, k;
    //         var data = {};
    //         var new_company = company ? company : 'null';
    //         var new_branch = branch ? branch : 'null';
    //         var new_clinician = clinician ? clinician : 'null';
    //
    //         data['dateFrom'] = daily_dateFrom;
    //         data['company'] = new_company;
    //         data['branch'] = new_branch;
    //         data['clinician'] = new_clinician;
    //
    //         var new_date_from = jQ('#daily_date_from').val().replace(/\//g, '-');
    //         var new_date_to = jQ('#daily_date_from').val().replace(/\//g, '-');
    //
    //
    //         jQ('.dailyReferralCount, .dailyCpapSetupCount, .dailyBranchStatusCount').empty();
    //         jQ.post(managementDashboard.settings.dailyDashboardUrl, data, function (response) {
    //             var totalDailyReferral = 0;
    //             jQ('.daily-management-dashboard').css('display','block');
    //             jQ('#daily-management-dashboard-data-loading').addClass('hide');
    //             jQ('#daily-management-dashboard-data-title').removeClass('hide');
    //             jQ('#daily-management-dashboard-data-container').removeClass('has-loading');
    //
    //             if(response.referralCount.length <= 0) {
    //                 jQ('.dailyReferralCount').append('<div>None</div>');
    //             } else {
    //                 for(i=0; i < response.referralCount.length; i++) {
    //                     jQ('.dailyReferralCount').append('<div>'+ response.referralCount[i].healthFacilityDesc +' - <a href="'+managementDashboard.settings.referralManagementDashboardReportUrl+'/'+new_date_from+'/'+new_date_to+'/'+response.referralCount[i].branchId+'" target="_blank"><strong>'+response.referralCount[i].dailyReferralCount+'</strong></a></div>');
    //                     totalDailyReferral = totalDailyReferral + parseInt(response.referralCount[i].dailyReferralCount);
    //                 }
    //             }
    //
    //             jQ('.totalDailyReferral').html('Total: '+totalDailyReferral);
    //
    //             if(response.branchTarget.length == 0) {
    //                 jQ('.dailyBranchStatusCount').append('<div>None</div>');
    //             } else {
    //                 for(k=0; k < response.branchTarget.length; k++) {
    //
    //                     if(response.branchTarget[k].healthFacilities.length != 0) {
    //                         jQ('.dailyBranchStatusCount').append('<u><strong>'+response.branchTarget[k].description+'</strong></u>').append(
    //                             function() {
    //                                 var html = '';
    //                                 for(x=0; x < response.branchTarget[k].healthFacilities.length; x++) {
    //                                     html +=
    //                                         '<div>\n' +
    //                                         '   <div>'+response.branchTarget[k].healthFacilities[x].description+' (Target: <strong>'+response.branchTarget[k].target * response.branchTarget[k].healthFacilities[x].no_of_clinician+'</strong>)</div>\n' +
    //                                         '   <ul>\n' +
    //                                         '       <li>Completed : <strong>'+response.branchTarget[k].healthFacilities[x].counts.completedCount+'</strong></li>\n' +
    //                                         '       <li>Cancelled : <strong>'+response.branchTarget[k].healthFacilities[x].counts.cancelledCount+'</strong></li>\n' +
    //                                         '       <li>No Show : <strong>'+response.branchTarget[k].healthFacilities[x].counts.noShowCount+'</strong></li>\n' +
    //                                         '       </ul>\n' +
    //                                         '</div>';
    //                                 }
    //                                 return html;
    //                             }
    //
    //                         );
    //                     }
    //                 }
    //             }
    //
    //             jQ('#dailyBookedCount').html(response.statusCount.dailyBookedCount);
    //             jQ('#dailyCallBackCount').html(response.statusCount.dailyCallBackCount);
    //             jQ('#dailyVoiceMailCount').html(response.statusCount.dailyVoiceMailCount);
    //
    //             managementDashboard.initDashboardDailyCount();
    //         }, 'json');
    //     }
    // },
    initMonthlyCount: function (monthly_dateFrom, monthly_dateTo, company, branch, clinician, callback = function () {
    }) {
        //ajaxPost Abort
        if (monthlyCountRequest != null)
            monthlyCountRequest.abort();

        if (jQ('#monthly-management-dashboard-data-container').length) {
            var i, j;
            var data = {};
            var new_company = company ? company : 'null';
            var new_branch = branch ? branch : 'null';
            var new_clinician = clinician ? clinician : 'null';

            data['dateFrom'] = monthly_dateFrom;
            data['dateTo'] = monthly_dateTo;
            data['company'] = new_company;
            data['branch'] = new_branch;
            data['clinician'] = new_clinician;

            var new_date_from = jQ('#monthly_date_from').val().replace(/\//g, '-');
            var new_date_to = jQ('#monthly_date_to').val().replace(/\//g, '-');

            jQ('.monthlyReferralCount, .monthlyBranchStatusCount').empty();

            jQ('.monthly-management-dashboard').css('display', 'none');
            jQ('#monthly-management-dashboard-data-loading').removeClass('hide');
            jQ('#monthly-management-dashboard-data-title').addClass('hide');
            jQ('#monthly-management-dashboard-data-container').addClass('has-loading');
            monthlyCountRequest = jQ.post(managementDashboard.settings.monthlyDashboardUrl, data, function (response) {
                var totalMonthlyReferral = 0;
                var totalBooking = 0;
                jQ('.monthly-management-dashboard').css('display', 'block');
                jQ('#monthly-management-dashboard-data-loading').addClass('hide');
                jQ('#monthly-management-dashboard-data-title').removeClass('hide');
                jQ('#monthly-management-dashboard-data-container').removeClass('has-loading');

                if (response.referralCount.length <= 0) {
                    jQ('.monthlyReferralCount').append('<div>No Data</div>');
                } else {
                    for (i = 0; i < response.referralCount.length; i++) {
                        jQ('.monthlyReferralCount').append('<div>' + response.referralCount[i].healthFacilityDesc + ' - <a href="' + managementDashboard.settings.managementDashboardPatientProgress_url + '/null/null/' + new_date_from + '/' + new_date_to + '/' + new_company + '/' + response.referralCount[i].branchId + '/null/null/null/null/null/null/null/null" target="_blank"><strong>' + response.referralCount[i].monthlyReferralCount + '</strong></a></div>');
                        totalMonthlyReferral = totalMonthlyReferral + parseInt(response.referralCount[i].monthlyReferralCount);
                    }
                }
                jQ('.totalMonthlyReferral').html('Total: ' + totalMonthlyReferral);

                if (response.bookingCount.length <= 0) {
                    jQ('.monthlyAppointmentCounts').append('<div>No Data</div>');
                } else {
                    for (i = 0; i < response.bookingCount.length; i++) {
                        jQ('.monthlyAppointmentCounts').append('<div>' + response.bookingCount[i].chartType + ' :' + response.bookingCount[i].countOfPatient);
                        totalBooking = totalBooking + parseInt(response.bookingCount[i].countOfPatient);
                    }
                }
                jQ('#totalBooking').html('Total Booking(s) :' + totalBooking);

                managementDashboard.initDashboardMonthlyCount();
                callback();
            }, 'json').fail(function(xhr, status, error) {
                // error handling
                jQ('#monthly-management-dashboard-data-container').removeClass('has-loading');
                global.flashMessage('alert', ['Error encountered loading dashboard data.']);
            });
        }
    },
    initCycle: function (cycle_dateFrom, cycle_dateTo, company, branch, clinician) {
        //ajaxPost Abort
        if (cycleRequest != null)
            cycleRequest.abort();

        var i;
        if (jQ('#cycle-management-dashboard-data-container').length) {
            var data = {};
            var new_company = company ? company : 'null';
            var new_branch = branch ? branch : 'null';
            var new_clinician = clinician ? clinician : 'null';

            data['dateFrom'] = cycle_dateFrom;
            data['dateTo'] = cycle_dateTo;
            data['company'] = new_company;
            data['branch'] = new_branch;
            data['clinician'] = new_clinician;

            var date_from = jQ('#cycle_date_from').val();
            var date_to = jQ('#cycle_date_to').val();
            var new_date_from = date_from.replace(/\//g, '-');
            var new_date_to = date_to.replace(/\//g, '-');
            jQ('.cycle').empty();
            jQ('.totalCycle').empty();
            jQ('.cycle-management-dashboard').css('display', 'none');
            jQ('#cycle-management-dashboard-data-loading').removeClass('hide');
            jQ('#cycle-management-dashboard-data-title').addClass('hide');
            jQ('#cycle-management-dashboard-data-container').addClass('has-loading');
            cycleRequest = jQ.post(managementDashboard.settings.cycleDashboardUrl, data, function (response) {
                var totalCycle = 0;
                jQ('.cycle-management-dashboard').css('display', 'block');
                jQ('#cycle-management-dashboard-data-loading').addClass('hide');
                jQ('#cycle-management-dashboard-data-title').removeClass('hide');
                jQ('#cycle-management-dashboard-data-container').removeClass('has-loading');

                if (response.length <= 0) {
                    jQ('.cycle').append('<div>No Data</div>');
                } else {
                    for (i = 0; i < response.encounter_cycle.length; i++) {
                        jQ('.cycle').append('<div>' + response.encounter_cycle[i].chartType + ' - <a href="' + managementDashboard.settings.chartTypeCyclePatientProgressUrl + '/' + new_date_from + '/' + new_date_to + '/' + new_company + '/' + new_branch + '/' + new_clinician + '/' + response.encounter_cycle[i].chartType + '" target="_blank"><strong>' + response.encounter_cycle[i].cnt + '</strong></a></div>');
                        totalCycle = totalCycle + parseInt(response.encounter_cycle[i].cnt);
                    }

                }

                jQ('.totalCycle').html('<strong>Total: </strong>' + totalCycle);
                jQ('.cnt1').html('<span>Patients w/o Encounter: <a href="' + managementDashboard.settings.conversionRedirect_url + '/' + new_date_from + '/' + new_date_to + '/' + new_company + '/' + new_branch + '/0" target="_blank"><strong>' + response.conv1[0].cnt1 + '</strong></a>');
                jQ('.cnt2').html('<span>Patients w/o HSAT Results Interpretation: </span><a href="' + managementDashboard.settings.conversionRedirect_url + '/' + new_date_from + '/' + new_date_to + '/' + new_company + '/' + new_branch + '/1" target="_blank"><strong>' + response.conv2[0].cnt2 + '</strong></a>');
                jQ('.cnt3').html('<span>Patients with HSAT Results Interpretation w/o CPAP Setup: </span><a href="' + managementDashboard.settings.conversionRedirect_url + '/' + new_date_from + '/' + new_date_to + '/' + new_company + '/' + new_branch + '/2" target="_blank"><strong>' + response.conv3[0].cnt3 + '</strong></a>');
                jQ('.cnt4').html('<span>CPAP Setup w/o Invoice: </span><a href="' + managementDashboard.settings.conversionRedirect_url + '/' + new_date_from + '/' + new_date_to + '/' + new_company + '/' + new_branch + '/3" target="_blank"><strong>' + response.conv4[0].cnt4 + '</strong></a>');
            }, 'json').fail(function(xhr, status, error) {
                // error handling
                jQ('#cycle-management-dashboard-data-container').removeClass('has-loading');
                global.flashMessage('alert', ['Error encountered loading dashboard data.']);
            });

        }
    },
    initConversion: function (conversion_dateFrom, conversion_dateTo, company, branch, clinician, callback = function () {
    }) {
        //ajaxPost Abort
        if (conversionRequest != null)
            conversionRequest.abort();

        if (jQ('#conversion-management-dashboard-data-container').length) {
            var i, j;
            var data = {};
            var new_company = company ? company : 'null';
            var new_branch = branch ? branch : 'null';
            var new_clinician = clinician ? clinician : 'null';

            data['dateFrom'] = conversion_dateFrom;
            data['dateTo'] = conversion_dateTo;
            data['company'] = new_company;
            data['branch'] = new_branch;
            data['clinician'] = new_clinician;
            var date_from = jQ('#conversion_date_from').val();
            var date_to = jQ('#conversion_date_to').val();

            var new_date_from = date_from.replace(/\//g, '-');
            var new_date_to = date_to.replace(/\//g, '-');

            jQ('.conversion').empty();
            jQ('.invoices').empty();
            jQ('.conversion_referral_count').removeClass('hide');
            jQ('.conversion_referral_count').empty();
            jQ('#conversion-management-dashboard-data-loading').removeClass('hide');
            jQ('.conversion-management-dashboard').css('display', 'none');
            jQ('#conversion-management-dashboard-data-title').addClass('hide');
            jQ('#conversion-management-dashboard-data-container').addClass('has-loading');
            conversionRequest = jQ.post(managementDashboard.settings.conversionDashboardUrl, data, function (response) {
                var referrals = response.length != 0 ? response[1].referrals : 0;
                jQ('.conversion-management-dashboard').css('display', 'block');
                jQ('#conversion-management-dashboard-data-loading').addClass('hide');
                jQ('#conversion-management-dashboard-data-title').removeClass('hide');
                jQ('#conversion-management-dashboard-data-container').removeClass('has-loading');
                jQ('.conversion_referral_count').html('Referral(s): <a href="' + managementDashboard.settings.managementDashboardPatientProgress_url + '/' + new_date_from + '/' + new_date_to + '/' + new_company + '/' + new_branch + '/' + new_clinician + '" target="_blank"><strong>' + referrals + '</strong></a>');
                if (response.length == 0) {
                    jQ('.conversion').html('<div>No Data</div>');
                } else {
                    var percentage = 0;
                    for (i = 0; i < response.length; i++) {
                        jQ('.conversion_referral_count').removeClass('hide');
                        if (response[i].length != 0) {

                            // console.log(response[i].percentage[i].percentage);
                            // var prcntg = response[i].percentage[i] !== undefined ? response[i].percentage[i].percentage : 0;
                            percentage = response[i].percentage != undefined ? '(' + Math.round(response[i].percentage.toFixed(2)) + '%)' : '';
                            jQ('.conversion').append('<div><strong>' + response[i].description + ' ' + percentage + '</strong></div>').append(
                                function () {
                                    var html = '';
                                    for (j = 0; j < response[i].chart_types.length; j++) {

                                        html +=
                                            '<div>\n' +
                                            '   <div></div>\n' + //(Number of Clinician * Target per Encounter Type)
                                            '   <ul>\n' +
                                            '       <div>' + response[i].chart_types[j].patient_chart_count.chart_status + ' : <a href="' + managementDashboard.settings.managementDashboardPatientProgress_url + '/' + new_date_from + '/' + new_date_to + '/' + new_company + '/' + new_branch + '/' + new_clinician + '/' + response[i].description + '/' + response[i].chart_types[j].patient_chart_count.chart_status + '" target="_blank"><strong>' + response[i].chart_types[j].patient_chart_count.chart_status_count + '</strong></a></div>\n' +
                                            '   </ul>\n' +
                                            '</div>';


                                    }
                                    return html;
                                }
                            );

                            jQ('.invoices').html('<div><strong>Invoices (' + Math.round(response[i].invoicePercentage.toFixed(2)) + '%)</strong> <a href="' + managementDashboard.settings.managementDashboardPatientProgress_url + '/' + new_date_from + '/' + new_date_to + '/' + new_company + '/' + new_branch + '/' + new_clinician + '/null/null/invoices" target="_blank"><strong>' + response[0].invoices + '</strong></div>')
                        } else {
                            jQ('.conversion_referral_count').addClass('hide');
                            jQ('.conversion').html('<div>No Data</div>');
                        }
                    }
                }
                callback();
            }, 'json').fail(function(xhr, status, error) {
                // error handling
                jQ('#conversion-management-dashboard-data-container').removeClass('has-loading');
                global.flashMessage('alert', ['Error encountered loading dashboard data.']);
            });
        }
    },
    initEncounter: function (encounter_dateFrom, encounter_dateTo, company, branch, clinician, callback = function () {
    }) {
        //ajaxPost Abort
        if (encounterRequest != null)
            encounterRequest.abort();

        if (jQ('#encounter-management-dashboard-data-container').length) {
            var i, j;
            var data = {};
            var new_company = company ? company : 'null';
            var new_branch = branch ? branch : 'null';
            var new_clinician = clinician ? clinician : 'null';

            data['dateFrom'] = encounter_dateFrom;
            data['dateTo'] = encounter_dateTo;
            data['company'] = new_company;
            data['branch'] = new_branch;
            data['clinician'] = new_clinician;
            var date_from = jQ('#encounter_date_from').val();
            var date_to = jQ('#encounter_date_to').val();

            var new_date_from = date_from.replace(/\//g, '-');
            var new_date_to = date_to.replace(/\//g, '-');

            jQ('.encounter').empty();
            jQ('.encounter_referral_count').empty();
            jQ('.encounter-management-dashboard').css('display', 'none');
            jQ('#encounter-management-dashboard-data-loading').removeClass('hide');
            jQ('#encounter-management-dashboard-data-title').addClass('hide');
            jQ('#encounter-management-dashboard-data-container').addClass('has-loading');
            encounterRequest = jQ.post(managementDashboard.settings.encounterDashboardUrl, data, function (response) {
                jQ('.encounter-management-dashboard').css('display', 'block');
                jQ('#encounter-management-dashboard-data-loading').addClass('hide');
                jQ('#encounter-management-dashboard-data-title').removeClass('hide');
                jQ('#encounter-management-dashboard-data-container').removeClass('has-loading');
                // jQ('.encounter_referral_count').html('Referral(s): <a href="'+managementDashboard.settings.referralManagementDashboardReportUrl+'/'+new_date_from+'/'+new_date_to+'/'+new_company+'/'+new_branch+'" target="_blank"><strong>'+response[0].referrals[0].totalReferral+'</strong></a>');
                if (response <= 0) {
                    jQ('.encounter').append('<div>No Data</div>');
                } else {
                    for (i = 0; i < response.length; i++) {
                        if (response[i].length != 0) {

                            // console.log(response[i].percentage[i].percentage);
                            // var prcntg = response[i].percentage[i] !== undefined ? response[i].percentage[i].percentage : 0;
                            jQ('.encounter').append('<div><strong>' + response[i].description + '</strong></div>').append(
                                function () {
                                    var html = '';
                                    for (j = 0; j < response[i].chart_types.length; j++) {
                                        html +=
                                            '<div>\n' +
                                            '   <div></div>\n' + //(Number of Clinician * Target per Encounter Type)
                                            '   <ul>\n' +
                                            '       <div>' + response[i].chart_types[j].patient_chart_count.chart_status + ' : <a href="' + managementDashboard.settings.chartTypeEncounterReportUrl + '/' + new_date_from + '/' + new_date_to + '/' + new_company + '/' + new_branch + '/' + new_clinician + '/' + response[i].chart_types[j].chart_type_id + '/' + response[i].chart_types[j].patient_chart_count.chart_status + '" target="_blank"><strong>' + response[i].chart_types[j].patient_chart_count.chart_status_count + '</strong></a></div>\n' +
                                            '   </ul>\n' +
                                            '</div>';
                                    }
                                    return html;
                                }
                            );
                        }
                    }


                    // for (i=0; i < response.length; i++) {
                    //     jQ('.encounter').append('<div>'+response[i].chartType+' - <a href="'+managementDashboard.settings.chartTypeEncounterReportUrl+'/'+new_date_from+'/'+new_date_to+'/'+response[i].chartTypeId+'" target="_blank"><strong>'+response[i].chartCount+'</strong></a></div>');
                    //     totalEncounter = totalEncounter + parseInt(response[i].chartCount);
                    // }

                }
                callback();
            }, 'json').fail(function(xhr, status, error) {
                // error handling
                jQ('#encounter-management-dashboard-data-container').removeClass('has-loading');
                if (status !== "abort") {
                    global.flashMessage('alert', ['Error encountered loading dashboard data.']);
                }
            });


        }
    },
    initEvents: function () {
        jQ('.monthly_filter').on('click', function () {
            var company = jQ('#prod-company-id').val();
            var branch = jQ('#prod-branch-id').val();
            var clinician = jQ('#prod-clinician-id').val();

            var monthly_date_from = jQ('#monthly_date_from').val().replace(/\//g, '-');
            var monthly_date_to = jQ('#monthly_date_to').val().replace(/\//g, '-');

            managementDashboard.initMonthlyCount(monthly_date_from, monthly_date_to, company, branch, clinician);
        });

        jQ('.encounter_filter').on('click', function () {
            var company = jQ('#prod-company-id').val();
            var branch = jQ('#prod-branch-id').val();
            var clinician = jQ('#prod-clinician-id').val();

            var encounter_date_from = jQ('#encounter_date_from').val().replace(/\//g, '-');
            var encounter_date_to = jQ('#encounter_date_to').val().replace(/\//g, '-');

            managementDashboard.initEncounter(encounter_date_from, encounter_date_to, company, branch, clinician);
        });

        jQ('.conversion_filter').on('click', function () {
            var company = jQ('#prod-company-id').val();
            var branch = jQ('#prod-branch-id').val();
            var clinician = jQ('#prod-clinician-id').val();

            var conversion_date_from = jQ('#conversion_date_from').val().replace(/\//g, '-');
            var conversion_date_to = jQ('#conversion_date_to').val().replace(/\//g, '-');

            managementDashboard.initConversion(conversion_date_from, conversion_date_to, company, branch, clinician);
        });

        jQ('.overdue_filter').on('click', function () {
            var company = jQ('#prod-company-id').val();
            var branch = jQ('#prod-branch-id').val();
            var clinician = jQ('#prod-clinician-id').val();

            var cycle_date_from = jQ('#cycle_date_from').val().replace(/\//g, '-');
            var cycle_date_to = jQ('#cycle_date_to').val().replace(/\//g, '-');

            managementDashboard.initCycle(cycle_date_from, cycle_date_to, company, branch, clinician);
        });

        jQ('input:radio[name="cycle"]').change(
            function () {

                if (jQ(this).is(':checked') && jQ(this).val() == 'month') {
                    var dateNow = new Date();
                    var monthDate = new Date(dateNow.getFullYear(), dateNow.getMonth(), 1);
                    jQ('.month_cycle_date_from').datepicker('setDate', monthDate);
                    jQ('.month_cycle_date_to').datepicker('setDate', dateNow);

                    var cycle_dateFrom = jQ('.month_cycle_date_from').val();
                    var cycle_dateTo = jQ('.month_cycle_date_to').val();

                    managementDashboard.initCycle(cycle_dateFrom, cycle_dateTo);
                }

                if (jQ(this).is(':checked') && jQ(this).val() == 'previous') {
                    var dateFrom = new Date();
                    var dateTo = new Date();
                    dateFrom.setDate(dateFrom.getDate() - 73);
                    dateTo.setDate(dateTo.getDate() - 73 + 30);

                    jQ('.month_cycle_date_from').datepicker('setDate', dateFrom);
                    jQ('.month_cycle_date_to').datepicker('setDate', dateTo);

                    var cycle_dateFrom = jQ('.month_cycle_date_from').val();
                    var cycle_dateTo = jQ('.month_cycle_date_to').val();

                    managementDashboard.initCycle(cycle_dateFrom, cycle_dateTo);
                }
            });

        jQ('.reset_filter').on('click', function () {
            var dateFrom = new Date();
            var dateTo = new Date();
            dateFrom.setDate(dateFrom.getDate() - 73);
            dateTo.setDate(dateTo.getDate() - 73 + 30);

            var dateNow = new Date();
            var monthDate = new Date(dateNow.getFullYear(), dateNow.getMonth(), 1);

            //RESET DATES
            jQ('.monthly_date_from').datepicker("setDate", monthDate);
            jQ('.monthly_date_to').datepicker("setDate", dateNow);

            jQ('.encounter_date_from').datepicker("setDate", monthDate);
            jQ('.encounter_date_to').datepicker("setDate", dateNow);

            jQ('.conversion_date_from').datepicker("setDate", dateFrom);
            jQ('.conversion_date_to').datepicker("setDate", dateTo);

            jQ('.month_cycle_date_from').datepicker("setDate", dateFrom);
            jQ('.month_cycle_date_to').datepicker("setDate", dateTo);

            //RESET FILTERS
            jQ('#prod-company-desc').val('');
            jQ('#prod-company-id').val('');

            jQ('#prod-clinician-desc').val('');
            jQ('#prod-clinician-id').val('');

            jQ('#prod-branch-desc').val('');
            jQ('#prod-branch-id').val('');

            // var daily_dateFrom = jQ('#daily_date_from').val();

            var monthly_date_from = jQ('#monthly_date_from').val().replace(/\//g, '-');
            var monthly_date_to = jQ('#monthly_date_to').val().replace(/\//g, '-');

            var encounter_date_from = jQ('#encounter_date_from').val().replace(/\//g, '-');
            var encounter_date_to = jQ('#encounter_date_to').val().replace(/\//g, '-');

            var conversion_date_from = jQ('#conversion_date_from').val().replace(/\//g, '-');
            var conversion_date_to = jQ('#conversion_date_to').val().replace(/\//g, '-');

            var cycle_dateFrom = jQ('.month_cycle_date_from').val();
            var cycle_dateTo = jQ('.month_cycle_date_to').val();

            // managementDashboard.initDailyCount(daily_dateFrom, null, null, null);
            managementDashboard.initMonthlyCount(monthly_date_from, monthly_date_to, null, null, null);
            managementDashboard.initEncounter(encounter_date_from, encounter_date_to, null, null, null);
            managementDashboard.initCycle(cycle_dateFrom, cycle_dateTo, null, null, null);
            managementDashboard.initConversion(conversion_date_from, conversion_date_to, null, null, null);
        });

        jQ('.preview_patient_cycle').click(function () {
            const path = jQ(this).data('path'),
                from = jQ('#cycle_date_from').val(),
                to = jQ('#cycle_date_to').val();

            window.open(`${global.settings.url}/${path}/null/null/${from.replace(/\//g, '-')}/${to.replace(/\//g, '-')}/null/null/null/null/null/null/null/null/null/null`, '_blank');
        });

    },
    initDashboardMonthlyCount: function () {
        function commaSeparateNumber(val) {
            while (/(\d+)(\d{3})/.test(val.toString())) {
                val = val.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
            }
            return val;
        }

        /** DASHBOARD MONTHLY COUNT **/
        // Animate the element's value from x to y:
        jQ({
            MonthlyBookedCount: 0,
            MonthlyCallBackCount: 0,
            MonthlyVoiceMailCount: 0,
        }).animate({
            MonthlyBookedCount: jQ('#monthlyBookedCount').html(),
            MonthlyCallBackCount: jQ('#monthlyCallBackCount').html(),
            MonthlyVoiceMailCount: jQ('#monthlyVoiceMailCount').html()
        }, {
            duration: 1000,
            easing: 'swing', // can be anything
            step: function () { // called on every step
                // Update the element's text with rounded-up value:
                jQ('.monthlyBookedCount').text(commaSeparateNumber(Math.round(this.MonthlyBookedCount)));
                jQ('.monthlyCallBackCount').text(commaSeparateNumber(Math.round(this.MonthlyCallBackCount)));
                jQ('.monthlyVoiceMailCount').text(commaSeparateNumber(Math.round(this.MonthlyVoiceMailCount)));
            }
        });
    },
};