$(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');
  });
  //筛选
  form.on('select(changecondition)', function(data){
    $("#search_form").submit();
  });

  //分页
  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: '/glketang/xzclass/index', 
    		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;
    }
  });

  //确定
  $("#submit_button").on("click", function(){
    var checkednum = $("#list_form").find("input[name='xzclassid[]']:checked").length;
    if(checkednum==0){
      layer.msg("您还没有选择", {icon: 2});
      return false;
    }
    var forclassidstr = ",";
    var forclassnames = "";
    var od_forclassidstr = parent.$("#detail_form").find("input[name='forclass']").val();
    var od_forclassnames = parent.$("#forclassnamebox").html();
    if(parent.firstchoose==1){
      $("#list_form").find("input[name='xzclassid[]']:checked").each(function(index, el) {
        forclassidstr += $(this).val() +",";
        forclassnames += '<span class="layui-badge layui-bg-blue" style="height:36px;line-height:36px;">'+ $(this).parent().next().html() +"</span>&nbsp;&nbsp;";
      });
      parent.$("#detail_form").find("input[name='forclass']").val(forclassidstr);
      parent.$("#forclassnamebox").html(forclassnames);
      parent.firstchoose ++;
    }else{
      $("#list_form").find("input[name='xzclassid[]']:checked").each(function(index, el) {
        if(od_forclassidstr.indexOf(","+ $(this).val() +",")<0){
          forclassidstr += $(this).val() +",";
          forclassnames += '<span class="layui-badge layui-bg-blue" style="height:36px;line-height:36px;">'+ $(this).parent().next().html() +"</span>&nbsp;&nbsp;";
        }
      });
      forclassidstr = od_forclassidstr + forclassidstr;
      forclassidstr = forclassidstr.replace(",,", ",");
      parent.$("#detail_form").find("input[name='forclass']").val(forclassidstr);
      parent.$("#forclassnamebox").append(forclassnames);
    }
    parent.closeXzclassList();
  });

});