var patient = {
    settings: {
        ajaxUrl: '',
        patient_export_all_hl7_formUrl: '',
        patient_export_all_hl7_formActionUrl: ''
    },
    init: function() {
        patient.initDataTable();
        patient.showHl7DownloadCheckbox();
        patient.checkAll();
        patient.bindExportAllHl7();
    },
    initDataTable: function() {

        var callBack = function() {
        };

        var dataList = jQ('#patient-datalist').DataTable({
            'processing': true,
            'serverSide': true,
            'ajax': {
                'url': patient.settings.ajaxUrl,
                'data': function(d) {
                    d.url = global.settings.url;
                    d.healthFacility_id = jQ('#branch-filter').val();
                    d.is_active = jQ('#active-filter').val();
                    d.checkBoxShow = jQ('#download-show-checkbox').data('trigger') ;
                }
            },
            'deferRender': true,
            'columnDefs': [
                { 'orderable': false, 'targets': 8 },
                { 'searchable': false, 'targets': 8 }
            ],
            'lengthMenu': [[10, 25, 50, 100, 300, 500, 1000], [10, 25, 50, 100, 300, 500, 1000]],
            'order': [[0, 'desc']],
            drawCallback: function() {
                callBack();
            },
            responsive: {
                details: {
                    renderer: function( api,rowIdx ) {
                        return global.dataTableResponsiveCallBack(api, rowIdx, callBack);
                    }
                }
            },
            'createdRow': function(row, data) {
                if(true === data[10].DT_RowData.ifExist) {
                    jQ(row).addClass('red-flag-row');
                }

            }

        });
        jQ('#branch-filter').on('change', function(){
            dataList.draw();
        });
        jQ('#active-filter').on('change', function(){
            dataList.draw();
        });
        jQ('.content-container').removeClass('has-loading');
        jQ('.content-container-content').removeClass('hide');
    },
    showHl7DownloadCheckbox: function () {
         /*jQ('#download-show-checkbox').unbind('click').bind('click', function () {
             if(jQ(this.data('trigger')))
         });*/
        jQ('#download-show-checkbox').toggle(function () {
            jQ(this).data('trigger', 'show');
            jQ('.patient-checkbox-hl7').show();
            jQ('.patient-checkbox-hl7-all').show();
            jQ('#hl7-types').show();
            jQ('#hl7-export').show();
        }, function () {
            jQ(this).data('trigger', 'hidden');
            jQ('.patient-checkbox-hl7').hide();
            jQ('.patient-checkbox-hl7-all').hide();
        })

    },
    checkAll: function () {
        jQ('.patient-checkbox-hl7-all').toggle(function () {
            jQ('.patient-checkbox-hl7').attr('checked', 'checked');
            jQ('.patient-checkbox-hl7-all').attr('checked', 'checked');
        },function () {
            jQ('.patient-checkbox-hl7').removeAttr('checked', 'checked');
            jQ('.patient-checkbox-hl7-all').removeAttr('checked', 'checked');
        })

    },
    bindExportAllHl7: function() {
        if(jQ('.href-export-all-to-hl7').length > 0) {

            jQ('.href-export-all-to-hl7').unbind('click').bind('click', function(){

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

                global.ajaxCancel(requestName);

                var settings = {
                    url: patient.settings.patient_export_all_hl7_formUrl,
                    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();
                        patient.bindPatientExportAllHl7FormAction();
                    }
                };

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

    bindPatientExportAllHl7FormAction: function() {

        jQ('.href-patient-export-all-hl7-action').unbind('click.patientExportHl7FormAction').bind('click.patientExportHl7FormAction', function () {
            jQ('.href-patient-export-all-hl7-action').addClass('disabled').removeClass('href-patient-export-all-hl7-action');
            var type = jQ("#hl7_message_type").val();
            location.href = (patient.settings.patient_export_all_hl7_formActionUrl  + "?type=" + type);
            setTimeout(function(){
                jQ('#modal-small').foundation('close');
                global.parseFlashMessages(global.settings.vars.flashMessagesContentId, 'success', [type + ' HL7 File is now downloading.']);
            }, 1000)
        });
    },

};