App.ListsIndexController = Ember.Controller.extend
  loading: true
  date: (new Date()).toISOString().substr(0,10) # 2015-11-02
  dateChanged: (->
    return @set('loading', false) unless date = @get('date')
    @set 'loading', true
    @store.query 'list', date: date
    .then => @set('loading', false)
  ).observes('date').on('init')
  lists: Ember.computed -> @store.peekAll('list')

  sorted_lists: Ember.computed 'date', 'lists.[]', ->
    lists = @get('lists')
    if selected_date = @get('date')
      lists = lists.filter (list)->
        return false unless list_creation_date = list.get('created_at')
        list_creation_date = list_creation_date.toISOString().substring(0,10) if typeof(list_creation_date) is 'object'
        selected_date is list_creation_date
    lists.sortBy('created_at').reverseObjects()
  pricesList: Ember.computed.mapBy 'sorted_lists', 'price'
  date_lists_total: Ember.computed.sum 'pricesList'
