<div class="page">
  <header class="page-header">
    <h1 class="page-title"><%= h @profile.name -%></h1>
    <div class="page-description">
      <a href="<%= ApplicationController.root_context -%>/profiles">&larr;&nbsp;<%= h message('quality_profiles.page') -%></a>
    </div>
  </header>

<%= render :partial => 'profiles/tabs', :locals => {:selected_tab => 'Projects'} %>

<div class="tabs-panel">
  <% if profiles_administrator? %>
    <form method="POST" action="<%= ApplicationController.root_context -%>/profiles/add_project" id="add_project_form">
      <input type="hidden" name="id" value="<%= @profile.id -%>"/>

      Add project: <%= resource_select_tag 'project', {
      :qualifiers => ['TRK'],
      :width => '400px',
      :html_id => "select-project",
    } -%>
      <script>$j('#select-project').on("change", function (e) {
        $j(this).select2("disable");
        $j('#add_project_form').submit();
      })</script>
    </form>

    <% unless @projects.empty? %>
      <table class="data" id="projects-table">
        <thead>
        <tr>
          <th></th>
          <th></th>
        </tr>
        </thead>
        <tbody>
        <% @projects.each do |project| %>
          <tr class="<%= cycle('even', 'odd') -%>">
            <td class="thin">
              <%= link_to_action message('quality_profiles.remove_project_action'),
                                 "#{ApplicationController.root_context}/profiles/remove_project?id=#{@profile.id}&project=#{project.id}",
                                 :class => 'link-action',
                                 :id => "link-remove-#{project.key.parameterize}",
                                 :confirm_title => 'quality_profiles.remove_project_confirm_title',
                                 :confirm_button => 'quality_profiles.remove_project_confirm_button',
                                 :confirm_msg => 'quality_profiles.remove_project_confirm_message',
                                 :confirm_msg_params => [project.name]
              -%>
            </td>
            <td><%= h project.name -%> <span class="small gray"><%= h project.key -%></span></td>
          </tr>
        <% end %>
        </tbody>
        <tfoot>
        <tr>
          <td colspan="2">
            <%= link_to_action message('quality_profiles.remove_projects_action'),
                               "#{ApplicationController.root_context}/profiles/remove_projects?id=#{@profile.id}",
                               :class => 'link-action',
                               :id => "link-remove-projects",
                               :confirm_title => 'quality_profiles.remove_projects_confirm_title',
                               :confirm_button => 'quality_profiles.remove_projects_confirm_button',
                               :confirm_msg => 'quality_profiles.remove_projects_confirm_message'
            -%>
          </td>
        </tr>
        </tfoot>
      </table>
    <% end %>
  <% else %>

    <% if @projects.empty? %>
      <p><%= message('quality_profiles.no_projects_associated_to_profile_x', :params => @profile.name) -%></p>
    <% else %>
      <p><%= message('quality_profiles.projects_warning') -%></p>

      <table class="data">
        <thead>
        <tr>
          <th></th>
        </tr>
        </thead>
        <tbody>
        <% @projects.each do |project| %>
          <tr class="<%= cycle('even', 'odd') -%>">
            <td><%= h project.name -%> <span class="small gray"><%= h project.key -%></span></td>
          </tr>
        <% end %>
        </tbody>
      </table>
    <% end %>
  <% end %>
</div>
</div>
