
layui.use(['table', 'jquery'], function () {
  var table = layui.table;
  var form = layui.form,
    layer = parent.layer === undefined ? layui.layer : parent.layer,
    $ = layui.jquery;
  //监听表格复选框选择
  table.on('checkbox(demo)', function (obj) {
    console.log(obj)
  });
  //监听工具条
  table.on('tool(demo)', function (obj) {
    var data = obj.data;
    if (obj.event === 'delete') {

    }
  });
  var $ = layui.$, active = {

    //新增
    rowAdd: function () {
      var parentId = $("#selectTypeId").val();
      
      helper.cookies.set('parentId', parentId);
      if (parentId >= 0) {
        layer.open({
          type: 2,
          anim: 4,
          scrollbar: false,
          content: '/Class/Add',
          area: ['600px', '400px'],
          id: 'BussinessTypeEdit',
          title: $("#selectTypeName").val()
          , end: function () {

            refushTreeAndData();

          }
        });
      }
      else {
        layui.layer.alert("请先选择树节点!", {
          icon: 2
        });
      }
    },
    //修改
    rowEdit: function () {
      var parentId = $("#selectTypeId").val();
      
      helper.cookies.set('parentId', parentId);
      if (parentId >= 0) {
        layer.open({
          type: 2,
          anim: 4,
          scrollbar: false,
          content: '/Class/Edit',
          area: ['600px', '400px'],
          id: 'BussinessTypeEdit',
          title: $("#selectTypeName").val()
          , end: function () {

            refushTreeAndData();

          }
        });
      }
      else {
        layui.layer.alert("请先选择树节点!", {
          icon: 2
        });
      }
    },
    //删除
    rowDelete: function () {
      layer.confirm('确认要删除该行数据么？', function (index) {
        $.ajax({
          url: "/Class/deletedata",
          type: "get",
          async: false,//同步请求
          contentType: "application/json",
          dataType: "text",
          cache: false,
          data: "id=" + $("#selectTypeId").val(),
          success: function (thisResult, result) {
            if (eval('(' + thisResult + ')').msg == "删除成功") {
              helper.cookies.set('parentId', "");
              layer.open({
                content: '删除成功'
                , icon: 1
                , btn: ['确定']
                , yes: function (index, layero) {
                  layer.close(index);
                  refushTreeAndData();
                }
              });
            } else {
              layui.layer.alert("删除失败!", {
                icon: 2
              });

            }

          },
          error: function (error) {
            return false;
          }
        });
        layer.close(index);
      });
    }
  };

  $('.layui-btn-group .layui-btn').on('click', function () {
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });

  //单击左侧数列表时
  function zTreeOnClick (event, treeId, treeNode) {
    $("#selectTypeId").val(treeNode.id);
    $("#selectTypeName").val(treeNode.name);
    selectNode = treeNode;
  };
  var zTreeObj;
  var selectNode;
  // zTree 的参数配置，深入使用请参考 API 文档（setting 配置详解）
  var setting = {
    check:
    {
      enable: false,
      chkStyle: "checkbox",
      chkboxType: { "Y": "ps", "N": "ps" }
    },
    callback: {
      onClick: zTreeOnClick

    }

  };
  var treeNodes;
  $(document).ready(function () {

    $.ajax({
      url: "/Class/GetTreeJsonModel",
      type: "get",
      contentType: "application/json",
      dataType: "text",
      cache: false,
      async: false,
      data: { "parentId": 0 },
      success: function (result, status) {
        treeNodes = eval('(' + result + ')');
      }
    });
    zTreeObj = $.fn.zTree.init($("#ztree"), setting, treeNodes);

  });


  function refushTreeAndData () {
    //执行重载
    table.reload('recordtable', {
      page: {
        curr: 1 //重新从第 1 页开始
      }
      , where: {
        NowDateTime: new Date(),
        parentId: $("#selectTypeId").val()

      }
    });
    $.ajax({
      url: "/Class/GetTreeJsonModel",
      type: "get",
      contentType: "application/json",
      dataType: "text",
      cache: false,
      async: false,
      data: { "parentId": 0 },
      success: function (result, status) {
        treeNodes = eval('(' + result + ')');
      }
    });
    zTreeObj = $.fn.zTree.init($("#ztree"), setting, treeNodes);
    zTreeObj.selectNode(selectNode, true, true);
    
  }
});