import Ember from 'ember'
import PagedRemoteArray from 'ember-cli-dunlop/utils/paged-remote-array'
export filters = Ember.Object.create
  state_in: []
  s: 'reference_date asc'

export default Ember.Route.extend
  modelName: 'panda/project-instance'
  queryParams:
    state:   {refreshModel: true}
    page:    {refreshModel: true}
    perPage: {refreshModel: true}
  setupController: (controller, model) ->
    controller.set 'filters', filters
    controller.set 'project', @modelFor('project.show')
    @_super arguments...

  model: (params) ->
    project = @modelFor 'project.show'
    filters.project_id = project.id
    filters.state_in = [params.state] if params.state
    PagedRemoteArray.create
      modelName: @modelName
      store:       @store
      filters: filters
      page:        params.page || 1
      perPage:     params.perPage || 30

  actions:
    new_project_instance: ->
      route = @
      @modal 'project/show/project-instance/new-project-instance',
        model: @store.createRecord('panda/project-instance', project: @modelFor('project.show'))
        title: t('action.new_model', model: t('models.project_instance'))
        save: (record) ->
          route.transitionTo('project.show.project-instance.show', record.id) if record.id


