var patient = {
    settings: {
        ajaxUrl: '',
        patient_export_all_hl7_formUrl: '',
        patient_export_all_hl7_formActionUrl: ''
    },
    init: function() {
        patient.initDataTable();
        patient.showHl7DownloadCheckbox();
        patient.checkAll();
        patient.bindExportAllHl7();
        jQ('#hl7-export').click(function (e) {
            let forms = jQ('#patient_mass_export_hl7_form :input').serialize();
            location.href = jQ('#patient_mass_export_hl7_form').data('action') + '?' + forms;
        });
    },
    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') ;
                    d.advancedSearch = jQ('#advanced_search_switch').is(':checked') ? 1 : 0 ;
                }
            },
            '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(null !== data[10].DT_RowData.hasRedFlag && '' !== data[10].DT_RowData.hasRedFlag) {
                    jQ(row).addClass('red-flag-row');
                }

            }

        });
        jQ('#branch-filter').on('change', function(){
            dataList.draw();
        });
        jQ('#active-filter').on('change', function(){
            dataList.draw();
        });
        jQ('#advanced_search_switch').on('change', function(){
            let isChecked = jQ(this).is(':checked');
            jQ("#patient-datalist_filter input[type='search']")
                .attr('placeholder', isChecked ? 'Advanced Search' : 'Full Name Search').focus();
        });
        jQ('.content-container').removeClass('has-loading');
        jQ('.content-container-content').removeClass('hide');

        jQ('#patient-datalist').on('init.dt', function () {
            jQ("#patient-datalist_filter input[type='search']")
                .attr('placeholder', jQ('#advanced_search_switch').is(':checked') ? 'Advanced Search' : 'Full Name Search')
                .unbind()
                .bind("input keyup", function(e) {
                    if((e.type === 'input' && this.value.length >= 5) || (e.type === 'keyup' && e.keyCode === 13)) {
                        e.preventDefault();
                        dataList.search(this.value).draw();
                    }
                    if(this.value === "") {
                        dataList.search("").draw();
                    }
                });
        });
    },
    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)
        });
    },

};