define(['jquery', 'backbone', 'arches', 'views/concept-search', 'models/concept'], function ($, Backbone, arches, ConceptSearch, ConceptModel) {
    return ConceptSearch.extend({

        events: {
            'click .modal-footer .savebtn': 'save'
        },

        initialize: function(){
            ConceptSearch.prototype.initialize.apply(this, arguments);
            this.modal = this.$el.find('.modal');
            this.relationshiptype = this.modal.find('#related-relation-type').select2({
                minimumResultsForSearch: 10,
                maximumSelectionSize: 1
            });
        },
        
        save: function(){
            var self = this;
            if (this.searchbox.val() !== ''){
                var relatedConcept = new ConceptModel({
                    id: this.searchbox.val(),
                    relationshiptype: this.relationshiptype.val()
                });
                this.model.set('relatedconcepts', [relatedConcept]);
                this.model.save(function() {
                    self.modal.modal('hide');
                    $('.modal-backdrop.fade.in').remove();  // a hack for now
                });
            }
        }
    });
});