<%
  project_params = @project_params || {}
  allowed_parents = @new_project.allowed_parents(nil, :force => :projects)
  p = @projects.shift
%>
<%= title l(:label_template_new) -%>

<%= form_tag({ :action => 'make_project_from_template', :id => @source_project }, :method => :post, :id => 'project-from-template', :class => 'form-box') do -%>
  <div class="box tabular long2 settings">
    <p id="project-template">
      <%= label_tag :project_root, l('project_template.label_name') %>
      <%= text_field_tag('', @source_project, :disabled => true) %>
      <%= hidden_field_tag 'template[assign_entity][type]', params[:assign_entity_type] %>
      <%= hidden_field_tag 'template[assign_entity][id]', params[:assign_entity_id] %>
    </p>
    <% if p %>
      <%= hidden_field_tag 'template[project][][id]', p.id %>
      <p id="project-name">
        <%= label_tag "template_project_#{p.id}_name", l('project_template.label_new_project'), :class => 'required' %>
        <%= text_field_tag 'template[project][][name]', project_params[:name] || p.name, {:id => "template_project_#{p.id}_name", :autofocus => true} -%>
        <%= javascript_tag "$('#template_project_#{p.id}_name').focus()" %>
      </p>
      <p id="project-root">
        <% if allowed_parents.compact.empty? %>
          <%= hidden_field_tag 'template[parent_id]', '' %>
        <% else -%>
          <%= label_tag('', l(:field_parent), :class => allowed_parents.include?(nil) ? nil : 'required') %>
          <%= parent_project_select_tag(@new_project, :force => :projects, :from_template => true, :html => {:name => 'template[parent_id]', :id => 'template_parent_id'}) -%>
        <% end %>
      </p>
      <% if EasySetting.value('project_display_identifiers') -%>
        <p>
          <%= label_tag 'template[project][][identifier]', l('field_identifier'), :class => 'required' %>
          <%= text_field_tag 'template[project][][identifier]', project_params[:identifier] || @project_identifier, :size => 50 -%>
          <br />
          <em>
            <%= l(:text_length_between, :min => 1, :max => Project::IDENTIFIER_MAX_LENGTH) %> <%= l(:text_project_identifier_info).html_safe %>
          </em>
        </p>
      <% end %>
      <% unless p.custom_field_values.blank? %>
        <%= toggling_container("project_template_custom_fields_#{p.id}", User.current, {:heading => l(:label_project_templates_custom_fields), :container_html => {:class => ''}, :default_button_state => false}) do %>
          <%= render :partial => 'projects/form_project_custom_fields', :locals => {:custom_field_values => p.custom_field_values, :form_name_prefix => 'template[project][]', :project => p, :values_from_params => project_params[:custom_field_values]} %>
        <% end %>
      <% end %>
    <% end %>

    <% new_identifier = @project_identifier %>
    <% @projects.each_with_index do |project, i| -%>
      <% subproject_params = @template_params[:project].try(:[], i + 1) || {} %>
      <% if Setting.sequential_project_identifiers? && !new_identifier.nil? %>
        <% new_identifier = new_identifier.to_s.succ %>
      <% end %>
      <fieldset>
        <%= hidden_field_tag 'template[project][][id]', project.id %>
        <p class="subproject">
          <%= label_tag 'template[project][][name]', project.to_s, :class => 'required' %>
          <%= text_field_tag 'template[project][][name]', subproject_params[:name] || project.name, {:size => 50, :id => "template_project_#{project.id}_name"} -%>
        </p>
        <% if EasySetting.value('project_display_identifiers') -%>
          <p>
            <%= label_tag 'template[project][][identifier]', l('field_identifier'), :class => 'required' %>
            <%= text_field_tag 'template[project][][identifier]', subproject_params[:identifier] || new_identifier, :size => 50 -%>
            <br />
            <em>
              <%= l(:text_length_between, :min => 1, :max => Project::IDENTIFIER_MAX_LENGTH) %> <%= l(:text_project_identifier_info).html_safe %>
            </em>
          </p>
        <% end %>
        <% unless project.custom_field_values.blank? %>
          <%= toggling_container("project_template_custom_fields_#{project.id}", User.current, {:heading => l(:label_project_templates_custom_fields), :container_html => {:class => ''}, :default_button_state => false}) do %>
            <%= render :partial => 'projects/form_project_custom_fields', :locals => {:custom_field_values => project.custom_field_values, :form_name_prefix => 'template[project][]', :project => project, :values_from_params => subproject_params[:custom_field_values]} %>
          <% end %>
        <% end %>
      </fieldset>
    <% end -%>

    <p id="form-start-date">
      <%= label_tag 'template[start_date]', @projects.size == 0 ? l(:label_template_easy_start_date) : l(:label_templates_easy_start_date) %>
      <%= text_field_tag 'template[start_date]', @template_params[:start_date] || Date.current, {:size => 10} -%>
      <%= calendar_for('template_start_date') -%>
    </p>

    <p id="form-checkbox">
      <%= label_tag 'template[update_dates]', l(:label_project_templates_update_dates) %>
      <%= check_box_tag 'template[update_dates]', '1', @template_params[:update_dates] == '1' -%>
    </p>

    <p>
      <%= label_tag 'template[match_starting_dates]', l(:label_match_starting_dates) %>
      <%= check_box_tag 'template[match_starting_dates]', '1', @template_params[:match_starting_dates] == '1' -%>
    </p>

    <div id="additional_template_options">
      <p>
        <%= label_tag(:change_issues_author, l(:label_project_templates_change_issues_author)) -%>
        <%= select_tag('template[change_issues_author]', options_for_select([[l(:label_no_change_option), '']] + User.active.non_system_flag.easy_type_internal.sorted.collect{|u| [u.name, u.id]}, @template_params[:change_issues_author])) %>
      </p>

      <%= call_hook(:view_templates_create_project_from_template, :source_project => @source_project, :template_params => @template_params) %>
    </div>
  </div>
  <div class="form-actions">
    <label class="inline"><%= check_box_tag 'notifications', '1', params[:notifications] %> <%= l(:label_project_notifications) %></label><br/>
    <%= submit_tag l(:button_new_project_from_template), :class => 'button-positive' %>
  </div>

<% end %>

<script type="text/javascript">
  $('#template_parent_id').on('change', function(){
    $(this).closest('form').find('.inheritance-option').toggle(this.value !== '');
  });
  $('#template_parent_id').change();
</script>

<% ### PAGE CUSTOMS ########################################################## %>

<% html_title(l(:label_template_new)) -%>
