<% issue = local_assigns[:issue] || @issue %>

<% if issue&.new_record? && UxToolkitTicketTemplate.table_available? %>
  <% templates = UxToolkitTicketTemplate.enabled.ordered.to_a %>
  <% if templates.any? %>
    <% default_template = nil %>
    <% if defined?(UxToolkitTrackerTicketTemplate) && UxToolkitTrackerTicketTemplate.table_available? %>
      <% default_template = UxToolkitTrackerTicketTemplate.available.global.find_by(tracker_id: issue.tracker_id)&.ticket_template %>
    <% end %>

    <div class="box ux-toolkit-ticket-template-insert"
         data-empty-template-message="<%= h(l(:text_ux_toolkit_select_ticket_template_first)) %>"
         data-empty-template-description-message="<%= h(l(:text_ux_toolkit_selected_ticket_template_has_no_description)) %>"
         data-append-confirmation="<%= h(l(:text_ux_toolkit_confirm_append_ticket_template)) %>">
      <p>
        <label for="ux_toolkit_ticket_template_select"><%= l(:label_ux_toolkit_ticket_template) %></label>
        <%= select_tag 'ux_toolkit_ticket_template_select',
                       options_for_select(
                         [[l(:label_ux_toolkit_no_ticket_template), '', {'data-template-text' => ''}]] +
                           templates.map do |template|
                             [
                               template.name,
                               template.id,
                               {
                                 'data-template-text' => template.description_template.to_s,
                                 'data-subject-template' => template.subject_template.to_s,
                                 'data-subject-template-mode' => template.subject_template_mode.to_s
                               }
                             ]
                           end,
                         default_template&.id
                       ),
                       id: 'ux_toolkit_ticket_template_select' %>
        <button type="button"
                class="button-small ux-toolkit-ticket-template-insert-button">
          <%= l(:button_ux_toolkit_insert_ticket_template) %>
        </button>
      </p>
      <em class="info"><%= l(:text_ux_toolkit_ticket_template_insert_help) %></em>
      <div class="ux-toolkit-template-preview" data-ux-toolkit-ticket-template-preview>
        <strong><%= l(:label_ux_toolkit_template_preview) %></strong>
        <span class="ux-toolkit-template-preview-empty" data-template-preview-empty><%= l(:text_ux_toolkit_select_template_for_preview) %></span>
        <div data-template-preview-content hidden>
          <span class="ux-toolkit-template-preview-subject" data-template-preview-subject></span>
          <pre data-template-preview-description></pre>
          <em class="info"><%= l(:text_ux_toolkit_template_content_will_be_inserted) %></em>
        </div>
      </div>
    </div>
  <% end %>
<% end %>
