<div class="project_r_h">
  <h2 class="project_h2"><%= l(:label_invite_join) %></h2>
</div>
<%#= render_flash_messages %>
<%
   roles = Role.givable.all
   if @project.project_type == Project::ProjectType_course
     roles = roles[3..5]
   else
     roles = roles[0..2]
   end
   members = @project.member_principals.includes(:roles, :principal).all.sort
%>

<div class="box_main">
   <h3 class="box_h3"><%= l(:label_invite_trustie_user) %></h3>
   <% if roles.any? %>
      <%= form_for(@member, {:as => :membership, :url => project_memberships_path(@project), :method => :post}) do |f| %>
              <div class="invi_search">
                <input hidden="hidden" value="true" name="flag">
                <input id="principal_search" class="invi_search_input fl" type="text" placeholder=<%= l(:label_invite_trustie_user_tips) %>>
                <a class="invi_search_btn fl" onclick="observeSearchfield('principal_search', null, '<%= escape_javascript autocomplete_project_memberships_path(@project, :format => 'js',:flag => true)%> ')">搜索</a>
                <%= javascript_tag "observeSearchfield('principal_search', null, '#{ escape_javascript autocomplete_project_memberships_path(@project, :format => 'js',:flag => true) }')" %>
                <div class="cl"></div>
                <div id="principals_for_new_member">
                  <%= render_project_members(@project) %>
                </div>
                <div class="cl"></div>
                <ul class="mb5">
                  <li>
                    <%= l(:label_role_plural) %>:
                  </li>
                  <% roles.each do |role| %>
                  <li class="fl mr5">
                    <%= check_box_tag 'membership[role_ids][]', role.id %>
                    <% if User.current.language == "zh" %>
                        <% if role.id == 3 %>
                            <label >管理人员</label>
                        <% elsif role.id == 4 %>
                            <label >开发人员</label>
                        <% else %>
                            <label >报告人员</label>
                        <% end %>
                    <% else %>
                        <label ><%= h role %></label>
                    <% end %>
                  </li>
                  <% end %>
                </ul>
                <div class="cl mb10"></div>
                <a href="javascript:void(0)" class="btn_free" onclick="submit_invite_members()">
                  <%= l(:label_invite_members)%>
                </a>
              </div>
          <% end %>
      <% end %>
</div>
<script type="text/javascript">
    $(document).ready(function () {
        var collection=$("#principals_for_new_member").children("#principals").children("label");
        collection.css("text-overflow","ellipsis");
        collection.css("white-space","nowrap");
        collection.css("width","200px");
        collection.css("overflow","hidden");
        for(i=0;i<collection.length;i++){    //增加悬浮显示
            var label=collection[i];
            var text=$(label).text();
            $(label).attr("title",text);
        }
    });

    function nh_show_err_message(msg){
        $("#RSide>.flash").remove();
        $("#RSide").prepend('<div class="flash error" id="flash_error">'+msg+'</div>');
    }

    function submit_invite_members()
    {
        var user_ischeck=false;
        $("input[name='membership[user_ids][]']").each(function(){
            if($(this).prop('checked')){
                user_ischeck=true;
            }
        });
        var role_ischeck=false;
        $("input[name='membership[role_ids][]']").each(function(){
            if($(this).prop('checked')){
                role_ischeck=true;
            }
        });
        if(user_ischeck==false && role_ischeck==false){
            nh_show_err_message('请选择用户和角色！');
            return false;
        }
        if(user_ischeck==false){
            nh_show_err_message('请选择用户！');
            return false;
        }
        if(role_ischeck==false){
            nh_show_err_message('请选择角色！');
            return false;
        }
        $('#new_membership').submit();
    }
</script>