App.IndexController = Ember.Controller.extend
  show_lists: true
  show_orders: true
  loading_data: true
  lists: Ember.computed -> @store.peekAll('list')
  orders: Ember.computed -> @store.peekAll('order')
  sections: Ember.computed -> @store.peekAll('section')
  #active_section: Ember.computed.alias 'globals.active_section'
  active_lists: Ember.computed 'lists.@each.state', 'globals.active_section.id', 'lists.@each.table', -> # table needed for when list changes table
    @get('orders') # trigger orders, otherwise observers are not initialized/triggered (active_orders)
    if section_id = @get('globals.active_section.id')
      lists = @get('lists').filter (list) =>
        return false unless list.get('state') is 'active'
        list.get('table.section.id') is section_id
    else
      lists = @get('lists').filterBy('state', 'active')
    lists.sortBy('created_at') # Not reversed, oldest on top, start with oldest order first, work your way down :-) Customer happyness

  active_orders: Ember.computed 'active_lists.[]', 'orders.@each.state', ->
    orders = Ember.A()
    @get('active_lists').forEach (list)->
      list.get('orders').filterBy('needs_supplier_attention').forEach (order)->
        orders.push order
    orders.sortBy('created_at') # Not reversed, oldest on top, start with oldest order first, work your way down :-) Customer happyness

  show_lists_table: Ember.computed 'show_lists', 'active_lists.[]', ->
    @get('show_lists') and @get('active_lists.length')
  show_orders_table: Ember.computed 'show_orders', 'active_orders.[]', ->
    @get('show_orders') and @get('active_orders.length')
  list_number_info: (-> " (#{@get('active_lists.length')})").property('active_lists.[]')
  order_number_info: (-> " (#{@get('active_orders.length')})").property('active_orders.[]')

  actions:
    toggleDashboardLists: -> @set 'show_lists', !@get('show_lists')
    toggleDashboardOrders: ->@set 'show_orders', !@get('show_orders')
