$(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');
  });

  //删除
  $("#delete_button").on("click", function(){
    var checkednum = $("#list_form").find("input[name='resourid[]']:checked").length;
    if(checkednum==0){
      layer.msg("您还没有选择", {icon: 2});
      return false;
    }
    var layerbox = layer.confirm('此操作不可恢复，<br>确认要删除吗？', {
      area: ['400px', '230px'],
      icon: 0,
      btn: ['确定','取消'] //按钮
    }, function(){
      $("#list_form").ajaxSubmit({
        url: '/glquesti/resource/delete', 
        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(){window.location.reload();});
          }else{
            layer.msg(data.msg, {icon:2});
          }
        },
        error: function(){
          layer.msg("系统错误，请联系管理员", {icon:2});
        },
        complete: function(){
          layer.closeAll('loading');
        }
      });
    }, function(){
      //do nothing
    });
    return false;
  });

  //移动
  $("#moveto_button").on("click", function(){
    var checkedelem = $("#list_form").find("input[name='resourid[]']:checked");
    if($(checkedelem).length==0){
      layer.msg("您还没有选择", {icon: 2});
    }else{
      var resourceids = "";
      $(checkedelem).each(function(){
        if(resourceids==""){
          resourceids = $(this).val();
        }else{
          resourceids += ","+ $(this).val();
        }
      });
      parent.$("#moveto_form").find("input[name='resourceids']").val(resourceids);
      parent.showResourMoveBox();
    }
    return false;
  });

});

//禁用题目
function lockedQuestion(islocked){
  $("#list_form").find("input[name='islocked']").val(islocked);
  var checkednum = $("#list_form").find("input[name='resourid[]']:checked").length;
  if(checkednum==0){
    layer.msg("您还没有选择", {icon: 2});
    return false;
  }
  $("#list_form").ajaxSubmit({
    url: '/glquesti/resource/locked', 
    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(){window.location.reload();});
      }else{
        layer.msg(data.msg, {icon:2});
      }
    },
    error: function(){
      layer.msg("系统错误，请联系管理员", {icon:2});
    },
    complete: function(){
      layer.closeAll('loading');
    }
  });
  return false;
}