<%= stylesheet_link_tag  'polls', :media => 'all' %>

<script type="text/javascript">
//编辑问卷描述之后
var popWindow ; //弹出框的引用
var importPollPopWindow; //选择导入的弹出框引用
function edit_head(){
    $("#polls_description").val($("#polls_description_div").html());
}
$(function(){
    //点击空白处
    $(document).bind('click',function(e){
        //弹出框非空 不是a标签 点击的不是弹出框 ，那么弹出框就会隐藏
        if(popWindow && e.target.nodeName != 'A' && !popWindow.is(e.target) && popWindow.has(e.target).length === 0){ // Mark 1
            popWindow.css('display', 'none');
        }
        if(importPollPopWindow && e.target.nodeName != 'A' && !importPollPopWindow.is(e.target) && importPollPopWindow.has(e.target).length === 0){
            importPollPopWindow.css('display', 'none');
        }
    });
})

function dismiss(quest_type,quest_id){
    popWindow = $("#div_"+quest_type+"_"+quest_id);
    if(popWindow){
        popWindow.css('display', 'none');
    }
}

function chooseQuestionType(quest_type,quest_id){
    //quest_type 分为 mc mcq single multi
    //quest_id 是quetion的id  下同
    if(popWindow){
        popWindow.css('display', 'none');
    }
    popWindow = $("#div_"+quest_type+"_"+quest_id);
    $("#div_"+quest_type+"_"+quest_id).click(function(e){
        e.stopPropagation(); //组织冒泡到document.body中去
    });
    $("#div_"+quest_type+"_"+quest_id).css("position", "absolute");

    $("#div_"+quest_type+"_"+quest_id).css("top", $("#add_"+quest_type+"_"+quest_id).offset().top+30);

    $("#div_"+quest_type+"_"+quest_id).css("left", $("#add_"+quest_type+"_"+quest_id).offset().left-10);
    if( $("#div_"+quest_type+"_"+quest_id).css('display') == 'block') {
        $("#div_"+quest_type+"_"+quest_id).css('display', 'none');
    }
    else{
        $("#div_"+quest_type+"_"+quest_id).css('display', 'block');
    }
}

function add_MC(){
      $("#new_poll_question_new").html("<%= escape_javascript(render :partial => 'new_MC') %>");
      $("#poll_questions_title_new").focus();
}

  function insert_MC(quest_type,quest_num,quest_id){
      $("#insert_new_poll_question_"+quest_type+"_"+quest_id).html(
                      '<%= form_for PollQuestion.new,:url =>create_poll_question_poll_path(@poll.id),:remote => true do |f|%>'+
                      ' <div class="ur_editor radio"> '+
                      '<div class="ur_editor_title"> '+
                      '<label>问题：&nbsp;&nbsp;</label>'+
                      '<input type="hidden" name="quest_id" value="'+quest_id+'"/>'+
                      '<input type="hidden" name="quest_num" value="'+quest_num+'"/>'+
                      '<input type="hidden" name="question_type" value="1"/>'+
                      '<input  maxlength="250" class="ur_question_title" type="text" name="poll_questions_title" id="poll_questions_title" placeholder="请输入单选题标题"/>'+
                      '<input type="checkbox" name="is_necessary" value="true" checked/>'+
                      '<label>必答</label>'+
                      '</div>'+
                      '<div class="ur_editor_content">'+
                      '<ul>'+
                      '<li class="ur_item">'+
                      '<label>选项<span class="ur_index"></span>：&nbsp;&nbsp;&nbsp;</label>'+
                      '<input maxlength="200" type="text" name="question_answer[0]" placeholder="新建选项"/>'+
                      '<a class="icon_add" title="向下插入选项" onclick="add_single_answer($(this));"></a>'+
                      '<a class="icon_remove"  title="删除" onclick="remove_single_answer($(this))"></a>'+
                      '</li>'+
                      '<div class="cl"></div>'+
                      '<li class="ur_item">'+
                      '<label>选项<span class="ur_index"></span>：&nbsp;&nbsp;&nbsp;</label>'+
                      '<input maxlength="200" type="text" name="question_answer[1]" placeholder="新建选项"/>'+
                      '<a class="icon_add" title="向下插入选项" onclick="add_single_answer($(this));"></a>'+
                      '<a class="icon_remove"  title="删除" onclick="remove_single_answer($(this))"></a>'+
                      '</li>'+
                      '<div class="cl"></div>'+
                      '<li class="ur_item">'+
                      '<label>选项<span class="ur_index"></span>：&nbsp;&nbsp;&nbsp;</label>'+
                      '<input maxlength="200" type="text" name="question_answer[2]" placeholder="新建选项"/>'+
                      '<a class="icon_add" title="向下插入选项" onclick="add_single_answer($(this));"></a>'+
                      '<a class="icon_remove" title="删除" onclick="remove_single_answer($(this))"></a>'+
                      '</li>'+
                      '<div class="cl"></div>'+
                      '</ul>'+
                      '</div>'+
                      '<div class="ur_editor_footer">'+
                      '<a class="btn btn_dark btn_submit c_white" data-button="ok" id="add_new_question">'+
                      '<%= l(:label_button_ok)%>'+
                      '</a>'+
                      '<a class="btn btn_light btn_cancel" data-button="cancel" onclick="$(this).parent().parent().parent().remove();">'+
                      '<%= l(:button_cancel)%>'+
                      '</a>'+
                      '</div>'+
                      '<div class="cl"></div>'+
                      '</div>'+
                      '<% end%>'
      );
      $("#poll_questions_title").focus();
      $("#add_new_question").one('click', function(){
          add_poll_question($(this));
      });
}

function add_MCQ(){
      $("#new_poll_question_new").html("<%= escape_javascript(render :partial => 'new_MCQ') %>");
      $("#poll_questions_title_new").focus();
}

function insert_MCQ(quest_type,quest_num,quest_id){
    $("#insert_new_poll_question_"+quest_type+"_"+quest_id).html(
    '<%= form_for PollQuestion.new,:url =>create_poll_question_poll_path(@poll.id),:remote => true do |f|%>'+
    '<div class="ur_editor checkbox">'+
                '<div class="ur_editor_title">'+
                '<label>问题：&nbsp;&nbsp;</label>'+
        '<input type="hidden" name="quest_id" value="'+quest_id+'"/>'+
        '<input type="hidden" name="quest_num" value="'+quest_num+'"/>'+
        '<input type="hidden" name="question_type" value="2"/>'+
                '<input maxlength="250" class="ur_question_title" type="text" name="poll_questions_title" id="poll_questions_title" placeholder="请输入多选题标题"/>'+
                '<input type="checkbox" name="is_necessary" value="true" checked/>'+
        '<label>必答</label>'+
        '</div>'+
        '<div class="ur_editor_content">'+
                '<ul>'+
                '<li class="ur_item">'+
                '<label>选项<span class="ur_index"></span>：&nbsp;&nbsp;&nbsp;</label>'+
        '<input maxlength="200" type="text" name="question_answer[0]" placeholder="新建选项"/>'+
                '<a class="icon_add" title="向下插入选项" onclick="add_single_answer($(this));"></a>'+
        '<a class="icon_remove"  title="删除"" onclick="remove_single_answer($(this))"></a>'+
        '</li>'+
        '<div class="cl"></div>'+
        '<li class="ur_item">'+
                '<label>选项<span class="ur_index"></span>：&nbsp;&nbsp;&nbsp;</label>'+
        '<input maxlength="200" type="text" name="question_answer[1]" placeholder="新建选项"/>'+
                '<a class="icon_add" title="向下插入选项" onclick="add_single_answer($(this));"></a>'+
        '<a class="icon_remove"  title="删除" onclick="remove_single_answer($(this))"></a>'+
        '</li>'+
        '<div class="cl"></div>'+
        '<li class="ur_item">'+
                '<label>选项<span class="ur_index"></span>：&nbsp;&nbsp;&nbsp;</label>'+
        '<input maxlength="200" type="text" name="question_answer[2]" placeholder="新建选项"/>'+
                '<a class="icon_add" title="向下插入选项" onclick="add_single_answer($(this));"></a>'+
        '<a class="icon_remove"  title="删除" onclick="remove_single_answer($(this))"></a>'+
        '</li>'+
        '<div class="cl"></div>'+
        '</ul>'+
        '</div>'+
        '<div class="ur_editor_footer">'+
                '<a class="btn btn_dark btn_submit c_white" data-button="ok" id="add_new_question">'+
                '<%= l(:label_button_ok)%>'+
                '</a>'+
        '<a class="btn btn_light btn_cancel" data-button="cancel" onclick="$(this).parent().parent().parent().remove();">'+
                '<%= l(:button_cancel)%>'+
                '</a>'+
        '</div>'+
        '<div class="cl"></div>'+
        '</div>'+
        '<% end%>'
    );
    $("#poll_questions_title").focus();
    $("#add_new_question").one('click', function(){
        add_poll_question($(this));
    });
  }

  function add_single(){
      $("#new_poll_question_new").html("<%= escape_javascript(render :partial => 'new_single') %>");
      $("#poll_questions_title_new").focus();
  }

  function insert_SINGLE(quest_type,quest_num,quest_id){
      $("#insert_new_poll_question_"+quest_type+"_"+quest_id).html(
      '<%= form_for PollQuestion.new,:url =>create_poll_question_poll_path(@poll.id),:remote => true do |f|%>'+
      '<div class="ur_editor text ">'+
              '<div class="ur_editor_title">'+
              '<label for="ur_question_title">问题：&nbsp;&nbsp;</label>'+
              '<input type="hidden" name="quest_id" value="'+quest_id+'"/>'+
              '<input type="hidden" name="quest_num" value="'+quest_num+'"/>'+
      '<input type="hidden" name="question_type" value="3"/>'+
              '<input maxlength="250" id="poll_questions_title" class="ur_question_title" contenteditable="true" type="text" name="poll_questions_title"  placeholder="请输入单行主观标题"/>'+
              '<input type="checkbox" name="is_necessary" value="true" checked/>'+
      '<label for="ur_question_require">必答</label>'+
      '</div>'+
      '<div class="ur_editor_footer">'+
              '<a class="btn_submit c_white" data-button="ok" id="add_new_question">'+
              '<%= l(:label_button_ok)%>'+
              '</a>'+
      '<a class="btn_cancel" data-button="cancel" onclick="$(this).parent().parent().parent().remove();">'+
              '<%= l(:button_cancel)%>'+
              '</a>'+
      '</div>'+
      '<div class="cl"></div>'+
      '</div>'+
      '<% end%>'
      );
      $("#poll_questions_title").focus();
      $("#add_new_question").one('click', function(){
          add_poll_question($(this));
      });
  }
  function add_mulit(){
      $("#new_poll_question_new").html("<%= escape_javascript(render :partial => 'new_mulit') %>");
      $("#poll_questions_title_new").focus();
  }

  function insert_MULIT(quest_type,quest_num,quest_id){
      $("#insert_new_poll_question_"+quest_type+"_"+quest_id).html(
      '<%= form_for PollQuestion.new,:url =>create_poll_question_poll_path(@poll.id),:remote => true do |f|%>'+
      '<div class="ur_editor textarea">'+
              '<div class="ur_editor_title">'+
              '<label for="ur_question_title">问题：&nbsp;&nbsp;</label>'+
              '<input type="hidden" name="quest_id" value="'+quest_id+'"/>'+
              '<input type="hidden" name="quest_num" value="'+quest_num+'"/>'+
              '<input type="hidden" name="question_type" value="4"/>'+
              '<input maxlength="250" id="poll_questions_title" class="ur_question_title" contenteditable="true" type="text" name="poll_questions_title"  placeholder="请输入多行主观标题"/>'+
              '<input type="checkbox" name="is_necessary" value="true" checked/>'+
      '<label>必答</label>'+
      '</div>'+
      '<div class="ur_editor_toolbar">'+
      '</div>'+
      '<div class="ur_editor_footer">'+
              '<a class="btn_submit c_white" data-button="ok" id="add_new_question">'+
              '<%= l(:label_button_ok)%>'+
              '</a>'+
      '<a class="btn_cancel" data-button="cancel" onclick="$(this).parent().parent().parent().remove();">'+
              '<%= l(:button_cancel)%>'+
              '</a>'+
      '</div>'+
      '<div class="cl"></div>'+
      '</div>'+
      '<% end%>'
      );
      $("#poll_questions_title").focus();
      $("#add_new_question").one('click', function(){
          add_poll_question($(this));
      });
  }

  //选择导入调查问卷
  function importPoll(){
      importPollPopWindow = $("#import_poll");
      $("#import_poll").css("position", "absolute");

      $("#import_poll").css("top", $("#import_btn").offset().top+30);

      $("#import_poll").css("left", $("#import_btn").offset().left-65);
      $("#import_poll").css("display","block")
  }

    function remote_import(){
        importPollPopWindow.css('display', 'none');
        if($("#import_poll").val() === 0){
            return;
        }else{
            if(confirm("确认导入问卷"+$("#import_poll").find("option:selected").text()+"?")){
                $("#import_form").submit();
            }else{
                return;
            }
        }

    }

  //添加标题时确定按钮
  function add_poll_question(doc)
  {
      var title = $.trim($("#poll_questions_title").val());
      if(title.length == 0){
          alert("标题不能为空");
          doc.one('click', function(){
              add_poll_question($(this));
          });
      }else{
          doc.parent().parent().parent().submit();
      }
  }

    function add_poll_question_new(doc)
    {
        var title = $.trim($("#poll_questions_title_new").val());
        if(title.length == 0){
            alert("标题不能为空");
            doc.one('click', function(){
                add_poll_question($(this));
            });
        }else{
            doc.parent().parent().parent().submit();
        }
    }
  //修改标题时确定按钮
  function edit_poll_question(doc,id)
  {
      var title = $.trim($("#poll_questions_title_" + id).val());
      if(title.length == 0){alert("标题不能为空");}else{doc.parent().parent().parent().submit();}
  }

  //问卷头
  function pollsCancel(){$("#polls_head_edit").hide();$("#polls_head_show").show();}
  function pollsSubmit(doc){
      var title = $.trim($("#polls_title").val());
      if(title.length == 0){alert("问卷标题不能为空");}else{doc.parent().parent().parent().submit();}
  }
  function pollsEdit(){$("#polls_head_edit").show();$("#polls_head_show").hide();}
  //
  function pollQuestionCancel(question_id){
      $("#show_poll_questions_"+question_id).show();
      $("#edit_poll_questions_"+question_id).hide();
  }
  function pollQuestionEdit(question_id){
      $("#show_poll_questions_"+question_id).hide();
      $("#edit_poll_questions_"+question_id).show();
      $("#poll_questions_title_"+question_id).focus();
  }
  //单选题
  function add_single_answer(doc)
  {
      doc.parent().after("<li class='ur_item'><label>选项<span class='ur_index'></span>：&nbsp;&nbsp;&nbsp;</label><input maxlength='200' type='text' name='question_answer["+new Date().getTime()+"]' placeholder='新建选项'/>" +
          "<a class='icon_add' title='向下插入选项' onclick='add_single_answer($(this));'></a><a class='icon_remove'  title='删除' onclick='remove_single_answer($(this))'></a>"+
          "</li><div class='cl'></div>");
  }
  function remove_single_answer(doc)
  {
      if(doc.parent().siblings("li").length == 0)
      {
          alert("选择题至少有一个选项");
      }
      else
      {
          doc.parent().remove();
      }
  }

  function poll_submit()
  {
      var title = $.trim($("#polls_name_h").html());
      if(title.length == 0)
      {
          alert("问卷标题不能为空");
      }
      else{
          if($("#show_result").is(":checked")) {
              $('#ajax-modal').html('<%= escape_javascript(render :partial => 'poll_submit', locals: { :poll => @poll,:is_remote => false,:show_result=> 1}) %>');
          } else{
              $('#ajax-modal').html('<%= escape_javascript(render :partial => 'poll_submit', locals: { :poll => @poll,:is_remote => false,:show_result=> 0}) %>');
          }
          showModal('ajax-modal', '310px');
          $('#ajax-modal').css('height','120px');
          $('#ajax-modal').siblings().remove();
          $('#ajax-modal').before("<span style='float: right;cursor:pointer;'>" +
                  "<a href='javascript:' onclick='clickCanel();'><img src='/images/bid/close.png' width='26px' height='26px' /></a></span>");
          $('#ajax-modal').parent().removeClass("alert_praise");
          $('#ajax-modal').parent().css("top","").css("left","").css("border","3px solid #269ac9");
          $('#ajax-modal').parent().addClass("popbox_polls");
      }
  }
</script>
<div class=" polls_content polls_edit" id="polls">

  <!-- 头部 -->
  <div id="polls_head_show"  style="display: none;">
    <%= render :partial => 'show_head', :locals => {:poll => @poll}%>
  </div>
  <div id="polls_head_edit">
    <%= render :partial => 'edit_head', :locals => {:poll => @poll}%>
  </div>

  <!-- 问题 -->
  <div id="poll_content">
    <%= render :partial => 'poll_content', :locals => {:poll => @poll}%>
  </div>

  <div class="tabs">
    <ul class="tabs_list">
      <li class="tab_item02 "  >
        <a title="<%=  l(:label_MC) %>" class="tab_icon icon_radio" onclick="add_MC();">
          <%=  l(:label_MC) %>
        </a>
      </li>
      <li class="tab_item02 " >
        <a title="<%=  l(:label_MCQ) %>" class=" tab_icon icon_checkbox" onclick="add_MCQ();">
          <%=  l(:label_MCQ) %>
        </a>
      </li>
      <li class="tab_item02 " >
        <a title="<%= l(:label_single) %>" class="tab_icon icon_text" onclick="add_single();">
          <%= l(:label_single) %>
        </a>
      </li>
      <li class="tab_item02 " >
        <a title="<%= l(:label_mulit)%>" class="tab_icon icon_textarea" onclick="add_mulit();">
          <%= l(:label_mulit)%>
        </a>
      </li>
    </ul>
    <div class="cl"></div>
  </div><!--选项 end-->

  <!-- 新增问题 -->
  <div id="new_poll_question_new">
  </div>

  <div class="ur_buttons">
    <a class="ur_button_submit" onclick="poll_submit();">
      <%= l(:label_memo_create)%>
    </a>
    <div class="polls_cha">
      <input id="show_result" type="checkbox" checked name="show_result" value="1" >
      <label for="">允许学生查看调查结果</label>
    </div>
  </div>
  <div class="cl"></div>
  <!-- 新增问题 -->
  <div id="import_poll" style="display: none">
    <%= form_tag({:controller => 'poll', :action => 'import_poll', :to_id => @poll.id},:remote=>'true', :method => :get,:id=>"import_form") do %>
        <%= select( :poll, :id, Poll.where("polls_group_id = #{@poll.polls_group_id} and polls_type='course' and id != #{@poll.id}").map{|c| [c.polls_name, c.id]}.unshift(["选择要导入的问卷",0]),
                    { :include_blank => false,:selected=> 0,:class=>"w90"
                    },
                    {:onchange=>"remote_import();",:id=>"import_id",:name=>"import_id"}
            )
        %>
    <% end%>
    </select>
  </div>
</div><!--编辑end-->
