<% content_for :header_tags do %>
    <%= import_ke(enable_at: true, prettify: false, init_activity: false) %>

    <%= javascript_include_tag 'homework','baiduTemplate' %>
    <script type="text/javascript">
      var first_click = true;
      $(function(){
        if($.trim($("#anonymous_comment").val()) == 1) {
            $("#anonymous_comment").attr('checked',true);
        } else {
            $("#anonymous_comment").attr('checked',false);
        }
        <% if edit_mode && homework.is_program_homework? %>
            $("#BluePopupBox a.BlueCirBtn").click();
        <% end %>
        <% if edit_mode && homework.is_group_homework? %>
            $("#GroupPopupBox a.group_save_btn").click();
        <% end %>
        <% if edit_mode %>
          $("#anonymous_comment").on('click',function(){
            $("#anonymous_hint").show();
          });
        <% end %>
      });
      function checked_val() {
          if ($("#anonymous_comment").is(":checked")) {
              $("#anonymous_comment").val(1);
          } else {
              $("#anonymous_comment").val(0);
          }
      }
      function nh_reset_homework_form(params){
          if(params.textarea.html() != "") {
              cancel_edit();
          } else {
              params.form[0].reset();
              params.textarea.empty();
              if(params.editor != undefined){
                  params.editor.html(params.textarea.html());
              }
          }
      }
      function init_homework_form(params){
          params.form.submit(function(){
              var flag = false;
              if(params.form.attr('data-remote') != undefined ){
                  flag = true
              }
              var is_checked = false;
              checked_val();
              if(!regex_homework_name()){
                  $("#homework_name").focus();
              }
              else if(!regex_homework_end_time()){
                  $("#homework_end_time").focus();
              }
              else if(!regex_homework_end_publish_time()){
                  $("#homework_end_time").focus();
              }
              else if(!regex_course_id()){
                  $("#course_id").focus();
              }
              else{
                  params.textarea.html(params.editor.html());
                  params.editor.sync();
                  is_checked = true;
              }
              /*var is_checked = nh_check_field({
                  issubmit:true,
                  content:params.editor,
                  contentmsg:params.contentmsg,
                  textarea:params.textarea
              });*/
              if(first_click && is_checked){
                  if(flag){
                      first_click = false;
                      return true;
                  }else{
                      first_click = false;
                      $(this)[0].submit();
                      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(homework_description_editor,'homework_<%=User.current.id %>');
              }
          }).loadPlugin('paste');
          return editor;
      }
      KindEditor.ready(function(K){
          $("div[nhname='homework_common_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='homework_textarea']",params.div_form);
              params.cancel_btn = $("#new_message_cancel_btn");
              params.submit_btn = $("#new_message_submit_btn");
              if(params.textarea.data('init') == undefined) {
                  params.editor = init_homework_editor(params);
                  homework_description_editor = params.editor;
                  init_homework_form(params);
                  params.submit_btn.click(function () {
                      params.form.submit();
                  });
                  params.cancel_btn.click(function () {
                      reset_homework();
                      nh_reset_homework_form(params);
                  });
                  params.textarea.data('init', 1);
              }
          });
      });
    </script>
<% end %>

<div class="HomeWorkCon">
  <a id="submit_homework"></a>
  <div>
    <input  type="text" name="homework_common[name]" id="homework_name" class="InputBox  w701" maxlength="255" onfocus="$('#homework_editor').show()" onkeyup="regex_homework_name();" placeholder="发布作业，请先输入作业标题" value="<%= homework.name%>" >
    <p id="homework_name_span" class="c_red mt5"></p>
  </div>
  <div class="cl"></div>

  <div class=" mt10">
    <%= link_to("从题库选用", user_import_homeworks_user_path(User.current.id,:select_course => defined?(select_course)),:class => "BlueCirBtn fl mr10",:remote => true,:title=>"导入自己发布过的作业，或者共享题库中的作业") unless edit_mode%>
    <% unless edit_mode %>
        <input type="hidden" name="quotes" id="ref_homework_id" value=""/>
    <% end %>
    <% if edit_mode %>
        <label class="fl c_grey f14" style="margin-top: 4px;">截止日期：</label>
    <% end %>
    <div class="calendar_div fl mr10">
      <input  type="text" name="homework_common[end_time]" id="homework_end_time" placeholder="截止日期" class="InputBox fl W120 calendar_input" readonly="readonly" value="<%= homework.end_time%>"  >
      <% if homework.homework_detail_manual.comment_status.to_i < 2 %>
        <%= calendar_for('homework_end_time')%>
      <% end %>
    </div>
    <% if edit_mode %>
        <label class="fl c_grey f14" style="margin-top: 4px;">发布日期(可选)：</label>
    <% end %>
    <div class="calendar_div fl">
      <input  type="text" name="homework_common[publish_time]" id="homework_publish_time" placeholder="发布日期(可选)" class="InputBox fl W120 calendar_input" readonly="readonly" value="<%= homework.publish_time%>"  >
      <% if homework.homework_detail_manual.comment_status.to_i == 0 %>
        <%= calendar_for('homework_publish_time')%>
      <% end %>
    </div>
    <% if !edit_mode || edit_mode && homework.homework_detail_manual.comment_status < 2 %>
        <div class="fl f14 ml10" style="margin-top: 4px;">
          <input type="checkbox" name="homework_common[anonymous_comment]" value="<%=edit_mode ? homework.anonymous_comment : 1 %>" id="anonymous_comment"/>
          <span class="f14 c_grey">禁用匿评</span>
          <span id="anonymous_hint" style="display: none; font-size: 12px;" class="c_red">更改后评分比例将恢复默认值</span>
        </div>
    <% end %>
  </div>
  <div class="cl"></div>
  <p id="homework_end_time_span" class="c_red mt5"></p>
  <div class="cl"></div>
  <div id="homework_editor" style="display: <%= edit_mode ? 'block':'none'%>">
    <div class="mt10">
      <% if edit_mode %>
          <textarea placeholder="有问题或有建议，请直接给我留言吧！" style="display: none" nhname='homework_textarea' name="homework_common[description]"><%=homework.description %></textarea>
          <%#= f.kindeditor :description, :editor_id => 'homework_description_editor', :height => "150px", :owner_id => homework.id, :owner_type => OwnerTypeHelper::HOMEWORKCOMMON, at_id: homework.id, at_type: homework.class.to_s %>
      <% else %>
          <%= hidden_field_tag :asset_id, params[:asset_id], :required => false, :style => 'display:none' %>
          <textarea placeholder="有问题或有建议，请直接给我留言吧！" style="display: none" nhname='homework_textarea' name="homework_common[description]"></textarea>
          <%#= f.kindeditor :description, :editor_id => 'homework_description_editor', :height => "150px",at_id: homework.id, at_type: homework.class.to_s  %>
      <% end %>
    </div>
    <div class="cl"></div>

    <!--<div class="mt10 <%= defined?(select_course) ? 'none' : ''%>">
      <%#= select_tag :course_id, options_for_select(get_as_teacher_courses(User.current), homework.course_id), {:class => "InputBox w709",:value => "请选择发布作业的课程"} %>
    </div>-->
    <%= hidden_field_tag :course_id, params[:course_id], :value =>homework.course_id %>
    <p id="homework_course_id_span" class="c_red mt5"></p>
    <p id="e_tip" class="c_grey"></p>
    <p id="e_tips" class="c_grey"></p>
    <div class="cl"></div>

    <div id="homework_attachments">
      <!-- 编辑作业 && 作业类型为匿评作业   时没有编程选项-->
      <%= render :partial => 'users/user_homework_attachment', :locals => {:container => homework, :has_program=>!(edit_mode && homework.homework_type != 2), :has_group=>(!(edit_mode && homework.homework_type != 3))&& homework.student_works.empty?,:show_member => true} %>
    </div>

    <div class="mt5">
      <% if edit_mode %>
          <a href="javascript:void(0);" id="new_message_submit_btn" class="BlueCirBtnMini fr">确定</a>
          <span class="fr mr10 mt3">或</span>
          <%#= link_to "取消",user_homeworks_user_path(User.current.id),:class => "fr mr10 mt3"%>
          <a href="javascript:void(0);" id="new_message_cancel_btn" class="fr mr10 mt3">取消</a>
    <% else %>
          <a href="javascript:void(0);" id="new_message_submit_btn" class="BlueCirBtnMini fr">发送</a>
          <span class="fr mr10 mt3">或</span>
          <a href="javascript:void(0);" id="new_message_cancel_btn" class="fr mr10 mt3">取消</a>
      <% end %>
      <div class="cl"></div>
    </div>

    <input type="hidden" name="homework_type" value="<%= edit_mode ? homework.homework_type : 1 %>">

  </div>
</div>
<div class="cl"></div>

<script id="t:test-answer-list" type="text/html">
  <div class="mt10">
    <label class="fl mt5" name="inputs_label">&nbsp;&nbsp;&nbsp;</label>
    <textarea class="InputBox  W320 fl mr10" placeholder="测试输入" name="program[input][]"></textarea>
    <textarea class="InputBox  W320  fl mr5" placeholder="测试输出" name="program[output][]"></textarea>
    <a href="javascript:void(0);" class=" fl icon_add" title="增加测试组"></a>
    <a href="javascript:void(0);" class=" fl icon_remove" title="删除测试组"></a>
    <div class="cl"></div>
  </div>
</script>

<script type="text/html" id="t:program-input-list">
  <div class="program-input">
    <input type="hidden" name="language_type" value="<!= language_type !>" />
    <! for(var i=0; i< input_groups.length; ++i) { !>
        <input  name="program[input][]" type="hidden" value="<!= input_groups[i].input !>" />
        <input  name="program[output][]" type="hidden" value="<!= input_groups[i].output !>" />
    <! } !>
  </div>
</script>

<script id="t:group-input-list" type="text/html">
  <div class="group-input">
    <input type="hidden" name="min_num" value="<!= min_num !>" />
    <input type="hidden" name="max_num" value="<!= max_num !>" />
    <input type="hidden" name="base_on_project" value="<!= base_on_project !>" />
  </div>
</script>
  
<div class="BluePopupBox" id="BluePopupBox" style="display:none;">
  <%= render :partial => 'users/user_programing_attr', :locals => {:edit_mode => edit_mode, :homework=>homework} %>
</div><!----BluePopupBox end-->

<div class="BluePopupBox" style="display:none;" id="GroupPopupBox">
  <%= render :partial => 'users/user_group_attr', :locals => {:edit_mode => edit_mode, :homework=>homework}%>
</div>

<% unless edit_mode %>
    <div class="BluePopupBox" style="display:none;" id="NoticePopupBox">
      <%= render :partial => 'users/homework_type_notice', :locals => {:edit_mode => edit_mode, :homework=>homework}%>
    </div>
<% end %>