<% if @homework.homework_detail_group %>
<div id="popbox02">
  <div>
    <div class="relateText fl">请从<%= @homework.homework_detail_group.base_on_project == 1 ? '项目成员':'课程成员' %>中添加小组成员</div>
  </div>
  <div class="resourcePopupClose"> <a href="javascript:void(0);" class="resourceClose" onclick="clickCanel();"></a></div>
  <div class="cl"></div>
  <div>
    <form class="resourcesSearchBox">
      <input type="text" name="serach" placeholder="输入关键字搜索" class="searchResourcePopup" />
    </form>
  </div>
  <ul class="addMemberC fl" id="all_students_list"></ul>
  <div class="rightArrow"><img src="/images/course/right-arrow.png" width="16" height="30" /></div>
  <ul class="addMemberC fr" id="choose_students_list">
    <li id="choose_student_<%=User.current.id %>"><%=User.current.show_name %>
      <% unless User.current.user_extensions.student_id == "" %>
          (<%=User.current.user_extensions.student_id %>)
      <% end %>
    </li>
  </ul>
  <div class="cl"></div>
  <div class="courseSendCancel mr15" style="float:right;"><a href="javascript:void(0);" class="sendSourceText" onclick="clickCanel();">取消</a></div>
  <div class="courseSendSubmit" style="float:right;"><a href="javascript:void(0);" class="sendSourceText" onclick="clickOK()">确定</a></div>
  <div class="cl"></div>
</div>
<script type="text/javascript">
    var lastSearchCondition = '';
    var count = 0;
    function search_stus(e){
        if($(e.target).val().trim() == lastSearchCondition && lastSearchCondition != '')
        {
            return;
        }
        lastSearchCondition = $(e.target).val().trim();
        $.ajax({
            url: '<%= url_for(:controller => 'student_work', :action => 'search_course_students') %>'+'?name='+ e.target.value+'&homework='+<%=@homework.id %>,
            type:'get'
        });
    }

    function throttle(method,context,e){
        clearTimeout(method.tId);
        method.tId=setTimeout(function(){
            method.call(context,e);
        },500);
    }

    //查询
    $("input[name='serach']").on('input', function (e) {
        throttle(search_stus,window,e);
    });

    function clickOK() {
        var str="";
        var show = "合作成员：";
        var lists = $("#choose_students_list li");
        if(lists.length > 0) {
            for(var i=0; i<lists.length; i++) {
                var id = $(lists[i]).attr("id").replace(/[^0-9]/ig,"");
                str += id;
                var show_name = $(lists[i]).html();
                var index = show_name.indexOf("(");
                if (index != -1) {
                    var name = show_name.substring(0,show_name.indexOf("("));
                } else {
                    var name = show_name;
                }
                show += name;
                if (i == 0) {
                    show += "(组长)";
                }
                if (i != lists.length -1) {
                    str += ",";
                    show += "、";
                }
            }
            $("#group_member_ids").val(str);
        }
        hideModal("#popbox02");
        $("#group_members_show").show().html(show);
    }

    function delete_student(id) {
        $("#choose_student_"+id).remove();
        $("#student_"+id).one("click",function choose_student() {
            $("#choose_students_list").append("<li id='choose_student_"+id+"' onclick='delete_student("+id+");'>"+$("#student_"+id).html()+"</li>");
        });
    }

    $(document).ready(function(){
        $.ajax({
            url: '<%= url_for(:controller => 'student_work', :action => 'search_course_students') %>'+'?homework='+<%=@homework.id %>,
            type:'get'
        });
        <% if defined?(edit_mode) && edit_mode %>
            <% pro = @homework.student_work_projects.where("user_id = ?",User.current.id).first.project_id.to_i %>
            <% members = @homework.student_work_projects.where("project_id = ? and is_leader =?",pro,0) %>
            <% members.each do |member| %>
                var link = "<li id='choose_student_<%=member.user_id%>' onclick='delete_student(<%=member.user_id %>);'><%=member.user.show_name %>";
                <% unless member.user.user_extensions.student_id == "" %>
                link += "(<%=member.user.user_extensions.student_id %>)";
                <% end %>
                link += "</li>";
                $("#choose_students_list").append(link);
            <% end %>
        <% end %>
    });
</script>
<% end %>