$(document).ready(function(){  
  //全选
  var form = layui.form;
  form.on('checkbox(allChoose)', function(data){
    var child = $(data.elem).parents('table').find('tbody input[type="checkbox"]');
    child.each(function(index, item){
      item.checked = data.elem.checked;
    });
    form.render('checkbox');
  });

  //分页
  var laypage = layui.laypage;
  laypage.render({
    elem: 'pagelinkbox',
    count: allsize,
    limit: pagesize,
    layout: ['count', 'prev', 'page', 'next', 'skip'],
    jump: function(obj){
      $("#gotopage_form").find("input[name='curpage']").val(obj.curr);
      $("#gotopage_form").find("input[name='rnum']").val(Math.random());
      $("#gotopage_form").ajaxSubmit({
        url: '/glquesti/special/'+ specialid, 
        type: 'POST',
        dataType: 'html',
        beforeSend: function(){
          $("#waitingtipsbox").show();
        },
        success: function(data){
          if(data==""){
            //layer.msg("已经到最后一页了", {icon: 2});
          }else{
            $("#pagelistbox").html(data);
            form.render('checkbox'); //重新渲染复选框
          }
          $("#waitingtipsbox").hide();
        },
        error: function(){
          layer.msg("系统错误，请联系管理员", {icon: 2});
        }
      });
      return false;
    }
  });

  //对学生公开
  form.on('switch(changeOpenType)', function(data){
    var courseid = $(this).val();
    var isopen = 0;
    if(this.checked==true){
      isopen = 1;
    }
    $.ajax({
      url: '/glquesti/special/changeopen', 
      type: 'POST',
      dataType: "json", 
      data: {courseid:courseid,isopen:isopen},
      success: function(data){
        if(data.code=="success"){
          //layer.msg("成功", {icon:1});
        }else{
          layer.msg(data.msg, {icon:2});
        }
      },
      error: function(){
        layer.msg("系统错误，请联系管理员", {icon:2});
      }
    });
    return false;
  });

  //重复使用次数
  $("#saveusage_button").on("click", function(){
    var checkednum = $("#list_form").find("input[name='courseid[]']:checked").length;
    if(checkednum==0){
      layer.msg("您还没有选择", {icon: 2});
      return false;
    }
    $("#list_form").ajaxSubmit({
      url: '/glquesti/special/saveusage', 
      type: 'POST',
      dataType: "json", 
      beforeSend: function(){
        layer.load(0, {shade: [0.2, '#393D49']});
      },
      success: function(data){
        if(data.code=="success"){
          layer.msg("成功", {icon:1});
        }else{
          layer.msg(data.msg, {icon:2});
        }
      },
      error: function(){
        layer.msg("系统错误，请联系管理员", {icon:2});
      },
      complete: function(){
        layer.closeAll('loading');
      }
    });
    return false;
  });

  //初始化题库
  $("#initialize_button").on("click", function(){
    var checkednum = $("#list_form").find("input[name='courseid[]']:checked").length;
    if(checkednum==0){
      layer.msg("您还没有选择", {icon: 2});
      return false;
    }
    $("#list_form").ajaxSubmit({
      url: '/glquesti/myqbank/initialize', 
      type: 'POST',
      dataType: "json", 
      beforeSend: function(){
        layer.load(0, {shade: [0.2, '#393D49']});
      },
      success: function(data){
        if(data.code=="success"){
          layer.msg("成功", {icon:1});
        }else{
          layer.msg(data.msg, {icon:2});
        }
      },
      error: function(){
        layer.msg("系统错误，请联系管理员", {icon:2});
      },
      complete: function(){
        layer.closeAll('loading');
      }
    });
    return false;
  });

});

//重新统计题目数量
function calcQuestinumAgain(specialid, courseid){
  $("#progressbox").show();
  $("#calcQuestinumbox").attr("src", "/glquesti/default/calcquestinum?specialid="+ specialid +"&courseid="+ courseid);
}