
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 === 'edit') {

      layer.open({
        type: 2,
        anim: 4,
        scrollbar: false,
        content: '/RepositoryCategory/RepositoryCategoryContentEdit/?categoryContentId=' + data.F_RepositoryId + "&editType=2",
        area: ['80%', '800px'],
        id: 'DepartmentEdit',
        title: $("#selectTypeName").val()
        , end: function () {
          refushTreeAndData();
        }
      });


    }
    else if (obj.event === 'view') {

      layer.open({
        type: 2,
        anim: 4,
        scrollbar: false,
        content: '/RepositoryCategory/RepositoryCategoryContentView/?categoryContentId=' + data.F_RepositoryId,
        area: ['80%', '800px'],
        id: 'RepositoryCategoryContentView',
        title: '查看内容'

      });


    }
    else if (obj.event === 'delete') {
      layer.confirm('确认要删除该行数据么？', function (index) {
        $.ajax({
          url: "/RepositoryCategory/DeleteCategoryContentData",
          type: "get",
          async: false,//同步请求
          contentType: "application/json",
          dataType: "text",
          cache: false,
          data: "categoryContentId=" + data.F_RepositoryId,
          success: function (thisResult, result) {

            if (thisResult == "True") {
              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);
      });

      // if (data.F_CategoryId <= 4) {
      //   layui.layer.alert("不能删除该分类!", {
      //     icon: 2
      //   });
      //   return false;
      // }
      // else {
        

      // }
    }
    else if (obj.event === 'copyContent') {


      var oInput = document.createElement('input');
      oInput.value = data.F_Description;
      document.body.appendChild(oInput);
      oInput.select(); // 选择对象
      document.execCommand("Copy"); // 执行浏览器复制命令


      layer.msg('复制成功', { icon: 6, time: 2000 });

    }



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

    //新增
    rowAdd: function () {
      var parentId = $("#selectTypeId").val();
      if (parentId != null && parentId != "") {
        layer.open({
          type: 2,
          anim: 4,
          scrollbar: false,
          content: '/RepositoryCategory/RepositoryCategoryContentEdit/?categoryContentId=' + parentId + "&editType=1",
          area: ['80%', '800px'],
          id: 'DictionaryBaseEdit',
          title: $("#selectTypeName").val()
          , end: function () {

            refushTreeAndData();

          }
        });

      }
      else {
        layui.layer.alert("请先选择左侧树节点!", {
          icon: 2
        });
      }
    }
    ,
    //查询按钮
    reload: function () {


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

  $('.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.code);
    $("#selectTypeName").val(treeNode.name);
    selectNode = treeNode;
    //执行重载
    table.reload('recordtable', {
      page: {
        curr: 1 //重新从第 1 页开始
      }
      , where: {
        NowDateTime: new Date(),
        parentId: treeNode.code,
        keyWord: $("#keyWord").val()


      }
    });

  };
  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: "/repositorycategory/GetrepositorycategoryJsonModel",
      type: "get",
      contentType: "application/json",
      dataType: "text",
      cache: false,
      async: false,
      data: { "parentId": $("#dType").val() },
      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(),
        keyWord: $("#keyWord").val()

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