var firstchoose = 1;

$(document).ready(function(){ 
  var form = layui.form;
  var laydate = layui.laydate;
  laydate.render({
    elem: "input[name='begintime']",
    type: 'datetime'
  });
  laydate.render({
    elem: "input[name='endtime']",
    type: 'datetime'
  });
});

//弹出层索引
var index11, index12;

//弹出课程列表
function showCourseList(){
  layer.load(0, {shade: [0.2, '#393D49']});
  index11 = layer.open({
    id: 'onedialog',
    type: 2,
    title: '选择课程',
    area: ['900px', '570px'],
    fixed: false, //不固定
    content: '/glexam/mycourse/index'
  });
  layer.closeAll('loading');
}

//关闭课程列表
function closeCourseList(){
  layer.close(index11);
}

//弹出班级列表
function showXzclassList(){
  layer.load(0, {shade: [0.2, '#393D49']});
  index12 = layer.open({
    id: 'onedialog',
    type: 2,
    title: '选择班级',
    area: ['900px', '570px'],
    fixed: false, //不固定
    content: '/glexam/xzclass/index'
  });
  layer.closeAll('loading');
}

//关闭班级列表
function closeXzclassList(){
  layer.close(index12);
}

//清空所选班级
function clearForclassnames(){
  $("#forclassnamebox").empty();
  $("#detail_form").find("input[name='forclass']").val("");
}

//提交表单
$("#finished_button").on("click", function(){
  var title = $("#detail_form").find("input[name='title']");
  var courseid = $("#detail_form").find("input[name='courseid']");
  var forclass = $("#detail_form").find("input[name='forclass']");
  var begintime = $("#detail_form").find("input[name='begintime']");
  var endtime = $("#detail_form").find("input[name='endtime']");
  var points = $("#detail_form").find("input[name='points']");
  var descrip = $("#detail_form").find("textarea[name='descrip']");
  var examid = $("#detail_form").find("input[name='examid']");
  if($(title).val()==""){
    layer.tips('请填写考试标题', $(title), {tips: [1, '#FF5722']});
    return false;
  }
  if($(courseid).val()=="" || $(courseid).val()=="0"){
    layer.msg("请选择所属课程", {icon:2});
    return false;
  }
  if($(forclass).val()=="" || $(forclass).val()=="0"){
    layer.msg("请选择适用班级", {icon:2});
    return false;
  }
  if($(begintime).val()==""){
    layer.tips('请选择考试时间', $(begintime), {tips: [1, '#FF5722']});
    return false;
  }
  if($(endtime).val()==""){
    layer.tips('请选择考试时间', $(endtime), {tips: [1, '#FF5722']});
    return false;
  }
  if($(points).val()=="" || isNaN($(points).val()) || $(points).val()<0){
    layer.tips('总分必须为大于0的数字', $(points), {tips: [1, '#FF5722']});
    return false;
  }
  $("#detail_form").ajaxSubmit({
    url: '/glexam/basic/basicok', 
    type: 'POST',
    dataType: "json", 
    success: function(data){
      if(data.code=="success"){
        layer.msg("成功", {icon:1});
        if($(examid).val()=="" || $(examid).val()=="0"){
          setTimeout(function(){window.location.href = "/glexam/question/"+ data.examid;},2000);
        }
      }else{
        layer.msg(data.msg, {icon:2});
      }
    },
    error: function(){
      layer.msg("系统错误，请联系管理员", {icon:2});
    }
  });
  return false;
});