
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/RepositoryCategoryEdit/?categoryId=' + data.F_CategoryId + "&editType=2",
                area: ['500px', '300px'],
                id: 'DepartmentEdit',
                title: $("#selectTypeName").val()
                , end: function () {
                    refushTreeAndData();
                }
            });


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


            if (data.F_CategoryId <= 4) {
                layui.layer.alert("不能删除该分类!", {
                    icon: 2
                });
                return false;
            }
            else {
                layer.confirm('确认要删除该行数据么？', function (index) {
                    $.ajax({
                        url: "/RepositoryCategory/DeleteCategoryData",
                        type: "get",
                        async: false,//同步请求
                        contentType: "application/json",
                        dataType: "text",
                        cache: false,
                        data: "categoryId=" + data.F_CategoryId,
                        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);
                });

            }



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

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

                       refushTreeAndData();

                   }
                });

            }
            else {
                layui.layer.alert("请先选择左侧树节点!", {
                    icon: 2
                });
            }
        }

    };

    $('.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

          }
        });

    };
    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": 1},
            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: "/repositorycategory/GetrepositorycategoryJsonModel",
            type: "get",
            contentType: "application/json",
            dataType: "text",
            cache: false,
            async: false,
            data: { "parentId": 1},
            success: function (result, status) {
                treeNodes = eval('(' + result + ')');
            }
        });
        zTreeObj = $.fn.zTree.init($("#ztree"), setting, treeNodes);
        zTreeObj.selectNode(selectNode, true, true);
    }
});