<div class="ux-toolkit-settings-section">
  <p><strong><%= l(:label_ux_toolkit_ticket_templates) %></strong></p>
  <p class="info"><%= l(:text_ux_toolkit_ticket_templates_help) %></p>

  <% if UxToolkitTicketTemplate.table_available? %>
    <% ticket_templates = UxToolkitTicketTemplate.ordered.to_a %>

    <p>
      <%= link_to l(:label_ux_toolkit_new_ticket_template),
                  new_ux_toolkit_ticket_template_path,
                  class: 'icon icon-add' %>
      <%= link_to l(:label_ux_toolkit_manage_ticket_templates),
                  ux_toolkit_ticket_templates_path,
                  class: 'icon icon-settings' %>
    </p>

    <% if ticket_templates.any? %>
      <table class="list ux-toolkit-template-list">
        <thead>
          <tr>
            <th><%= l(:label_ux_toolkit_template_name) %></th>
            <th><%= l(:label_ux_toolkit_template_enabled) %></th>
            <th><%= l(:label_ux_toolkit_ticket_template_subject_template) %></th>
            <th><%= l(:field_updated_on) %></th>
            <th></th>
          </tr>
        </thead>
        <tbody>
          <% ticket_templates.each do |template| %>
            <tr class="<%= cycle('odd', 'even') %>">
              <td class="name"><%= link_to h(template.name), edit_ux_toolkit_ticket_template_path(template) %></td>
              <td><%= checked_image template.enabled? %></td>
              <td><%= h(template.subject_template.presence || '-') %></td>
              <td><%= format_time(template.updated_at) %></td>
              <td class="buttons">
                <%= link_to l(:button_edit),
                            edit_ux_toolkit_ticket_template_path(template),
                            class: 'icon icon-edit' %>
                <% if template.enabled? %>
                  <%= link_to l(:button_ux_toolkit_disable),
                              disable_ux_toolkit_ticket_template_path(template),
                              method: :post,
                              class: 'icon icon-lock' %>
                <% else %>
                  <%= link_to l(:button_ux_toolkit_enable),
                              enable_ux_toolkit_ticket_template_path(template),
                              method: :post,
                              class: 'icon icon-unlock' %>
                <% end %>
                <%= link_to l(:button_delete),
                            ux_toolkit_ticket_template_path(template),
                            method: :delete,
                            data: {confirm: l(:text_ux_toolkit_confirm_delete_ticket_template)},
                            class: 'icon icon-del' %>
              </td>
            </tr>
          <% end %>
        </tbody>
      </table>
    <% else %>
      <p class="nodata"><%= l(:text_ux_toolkit_no_ticket_templates) %></p>
    <% end %>
  <% else %>
    <p class="nodata"><%= l(:text_ux_toolkit_ticket_templates_unavailable) %></p>
  <% end %>
</div>

<div class="ux-toolkit-settings-section">
  <p><strong><%= l(:label_ux_toolkit_tracker_ticket_template_defaults) %></strong></p>
  <p class="info"><%= l(:text_ux_toolkit_tracker_ticket_template_defaults_help) %></p>

  <% if UxToolkitTicketTemplate.table_available? && UxToolkitTrackerTicketTemplate.table_available? %>
    <% trackers = Tracker.sorted.to_a %>
    <% enabled_templates = UxToolkitTicketTemplate.enabled.ordered.to_a %>
    <% defaults = UxToolkitTrackerTicketTemplate.global.where(tracker_id: trackers.map(&:id)).includes(:ticket_template).index_by(&:tracker_id) %>

    <% if enabled_templates.empty? && defaults.empty? %>
      <p class="nodata"><%= l(:text_ux_toolkit_no_enabled_ticket_templates) %></p>
    <% else %>
      <table class="list ux-toolkit-tracker-ticket-template-defaults">
        <thead>
          <tr>
            <th><%= l(:field_tracker) %></th>
            <th><%= l(:label_ux_toolkit_default_ticket_template) %></th>
            <th><%= l(:field_enabled) %></th>
          </tr>
        </thead>
        <tbody>
          <% trackers.each do |tracker| %>
            <% default = defaults[tracker.id] %>
            <% selected_template = default&.ticket_template %>
            <% selected_template_disabled = selected_template && !selected_template.enabled? %>
            <tr class="<%= cycle('odd', 'even') %>">
              <td><%= h(tracker.name) %></td>
              <td>
                <%= select_tag "ux_toolkit_tracker_ticket_templates[#{tracker.id}][ticket_template_id]",
                               options_for_select(
                                 [[l(:label_ux_toolkit_no_default_ticket_template), '']] +
                                   enabled_templates.map { |template| [template.name, template.id] },
                                 selected_template&.enabled? ? selected_template.id : ''
                               ) %>
                <% if selected_template_disabled %>
                  <em class="info"><%= l(:text_ux_toolkit_selected_ticket_template_disabled, name: selected_template.name) %></em>
                <% end %>
              </td>
              <td>
                <%= hidden_field_tag "ux_toolkit_tracker_ticket_templates[#{tracker.id}][enabled]", '0', id: nil %>
                <%= check_box_tag "ux_toolkit_tracker_ticket_templates[#{tracker.id}][enabled]",
                                  '1',
                                  default ? default.enabled? : true,
                                  id: "ux_toolkit_tracker_ticket_template_#{tracker.id}_enabled" %>
              </td>
            </tr>
          <% end %>
        </tbody>
      </table>
    <% end %>
  <% else %>
    <p class="nodata"><%= l(:text_ux_toolkit_ticket_template_defaults_unavailable) %></p>
  <% end %>
</div>
