let global_reminder = {
    modal: null,
    modalContent: null,
    settings:{
        reminder_list_url: null,
        reminder_form_url: null,
        reminder_count_url: null,
        patient_autocompleteUrl: null,
        account_user_autocompleteUrl: null,
        user_group_autocompleteUrl: null,
        initialPtIdx: 0,
    },
    init: function(){
        global.createModal('global-reminder-modal','global-reminder-modal-content', 'medium');
        global_reminder.modal = jQ('#global-reminder-modal');
        global_reminder.modalContent = jQ('#global-reminder-modal-content');
        global_reminder.count();
        global_reminder.bindButtonAction();
        global_reminder.bindRemovePatientRef();
        global_reminder.bindPatientAutoComplete();
    },
    bindAutoComplete: function(){
        global.autocomplete.bind(global_reminder.settings.account_user_autocompleteUrl, '#account_user_reminder_form_assigned_to_account_user_desc', '#account_user_reminder_form_assigned_to_account_user');
        global.autocomplete.bind(global_reminder.settings.user_group_autocompleteUrl, '#account_user_reminder_form_assigned_to_user_group_desc', '#account_user_reminder_form_assigned_to_user_group');
    },
    bindButtonAction: function() {
        jQ('#global-reminder-toggle').unbind('click').bind('click', function() {
            let reminderContent = jQ('#global-reminder-content');
            if(reminderContent.css('display') === 'block') {
                reminderContent.fadeOut(300);
            } else {
                global_reminder.list();
                reminderContent.slideDown(300);
            }
        });

        jQ("#global-reminder-close").click(function (e) {
            e.preventDefault();
            jQ('#global-reminder-content').toggle();
        });

        jQ("#global-reminder-add-button").click(function (e) {
            e.preventDefault();
            global_reminder.form('n', 0);
        });

        jQ(".href-patient-account-user-reminder-action").unbind('click').bind('click',function (e) {
            e.preventDefault();
            var action = jQ(this).data('axn');
            var id = jQ(this).data('id');
            var value = jQ(this).data('value');
            jQ("#account_user_reminder_form_action").val(value);
            var serial = jQ("#form-accountUserReminder").serialize();
            global.ajaxCancel('reminderFormSubmitGlobal');
            let settings = {
                url: global_reminder.settings.reminder_form_url + "/" + action + "/" + id,
                type: 'POST',
                data: serial,
                error: function(xhr, responseText) {
                    if(responseText !== 'abort' && responseText !== 'error') {
                        global.formSubmittedReverse();
                        alert(responseText);
                    }
                },
                beforeSend: function(){
                    global.formSubmitted();
                },
                success: function(json) {
                    global.formSubmittedReverse();
                    if(json.success){
                        global_reminder.list();
                        global_reminder.count();
                        global_reminder.modal.foundation('close');
                        if(typeof reminder !== 'undefined'){
                            reminder.dataList.draw();
                        }
                    } else {
                        alert(json.errors)
                    }
                }
            };
            global.ajax(settings, 'reminderFormSubmitGlobal');
        });

        jQ("#global-reminder-form-add-patient").unbind('click').bind('click', function (e) {
            e.preventDefault();
            global_reminder.addPatientRefForm();
        });
    },
    form: function(action,id, defaultPatient = ''){
        global.ajaxCancel('reminderFormGlobal');
        let settings = {
            url: global_reminder.settings.reminder_form_url + "/" + action + "/" + id + (defaultPatient !== '' ? `?pt=${defaultPatient}` : ''),
            type: 'GET',
            error: function(xhr, responseText) {
                if(responseText !== 'abort' && responseText !== 'error') {
                    alert(responseText);
                }
            },
            beforeSend: function(){
                global_reminder.modalContent.html('').addClass('has-loading');
                global_reminder.modal.foundation('open');
            },
            success: function(json) {
                global_reminder.modal.foundation('close');
                global_reminder.modalContent.removeClass('has-loading').html(json.html);
                global_reminder.modal.foundation('open');
                global.maskInput();
                global_reminder.bindButtonAction();
                global_reminder.bindAutoComplete();
                global_reminder.bindRemovePatientRef();
                global_reminder.bindPatientAutoComplete();
                jQ("#account_user_reminder_form_assigned_to_account_user_desc").focus();
            }
        };
        global.ajax(settings, 'reminderFormGlobal');
    },
    list: function () {
        global.ajaxCancel('reminderListGlobal');
        let settings2 = {
            url: global_reminder.settings.reminder_list_url,
            type: 'GET',
            error: function(xhr, responseText) {
                if(responseText !== 'abort' && responseText !== 'error') {
                    alert(responseText);
                }
            },
            beforeSend: function(){
                jQ("#global-reminder-list-content").html('').addClass('has-loading');
            },
            success: function(json) {
                jQ("#global-reminder-list-content").removeClass('has-loading').html(json.html);
            }
        };
        global.ajax(settings2, 'reminderListGlobal');
    },
    count: function () {
        global.ajaxCancel('reminderCountGlobal');
        let settings2 = {
            url: global_reminder.settings.reminder_count_url,
            type: 'GET',
            error: function(xhr, responseText) {
                if(responseText !== 'abort' && responseText !== 'error') {
                    alert(responseText);
                }
            },
            beforeSend: function(){
                jQ('#global-reminder-ctr').addClass('hide');
                jQ('#global-recent-reminder-ctr').html(jQ('<i>').attr({class: 'fa fa-spinner fa-spin'}))
            },
            success: function(json) {
                if(json.success){
                    if(json.count > 0){
                        jQ('#global-reminder-ctr').removeClass('hide').html(json.count);
                    }
                    jQ('#global-recent-reminder-ctr').html(json.count);
                }
            }
        };
        global.ajax(settings2, 'reminderCountGlobal');
    },
    bindRemovePatientRef: function(){
      /*jQ(".removePatientRef").unbind('click').bind('click', function (e) {
          e.preventDefault();
          jQ(this).parent().parent().remove();
      });*/
      jQ("#global-reminder-patient-ref-table-body").on('click', '.removePatientRef', function (e) {
          e.preventDefault();
          jQ(this).parent().parent().remove();
      });
    },
    bindPatientAutoComplete: function(){
        jQ("tr.global-reminder-patient_row").each(function(i,el){
            let idx = jQ(el).data('idx');
            global.autocomplete.bind(global_reminder.settings.patient_autocompleteUrl, '#account_user_reminder_form_accountUserReminderRefPatient_patient_fullName'+idx, '#account_user_reminder_form_accountUserReminderRefPatient_patient'+idx);
        });
    },
    addPatientRefForm: function(){
        var lastIdx = global_reminder.settings.initialPtIdx;
        jQ("tr.global-reminder-patient_row").each(function(i, el){
            lastIdx = parseInt(jQ(el).data('idx'));
        });
        let idx = lastIdx + 1;

        var inputPatientDesc = jQ("<input>").attr({
            type: 'text',
            class: 'account_user_reminder_form_accountUserReminderRefPatient_patient_fullName',
            id: 'account_user_reminder_form_accountUserReminderRefPatient_patient_fullName'+idx,
            placeholder: 'Patient',
            name: `account_user_reminder_form[accountUserReminderRefPatients][${idx}][accountUserReminderRefPatient_fullName]`,
            'data-idx' : idx,
        }),
        inputPatient = jQ("<input>").attr({
            type: 'hidden',
            class: 'account_user_reminder_form_accountUserReminderRefPatient_patient',
            id: 'account_user_reminder_form_accountUserReminderRefPatient_patient'+idx,
            name: `account_user_reminder_form[accountUserReminderRefPatients][${ idx }][patient]`,
            'data-idx' : idx,
        }),
        inputId = jQ("<input>").attr({
            type: 'hidden',
            class: 'account_user_reminder_form_accountUserReminderRef_id',
            id: 'account_user_reminder_form_accountUserReminderRefPatient_id'+idx,
            name: `account_user_reminder_form[accountUserReminderRefPatients][${ idx }][id]`,
            'data-idx' : idx,
        }),
        tr = jQ("<tr>").attr({
            class: 'global-reminder-patient_row',
            'data-idx' : idx,
        }),
        td1 = jQ("<td>"),
        td2 = jQ("<td>").attr({
            class: 'text-center'
        }),
        btn = jQ("<button>").attr({
            class: 'button tiny secondary removePatientRef',
            type: 'button',
        }).html("&times;");
        inputPatientDesc.appendTo(td1);
        inputPatient.appendTo(td1);
        inputId.appendTo(td1);
        td1.appendTo(tr);
        btn.appendTo(td2);
        td2.appendTo(tr);
        tr.appendTo("#global-reminder-patient-ref-table-body");
        global_reminder.bindRemovePatientRef();
        global_reminder.bindPatientAutoComplete();
    }
}