<% yield %>

<%
form ||= current_fields_form
html_options ||= {}
html_options[:id] ||= form.field_id(method)
multiple ||= false
show_select_all_top ||= false
show_select_all_bottom ||= false
use_columns ||= false
other_options ||= {}
options ||= options_for(form, method)
labels = labels_for(form, method)

option_field_options ||= {}
option_field_options[:data] ||= {}
option_field_options[:data][:controller] ||= ""
option_field_options[:data][:controller] += " fields--field"

if multiple
  option_field_options[:data] = option_field_options[:data].merge({
    "select-all-target": 'checkbox'
  })
  option_field_options[:data][:action] ||= ""
  option_field_options[:data][:action] += " select-all#updateToggle"
end

%>

<% if multiple && (show_select_all_top || show_select_all_bottom) %>
  <% select_all = capture do %>
    <div class="flex">
      <%= tag.div class: [
          "hidden",
          "inline-block dark:border-slate-600",
          "border-b pb-4 mb-4": show_select_all_top && !show_select_all_bottom,
          "border-t pt-4 mt-4": !show_select_all_top && show_select_all_bottom
      ], data: {
        "select-all-target": "wrapper"
      } do %>
        <label class="relative flex items-start">
          <div class="flex items-center h-5">
            <%= check_box_tag "#{html_options[:id]}-select_all", 'select-all', false, data: { 
              "select-all-target": 'toggleCheckbox',
              'action': "select-all#selectAllOrNone"
            }, class: "focus:ring-blue h-4 w-4 text-blue border-slate-300 rounded" %>
          </div>
          <div class="ml-2.5 text-sm pr-4">
            <%# TODO: should be localized %>
            All
          </div>
        </label>
      <% end %>
      <% if content_for? :actions_on_multiple %>
        <%= yield :actions_on_multiple %>
        <% flush_content_for :actions_on_multiple %>
      <% end %>
    </div>
  <% end %>
<% end %>

<%= render 'shared/fields/field', form: form, method: method, options: html_options, other_options: other_options do %>
  <% content_for :field do %>
    <%= tag.div class: "pt-1.5 pb-1 sm:col-span-2", data: {
      controller: "select-all",
      "select-all-unavailable-class": "hidden"
    } do %>
    
      <% if multiple && show_select_all_top && !show_select_all_bottom %>
        <%= select_all %>
      <% end %>
      
      <%= tag.div class: ["max-w-lg": !use_columns, "max-w-3xl": use_columns, "columns-[var(--column-width,_15ch)_3]": use_columns] do %>

        <% options.each do |value, label| %>

          <label class="relative flex items-start mb-3">
            <div class="flex items-center h-5">

              <% if multiple %>
                <% checked_value = form.object.send(method).nil? ? nil : form.object.send(method).map(&:to_s).include?(value.to_s) %>
                <%= form.check_box method, {
                  multiple: multiple, checked: checked_value,
                  data: option_field_options[:data],
                  class: "focus:ring-blue h-4 w-4 text-blue border-slate-300 rounded"
                }, value, "" %>
              <% else %>
                <%= form.radio_button method, value, {class: "focus:ring-blue h-4 w-4 text-blue border-slate-300", data: option_field_options[:data]} %>
              <% end %>

            </div>
            <div class="ml-2.5 text-sm">
              <div class="select-none"><%= label %></div>
              <% if labels.options_help&.dig(value)&.present? %>
                <p class="mt-0.5 text-xs text-slate-500">
                  <%= labels.options_help.dig(value) %>
                </p>
              <% end %>
            </div>
          </label>

        <% end %>
      <% end %>

      <% if multiple && !show_select_all_top && show_select_all_bottom %>
        <%= select_all %>
      <% end %>

    <% end %>
  <% end %>
<% end %>