<% url = @template.persisted? ? public_send("#{@type_name_singular}_path", @template) : public_send("#{@type_name_singular}_path", @resource_class.new) %>
<%= form_for @template, :url => url, :html => { :multipart => true, :onsubmit => 'submit_code();' } do |f| %>
  <%= base_errors_for @template %>
  <ul class="nav nav-tabs" data-tabs="tabs">
    <li class="active"><a id="primary_tab" href="#primary" data-toggle="tab"><%= _("Template") %></a></li>
    <%= render "#{@type_name_plural}/custom_tab_headers" unless type == 'ptable'%>

    <li><a id='history_tab' href="#history" data-toggle="tab"><%= _("History") %></a></li>
    <% if show_location_tab? %>
      <li><a href="#locations" data-toggle="tab"><%= _("Locations") %></a></li>
    <% end %>
    <% if show_organization_tab? %>
      <li><a href="#organizations" data-toggle="tab"><%= _("Organizations") %></a></li>
    <% end %>
  </ul>

  <div class="tab-content">
    <div class="tab-pane active" id="primary">
      <%= text_f f, :name, :disabled => @template.locked? %>

      <% if show_default? %>
        <%= checkbox_f f, :default, :label=>_('Default'), :help_block => default_template_description %>
      <% end -%>

      <%= render "custom_tabs", :f => f if type == 'ptable' %>

      <% if @template.locked? -%>
        <%= alert :class => 'alert-warning', :header => '',
                  :text  => icon_text("warning-sign", (_("Warning: This template is locked. You may only change the associations. Please %s it to customize.") %
                                                            link_to(_('clone'), template_hash_for_member(::Template.find(params[:id]), 'clone_template'))).html_safe) %>

      <% else -%>
        <%= alert :class => 'alert-success', :header => '',
                  :text  => icon_text("info-sign", (_('Note: %s ') % link_to(_('Useful template functions and macros'),
                                                                             "http://www.theforeman.org/manuals/#{SETTINGS[:version].short}/index.html#4.4.3ProvisioningTemplates", :rel => 'external')).html_safe) %>
      <% end -%>


      <% if pxe_with_building_hosts?(@template) -%>
        <% warning_text = (_("Warning: The template is associated to at least one host in build mode. To apply the change, disable and enable build mode on hosts to update the live templates or choose to %s their configuration from 'Select Action' menu") % link_to(_('recreate'), building_hosts_path(@template))).html_safe %>
        <%= alert :class => 'alert-warning', :header => '',
                  :text => icon_text("warning-sign", warning_text) %>
      <% end -%>

      <div class='form-group'>
        <div class="col-md-12">
            <label class="control-label"  for="template"><%= _("Template editor") %></label>
        </div>

        <div class="col-md-12">
          <div class="editor-container">
            <%= render :partial => 'editor/toolbar', :locals => {:show_preview => true} %>

            <%= alert :class => 'alert-danger hide', :id => 'preview_error', :close => false %>
            <%= textarea_f f, :template, :class => "editor_source", :label =>:none, :disabled => @template.locked?, :size => "max",
                            :'data-file-name' => @template.name, :'data-render-path' => url_for(template_hash_for_member(@template, 'preview')) %>
          </div>
        </div>
      </div>

      <% unless @template.locked? -%>
        <%= file_field_f f, :template, :class => "editor_file_source",:size => "col-md-10", :id => 'template_file',
                         :help_block  => _("Selecting a file will override the editor and load the file instead") %>
      <% end -%>

      <%= textarea_f f, :audit_comment, :size => "col-md-10", :rows => 3, :label => _("Audit Comment"),
                     :help_block => _("The Audit Comment field is saved with the template auditing to document the template changes") %>
    </div>

    <div class="tab-pane" id="history">
      <% if @history.try(:any?) %>
        <% @history.each do |audit| %>
          <% next unless audit_template? audit %>
          <div class='row'>
            <div class='col-md-6'>
              <b><%= audit_user(audit) %> <%= audit.comment %></b>
            </div>
            <div class='col-md-2 ra'><h6><%= audit_time audit %></h6></div>
            <div class='col-md-8 audit-content'>
              <%= link_to_function icon_text("retweet", _("Revert")), "revert_template(this)", :data => {:url => revision_provisioning_templates_url, :version => audit.id } %>
              <%= link_to icon_text("eye-open", _("Show Diff")), audit_path(audit), :rel => 'external' %>
            </div>
          </div>
        <% end %>
      <% else %>
        <%= alert :header => _('No history found'), :text => _('Save something and try again') %>
      <% end %>
    </div>


    <%= render "custom_tabs", :f => f unless type == 'ptable'%>
    <%= render 'taxonomies/loc_org_tabs', :f => f, :obj => @template %>

    <%= submit_or_cancel f, false, :cancel_path => template_path_for(@template.class) %>
  </div>
  <%# These hidden inputs are used for the diff Preview. They intentionally do
      not have a 'name' attribute so browsers will not send them in the form POST. %>
  <input type="hidden" id="old" value="<%= @template.template %>" />
  <input type="hidden" id="new" value="<%= @template.template %>" />
<% end %>
