= form_with url: bulk_update_admin_products_path, method: :post, id: "products-form",
            builder: BulkFormBuilder,
            html: { data: { 'turbo-frame': "_self",
              controller: "bulk-form",
              'bulk-form-disable-selector-value': "#sort,#filters",
              'bulk-form-error-value': defined?(@error_counts),
              } } do |form|
  = render(partial: "admin/shared/flashes", locals: { flashes: }) if defined? flashes

  = hidden_field_tag :page, @page
  = hidden_field_tag :per_page, @per_page
  = hidden_field_tag :search_term, @search_term
  = hidden_field_tag :producer_id, @producer_id
  = hidden_field_tag :category_id, @category_id

  %table.products
    %colgroup
      %col{ width:"56" }= #  Img (size + padding)
      %col= # (grow to fill) Name
      %col{ width:"5%"}
      %col{ width:"8%"}
      %col{ width:"8%"}
      %col{ width:"5%"}
      %col{ width:"10%"}
      %col{ width:"15%"}= # Producer
      %col{ width:"8%"}
      %col{ width:"8%"}
      %col{ width:"8%"}
      %col{ width:"8%"}= # Actions
    %thead
      %tr
        %td.form-actions-wrapper{ colspan: 12 }
          .form-actions-wrapper2
            %fieldset.form-actions{ class: ("hidden" unless defined?(@error_counts)), 'data-bulk-form-target': "actions" }
              .container
                .status
                  .modified_summary{ 'data-bulk-form-target': "changedSummary", 'data-translation-key': 'admin.products_v3.table.changed_summary'}
                  - if defined?(@error_counts)
                    .error_summary
                      - if @error_counts[:saved] > 0
                        -# X products were saved correctly, but Y products could not be saved correctly. Please review errors and try again
                        = t('.error_summary.saved', count: @error_counts[:saved]) + t('.error_summary.invalid', count: @error_counts[:invalid])
                      - else 
                        -# Y products could not be saved correctly. Please review errors and try again
                        = t('.error_summary.invalid', count: @error_counts[:invalid])
                .form-buttons
                  %a.button.reset.medium{ href: admin_products_path(page: @page, per_page: @per_page, search_term: @search_term, producer_id: @producer_id, category_id: @category_id), 'data-turbo': "false" }
                    = t('.reset')
                  = form.submit t('.save'), class: "medium"
      %tr
        %th.align-left= # image
        = render partial: 'spree/admin/shared/stimulus_sortable_header',
            locals: { column: :name, sorted: params.dig(:q, :s), default: 'name asc' }
        %th.align-left.with-input= t('admin.products_page.columns.sku')
        %th.align-left.with-input= t('admin.products_page.columns.unit_scale')
        %th.align-left.with-input= t('admin.products_page.columns.unit')
        %th.align-left.with-input= t('admin.products_page.columns.price')
        %th.align-left.with-input= t('admin.products_page.columns.on_hand')
        %th.align-left= t('admin.products_page.columns.producer')
        %th.align-left= t('admin.products_page.columns.category')
        %th.align-left= t('admin.products_page.columns.tax_category')
        %th.align-left= t('admin.products_page.columns.inherits_properties')
        %th.align-right= t('admin.products_page.columns.actions')
    - products.each_with_index do |product, product_index|
      = form.fields_for("products", product, index: product_index) do |product_form|
        %tbody.relaxed{ data: { 'record-id': product_form.object.id,
                                controller: "nested-form product",
                                action: 'rails-nested-form:add->bulk-form#registerElements rails-nested-form:remove->bulk-form#toggleFormChanged' } }
          %tr
            = render partial: 'product_row', locals: { f: product_form, product:, producer_options: }

          - product.variants.each_with_index do |variant, variant_index|
            = form.fields_for("products][#{product_index}][variants_attributes][", variant, index: variant_index) do |variant_form|
              %tr.condensed{ 'data-controller': "variant", 'class': "nested-form-wrapper", 'data-new-record': variant.new_record? ? "true" : false }
                = render partial: 'variant_row', locals: { variant:, f: variant_form, category_options:, tax_category_options: }

          = form.fields_for("products][#{product_index}][variants_attributes][NEW_RECORD", product.variants.build) do |new_variant_form|
            %template{ 'data-nested-form-target': "template" }
              %tr.condensed{ 'data-controller': "variant", 'class': "nested-form-wrapper", 'data-new-record': "true" }
                = render partial: 'variant_row', locals: { variant: new_variant_form.object, f: new_variant_form, category_options:, tax_category_options:  }

          %tr{ 'data-nested-form-target': "target" }
          %tr.condensed
            %td
            %td{ colspan: 11 }
              %button.secondary.condensed.naked.icon-plus{ 'data-action': "nested-form#add",
                                                           'aria-label': t('.new_variant') }
                =t('.new_variant')
