<% html_title l(:label_ux_toolkit_edit_checklist_template) %>

<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_edit_checklist_template) %></h2>

<%= render 'form', template: @template %>

<h3><%= l(:label_ux_toolkit_template_items) %></h3>

<div class="box ux-toolkit-template-items">
  <% items = @template.items.to_a %>
  <% if items.any? %>
    <table class="list">
      <thead>
        <tr>
          <th><%= l(:field_position) %></th>
          <th><%= l(:label_ux_toolkit_template_item) %></th>
          <th></th>
        </tr>
      </thead>
      <tbody>
        <% items.each_with_index do |item, index| %>
          <tr class="<%= cycle('odd', 'even') %>">
            <td class="position"><%= item.position %></td>
            <td>
              <%= form_for item,
                           url: ux_toolkit_checklist_template_item_path(@template, item),
                           method: :patch,
                           html: {class: 'ux-toolkit-template-item-form'} do |f| %>
                <%= f.text_field :subject,
                                 size: 60,
                                 maxlength: 255,
                                 aria: {label: l(:label_ux_toolkit_edit_template_item)} %>
                <%= f.submit l(:button_save), class: 'button-small' %>
              <% end %>
            </td>
            <td class="buttons">
              <% unless index.zero? %>
                <%= link_to l(:label_ux_toolkit_move_template_item_up),
                            move_up_ux_toolkit_checklist_template_item_path(@template, item),
                            method: :post,
                            class: 'icon icon-up' %>
              <% end %>
              <% unless index == items.size - 1 %>
                <%= link_to l(:label_ux_toolkit_move_template_item_down),
                            move_down_ux_toolkit_checklist_template_item_path(@template, item),
                            method: :post,
                            class: 'icon icon-down' %>
              <% end %>
              <%= link_to l(:label_ux_toolkit_delete_template_item),
                          ux_toolkit_checklist_template_item_path(@template, item),
                          method: :delete,
                          data: {confirm: l(:text_ux_toolkit_confirm_delete_template_item)},
                          class: 'icon icon-del' %>
            </td>
          </tr>
        <% end %>
      </tbody>
    </table>
  <% else %>
    <p class="nodata"><%= l(:text_ux_toolkit_no_template_items) %></p>
  <% end %>

  <%= form_for UxToolkitChecklistTemplateItem.new,
               url: ux_toolkit_checklist_template_items_path(@template),
               html: {class: 'ux-toolkit-template-item-form ux-toolkit-template-item-new'} do |f| %>
    <%= f.text_field :subject,
                     size: 60,
                     maxlength: 255,
                     placeholder: l(:label_ux_toolkit_template_item) %>
    <%= f.submit l(:label_ux_toolkit_add_template_item) %>
  <% end %>
</div>
