= page_title :index, workflow_instance_batch_class
= paginate @records
table.full-width
  thead
    tr
      th= sort_link @q, :id
      th= sort_link @q, :plan_date
      th= "# #{workflow_instance_class.model_name.human_plural}"
      th
      th.actions
    = search_form_for @q do |f|
      tr
        th.column-filter.name= f.search_field :id_eq, placeholder: '=='
        th.column-filter.name= f.search_field :plan_date_eq, placeholder: 'date', class: 'datepicker'
        th
        th
        th.column-filter.actions
          = f.submit t('dunlop.filter.apply').html_safe, class: 'submit-filters-button'
          = link_to workflow_instance_batch_class, class: 'clear-filters-button' do
            span== t('dunlop.filter.reset')
  tbody
    - workflow_step_map = @records.workflow_step_map(current_user, archived: session[:archived_mode])
    - @records.each do |workflow_instance_batch|
      tr
        td= link_to workflow_instance_batch.presentation_name, workflow_instance_batch
        td.plan_date data-date=workflow_instance_batch.plan_date.try(:iso8601)
        td= workflow_instance_batch.workflow_instances.scope_for(current_user, archived: session[:archived_mode]).size
        td.workflow_instance_batch-index-progress-container
          - workflow_step_map[workflow_instance_batch.id].each do |workflow_step, state_counts|
            span.workflow_instance_batch-index-workflow_step= workflow_step.model_name.human
            - state_counts.each do |state, count|
              span.state-container= state_badge workflow_step.new(state: state), state_append_text: " (#{count})", data: {workflow_instance_batch_id: workflow_instance_batch.id}
              /TODO workflow step filtering #, link_to: main_app.polymorphic_path(workflow_instance_class, q: {workflow_instance_batch_id_eq: workflow_instance_batch.id, workflow_step: {workflow_step.process_name => {state: state}}})
        td.actions
          = link_to polymorphic_path(workflow_instance_class, q: {workflow_instance_batch_id_eq: workflow_instance_batch.id}, per_page: 10_000), class: 'show-workflow_instance_batch-workflow_instances' do
            span
          = table_show_link workflow_instance_batch
          - if can? :edit, workflow_instance_batch
            = table_edit_link workflow_instance_batch
.page-actions
  = link_to t('action.new.label', model: workflow_instance_batch_class.model_name.human), new_polymorphic_path(workflow_instance_batch_class), class: 'new'
