var faxTran = {
    settings: {
        ajaxUrl: '',
        unite_ajaxUrl: '',
        faxTranFormUrl: '',
        faxTran_formActionUrl: '',
        health_facility_autocomplete_url: '',
        account_user_autocomplete_url: '',
        patient_autocomplete_url: '',
    },
    dataList: null,
    unite_dataList: null,
    init: function() {
        faxTran.initDataTable();
        // faxTran.bindFaxEvent();

    },
    initDataTable: function() {

        var callBack = function() {
            faxTran.bindFaxTranForm();
        };

        faxTran.dataList = jQ('#portal-datalist').DataTable({
            'processing': true,
            'serverSide': true,
            'ajax': {
                'url': faxTran.settings.ajaxUrl,
                'data': function(d) {
                    d.url = global.settings.url;
                    d.month = jQ("#month-filter-type").val();
                    d.status = jQ("#status-filter-type").val();
                }
            },
            "order": [[0,'desc']],
            'deferRender': true,
            'columnDefs': [
                { 'orderable': false, 'targets': 7 },
                { 'searchable': false, 'targets': 7 }
            ],
            drawCallback: function() {
                callBack();
            },
            responsive: {
                details: {
                    renderer: function( api,rowIdx ) {
                        return global.dataTableResponsiveCallBack(api, rowIdx, callBack);
                    }
                }
            }
        });

        jQ('#month-filter-type').on('change', function(){
            faxTran.dataList.draw();
        });
        jQ('#status-filter-type').on('change', function(){
            faxTran.dataList.draw();
        });

        var callBack2 = function() {
            faxTran.bindFaxTranForm();
        };

        faxTran.unite_dataList = jQ('#unite-fax-datalist').DataTable({
            'processing': true,
            'serverSide': true,
            'ajax': {
                'url': faxTran.settings.unite_ajaxUrl,
                'data': function(d) {
                    d.url = global.settings.url;
                    d.month = jQ("#month-filter-type").val();
                    d.status = jQ("#status-filter-type").val();
                }
            },
            "order": [[0,'desc']],
            'deferRender': true,
            'columnDefs': [
                { 'orderable': false, 'targets': 7 },
                { 'searchable': false, 'targets': 7 }
            ],
            drawCallback: function() {
                callBack2();
            },
            responsive: {
                details: {
                    renderer: function( api,rowIdx ) {
                        return global.dataTableResponsiveCallBack(api, rowIdx, callBack2);
                    }
                }
            }
        });

        jQ('#month-filter-type').on('change', function(){
            faxTran.unite_dataList.draw();
        });
        jQ('#status-filter-type').on('change', function(){
            faxTran.unite_dataList.draw();
        });

        jQ('.content-container').removeClass('has-loading');
        jQ('.content-container-content').removeClass('hide');
    },
    bindFaxTranForm: function() {
        if(jQ('.href-faxTran-form').length > 0) {

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

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

                global.ajaxCancel(requestName);

                var settings = {
                    url: faxTran.settings.faxTranFormUrl + '/' + 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();
                        faxTran.bindFaxTranFormAction();
                        global.autocomplete.bind(faxTran.settings.health_facility_autocomplete_url, '#faxTran_health_facility_desc', '#faxTran_health_facility');
                        global.autocomplete.bind(faxTran.settings.account_user_autocomplete_url, '#faxTran_account_user_name', '#faxTran_account_user');
                        global.autocomplete.bind(faxTran.settings.patient_autocomplete_url, '#faxTran_patient_name', '#faxTran_patient');
                    }
                };
                global.ajax(settings, requestName);
            });
        }
    },
    bindFaxTranFormAction: function() {

        jQ('.href-faxTran-action').unbind('click.faxTranFormAction').bind('click.faxTranFormAction', function(){

            var requestName = 'faxTranFormAction',
                modal = jQ('#modal-small'),
                modalContent = jQ('#modal-content-small'),
                _form = jQ('#form-faxTran'),
                axn = jQ(this).data('axn');

            _form.find('.form-action').val(axn);

            var formData = new FormData(jQ('#form-faxTran')[0]);
            jQ.ajax({
                url: faxTran.settings.faxTran_formActionUrl,  //Server script to process data
                type: 'POST',
                xhr: function() {  // Custom XMLHttpRequest
                    var myXhr = jQ.ajaxSettings.xhr();
                    if(myXhr.upload){ // Check if upload property exists
                        myXhr.upload.addEventListener('progress', function(e) {
                            if(e.lengthComputable){
                                var percentComplete = (e.loaded / e.total) * 100;
                                _form.find(".progress-meter").width(percentComplete+'%');
                            }
                        }, false); // For handling the progress of the upload
                    }
                    return myXhr;
                },
                //Ajax events
                beforeSend: function() {
                    global.formSubmitted();
                    if(axn !== 'd') {
                        _form.find(".progress-meter").width('0');
                        _form.find('.progress').show();
                    }
                },
                success: function(json) {
                    global.formSubmittedReverse();
                    if(json.success) {
                        faxTran.dataList.draw();
                        faxTran.unite_dataList.draw();
                        global.parseFlashMessages(global.settings.vars.flashMessagesContentId, 'success', json.successMessages);
                        modal.foundation('close');
                    } else {
                        _form.find('.progress').hide();
                        // alert(json.errorMessages);
                        global.parseFlashMessages('#flash-messages-content-modal', 'alert', json.errorMessages);
                    }
                },
                error: function(xhr, response_text) {
                    global.formSubmittedReverse();
                    if(response_text !== 'abort' && response_text !== 'error') {
                        alert('Error. Please try again.');
                    }
                },
                // Form data
                data: formData,
                //Options to tell jQuery not to process data or worry about content-type.
                cache: false,
                contentType: false,
                processData: false
            });
        });
    },
    // bindFaxEvent: function() {
    //     jQ('#faxTran_is_existing').unbind('click').bind('click', function () {
    //         if(this.checked){
    //             jQ('#faxTran_patient_name').fadeIn(300);
    //         }else{
    //             jQ('#faxTran_patient_name').slideUp(300);
    //             jQ('#faxTran_patient_name').val(null);
    //         }
    //     });
    //
    //     if(jQ('#faxTran_is_existing').prop('checked')){
    //         jQ('#patient_endorsed_by').fadeIn(300);
    //     }
    // }
};