App.TableController = Ember.ObjectController.extend
  join_request_sent: (-> @get('controllers.application.join_request_sent')).property('controllers.application.join_request_sent')
  tableCanTakeOrders: (->
    return false unless @get('supplier.can_take_orders')
    list = @get('controllers.application.list')
    if 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('controllers.application.list.id', 'supplier.can_take_orders', 'model.occupied', 'model.id', 'controllers.application.list.table.id')
  showJoinButton: (->
    return false unless @get('supplier.can_take_orders')
    return false if @get('controllers.application.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
  ).property('controllers.application.list.id', 'supplier.can_take_orders', 'model.occupied', 'model.id', 'controllers.application.list.table.id')
  actions:
    addProduct: (product)->
      if existing = @store.all('product_order').find((po)-> po.get('product') == product and not po.get('order'))
        existing.increment()
      else
        @store.createRecord 'product_order', product: product, price: product.get('price')
    joinOccupiedTable: ->
      #@secured =>
      Ember.$.post("#{$data_host}/user/join_occupied_table.json", table_id: @get('model.id'))
      @set 'controllers.application.join_request_sent', true # keeps the button deactivated
    toggleProductCategory: (product_category)->
      product_category.set 'collapsed', not product_category.get('collapsed')
    showProductDescription: (product)->
      @send 'openModal', 'modal_product_info', product
