<% html_title l(:label_ux_toolkit_tracker_checklist_defaults) %>

<div class="contextual">
  <%= link_to l(:label_ux_toolkit_checklist_templates),
              ux_toolkit_checklist_templates_path,
              class: 'icon icon-list' %>
</div>

<h2><%= l(:label_ux_toolkit_tracker_checklist_defaults) %></h2>

<p><%= l(:text_ux_toolkit_tracker_checklist_defaults_help) %></p>

<% if @templates.empty? %>
  <p class="nodata"><%= l(:text_ux_toolkit_no_active_checklist_templates_with_items) %></p>
<% end %>

<% if @templates.any? || @defaults.any? %>
  <%= form_tag ux_toolkit_tracker_checklist_templates_path, method: :patch do %>
    <table class="list ux-toolkit-tracker-checklist-defaults">
      <thead>
        <tr>
          <th><%= l(:field_tracker) %></th>
          <th><%= l(:label_ux_toolkit_default_checklist_template) %></th>
          <th><%= l(:field_updated_on) %></th>
        </tr>
      </thead>
      <tbody>
        <% @trackers.each do |tracker| %>
          <% default = @defaults[tracker.id] %>
          <tr class="<%= cycle('odd', 'even') %>">
            <td><%= h(tracker.name) %></td>
            <td>
              <%= select_tag "tracker_checklist_templates[#{tracker.id}]",
                             options_for_select(
                               [[l(:label_ux_toolkit_no_automatic_checklist), '']] +
                                 @templates.map { |template| [template.name, template.id] },
                               default&.checklist_template_id
                             ) %>
            </td>
            <td><%= default ? format_time(default.updated_at) : '-' %></td>
          </tr>
        <% end %>
      </tbody>
    </table>

    <p>
      <%= submit_tag l(:label_ux_toolkit_save_tracker_checklist_defaults) %>
    </p>
  <% end %>
<% end %>
