<% if filter.stages.any? || @expand_all %>
  <div class="quick-filter position-relative" data-controller="dialog" data-action="keydown.esc->dialog#close click@document->dialog#closeOnClickOutside">
    <button class="btn input input--select flex-inline txt-x-small" data-action="click->dialog#open:stop">
      <span class="overflow-ellipsis">
        <% if filter.stages.any? %>
          <%= filter.stages.map(&:name).to_sentence %>
        <% else %>
          In stage…
        <% end %>
      </span>
    </button>

    <dialog class="margin-block-start-half popup panel flex-column align-start gap-half fill-white shadow"
        aria-label="In stage…" aria-description="In stage…"
        data-dialog-target="dialog" data-action="turbo:before-cache@document->dialog#close">
      <strong class="popup__title margin-block-start-half pad-inline-half overflow-ellipsis">In stage…</strong>
      <%= form_with url: cards_path, method: :get, class: "popup__list",
            data: { controller: "form" } do |form| %>
        <% filter.as_params.except(:stage_ids).each do |key, value| %>
          <%= filter_hidden_field_tag key, value %>
        <% end %>

        <% if @expand_all %>
          <%= hidden_field_tag :expand_all, @expand_all %>
        <% end %>

        <% filter.stages.first.workflow.stages.each do |stage| %>
          <div class="btn popup__item">
            <%= form.check_box :stage_ids, {
              multiple: true,
              checked: filter.stages.include?(stage),
              data: { action: "change->form#submit" },
              include_hidden: false,
              }, stage.id %>

            <%= form.label :stage_ids, stage.name, for: form.field_id(:stage_ids, stage.id), class: "overflow-ellipsis" %>
            <%= icon_tag "check", class: "checked flex-item-justify-end" %>
          </div>
        <% end %>
      <% end %>
    </dialog>
  </div>
<% end %>
