var report_preview = {
    settings: {
        Ids: '',
        transfer_formUrl: '',
        tranType: '',

        view_noteUrl: '',
        view_patient_noteUrl: '',
    },
    init: function() {
        report_preview.bindTableReportData();
        report_preview.bindTransferForm();
        report_preview.bindViewNoteForm();
        report_preview.bindViewPatientNoteForm();

        //Transfer
        report_preview.settings.Ids = [];
        report_preview.settings.tranType = '';

        jQ("#check_all").click(function(){
            jQ('input:checkbox').not(this).prop('checked', this.checked);
        });

        jQ("#patient-report-list").on('change',"input[type='checkbox']",function(e){
            var ids = atob(jQ(this).data("id"));
            if(this.checked) {
                report_preview.settings.Ids.push(ids);
                report_preview.settings.tranType = jQ(this).data("type");
            } else {
                report_preview.settings.Ids.splice(report_preview.settings.Ids.indexOf(ids));
            }
        });
    },
    bindTableReportData: function() {
        if(jQ('.report-data').length) {
            jQ('.report-data:not([data-floathead="false"])').floatThead({
                position: 'fixed'
            });
        }
    },
    bindTransferForm: function() {
        if(jQ('.href-transfer-form').length > 0) {
            jQ('.href-transfer-form').unbind('click').bind('click', function(){

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

                global.ajaxCancel(requestName);

                var settings = {
                    url: report_preview.settings.transfer_formUrl,
                    type: 'GET',
                    beforeSend: function() {
                        modalContent.html('').addClass('has-loading');
                        modal.foundation('open').on('closed.zf.reveal', function () {
                            modalContent.html('');
                        });
                    },
                    success: function(json) {
                        modal.foundation('close');
                        modalContent.removeClass('has-loading').html(json.html);
                        modal.foundation('open').on('closed.zf.reveal', function () {
                            modalContent.html('');
                        });
                        global.bindFormSubmitted();
                        report_preview.bindTransferFormAction();
                    }
                };

                global.ajax(settings, requestName);
            });
        }
    },
    bindTransferFormAction: function() {
        jQ('.href-transfer-action').unbind('click.transferFormAction').bind('click.transferFormAction', function () {

            var requestName = 'transferFormAction',
                _form = jQ('#transfer'),
                axn = jQ(this).data('axn'),
                modal = jQ('#modal-small');
            _form.find('.form-action').val(axn);

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

            var settings = {
                url: report_preview.settings.transfer_formActionUrl,
                type: 'POST',
                // data: _form.serialize(),
                data: {
                    referring_provider : jQ('#referring-provider-id').val(),
                    ids: report_preview.settings.Ids,
                    type: report_preview.settings.tranType
                },
                success: function (json) {
                    if (json.success) {
                        modal.foundation('close');
                        location.reload();
                        report_preview.settings.Ids = [];
                        report_preview.settings.tranType = '';
                        alert(json.successMessages);
                    } else {
                        global.formSubmittedReverse();
                        alert(json.errorMessages);
                    }
                }
            };

            global.ajax(settings, requestName);
        });
    },
    bindViewNoteForm: function() {
        if(jQ('.href-note-form').length > 0) {
            jQ('.href-note-form').unbind('click').bind('click', function(){

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

                global.ajaxCancel(requestName);

                var settings = {
                    url: report_preview.settings.view_noteUrl,
                    data: {
                        patient_id:jQ(this).data('patient-id')
                    },
                    type: 'GET',
                    beforeSend: function() {
                        modalContent.html('').addClass('has-loading');
                        modal.foundation('open').on('closed.zf.reveal', function () {
                            modalContent.html('');
                        });
                    },
                    success: function(json) {
                        modal.foundation('close');
                        modalContent.removeClass('has-loading').html(json.html);
                        modal.foundation('open').on('closed.zf.reveal', function () {
                            modalContent.html('');
                        });
                        global.bindFormSubmitted();
                    }
                };

                global.ajax(settings, requestName);
            });
        }
    },
    bindViewPatientNoteForm: function() {
        if(jQ('.href-patient-note-form').length > 0) {
            jQ('.href-patient-note-form').unbind('click').bind('click', function(){

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

                global.ajaxCancel(requestName);

                var settings = {
                    url: report_preview.settings.view_patient_noteUrl,
                    data: {
                        patient_id:jQ(this).data('patient-id')
                    },
                    type: 'GET',
                    beforeSend: function() {
                        modalContent.html('').addClass('has-loading');
                        modal.foundation('open').on('closed.zf.reveal', function () {
                            modalContent.html('');
                        });
                    },
                    success: function(json) {
                        modal.foundation('close');
                        modalContent.removeClass('has-loading').html(json.html);
                        modal.foundation('open').on('closed.zf.reveal', function () {
                            modalContent.html('');
                        });
                        global.bindFormSubmitted();
                    }
                };

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