<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:set value="${pageContext.request.contextPath}" var="path" scope="page"/>
<html>
<head>
    <title>字典列表</title>
    <meta charset="utf-8">
    <%@include file="../../jspf/comm.jspf" %>
    <%@ include file="../../jspf/ztreeCommom.jsp" %>
    <script type="text/javascript" src="${path}/static/js/pyAndWb.js"></script>
    <style>
        .body {
            overflow-x: hidden;
            overflow-y: hidden;
            height: calc(100vh - 0px);
        !important;
        }

        .titleCss {
            text-align: center;
            vertical-align: middle;
            background-color: #2e6da4;
            color: #fff;
            font-size: 19px;
            font-weight: bold;
            height: 30px;
            margin-bottom: 10px;
        }

        .treeBtn1 {
            margin-left: 10px;
            margin-top: 10px;
            margin-right: 10px;
            width: 42%;
        }

        .treeBtn {
            margin-top: 10px;
            margin-left: 10px;
            margin-right: 10px;
            width: 42%;
        }

        /* .zTreeDemo {
             height: calc(100vh - 100px);
             overflow: hidden;
         }*/

        .divCss {
            margin-top: 10px;
        }

        .content {
            /*margin-left: 20px;*/
            border-width: 0 0 0 1px;
            border-style: solid;
            border-color: #2e6da4;
            /*background-color: #2e6da4;*/
            height: calc(100vh - 0px);
            overflow-x: hidden;
            overflow-y: auto;
            width: 75%;
            float: left;
        }

        .control-label {
            margin-right: -25px;
        }

        .trg {
            width: 0;
            height: 0;
            border-left: 3px solid transparent;
            border-right: 3px solid transparent;
            border-top: 6px solid black;;
            position: absolute;
            left: 181px;
            top: 8px;

        }

        .org-select {
            cursor: default;
            z-index: -1;
            width: 200px;
        }

        #leftCol {
            background-color: #fff;
            height: calc(100vh - 20px);
            width: 25%;
            float: left;
        !important;
        }

        #key {
            margin-left: 10px;
            margin-bottom: 5px;
            width: 70%;
        !important;
        }

        #form1 {
            margin-top: 55px;
            margin-left: 20px;
            width: 100%;
        !important;
        }

        #ztree {
            margin-top: 10px;
            margin-left: -35px;
            height: calc(100vh - 180px);
            overflow: auto;
        !important;
        }

        #searchBtn {
            width: 20%;
        !important;
        }
    </style>
</head>
<body>
<div id="leftCol">
    <div class="row titleCss">
        &nbsp;字典列表
    </div>
    <div class="form-inline ">
        <input type="text" id="key" value="" class="input-sm " placeholder="字典名称"/>
        <button type="button" class="btn btn-primary btn-sm" id="searchBtn">查询</button>
    </div>
    <div class="row" style="margin-left: 5px;">
        <button type="button" class="btn  btn-success btn-sm treeBtn1" onclick="expandAll();">全部展开</button>
        <button type="button" class="btn  btn-success btn-sm treeBtn" onclick="collapseAll();">全部收缩</button>
    </div>
    <div class="row">
        <!--树-->
        <div class="zTreeDemo">
            <ul id="ztree" class="ztree" style="margin-left:-18px;"></ul>
        </div>
    </div>
</div>
<!--内容-->
<div class="content">
    <div class="row titleCss">
        &nbsp;字典信息
    </div>
    <form id="form1">
        <div class="row" id="btnDiv">
            <button type="button" class="btn btn-info  btn-sm" id="addBtn">添加叶子</button>
            <button type="button" class="btn btn-danger btn-sm" id="delBtn">删除类别</button>
            <button type="button" class="btn btn-primary btn-sm" id="saveBtn">保存类别</button>
            <%--<button type="button" class="btn btn-warning  btn-sm" id="addTypeBtn">添加类别</button>--%>
        </div>
        <div id="allTypeDiv">
            <div class="row divCss">
                <div class="form-group">
                    <label class="col-sm-2 control-label">类别代码：</label>
                    <div class="col-sm-8"><input type="text" hidden id="id" name="id" value=""/>
                        <input type="text" class="input-sm form-control" id="typecode" name="typeFlag"/>
                    </div>
                    <div class="col-sm-1"></div>
                </div>
            </div>
            <div class="row divCss">
                <div class="form-group">
                    <label class="col-sm-2 control-label">类别名称：</label>
                    <div class="col-sm-8">
                        <input type="text" class="input-sm form-control" id="typename" name="typename"/>
                    </div>
                    <div class="col-sm-1"></div>
                </div>
            </div>
            <div id="subTypeDiv">
                <div class="row divCss">
                    <div class="form-group">
                        <label class="col-sm-2 control-label">代码：</label>
                        <div class="col-sm-8">
                            <input type="text" class="input-sm form-control" id="code" name="code"/>
                        </div>
                        <div class="col-sm-1"></div>
                    </div>
                </div>
                <div class="row divCss">
                    <div class="form-group">
                        <label class="col-sm-2 control-label">名称：</label>
                        <div class="col-sm-8">
                            <input type="text" class="input-sm form-control" id="name" name="name"/>
                        </div>
                        <div class="col-sm-1"></div>
                    </div>
                </div>
                <div class="row divCss">
                    <div class="form-group">
                        <label class="col-sm-2 control-label">拼音码：</label>
                        <div class="col-sm-4">
                            <input type="text" readonly class="input-sm form-control" id="pyCode" name="pyCode"/>
                        </div>
                        <div class="col-sm-1"></div>
                    </div>
                </div>
                <div class="row divCss">
                    <div class="form-group">
                        <label class="col-sm-2 control-label">五笔码：</label>
                        <div class="col-sm-4">
                            <input type="text" readonly class="input-sm form-control" id="wbCode" name="wbCode"/>
                        </div>
                        <div class="col-sm-1"></div>
                    </div>
                </div>
                <div class="row divCss">
                    <div class="form-group">
                        <label class="col-sm-2 control-label">邮编：</label>
                        <div class="col-sm-4">
                            <input type="text" class="input-sm form-control" id="zipCode" name="zipCode"/>
                        </div>
                        <div class="col-sm-1"></div>
                    </div>
                </div>
                <div class="row divCss">
                    <div class="form-group">
                        <label class="col-sm-2 control-label">国标码：</label>
                        <div class="col-sm-4">
                            <input type="text" class="input-sm form-control" id="gbCode" name="gbCode"/>
                        </div>
                        <div class="col-sm-1"></div>
                    </div>
                </div>

                <%--<div class="row">--%>
                <%--<div class="form-group">--%>
                <%--<label class="col-sm-2 control-label">父级：</label>--%>
                <%--<div class="col-sm-2">--%>
                <%--<select id="parentId" class="input-sm form-control">                --%>
                <%--&lt;%&ndash;<option value="1">有效</option>                                           &ndash;%&gt;--%>
                <%--&lt;%&ndash;<option value="0">无效</option>&ndash;%&gt;--%>
                <%--</select>--%>
                <%--</div>--%>
                <%--<div class="col-sm-1"></div>--%>
                <%--</div>--%>
                <%--</div>--%>
                <div class="row divCss">
                    <div class="form-group">
                        <label class="col-sm-2 control-label">报表上可见：</label>
                        <div class="col-sm-2">
                            <select id="flag" class="input-sm form-control">
                                <option value="">不可见</option>
                                <option value="1">可见</option>
                            </select>
                        </div>
                        <div class="col-sm-1"></div>
                    </div>
                </div>
            </div>
            <div class="row divCss" id="parentDiv" hidden>
                <div class="form-group">
                    <label class="col-sm-2  control-label">上级目录</label>
                    <div class="col-sm-4">
                        <input id="parentId" name="parentId" class="input-sm form-control" style="width: 265px;"/>
                    </div>
                </div>
                <%--<input id="ts_input">--%>
            </div>
            <div class="row divCss">
                <div class="form-group">
                    <label class="col-sm-2 control-label">是否有效：</label>
                    <div class="col-sm-2">
                        <select id="effective" class="input-sm form-control">                
                            <option value="1">有效</option>
                                           
                            <option value="0">无效</option>
                        </select>
                    </div>
                    <div class="col-sm-1"></div>
                </div>
            </div>
            <div class="row divCss">
                <div class="form-group">
                    <label class="col-sm-2 control-label">说明：</label>
                    <div class="col-sm-8">
                        <textarea class="input-sm form-control" rows="5" id="cComment" name="cComment"></textarea>
                    </div>
                    <div class="col-sm-1"></div>
                </div>
            </div>
            <div class="row divCss">
                <div class="form-group">
                    <label class="col-sm-2 control-label">备注：</label>
                    <div class="col-sm-8">
                        <textarea class="input-sm form-control" rows="5" id="remark" name="recallReason"></textarea>
                    </div>
                    <div class="col-sm-1"></div>
                </div>
            </div>
        </div>
    </form>
</div>

<script type="text/javascript">
    var zNodes = [];
    var nodeList = [];
    var currNode;//当前选中节点
    var addBol = 0;//判断添加类型或添加叶子节点
    var checkNodes = [];

    var opts = {
        lines: 13, // The number of lines to draw |小长条的数量
        length: 20, // The length of each line |小长条的长度
        width: 5, // The line thickness |小长条的宽度
        //left: "50%",
        radius: 15, // The radius of the inner circle |内环的半径长
        corners: 1, // Corner roundness (0..1)
        rotate: 0, // The rotation offset |旋转角度
        direction: 1, // 1: clockwise, -1: counterclockwise |1：顺时针方向 -1：逆时针方向
        color: '#000', // #rgb or #rrggbb or array of colors |颜色
        speed: 1, // Rounds per second |每秒转多少圈
        trail: 60, // Afterglow percentage |余晖效果百分比
        shadow: false, // Whether to render a shadow |渲染阴影
        hwaccel: false, // Whether to use hardware acceleration |加速
        className: 'spinner', // The CSS class to assign to the spinner |类名spinner
        zIndex: 2e9, // The z-index (defaults to 2000000000) |显示在最顶层
        top: '150px', // Top position relative to parent in px |相对父元素的top
        //left: '90' // Left position relative to parent in px |相对父元素的left ，默认情况spinner是显示于父元素居中位置

    };
    var spinner = new Spinner(opts);

    // function trans_tree(jsonData) {
    //     //result存储json数据转为树形结构后的结果。
    //     //temp为临时对象，将json数据按照id值排序.
    //     //len是json长度，用于循环遍历结束的条件
    //     var result = [], temp = {}, len = jsonData.length
    //     for (var i = 0; i < len; i++) {
    //         // 以id作为索引存储元素，可以无需遍历直接快速定位元素
    //         temp[jsonData[i]['id']] = jsonData[i]
    //     }
    //     for (var j = 0; j < len; j++) {
    //         var currentElement = jsonData[j]
    //         // 临时变量里面的当前元素的父元素，即pid的值，与找对应id值
    //         var tempCurrentElementParent = temp[currentElement['pid']]
    //         // 如果存在父元素，即如果有pid属性
    //         if (tempCurrentElementParent) {
    //             // 如果父元素没有chindren键
    //             if (!tempCurrentElementParent['children']) {
    //                 // 设上父元素的children键
    //                 tempCurrentElementParent['children'] = []
    //             }
    //             // 给父元素加上当前元素作为子元素
    //             tempCurrentElementParent['children'].push(currentElement)
    //         }
    //         // 不存在父元素，意味着当前元素是一级元素
    //         else {
    //             result.push(currentElement);
    //         }
    //     }
    //     return result;
    // }


    function initTree() {
        var target = document.getElementById('treediv');
        spinner.spin(target);//给id为foo的div 中绑上spinner
        zNodes = [];
        checkNodes = [];
        //获取所有缺陷类别列表
        $.ajax({
            url: "${path}/dictionary/dicList",
            type: "POST",
            data: {},
            async: true,
            success: function (result) {
                //console.log(result);
                zNodes.push({id: 0, pId: 0, name: "字典列表", open: true});
                checkNodes.push({id: 0, pId: 0, name: "字典列表", open: true});
                //var htmlStr="";
                if (result != null) {
                    for (var i = 0; i < result.length; i++) {

                        var obj = {};
                        obj.id = result[i].id;
                        obj.pid = result[i].parentId;
                        obj.pId = result[i].parentId;
                        obj.typecode = result[i].typecode;
                        obj.code = result[i].code;
                        if (result[i].parentId == 0 || obj.code == null) {
                            obj.name = result[i].typename.trim();
                            // htmlStr += "<option value=" + result[i].id + ">" + result[i].typename + "</option>";
                        } else {
                            obj.name = result[i].name.trim();
                            // htmlStr += "<option value=" + result[i].id + ">" + result[i].name + "</option>";
                        }
                        obj.typename = result[i].typename;
                        obj.cComment = result[i].cComment;
                        obj.effective = result[i].effective;
                        obj.flag = result[i].flag;
                        obj.gbCode = result[i].gbCode;
                        obj.pyCode = result[i].pyCode;
                        obj.wbCode = result[i].wbCode;
                        //console.log("obj");
                        //zNodes=trans_tree(zNodes);
                        zNodes.push(obj);
                        if (obj.typecode != null && obj.typecode != "") {
                            checkNodes.push(obj);
                        }

                    }
                }
                $.fn.zTree.init($("#ztree"), setting, zNodes);
                var treeObj = $.fn.zTree.init($("#ztree"), setting, zNodes);
                treeObj.expandAll(true);
                $("#addBtn").hide();
                $("#delBtn").hide();
                $("#saveBtn").hide();
                $("#parentId").treeSelect(checkNodes);
                var node;
                var currId = $("#id").val();
                if (currId != "" && currId != null) {
                    //修改选中已选节点
                    node = treeObj.getNodeByParam("id", currId);
                    // $("#addBtn").show();
                    // $("#subTypeDiv").hide();
                    // $("#allTypeDiv").show();
                    // $("#addTypeBtn").hide();

                } else {
                    //添加选中父节点
                    node = treeObj.getNodeByParam("id", 0);
                    // $("#addBtn").hide();
                    // $("#subTypeDiv").show();
                    // $("#allTypeDiv").hide();
                    // $("#addTypeBtn").show();
                }
                currNode = node;
                treeObj.selectNode(node, true);//将指定ID的节点选中
                $("#addBtn").hide();
                spinner.spin();

            }, error: function () {
                spinner.spin();
                //toastr.error("加载失败!");
            }
        });


    }

    function emptyEntity(bol) {
        if (bol == 1) {
            $("#typecode").val("");
            $("#typename").val("");
        }
        $("#id").val("");
        $("#name").val("");
        $("#code").val("");
        $("#pyCode").val("");
        $("#wbCode").val("");
        $("#zipCode").val("");
        $("#cComment").val("");
        $("#flag").val("");
        $("#parentId").val("");
        $("#effective").val(1);
        $("#remark").val("");
    }

    var setting = {
        view: {
            addHoverDom: false,
            removeHoverDom: false,
            selectedMulti: false,
            autoCancelSelected: false,
            fontCss: getFontCss
        },
        check: {
            enable: false
        },
        data: {
            simpleData: {
                enable: true,
                idKey: "id",
                pIdKey: "pId",
                rootPId: "0"
            }
        },
        edit: {
            enable: false
        },
        callback: {
            beforeClick: function (treeId, treeNode) {
                $("#delBtn").show();
                $("#saveBtn").show();
                $("#allTypeDiv").show();
                currNode = treeNode;
                //console.log(treeNode.id);
                if (treeNode.id != 0) {
                    if (treeNode.isParent || treeNode.pid == 0 || treeNode.code == null) {
                        $("#typecode").removeAttr("readOnly");
                        $("#typename").removeAttr("readOnly");
                        $("#typecode").val(treeNode.typecode);
                        $("#typename").val(treeNode.typename);
                        $("#subTypeDiv").hide();
                        //$("#addTypeBtn").show();
                        $("#addBtn").show();
                        //判断节点是否有叶子节点，有则不可改父级，无则可改动
                        var childrenNodes = treeNode.children;
                        /*if(childrenNodes!= undefined){
                            $("#parentDiv").hide();
                        }else{
                            $("#parentDiv").show();
                        }*/

                    } else {
                        $("#typecode").attr("readOnly", "true");
                        $("#typename").attr("readOnly", "true");
                        var pNode = treeNode.getParentNode();
                        $("#typecode").val(pNode.typecode);
                        $("#typename").val(pNode.typename);
                        $("#subTypeDiv").show();
                        //$("#addTypeBtn").hide();
                        $("#addBtn").hide();
                        //$("#parentDiv").show();
                    }
                    $("#id").val(treeNode.id);
                    $("#name").val(treeNode.name);
                    $("#code").val(treeNode.code);
                    $("#pyCode").val(treeNode.pyCode);
                    $("#wbCode").val(treeNode.wbCode);
                    $("#zipCode").val(treeNode.zipCode);
                    $("#cComment").val(treeNode.cComment);
                    $("#flag").val(treeNode.flag);
                    $("#parentId").val("");
                    $("#effective").val(treeNode.effective);
                    $("#remark").val(treeNode.remark);

                    $("#allTypeDiv").show();
                } else {
                    $("#subTypeDiv").removeAttr("display");
                    $("#addBtn").hide();
                    $("#delBtn").hide();
                    $("#saveBtn").hide();
                    $("#allTypeDiv").hide();
                    //$("#addTypeBtn").show();
                    //$("#parentDiv").hide();
                    emptyEntity(1);
                }
            }
        }
    };


    // function searchNode() {
    //     var zTree = $.fn.zTree.getZTreeObj("ztree");
    //     var value = $("#key").val();
    //     if (value == "") {
    //         var nodes = zTree.getNodesByParam("isHidden", true);
    //         zTree.showNodes(nodes);
    //     } else {
    //         nodeList = zTree.getNodesByParamFuzzy("name", value);
    //         var nodes = zTree.getNodes();
    //         zTree.hideNodes(nodes[0].children);
    //         updateNodes(true);
    //     }
    //     zTree.expandAll(true);
    // }
    //
    // function updateNodes(highlight) {
    //     var zTree = $.fn.zTree.getZTreeObj("ztree");
    //     for (var i = 0, l = nodeList.length; i < l; i++) {
    //         zTree.showNode(nodeList[i]);
    //     }
    // }

    function focusKey(e) {
        if (key.hasClass("empty")) {
            key.removeClass("empty");
        }
    }

    function blurKey(e) {
        if (key.get(0).value === "") {
            key.addClass("empty");
        }
    }

    var lastValue = "", nodeList = [], fontCss = {};


    function updateNodes(highlight) {
        var zTree = $.fn.zTree.getZTreeObj("ztree");
        for (var i = 0, l = nodeList.length; i < l; i++) {
            nodeList[i].highlight = highlight;
            zTree.updateNode(nodeList[i]);
        }
    }

    function getFontCss(treeId, treeNode) {
        return (!!treeNode.highlight) ? {color: "#A60000", "font-weight": "bold"} : {
            color: "#333",
            "font-weight": "normal"
        };
    }

    /**
     * 遍历树节点，将
     * 1.自身不满足搜索条件
     *     且
     * 2.其子节点不包含有满足条件的节点
     * 的节点加入到filterResult中
     *
     * @param node 查询的节点
     * @param inputStr 搜索条件
     * @param filterResult 过滤的结果集
     * @return 该节点是否满足条件
     */
    function filterNodes(node, inputStr, filterResult) {
        if (node != null) {
            //自身是否符合搜索条件
            var selfMatch = node.name.indexOf(inputStr) > -1;
            //子节点是否有满足的条件的节点
            var childMatch = false;

            var children = node.children;
            if (children != undefined) {
                for (index in children) {
                    childMatch = filterNodes(children[index], inputStr, filterResult) || childMatch;
                }
            }

            //自身不满足搜索条件 且其子节点不包含有满足条件的节点
            if (!selfMatch && !childMatch) {
                filterResult.push(node);
            }

            return selfMatch || childMatch;
        } else {
            return true;
        }
    }

    function filter(node) {
        return !node.isParent && node.isFirstNode;
    }

    function searchNode() {
        var zTree = $.fn.zTree.getZTreeObj("ztree");
        var value = $("#key").val();
        if (value == "") {
            //显示隐藏的节点
            var nodes = zTree.getNodesByParam("isHidden", true);
            zTree.showNodes(nodes);
        } else {
            //显示隐藏的节点
            nodes = zTree.getNodesByParam("isHidden", true);
            zTree.showNodes(nodes);

            var root = zTree.getNodeByParam("level", "0");

            var hiddenNodes = new Array();


            filterNodes(root, value, hiddenNodes);
            zTree.hideNodes(hiddenNodes);
        }
        zTree.expandAll(true);
    }

    // function searchNode2() {
    //     //查询叶子节点
    //     var zTree = $.fn.zTree.getZTreeObj("ztree");
    //     var value = $("#key").val();
    //     if (key.hasClass("empty")) {
    //         value = "";
    //     }
    //     if (lastValue === value) return;
    //     lastValue = value;
    //     updateNodes(false);
    //     if (value === "") return;
    //     nodeList = zTree.getNodesByParamFuzzy("name", value);
    //     updateNodes(true);
    // }

    $(document).keydown(function (event) {
        if (event.keyCode == 13) {
            searchNode();
        }
    });
    var key;
    $(function () {
        key = $("#key");
        key.bind("focus", focusKey)
            .bind("blur", blurKey)
            .bind("propertychange", searchNode)
            .bind("input", searchNode);

        var url = window.location.href;
        var index = url.substring(url.lastIndexOf('?') + 1).replace("num=", "");
        // if (index != null && index != "" && index.indexOf("addType") != -1) {
        //     $("#addTypeBtn").show();
        // } else {
        //     $("#addTypeBtn").hide();
        // }
        if (index != null && index != "" && index.indexOf("add") != -1) {
            $("#addBtn").show();
        } else {
            $("#addBtn").hide();
        }
        if (index != null && index != "" && index.indexOf("save") != -1) {
            $("#saveBtn").show();
        } else {
            $("#saveBtn").hide();
        }
        if (index != null && index != "" && index.indexOf("del") != -1) {
            $("#delBtn").show();
        } else {
            $("#delBtn").hide();
        }


        initTree();
        $("#allTypeDiv").hide();

        // $("#parentDiv").hide();


        function getEntity() {
            var entity = {};
            entity.id = $("#id").val().trim();
            entity.typecode = $("#typecode").val().trim();
            entity.typename = $("#typename").val().trim();
            entity.code = $("#code").val().trim();
            entity.name = $("#name").val().trim();
            entity.pyCode = $("#pyCode").val().trim();
            entity.wbCode = $("#wbCode").val().trim();
            entity.zipCode = $("#zipCode").val().trim();
            entity.cComment = $("#cComment").val().trim();
            entity.flag = $("#flag").val().trim();
            entity.parentId = $("#parentId").val().trim();
            entity.effective = $("#effective").val().trim();
            entity.remark = $("#remark").val().trim();
            return entity;
        }

        $("#searchBtn").click(function () {
            searchNode();
        });


        $("#delBtn").click(function () {
            if ($("#id").val() == null || $("#id").val() == "") {
                toastr.warning("请选中一个字典节点");
                return;
            }
            var childrenNodes = currNode.children;
            if (childrenNodes && childrenNodes.length > 0) {
                console.log(childrenNodes);
                toastr.warning("拥有子节点不可删除!");
                return;
            }
            $.ajax({
                url: "${path}/dictionary/delById",
                type: "POST",
                data: {id: $("#id").val()},
                success: function (result) {
                    if (result == 1) {
                        var ztree = $.fn.zTree.getZTreeObj("ztree");
                        var nodes = ztree.getSelectedNodes();
                        for (var i = 0, l = nodes.length; i < l; i++) {
                            ztree.removeNode(nodes[i]);
                        }
                        $("#id").val("");
                        //添加选中父节点
                        var node = ztree.getNodeByParam("id", 0);
                        ztree.selectNode(node, true);//将指定ID的节点选中
                        $("#allTypeDiv").hide();
                        toastr.success("删除成功!")
                    } else {
                        toastr.error("删除失败!")
                    }
                }, error: function () {
                    toastr.error("删除失败!")
                }
            });
        });


        // $("#addTypeBtn").click(function () {
        //     addBol = 1;
        //     $("#delBtn").hide();
        //     if (currNode == null) {
        //         toastr.warning("请先选择一个节点!");
        //         return;
        //     }
        //     $("#typecode").removeAttr("readOnly");
        //     $("#typename").removeAttr("readOnly");
        //     $("#allTypeDiv").show();
        //     $("#subTypeDiv").hide();
        //     //判断树选中节点，为父节点部分显示，为叶子节点全部显示
        //
        //     //console.log(currNode.isParent+"====d");
        //     // else if(currNode.isParent){
        //     //     $("#parentDiv").hide();
        //     // }else{
        //     //     $("#subTypeDiv").show();
        //     // }
        //     emptyEntity(1);
        // });

        $("#addBtn").click(function () {
            addBol = 2;
            $("#delBtn").hide();
            //console.log(currNode);
            if (currNode == null) {
                toastr.warning("请先选择一个节点!");
                return;
            }

            $("#typecode").attr("readOnly", "true");
            $("#typename").attr("readOnly", "true");
            $("#typecode").val(currNode.typecode);
            $("#typename").val(currNode.typename);

            $("#allTypeDiv").show();
            $("#subTypeDiv").show();
            //判断树选中节点，为父节点部分显示，为叶子节点全部显示

            //typecode和code不重复


            // else if(currNode.isParent){
            //     $("#subTypeDiv").hide();
            // }else{
            //     $("#subTypeDiv").show();
            // }
            emptyEntity(0);
        });


        //获取当前节点的所有叶子节点并根据code
        function getAllChildrenNodes(treeNode, code) {
            if (treeNode.isParent) {
                var childrenNodes = treeNode.children;
                if (childrenNodes) {
                    for (var i = 0; i < childrenNodes.length; i++) {
                        if (childrenNodes[i].code == code) {
                            return 1;
                        }
                        //result += ',' + childrenNodes[i].id;
                        //result = getAllChildrenNodes(childrenNodes[i], result);
                    }
                }
            }
            return 0;
        }


        $("#name").blur(function () {
            if (this.value != null && this.value) {
                // 获取拼音首字母
                var pinyin = makePy(this.value);
                var wubi = makeWb(this.value);
                $("#pyCode").val(pinyin);
                $("#wbCode").val(wubi);
            }
        });

        $("#saveBtn").click(function () {
            var entity = getEntity();
            var zTree = $.fn.zTree.getZTreeObj("ztree");
            if (entity.id != "") {
                //叶子不能放到叶子节点，类别节点不能放到叶子节点
                if (entity.parentId != "") {
                    if (currNode.id == pIdVal) {
                        toastr.warning("上级目录不能是本身!");
                        return;
                    }
                    entity.parentId = pIdVal;
                } else {
                    entity.parentId = "";
                }
                if (currNode.code != null) {
                    //修改叶子节点
                    if (entity.code == null || entity.code == "") {
                        toastr.warning("代码必填!");
                        return;
                    } else if (entity.code.length > 50) {
                        toastr.warning("代码长度不超过50!");
                        return;
                    } else if (entity.code != null || entity.code != "") {
                        //查找该父节点下的所有叶子节点的代码不重复
                        var bol = getAllChildrenNodes(currNode, entity.code);
                        if (bol == 1) {
                            toastr.warning("代码不可重复!");
                            return;
                        }
                    } else if (entity.name == null || entity.name == "") {
                        toastr.warning("名称必填!");
                        return;
                    } else if (entity.name.length > 50) {
                        toastr.warning("名称长度不超过50!");
                        return;
                    } else if (entity.parentId != null || entity.parentId != "") {
                        var nodeList = zTree.getNodesByParamFuzzy("name", entity.parentId); //调用ztree的模糊查询功能，得到符合条件的节点
                        if (nodeList.length == 0) {
                            toastr.warning("无该类上级目录!");
                            return;
                        }
                    } else if (entity.cComment.length > 30) {
                        toastr.warning("说明长度不超过30!");
                        return;
                    } else if (entity.remark.length > 50) {
                        toastr.warning("备注长度不超过50!");
                        return;
                    }
                    entity.typecode = "";
                    entity.typename = "";

                } else {
                    //修改类别节点
                    if (entity.typecode == null || entity.typecode == "") {
                        toastr.warning("类别代码必填!");
                        return;
                    } else if (entity.typecode.length > 50) {
                        toastr.warning("类别代码长度不超过50!");
                        return;
                    } else if (entity.typename == null || entity.typename == "") {
                        toastr.warning("类别名称必填!");
                        return;
                    } else if (entity.typename.length > 50) {
                        toastr.warning("类别名称长度不超过50!");
                        return;
                    } else if (entity.cComment.length > 30) {
                        toastr.warning("说明长度不超过30!");
                        return;
                    } else if (entity.remark.length > 50) {
                        toastr.warning("备注长度不超过50!");
                        return;
                    }
                    entity.name = "";
                }

            }

            //添加
            if (entity.id == "") {
                if (addBol == 1) {
                    //添加类别
                    //添加
                    if (entity.typecode == null || entity.typecode == "") {
                        toastr.warning("类别代码必填!");
                        return;
                    } else if (entity.typecode.length > 50) {
                        toastr.warning("类别代码长度不超过50!");
                        return;
                    } else if (entity.typecode != null || entity.typecode != "") {
                        var nodeList = zTree.getNodesByParamFuzzy("typecode", entity.typecode);
                        if (nodeList.length > 0) {
                            toastr.warning("类别代码不可重复!");
                            return;
                        }
                    } else if (entity.typename == null || entity.typename == "") {
                        toastr.warning("类别名称必填!");
                        return;
                    } else if (entity.typename.length > 50) {
                        toastr.warning("类别名称长度不超过50!");
                        return;
                    } else if (entity.cComment.length > 30) {
                        toastr.warning("说明长度不超过30!");
                        return;
                    } else if (entity.remark.length > 50) {
                        toastr.warning("备注长度不超过50!");
                        return;
                    }
                    if (currNode != null) {
                        entity.parentId = currNode.id;
                    } else {
                        entity.parentId = 0;

                    }

                } else if (addBol == 2) {
                    entity.typecode = "";
                    entity.typename = "";
                    //添加叶子节点
                    if (entity.code == null || entity.code == "") {
                        toastr.error("代码必填!");
                        return;
                    } else if (entity.code != null || entity.code != "") {
                        //查找该父节点下的所有叶子节点的代码不重复
                        var bol = getAllChildrenNodes(currNode, entity.code);
                        if (bol == 1) {
                            toastr.warning("代码不可重复!");
                            return;
                        }
                    } else if (entity.code.length > 50) {
                        toastr.warning("代码长度不超过50!");
                        return;
                    } else if (entity.name == null || entity.name == "") {
                        toastr.warning("名称必填!");
                        return;
                    } else if (entity.name.length > 50) {
                        toastr.warning("名称长度不超过50!");
                        return;
                    } else if (entity.cComment.length > 30) {
                        toastr.warning("说明长度不超过30!");
                        return;
                    } else if (entity.remark.length > 50) {
                        toastr.warning("备注长度不超过50!");
                        return;
                    }
                    if (currNode != null) {
                        entity.parentId = currNode.id;
                    } else {
                        toastr.warning("请选中一个字典节点");
                        return;
                    }
                }

            }
            //console.log(entity);

            //修改字典
            $.ajax({
                url: "${path}/dictionary/saveInfo",
                type: "POST",
                data: entity,
                success: function (result) {
                    if (result.indexOf("成功") != -1) {
                        toastr.success(result);
                        initTree();
                        addBol = 0;
                    } else if (result.indexOf("失败") != -1) {
                        toastr.error(result);
                    } else {
                        toastr.warning(result);
                    }

                }, error: function () {
                    // toastr.error("加载失败!")
                }
            });
        });
    });

    //展开
    function expandAll() {
        var treeObj = $.fn.zTree.getZTreeObj("ztree");
        treeObj.expandAll(true);
    }

    //收缩
    function collapseAll() {
        var treeObj = $.fn.zTree.getZTreeObj("ztree");
        treeObj.expandAll(false);
    }
</script>

</body>
</html>