var accountUser_detail = {
    settings: {
        account_user_id: null,
        healthFacilities_ajaxUrl: '',
        healthFacilities_formUrl: '',
        healthFacilities_autocompleteUrl: '',
        healthFacilities_formActionUrl: '',

        company_ajaxUrl: '',
        company_formUrl: '',
        company_autocompleteUrl: '',
        company_formActionUrl: '',

        // userGroup_ajaxUrl: '',
        // userGroup_formUrl: '',
        // userGroup_autocompleteUrl: '',
        // userGroup_formActionUrl: '',
    },
    healthFacilitiesDataList: null,
    companyDataList: null,
    userGroupDataList: null,
    init: function() {
        accountUser_detail.initDataTable();
        accountUser_detail.bindhealthFacilitiesForm();
        accountUser_detail.bindCompanyForm();
    },
    bindhealthFacilitiesForm: function() {
        if(jQ('.href-health-facility-form').length > 0) {

            jQ('.href-health-facility-form').unbind('click').bind('click', function(){

                var requestName = 'healthFacilityForm',
                    modal = jQ('#modal'),
                    modalContent = jQ('#modal-content');

                global.ajaxCancel(requestName);

                var settings = {
                    url: accountUser_detail.settings.healthFacilities_formUrl + '/' + jQ(this).data('action') + '/' + jQ(this).data('id'),
                    type: 'GET',
                    beforeSend: function() {
                        modalContent.html('').addClass('has-loading');
                        modal.foundation('open');
                    },
                    success: function(json) {
                        modal.foundation('close');
                        modalContent.removeClass('has-loading').html(json.html);
                        modal.foundation('open');
                        global.bindFormSubmitted();
                        accountUser_detail.bindhealthFacilitiesFormAction();
                    }
                };

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

    bindhealthFacilitiesFormAction: function() {

        jQ('.href-health-facility-action').unbind('click.healthFacilitiesFormAction').bind('click.healthFacilitiesFormAction', function () {

            var requestName = 'healthFacilitiesFormAction',
                modal = jQ('#modal'),
                modalContent = jQ('#modal-content'),
                _form = jQ('#form-healthFacility'),
                axn = jQ(this).data('axn');
            _form.find('.form-action').val(axn);

            global.formSubmitted();
            global.ajaxCancel(requestName);

            var settings = {
                url: accountUser_detail.settings.healthFacilities_formActionUrl,
                type: 'POST',
                data: _form.serialize(),
                success: function (json) {
                    if (json.success) {
                        location.reload();
                    } else {
                        global.formSubmittedReverse();
                        global.parseFlashMessages('#flash-messages-content-modal', 'alert', json.errorMessages);
                    }
                }
            };

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

    bindCompanyForm: function() {
        if(jQ('.href-company-form').length > 0) {

            jQ('.href-company-form').unbind('click').bind('click', function(){

                var requestName = 'companyForm',
                    modal = jQ('#modal'),
                    modalContent = jQ('#modal-content');

                global.ajaxCancel(requestName);

                var settings = {
                    url: accountUser_detail.settings.company_formUrl + '/' + jQ(this).data('action') + '/' + jQ(this).data('id'),
                    type: 'GET',
                    beforeSend: function() {
                        modalContent.html('').addClass('has-loading');
                        modal.foundation('open');
                    },
                    success: function(json) {
                        modal.foundation('close');
                        modalContent.removeClass('has-loading').html(json.html);
                        modal.foundation('open');
                        global.bindFormSubmitted();
                        accountUser_detail.bindCompanyFormAction();
                    }
                };

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

    // bindUserGroupForm: function() {
    //     if(jQ('.href-userGroup-form').length > 0) {
    //
    //         jQ('.href-userGroup-form').unbind('click').bind('click', function(){
    //
    //             var requestName = 'userGroupForm',
    //                 modal = jQ('#modal'),
    //                 modalContent = jQ('#modal-content');
    //
    //             global.ajaxCancel(requestName);
    //
    //             var settings = {
    //                 url: accountUser_detail.settings.userGroup_formUrl + '/' + jQ(this).data('action') + '/' + jQ(this).data('id'),
    //                 type: 'GET',
    //                 beforeSend: function() {
    //                     modalContent.html('').addClass('has-loading');
    //                     modal.foundation('open');
    //                 },
    //                 success: function(json) {
    //                     modal.foundation('close');
    //                     modalContent.removeClass('has-loading').html(json.html);
    //                     modal.foundation('open');
    //                     global.bindFormSubmitted();
    //                     accountUser_detail.bindUserGroupFormAction();
    //                 }
    //             };
    //
    //             global.ajax(settings, requestName);
    //         });
    //     }
    // },

    bindCompanyFormAction: function() {

        jQ('.href-company-action').unbind('click.companyFormAction').bind('click.companyFormAction', function () {

            var requestName = 'companyFormAction',
                modal = jQ('#modal'),
                modalContent = jQ('#modal-content'),
                _form = jQ('#form-company'),
                axn = jQ(this).data('axn');
            _form.find('.form-action').val(axn);

            global.formSubmitted();
            global.ajaxCancel(requestName);

            var settings = {
                url: accountUser_detail.settings.company_formActionUrl,
                type: 'POST',
                data: _form.serialize(),
                success: function (json) {
                    if (json.success) {
                        location.reload();
                    } else {
                        global.formSubmittedReverse();
                        global.parseFlashMessages('#flash-messages-content-modal', 'alert', json.errorMessages);
                    }
                }
            };

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

    // bindUserGroupFormAction: function() {
    //
    //     jQ('.href-userGroup-action').unbind('click.userGroupFormAction').bind('click.userGroupFormAction', function () {
    //
    //         var requestName = 'userGroupFormAction',
    //             modal = jQ('#modal'),
    //             modalContent = jQ('#modal-content'),
    //             _form = jQ('#form-userGroup'),
    //             axn = jQ(this).data('axn');
    //         _form.find('.form-action').val(axn);
    //
    //         global.formSubmitted();
    //         global.ajaxCancel(requestName);
    //
    //         var settings = {
    //             url: accountUser_detail.settings.userGroup_formActionUrl,
    //             type: 'POST',
    //             data: _form.serialize(),
    //             success: function (json) {
    //                 if (json.success) {
    //                     modal.foundation('close');
    //                     accountUser_detail.userGroupDataList.draw();
    //                     //location.reload();
    //                 } else {
    //                     global.formSubmittedReverse();
    //                     global.parseFlashMessages('#flash-messages-content-modal', 'alert', json.errorMessages);
    //                 }
    //             }
    //         };
    //
    //         global.ajax(settings, requestName);
    //     });
    // },

    initDataTable: function() {

        var callBack1 = function() {
            accountUser_detail.bindhealthFacilitiesForm()
        };

        accountUser_detail.healthFacilitiesDataList = jQ('#healthFacilities-datalist').DataTable({
            'processing': true,
            'serverSide': true,
            'ajax': {
                'url': accountUser_detail.settings.healthFacilities_ajaxUrl,
                'data': function(d) {
                    d.url = global.settings.url;
                    d.account_user_id = accountUser_detail.settings.account_user_id;
                }
            },
            'deferRender': true,
            'columnDefs': [
                { 'orderable': false, 'targets': 9 },
                { 'searchable': false, 'targets': 9 }
            ],
            'orderClasses': false,
            drawCallback: function() {
                callBack1();
            },
            responsive: {
                details: {
                    renderer: function( api,rowIdx ) {
                        return global.dataTableResponsiveCallBack(api, rowIdx, callBack1);
                    }
                }
            }
        });

        var callBack2 = function() {
            accountUser_detail.bindCompanyForm()
        };

        accountUser_detail.companyDataList = jQ('#company-datalist').DataTable({
            'processing': true,
            'serverSide': true,
            'ajax': {
                'url': accountUser_detail.settings.company_ajaxUrl,
                'data': function(d) {
                    d.url = global.settings.url;
                    d.account_user_id = accountUser_detail.settings.account_user_id;
                }
            },
            'deferRender': true,
            'columnDefs': [
                { 'orderable': false, 'targets': 4 },
                { 'searchable': false, 'targets': 4 }
            ],
            'orderClasses': false,
            drawCallback: function() {
                callBack2();
            },
            responsive: {
                details: {
                    renderer: function( api,rowIdx ) {
                        return global.dataTableResponsiveCallBack(api, rowIdx, callBack2);
                    }
                }
            }
        });


        // var callBack3 = function() {
        //     accountUser_detail.bindUserGroupForm()
        // };
        // accountUser_detail.userGroupDataList = jQ('#userGroup-datalist').DataTable({
        //     'processing': true,
        //     'serverSide': true,
        //     'ajax': {
        //         'url': accountUser_detail.settings.userGroup_ajaxUrl,
        //         'data': function(d) {
        //             d.url = global.settings.url;
        //             d.account_user_id = accountUser_detail.settings.account_user_id;
        //         }
        //     },
        //     'deferRender': true,
        //     'columnDefs': [
        //         { 'orderable': false, 'targets': 4 },
        //         { 'searchable': false, 'targets': 4 }
        //     ],
        //     'orderClasses': false,
        //     drawCallback: function() {
        //         callBack3();
        //     },
        //     responsive: {
        //         details: {
        //             renderer: function( api,rowIdx ) {
        //                 return global.dataTableResponsiveCallBack(api, rowIdx, callBack2);
        //             }
        //         }
        //     }
        // });

        jQ('.content-container').removeClass('has-loading');
        jQ('.content-container-content').removeClass('hide');
    }
};