import Ember from 'ember'
export default Ember.Controller.extend
  nickname_filter: ''
  email_filter: ''
  organization_filter: null
  possible_organizations: (-> @store.peekAll('panda/organization')).property()
  users: Ember.computed 'model', 'nickname_filter', 'email_filter', 'organization_filter', ->
    return [] unless records = @get 'model'
    if nickname_filter = @get('nickname_filter')
      nickname_matcher = new RegExp nickname_filter, 'i'
      records = records.filter (record) -> nickname_matcher.test record.get('nickname')
    if email_filter = @get('email_filter')
      email_matcher = new RegExp email_filter, 'i'
      records = records.filter (record) -> email_matcher.test record.get('email')
    if organization_filter = @get('organization_filter')
      records = records.filter (record) -> record.get('organizations').includes organization_filter
    #records.sortBy 'nickname'
    records

  actions:
    clear_nickname_filter: -> @set 'nickname_filter', ''
    clear_email_filter:    -> @set 'email_filter', ''
    set_organization_filter: (organization) -> @set 'organization_filter', organization
