<script type="text/html" id="status">
  <input type="checkbox" name="status" {{#  if(d.status==1){ }}checked{{#  } }} lay-skin="switch" lay-text="正常|禁用" lay-filter="status" value="{{d.id}}">
  <div class="layui-unselect layui-form-switch" lay-skin="_switch">
</script>
<script type="text/html" id="status_switch">
  <input type="checkbox" name="status" {{#  if(d.status==1){ }}checked{{#  } }} lay-skin="switch" lay-text="正常|禁用" lay-filter="status" value="{{d.id}}">
  <div class="layui-unselect layui-form-switch" lay-skin="_switch">
</script>
<script type="text/html" id="rfile">
  <div>
    {{d.rfile}}
  </div>
</script>
<script>
  <?
  if (ACT == "top") {
    $url = u(MOD, "list&top=1");
  } else if (ACT == "topuser") {
    $url = u(MOD, "list&topuser=1");
  } else {
    $url = u(MOD, ACT);
  }
  ?>
  layui.use(['form', 'table', 'miniPage', 'element', 'miniAdmin', 'laydate', "common"], function() {
    var $ = layui.jquery,
      form = layui.form,
      common = layui.common,
      table = layui.table,
      laydate = layui.laydate,
      miniPage = layui.miniPage;
    miniAdmin = layui.miniAdmin;
    table.render({
      elem: '#currentTableId',
      toolbar: '#toolbarDemo',
      method: 'post',
      defaultToolbar: [],
      url: '<?= $url ?>',
      cols: cols,
      where: {
      },
      limits: [10, 20, 50, 100, 200, 500],
      limit: 10,
      page: true,
      skin: 'line'
    });
    form.render();
    table.on('edit(currentTableFilter)', function(obj) { //注：edit是固定事件名，test是table原始容器的属性 lay-filter="对应的值"
      if (isNaN(obj.value) && obj.field == "sort") {
        miniAdmin.error("输入值须为数字");
        return;
      }
      layer.load(2);
      $.post("ajax.php?act=changefield", {
        id: obj.data.id,
        value: obj.value,
        table: "<?= MOD ?>",
        field: obj.field
      }, function(data) {
        layer.closeAll('loading');
        //table.reload("currentTableId")
      }, "json");
    });
    laydate.render({
      elem: '#sdate',
      trigger: 'click'
    });
    laydate.render({
      elem: '#edate',
      trigger: 'click'
    });
    // 监听搜索操作
    form.on('submit(data-search-btn)', function(data) {
      var result = JSON.stringify(data.field);
      //执行搜索重载
      table.reload('currentTableId', {
        page: {
          curr: 1
        },
        where: data.field
      }, 'data');
      return false;
    });
    form.on('switch(status)', function(data) {
      var id = data.value;
      var status = this.checked ? '1' : '2';
      $.post("ajax.php?act=changefield", {
        id: id,
        table: "<?= MOD ?>",
        field: "status",
        value: status
      }, function(data) {
        layer.closeAll('loading');
        if (data.code == 0) {

        } else {
          miniAdmin.error(data.msg);
        }
      }, "json");

    });
    form.on('switch(tj)', function(data) {
      var id = data.value;
      var status = this.checked ? '2' : '1';
      $.post("ajax.php?act=changefield", {
        id: id,
        table: "<?= MOD ?>",
        field: "tj",
        value: status
      }, function(data) {
        layer.closeAll('loading');
        if (data.code == 0) {
          layer.close(index);
        } else {
          miniAdmin.error(data.msg);
        }
      }, "json");

    });
    /**
     * toolbar事件监听
     */
    table.on('toolbar(currentTableFilter)', function(obj) {


      var checkStatus = table.checkStatus('currentTableId')
      data = checkStatus.data;
      if (data.length == 0 && obj.event != "add") {
        layer.alert("请先选择");
        return;
      }
      var ids = [];
      for (var i = 0; i < data.length; i++) {
        ids.push(data[i].id)
      }
      var ids_str = (ids.toString());


      if (obj.event === 'add') { // 监听添加操作

        addedi("添加<?= $name ?>");
      }
      if (obj.event === 'hpywy') { // 监听添加操作

        for (var i = 0; i < data.length; i++) {
          if (data[i].type == 3) {
            miniAdmin.error(data[i].ddusername + "角色是班主任你不能分配");
            return;
          }
        }
        addedi("批量更改班主任", 0, "index.php?mod=user&act=hpywy&ids=" + ids_str);
      } else if (obj.event === 'tie_check2') { // 监听删除操作
        layer.load(2);
        $.post("index.php?class=tie&act=updatestatus", {
          ids: ids_str,
          status: 2
        }, function(data) {
          layer.closeAll('loading');
          if (data.code == 0) {
            table.reload("currentTableId")
          } else {

            miniAdmin.error(data.msg);
          }

        }, "json");


      } else if (obj.event === 'tie_check3') { // 监听删除操作
        layer.load(2);
        $.post("index.php?class=tie&act=updatestatus", {
          ids: ids_str,
          status: 3
        }, function(data) {
          layer.closeAll('loading');
          if (data.code == 0) {
            table.reload("currentTableId")
          } else {

            miniAdmin.error(data.msg);
          }

        }, "json");


      } else if (obj.event === 'check2') { // 监听删除操作
        layer.load(2);
        $.post("index.php?class=user&act=updatestatus", {
          ids: ids_str,
          status: 2
        }, function(data) {
          layer.closeAll('loading');
          if (data.code == 0) {
            table.reload("currentTableId")
          } else {

            miniAdmin.error(data.msg);
          }

        }, "json");


      } else if (obj.event === 'check3') { // 监听删除操作
        layer.load(2);
        $.post("index.php?class=user&act=updatestatus", {
          ids: ids_str,
          status: 3
        }, function(data) {
          layer.closeAll('loading');
          if (data.code == 0) {
            table.reload("currentTableId")
          } else {
            miniAdmin.error(data.msg);
          }

        }, "json");


      } else if (obj.event === 'delete') { // 监听删除操作



        layer.confirm('真的批量删除吗', function(index) {
          layer.load(2);
          $.post("<?= u(MOD, "del") ?>", {
            ids: ids_str
          }, function(data) {
            layer.closeAll('loading');
            if (data.code == 0) {
              layer.close(index);
              table.reload("currentTableId")
            } else {
              miniAdmin.error(data.msg);
            }

          }, "json");
        });

      }
    });

    //监听表格复选框选择
    table.on('checkbox(currentTableFilter)', function(obj) {
      console.log(obj)
    });


    // 监听搜索操作
    form.on('submit(import_xls)', function(data) {
      //  layer.load(2);
      data.field.xls = 1;

      $.post("index.php?mod=<?= MOD ?>&act=list",
        data.field,
        function(res) {
          layer.closeAll('loading');
          if (res.code == 0)
            window.location.href = res.data
          else
            miniAdmin.error(res.msg)
        }, "json");


    });

    table.on('rowDouble(currentTableFilter)', function(obj) {
      data = obj.data
      <? if (MOD == "exam") { ?>
        if (data.id > 0)
          addedi(data.title + "匹配的题目", 0, 'index.php?mod=problems&act=list1&exam_id=' + data.id)
      <? } ?>


      <? if (ACT == "topuser") { ?>
        addedi("用户数据", 0, 'index.php?mod=visit&act=list1_topuser&uid=' + data.uid)
      <? } ?>
      <? if (ACT == "top") { ?>
        addedi("用户数据", 0, 'index.php?mod=visit&act=list1_top&aid=' + data.aid)
      <? } ?>
    });

    table.on('tool(currentTableFilter)', function(obj) {
      var data = obj.data;
      if (obj.event === 'edit') {

        addedi("修改<?= $name ?>", data.id)
        return false;
      } else if (obj.event === 'zpshow') {
        addedi("应聘详情", "", "index.php?mod=zp&act=show&id=" + data.id);
      } else if (obj.event == "problems_import") {

        addedi("导入试题", data.id, "index.php?mod=problems&act=import&id=" + data.id)
      } else if (obj.event == "xlsx_import") {

        addedi("xlsx导入试题", data.id, "index.php?mod=exam&act=xlsx_import&id=" + data.id)
      } else if (obj.event == "problems_bmimport") {

        addedi("百闽导入", data.id, "index.php?mod=exam&act=bmimport&id=" + data.id)
      } else if (obj.event == "check3") {

        addedi("审核不通过", data.id, "index.php?mod=user&act=check3&id=" + data.id)
      } else if (obj.event == "tie_check3") {

        addedi("审核不通过", data.id, "index.php?mod=tie&act=check3&id=" + data.id)
      } else if (obj.event === 'delete') {
        layer.confirm('真的删除么', function(index) {
          layer.load(2);
          $.post("<?= u(MOD, "del") ?>", {
            ids: data.id
          }, function(data) {
            layer.closeAll('loading');
            if (data.code == 0) {
              layer.close(index);
              table.reload("currentTableId")
            } else {
              miniAdmin.error(data.msg);
            }

          }, "json");
        });
      }
    });




    function addedi(title, id, url) {
      var jumpurl = url ? url : 'index.php?mod=<?= MOD ?>&act=addedi&id=' + id;
      var content = miniPage.getHrefContent(jumpurl);
      var openWH = miniPage.getOpenWidthHeight();

      var index = layer.open({
        title: title,
        type: 1,
        shade: 0.2,
        maxmin: true,
        shadeClose: true,
        area: [openWH[0] + 'px', openWH[1] + 'px'],
        offset: [openWH[2] / 2 + 'px', openWH[3] / 2 + 'px'],
        content: content,

        success: function(layreo, index) {



          form.on('submit(saveBtn)', function(data) {
            data.field.sub = 1
            //console.log(data);return
            //layer.load(2);
            var posturl = "<?= u(MOD, "addedi") ?>";
            if (data.field.problemsjump == 1) {
              posturl = "<?= u("problems", "addedi") ?>";
            }
            $.post(posturl, data.field, function(res) {
              layer.closeAll('loading');


              if (res.code == 0) {
                layer.close(index);
                miniAdmin.success(res.msg)
                table.reload("currentTableId")
              } else {

                miniAdmin.error(res.msg)
              }
            }, "json");

          })
        }
      });
      $(window).on("resize", function() {
        KindEditor.ready(function(K) {
          K.remove('#content');
        });
        layer.full(index);
      });
    }

  });
</script>