var managementDashboardCbt = {
    settings: {
        ajaxUrl: '',
        dailyManagementCbtDashboardUrl: '',
        monthlyManagementCbtDashboardUrl: '',
        company_autocomplete_url: '',
        branch_autocomplete_url: '',
        provider_autocomplete_url: '',
    },
    init: function() {
        this.initDailyCount(null, null, null, null);
        this.initMonthlyCount(null, null, null, null, null);
        this.initAutocomplete();
    },
    initAutocomplete : function() {

       global.autocomplete.bind(managementDashboardCbt.settings.company_autocomplete_url, '#prod-company-desc', '#prod-company-id');
       global.autocomplete.bind(managementDashboardCbt.settings.branch_autocomplete_url, '#prod-branch-desc', '#prod-branch-id');
       global.autocomplete.bind(managementDashboardCbt.settings.provider_autocomplete_url, '#prod-clinician-desc', '#prod-clinician-id');
    },
    initDailyCount : function (daily_dateFrom, company, branch, clinician) {

        if(jQ('#daily-management-dashboard-cbt-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;

            jQ('.dailyCbtCount').empty();
            jQ.post(managementDashboardCbt.settings.dailyManagementCbtDashboardUrl, data, function (response) {

                jQ('.daily-management-dashboard-cbt').css('display','block');
                jQ('#daily-management-dashboard-cbt-data-loading').addClass('hide');
                jQ('#daily-management-dashboard-cbt-data-title').removeClass('hide');
                jQ('#daily-management-dashboard-cbt-data-container').removeClass('has-loading');

                if(response.appointment.length == 0) {
                    jQ('.dailyCbtCount').append('<div>None</div>');
                } else {
                    for(k=0; k < response.appointment.length; k++) {
                        if(response.appointment[k].length != 0) {
                            jQ('.dailyCbtCount').append('<div><strong>'+response.appointment[k].description+'</strong></div>').append(
                                function() {
                                    var html = '';
                                    for(x=0; x < response.appointment[k].booked.length; x++) {
                                        html +=
                                            '<div>\n' +
                                            '   <ul>\n' +
                                            '       <div>HSAT : <strong>'+response.appointment[k].booked[x].booked_count.hsat_count+'</strong></div>\n' +
                                            '       <div>6 Months Follow up : <strong>'+response.appointment[k].booked[x].booked_count.six_month_ff_count+'</strong></div>\n' +
                                            '   </ul>\n' +
                                            '</div>';
                                    }
                                    return html;
                                }

                            );
                        }
                    }
                }

            }, 'json');
        }
    },
    initMonthlyCount : function (monthly_dateFrom, monthly_dateTo, company, branch, clinician) {

        if(jQ('#monthly-management-dashboard-cbt-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'] = 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('.monthlyCbtCount').empty();

            jQ.post(managementDashboardCbt.settings.monthlyManagementCbtDashboardUrl, data, function (response) {

                jQ('.monthly-management-dashboard-cbt').css('display','block');
                jQ('#monthly-management-dashboard-cbt-data-loading').addClass('hide');
                jQ('#monthly-management-dashboard-cbt-data-title').removeClass('hide');
                jQ('#monthly-management-dashboard-cbt-data-container').removeClass('has-loading');

                if(response.appointment.length == 0) {
                    jQ('.monthlyCbtCount').append('<div>None</div>');
                } else {
                    for(k=0; k < response.appointment.length; k++) {
                        if(response.appointment[k].length != 0) {
                            jQ('.monthlyCbtCount').append('<div><strong>'+response.appointment[k].description+'</strong></div>').append(
                                function() {
                                    var html = '';
                                    for(x=0; x < response.appointment[k].booked.length; x++) {
                                        html +=
                                            '<div>\n' +
                                            '   <ul>\n' +
                                            '       <div>HSAT : <strong>'+response.appointment[k].booked[x].booked_count.hsat_count+'</strong></div>\n' +
                                            '       <div>6 Months Follow up : <strong>'+response.appointment[k].booked[x].booked_count.six_month_ff_count+'</strong></div>\n' +
                                            '   </ul>\n' +
                                            '</div>';
                                    }
                                    return html;
                                }

                            );
                        }
                    }
                }

            }, 'json');
        }
    },
};