<%= form_tag(current_script_listing_path, id: 'script-search', method: :get) do %>
  <h3><%= t('search.script_search.title') %></h3>
  <p><%= it('search.script_search.description') %></p>

  <p><input type="search" name="q" placeholder="Search terms" value="<%= params[:q]%>"></p>

  <% regular_search_params.except(:q, :site).each do |p, v| %>
    <input type="hidden" name="<%= p %>" value="<%= v %>">
  <% end %>

  <table>
    <% ScriptListings::ADVANCED_SEARCH_FIELDS.each do |field, field_data| %>
      <tr>
        <td>
          <label for="search-<%= field %>"><%= t("scripts.listing_sort_#{field_data[:label] || field}") %></label>
          <% if field_data[:info] %>
            <span class="info" title="<%= t(field_data[:info]) %>">ⓘ</span>
          <% end %>
        </td>
        <% case field_data[:type]
        when :integer %>
          <td>
            <%= search_comparator_field(field, include_equals: true) %>
          </td>
          <td>
            <input type="number" id="search-<%= field %>" name="<%= field %>" value="<%= params[field] %>" step="1" min="<%= field_data[:min] || 0 %>" max="<%= field_data[:max] %>" size="8" placeholder="(Any)">
          </td>
        <% when :float %>
          <td>
            <%= search_comparator_field(field, include_equals: false) %>
          </td>
          <td>
            <input type="number" id="search-<%= field %>" name="<%= field %>" value="<%= params[field] %>" step="any" min="<%= field_data[:min] || 0 %>" max="<%= field_data[:max] %>" size="8" placeholder="(Any)">
          </td>
        <% when :datetime %>
          <td>
            <%= search_comparator_field(field, include_equals: false) %>
          </td>
          <td>
            <input type="datetime-local" id="search-<%= field %>" name="<%= field %>" value="<%= params[field] %>" placeholder="(Any)" min="<%=(Script.first.created_at - 1.year).strftime('%Y-%m-%dT%H:%M')%>" max="<%=1.year.from_now.strftime('%Y-%m-%dT%H:%M')%>">
          </td>
        <% when :select %>
          <td></td>
          <td>
            <% case field %>
            <% when :entry_locales %>
              <select id="search-<%= field %>" name="<%= field %>[]" multiple>
                <%= Locale.sort_by_name(Locale.locales_used_by_scripts, in_locale: request_locale).each do |locale| %>
                  <option value="<%= locale.id %>" <%= 'selected' if params[field]&.include?(locale.id.to_s)%>><%= locale.display_text(in_locale: request_locale) %></option>
                <% end %>
              </select>
            <% else %>
              <% raise "Unknown select field: #{field}" %>
            <% end %>
          </td>
        <% else 
          raise "Unknown field type: #{field_type}"
        end %>
      </tr>
    <% end %>
  </table>

  <p>
    <input type="hidden" name="tz" value="<%= params[:tz]%>">
    <input class="search-submit" type="submit" value="<%= t('scripts.search_placeholder') %>">
  </p>
<% end %>

<%= form_tag(libraries_scripts_path, id: 'library-search', method: :get) do %>
  <h3><%= t('search.library_search.title') %></h3>
  <p><%= it('search.library_search.description') %></p>
  <input type="search" name="q"><input class="search-submit" type="submit" value="🔎">
<% end %>

<%= form_tag(code_search_scripts_path, id: 'code-search', method: :get) do %>
  <h3><%= t('search.code_search.title') %></h3>
  <p><%= t('search.code_search.description') %></p>
  <input type="search" name="c"><% if current_user&.moderator? %><input id="code-search-include-deleted" type="checkbox" name="include_deleted" value="1"><label for="code-search-include-deleted">Include deleted</label> <% end %><input class="search-submit" type="submit" value="🔎">
<% end %>

<%= form_tag(users_path, id: 'user-search', method: :get) do %>
  <h3><%= t('search.user_search.title') %></h3>
  <p><%= t('search.user_search.description') %></p>
  <input type="search" name="q"><input class="search-submit" type="submit" value="🔎">
<% end %>
