<%= include_javascript %>
<% 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" %>

    <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 -%>

      <% 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 -%>

      <div class='form-group'>
        <label class="control-label" for="template"><%= _("Template editor") %></label>
        <div class="navbar navbar-form">
          <div class="btn-group" data-toggle="buttons" >
            <label class="btn btn-default btn-sm active" onclick="set_code()">
              <input type="radio" name="options" id="option1" ><%= _("Code") %>
            </label>
            <label class="btn btn-default btn-sm" onclick="set_preview()">
              <input type="radio" name="options" id="option2" ><%= _("Diff") %>
            </label>
            <label class="btn btn-default btn-sm" onclick="set_render()">
              <input type="radio" name="options" id="option3" ><%= _("Preview") %>
            </label>
          </div>
          <span id="preview_host_selector" style="display:none">
            <%= select_tag :preview_host_id, options_from_collection_for_select(@template.preview_host_collection, :id, :name),
                           :onchange => 'get_rendered_template()' %>
          </span>
          <%= fullscreen_button("$('#editor1')") %>
          <div class="fr">
            <%= select_tag('keybinding', content_tag(:optgroup, options_for_select([_('Default'), 'Vim', 'Emacs']), :label => _('Key Binding')), :class => 'form-control input-sm') %>
          </div>
        </div>
      </div>
      <%= alert :class => 'alert-danger hide', :id => 'preview_error', :close => false %>
      <%= textarea_f f, :template, :class => "template_text", :label =>:none, :disabled => @template.locked?, :size => "max",
                     :'data-file-name' => @template.name, :'data-render-path' => url_for(template_hash_for_member(@template, 'preview')) %>

      <% unless @template.locked? -%>
        <%= file_field_f f, :template, :class => "template_file",: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 %>

    <%= 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 %>
