<% content_for :header_tags do %>
    <%= import_ke(enable_at: true, prettify: false, init_activity: false) %>
    <%= javascript_include_tag 'homework','baiduTemplate' %>
<% end %>
<div class="homepageRightBanner mb10">
  <div class="NewsBannerName">编辑作品</div>
</div>
<div class="cl"></div>

<div class="HomeWork" id="users_setting">

  <div class="HomeWorkBox">
    <div class="">
      <div class="homepagePostTitle fl m_w530 hidden">
        <%= @homework.name%>（作业名称）
      </div>
      <span class="fr c_grey">
        截止时间：<%= @homework.end_time%>
      </span>
      <div class="cl"></div>
      <a href="javascript:void(0);" class="c_blue">
        <%= link_to @homework.user.show_name, user_activities_path(@homework.user_id), :class => "c_blue"%>
      </a>
      <div class="cl"></div>
      <div class="HomeWorkP">
        <%= @homework.description.html_safe %>
      </div>
    </div>
  </div><!----HomeWorkBox end-->
  <div class="cl"></div>

  <div class="HomeWorkCon mt15" nhname='student_work_form'>
    <%= labelled_form_for @work,:html => { :multipart => true },:remote=>true   do |f|%>
        <div class=" c_red mb10">
          提示：作品名称和描述中不要出现真实的姓名信息
        </div>
        <div class="cl"></div>
        <% if @homework.homework_type == 3 %>
            <span id="min_num_member" style="display: none"><%=@homework.homework_detail_group.min_num %></span>
            <span id="max_num_member" style="display: none"><%=@homework.homework_detail_group.max_num %></span>
            <% str = User.current.id.to_s%>
            <% @work.student_work_projects.where("is_leader = ?", 0).each do |pro| %>
                <% str += ','+pro.user_id.to_s %>
            <% end %>
            <%=hidden_field_tag 'group_member_ids', params[:group_member_ids], :value=>str %>
        <% end %>
        <div>
          <input type="text" name="student_work[name]" id="student_work_name" placeholder="请输入作品名称" class="InputBox  W700" maxlength="200" onkeyup="regexStudentWorkName();" value="<%= @work.name%>">
          <div class="cl"></div>
          <p id="student_work_name_span" class="c_red mb10"></p>
        </div>
        <div class="mt10">
          <textarea placeholder="请输入作品描述" style="display: none" nhname='student_work_textarea' name="student_work[description]"><%= @work.description%></textarea>
          <!--<textarea  name="student_work[description]" id="student_work_description" placeholder="请输入作品描述"  class="InputBox  W700 H150"  maxlength="6000" onkeyup="regexStudentWorkDescription();"><%#= @work.description%></textarea>-->
          <!--<script>
              var text = document.getElementById("student_work_description");
              autoTextarea(text);// 调用
          </script>-->
          <div class="cl"></div>
          <p id="student_work_description_textarea" class="c_red mb10"></p>
          <p id="e_tip" class="c_grey"></p>
          <p id="e_tips" class="c_grey"></p>
        </div>

        <div id="homework_attachments">
          <%= render :partial => 'users/user_homework_attachment', :locals => {:container => @work, :has_program=>false,:has_group=>false} %>
        </div>

        <% if @homework.homework_type == 3 %>
            <div class="mt5 fl">
              <a href="javascript:void(0);" class="memberBtn fl mt3 mr15" title="请添加小组的其他成员" onclick="show_group_member();">合作成员</a>
            </div>
        <% end %>

        <div class="mt5">
          <a href="javascript:void(0);" class="BlueCirBtnMini fr" id="new_message_submit_btn">确定</a>
          <span class="fr mr10 mt3">或</span>
          <%= link_to "取消",  student_work_index_path(:homework => @homework), :class => "fr mr10 mt3"%>
        </div>
        <div class="cl"></div>
    <% end%>
  </div><!----HomeWorkCon end-->
</div>
<script type="text/javascript">
    <% if @homework.homework_detail_group %>
    $(function(){
        <%members = @work.student_work_projects.where("is_leader =?",0) %>
        var str = $('#group_members_show').html();
        <% members.each do |member| %>
            str += '、<%= (User.find member.user_id).show_name %>';
        <% end %>
        $('#group_members_show').html(str);
        $('span.group_detail_info').text('分组人数：<%=@homework.homework_detail_group.min_num %>-<%=@homework.homework_detail_group.max_num %> 人');
    });
    <% end %>
    // 添加组成员
    function show_group_member() {
        $('#ajax-modal').html('<%= escape_javascript(render :partial => 'student_work/choose_group_member',:locals => {:homework=>@homework,:edit_mode => true}) %>');
        showModal('ajax-modal', '528px');
        $('#ajax-modal').siblings().remove();
        $('#ajax-modal').parent().css("top","").css("left","");
        $('#ajax-modal').parent().addClass("addMemberCP");
    }

    function popupRegex(){
        if($("#group_member_ids").length > 0) {
            if(regexStudentWorkMember(parseInt($.trim($("#min_num_member").html())),parseInt($.trim($("#max_num_member").html())))) {
                $('#ajax-modal').html("<div><p align='center' style='margin-top: 35px'>作品信息完整性校验中，请稍等...</p></div>");
                showModal('ajax-modal', '500px');
                $('#ajax-modal').siblings().remove();
                $('#ajax-modal').parent().css("top","").css("left","").css("border","3px solid #269ac9");
                $('#ajax-modal').parent().addClass("anonymos");
            }
        } else {
            $('#ajax-modal').html("<div><p align='center' style='margin-top: 35px'>作品信息完整性校验中，请稍等...</p></div>");
            showModal('ajax-modal', '500px');
            $('#ajax-modal').siblings().remove();
            $('#ajax-modal').parent().css("top","").css("left","").css("border","3px solid #269ac9");
            $('#ajax-modal').parent().addClass("anonymos");
        }
    }

    function nh_check_field(params){
        var result=true;
        if(!regexStudentWorkName()) {
            result=false;
            return result;
        }
        if(params.content!=undefined){
            if(params.content.isEmpty()){
                result=false;
            }
            if(params.content.html()!=params.textarea.html() || params.issubmit==true){
                params.textarea.html(params.content.html());
                params.content.sync();

                if(params.content.isEmpty()){
                    params.contentmsg.html('作品描述不能为空');
                }else{
                    params.contentmsg.html('');
                }
            }
        }
        return result;
    }
    function init_homework_form(params){
        params.form.submit(function(){
            params.textarea.html(params.editor.html());
            params.editor.sync();
            var flag = false;
            if(params.form.attr('data-remote') != undefined ){
                flag = true
            }
            var is_checked = nh_check_field({
                issubmit:true,
                content:params.editor,
                contentmsg:params.contentmsg,
                textarea:params.textarea
            });

            if(is_checked){
                if(flag){
                    popupRegex();
                    return true;
                }else{
                    $(this)[0].submit();
                    $("#ajax-indicator").hide();
                    return false;
                }
            }
            return false;
        });
    }
    function init_homework_editor(params){
        params.textarea.removeAttr('placeholder');
        var editor = params.kindutil.create(params.textarea, {
            resizeType : 1,minWidth:"1px",width:"100%",minHeight:"30px",height:"30px",
            items : ['code','emoticons','fontname',
                'forecolor', 'hilitecolor', 'bold', '|', 'justifyleft', 'justifycenter', 'insertorderedlist','insertunorderedlist', '|',
                'formatblock', 'fontsize', '|','indent', 'outdent',
                '|','imagedirectupload','table',   'media', 'preview',"more"
            ],
            afterChange:function(){//按键事件
                var edit = this.edit;
                var body = edit.doc.body;
                //paramsHeight = params.kindutil.removeUnit(this.height);
                edit.iframe.height(150);
                this.resize(null, Math.max((params.kindutil.IE ? body.scrollHeight : body.offsetHeight) +  33, 150));
            },
            afterCreate:function(){
                //init
                var edit = this.edit;
                var body = edit.doc.body;
                edit.iframe[0].scroll = 'no';
                body.style.overflowY = 'hidden';
                //reset height
                var edit = this.edit;
                var body = edit.doc.body;
                edit.html(params.textarea.innerHTML);
                //paramsHeight = params.kindutil.removeUnit(this.height);
                edit.iframe.height(150);
                this.resize(null, Math.max((params.kindutil.IE ? body.scrollHeight : body.offsetHeight) , 150));
                elocalStorage(editor2,'student_work_<%=@work.id %>');
            }
        }).loadPlugin('paste');
        return editor;
    }
    KindEditor.ready(function(K){
        $("div[nhname='student_work_form']").each(function(){
            var params = {};
            params.kindutil = K;
            params.div_form = $(this);
            params.form = $("form",params.div_form);
            if(params.form==undefined || params.form.length==0){
                return;
            }
            params.textarea = $("textarea[nhname='student_work_textarea']",params.div_form);
            params.contentmsg = $("#student_work_description_textarea");
            params.submit_btn = $("#new_message_submit_btn");
            if(params.textarea.data('init') == undefined) {
                params.editor = init_homework_editor(params);
                editor2 = params.editor;
                init_homework_form(params);
                params.submit_btn.click(function () {
                    params.form.submit();
                    $("#ajax-indicator").hide();
                });
                params.textarea.data('init', 1);
            }
        });
    });
</script>