App.TableController = Ember.Controller.extend
  tableCanTakeOrders: (->
    return false unless @get('supplier.can_take_orders')
    if list = @get('globals.active_list')
      return false unless list.get('supplier.id') == @get('supplier.id')
      return false unless list.get('table.id') == @get('model.id')
      true
    else
      # no list and open supplier
      if @get('model.occupied') then false else true
  ).property('globals.active_list.id', 'model.supplier.can_take_orders', 'model.occupied', 'model.id', 'globals.active_list.table.id', 'model.supplier.id')
  supplier: Ember.computed.alias 'model.supplier'
  showJoinButton: Ember.computed 'globals.active_list.id', 'supplier.can_take_orders', 'model.occupied', 'model.id', 'globals.active_list.table.id', ->
    return false unless @get('supplier.can_take_orders')
    return false if @get('globals.active_list') # if you already have an active list, do not join another
    if @get('model.occupied') then true else false # no point in joining tables that are not occupied
  actions:
    joinOccupiedTable: ->
      Ember.$.post("#{$data_host}/user/join_occupied_table.json", table_id: @get('model.id'))
      @set 'globals.join_request_sent', true # keeps the button deactivated
  #setupProductOrders: (-> @set('unordered_product_orders', @store.peekAll('product_order'))).on('init')
  #openDebugger: Ember.computed 'model.id', (-> debugger; true)
