var form;
$(document).ready(function(){
  form = layui.form;
});

//增加题目选项
function optionAdded(){
  var lastcodeindex = parseInt($("#lastcodeindex").val());
  var lastcodeletter = optioncodearr[lastcodeindex];
  if(lastcodeindex+1>=questmaxoption){
    layer.msg("题目选项最多为"+ questmaxoption +"个"); return false;
  }
  var nextcodeletter = optioncodearr[lastcodeindex+1];
  $.ajax({
    url: '/glquesti/basic1/optionadd', 
    type: 'POST',
    async: false,
    dataType: 'html',
    data: {lastcodeindex:lastcodeindex, rnum:Math.random()},
    success: function(data){
      $("#addordelbuttonbox").before(data);
      $("#lastcodeindex").val(lastcodeindex+1);
      $("#answerbox_big").append('<input type="radio" name="answercode" value="'+ nextcodeletter +'" title="'+ nextcodeletter +'" id="answerbox_'+ nextcodeletter +'">');
    },
    error: function(){
      layer.msg("系统错误，请联系管理员");
    }
  });
  form.render('radio'); //重新渲染复选框
}

//减少题目选项
function optionDelete(){
  var lastcodeindex = parseInt($("#lastcodeindex").val());
  var lastcodeletter = optioncodearr[lastcodeindex];
  if(lastcodeindex<=0){
    layer.msg("选项数量不能为0"); return false;
  }
  UE.getEditor('editor_'+ lastcodeletter).destroy();
  $("#optionbox_"+ lastcodeletter).remove();
  $("#answerbox_"+ lastcodeletter).next().remove();
  $("#answerbox_"+ lastcodeletter).remove();
  $("#lastcodeindex").val(lastcodeindex-1);
  form.render('radio'); //重新渲染复选框
}

$("#finished_button").click(function(){
  var treeid = $("#detail_form").find("selected[name='treeid']").val();
  var courseid = $("#detail_form").find("input[name='courseid']").val();
  var listview = $("#detail_form").find("input[name='listview']").val();
  if(treeid==0 || treeid==""){
    layer.msg("请选择所属知识点", {icon:2}); return false;
  }
  var levels = $("#detail_form").find("input[name='levels']:checked").length;
  if(levels==0){
    layer.msg("请选择难度系数", {icon:2}); return false;
  }
  var hascontent = UE.getEditor('editor_cont').hasContents();
  if(hascontent==false){
    layer.msg("请填写题目内容", {icon:2}); return false;
  }
  var optioncodes = $("#detail_form").find("input[name='optioncode[]']");
  var isoptionerror = false;
  if($(optioncodes).length==0){
    isoptionerror = true;
  }
  $(optioncodes).each(function(){
    hascontent = UE.getEditor('editor_'+ $(this).val()).hasContents();
    if(hascontent==false){
      isoptionerror = true;
    }
  });
  if(isoptionerror==true){
    layer.msg("请填写选项内容", {icon:2}); return false;
  }
  var answercodes = $("#detail_form").find("input[name='answercode']:checked").length;
  if(answercodes==0){
    layer.msg("请选择正确答案", {icon:2}); return false;
  }
  if(listview=="tree"){
	$("#goback_form").attr("action", "/glquesti/tree/"+ courseid);
  }else{
	$("#goback_form").attr("action", "/glquesti/list/"+ courseid);    
  }
  $("#detail_form").ajaxSubmit({
    url: '/glquesti/basic1/basicok', 
    type: 'POST',
    dataType: 'json',
    beforeSend: function(){
      layer.load(0, {shade: [0.2, '#393D49']});
    },
    success: function(data){
      if(data.code=="success"){
        layer.msg("成功", {icon:1}, function(){$("#goback_form").submit();});
      }else{
        layer.msg(data.msg, {icon:2});
      }
    },
    error: function(){
      layer.msg("系统错误，请联系管理员", {icon:2});
    },
    complete: function(){
      layer.closeAll('loading');
    }
  });
  return false;
});