App.ProductOrdersController = Ember.ArrayController.extend
  needs: ['application', 'table']
  orderTotal: (->
    #Math.round(Math.random()*100)
    @get('model').getEach('total').reduce(((sum, total) -> sum + total), 0)
  ).property('model.@each.quantity')
  product_orders: (->@get('model')).property('model')
  showTotal: (-> if @get('model.length') && @get('model.length') > 1 then true else false ).property('model.length')
  actions:
    # clearProductOrders: ->
    #   #TODO: make clearing of unpersisted product orders
    #   @get('product_orders').invoke 'eraseRecord'
    orderProducts: ->
      # table = @get('controllers.table.model')
      # list = @get('controllers.application.list')
      # order = @store.createRecord('order', list: list, table: table)
      # new_product_orders = @store.all('product_order').filterProperty('order', null)
      # order.get('product_orders').pushObjects(new_product_orders)
      #
      # order.save().then (response)=>
      #   new_product_orders.invoke 'eraseRecord'
      #   if @get('controllers.application.list')
      #     @transitionToRoute 'active_list'
      #   else
      #     # Get list info from the server
      #     @get('controllers.application').setCurrentList ->
      #       @transitionToRoute 'active_list'
      # , @ajaxError (jqXHR)=>
      #   new_product_orders.invoke 'eraseRecord'
      #   @redirect_to 'index',  message: 'order_could_not_be_processed'

      #orders = @store.all('product_order').toArray()
      #data = @get('product_orders').map( (po)->po.serialize() )
      dataObject = order: {table_id: @get('controllers.table.model.id')}
      @get('product_orders').forEach (product_order)-> dataObject['order'][product_order.get('product.id')] = product_order.get('quantity')
      Ember.$.post "#{$data_host}/user/orders.json", dataObject
      @transitionToRoute 'active_list'
      @get('product_orders').invoke 'unloadRecord'

    removeProductOrder: (product_order)->
      product_order.eraseRecord()
