function initMenu() {

    //  $("#main_content_title").html("菜单管理")

    $('#dashboard').dashboard({ draggable: true });
    var zTreeObj;
    var zNodes = [];
    menu_tree = $.fn.zTree.init($("#menu_tree"), {}, zNodes);
    getMenu();
    $("#dept_add").on("click", function() {
        var nodedata = menu_tree.getSelectedNodes();
        edittype = "add"
        loadData(nodedata)
        $('#addModal').modal('show')
    })

    $("#dept_edit").on("click", function() {
        //获取选中的节点
        var nodedata = menu_tree.getSelectedNodes();
        if (nodedata.length < 1) {
            layer.msg('请先选择部门！', {
                time: 2000, //20s后自动关闭
            });
            return;
        }
        edittype = "edit"
        loadData(nodedata)
        $('#addModal').modal('show');
    });

    $("#dept_delete").on("click", function() {
        //获取选中的节点
        var nodedata = menu_tree.getSelectedNodes();
        if (nodedata.length < 1) {
            layer.msg('请先选择部门！', {
                time: 2000, //20s后自动关闭
            });
            return;
        }
        layer.confirm('您确定要删除该部门？', {
            offset: "auto",
            shade: 0,
            btn: ['确定'] //按钮
        }, function() {
            deleteItem4CallBack(function() {
                DeptDelete(nodedata[0].id, function(data) {
                    layer.msg('删除成功！', {
                        time: 2000, //20s后自动关闭
                    });
                    menu_tree.removeNode(nodedata[0]);
                }, function(error) {

                });
            });
        });

    });
}

function getMenu() {
    function zTreeOnRename(event, treeId, treeNode, isCancel) {
        alert(treeNode.tId + ", " + treeNode.name);
    }

    function zTreeOnRemove(event, treeId, treeNode) {
        alert(treeNode.tId + ", " + treeNode.name);
    }


    var setting = {
        data: {
            simpleData: {
                enable: true,
                idKey: "id",
                pIdKey: "pId",
                rootPId: null
            }
        },
        view: { //表示tree的显示状态
            selectMulti: false //表示禁止多选
        },
        check: { //表示tree的节点在点击时的相关设置
            enable: false, //是否显示radio/checkbox
            chkStyle: "checkbox", //值为checkbox或者radio表示
            checkboxType: { p: "", s: "" }, //表示父子节点的联动效果
            radioType: "level" //设置tree的分组
        },
        callback: {
            onClick: zTreeOnClick
        },
        edit: {
            enable: true,
            drag: {
                autoExpandTrigger: true,
                isMove: true,
                prev: false,
                next: true,
                inner: true,
                autoOpenTime: 0
            },
            showRenameBtn: false,
            showRemoveBtn: false
        }
    };

    post_common_service('menu/get/all?judge=10', {}, function(data) {
        zdata = [];
        for (var i = 0; i < data.length; i++) {
            var m = data[i];
            var zdataItem = {};
            zdataItem["id"] = m.id;
            zdataItem["value"] = m.id;
            zdataItem["pId"] = null;
            zdataItem["name"] = m.menuname;
            zdataItem["children"] = addobj(m);
            zdata.push(zdataItem);
        }
        menu_tree = $.fn.zTree.init($("#menu_tree"), setting, zdata)
    }, function(error) {

    });
}

function addobj(data) {
    var zdata = [];
    if (typeof(data.childs) != "undefinde" && data.childs != null &&
        data.childs.length > 0) {
        for (var i = 0; i < data.childs.length; i++) {
            var m = data.childs[i];
            var zdataItem = {};
            zdataItem["id"] = m.id;
            zdataItem["value"] = m.id;
            zdataItem["pId"] = data.id;
            zdataItem["icon"] = "";
            zdataItem["name"] = m.menuname;
            zdataItem["children"] = addobj(m);
            zdata.push(zdataItem);
        }
        return zdata;
    } else {
        return [];
    }
}
var curTreeNode;

function zTreeOnClick(event, treeId, treeNode) {
    // alert(treeNode.id);
    curTreeNode = treeNode;
    queryMenu(treeNode.id);
}

function refresh() {
    // alert(treeNode.id);
    getMenu();
    $('.datatable').mytable('empty');
    //queryMenu(curTreeNode.id);
}

function queryMenu(id) {
    var data = {
        "pid": id
    }
    var cols = [
        { width: 80, text: '菜单名称', type: 'number', flex: false, field: 'menuname' },
        { width: 160, text: '地址', type: 'string', flex: false, field: 'url' },
        // {width: 80, text: '字典名称', type: 'string', flex: true, field: 'parentId'},
        { width: 60, text: '图标', type: 'string', flex: false, field: 'iconClass' },
        { width: 50, text: '类型', type: 'string', flex: false, field: 'type' },
        { width: 50, text: '排序号', type: 'number', flex: false, field: 'sortNo' },
        {
            width: 60,
            text: '操作',
            type: 'string',
            flex: false,
            field: 'id',
            oper: [
                { func: 'showEditMenu', text: '修改', icon_class: 'icon-edit' },
                { func: 'deleteMenu', text: '删除', icon_class: 'icon-remove-circle' }
            ]
        }
    ];
    $('.datatable').mytable({
        'cols': cols,
        'url': "/menu/getMenuByParent/" + id + "/",
        'param': data
    });
}

function showAddMenu() {
    $('#menuname').val('');
    $('#url').val('');
    if (curTreeNode)
        $('#parentId').val(curTreeNode.id)
    else
        $('#parentId').val('');
    $('#iconClass').val('');
    $('#type').val('');
    $('#sortNo').val('');
    showPopup4Common('添加菜单', function() {
        addOrUpdateConstant("/menu/addMenu/");
    }, '400px');
}
// var cur_id;
function addOrUpdateConstant(url) {
    var valid = 1;
    if (!$("#valid").is(':checked')) valid = 0;
    var param = {
        // 'id':cur_id,
        'menuname': $('#menuname').val(),
        'url': $('#url').val(),
        'parentId': $('#parentId').val(),
        'iconClass': $('#iconClass').val(),
        'type': $('#type').val(),
        'sortNo': $('#sortNo').val()
    };
    addOrUpdateItem4Common(param, url, refresh);
}

function showEditMenu(id) {
    // cur_id='';
    var rowData = getItemByIdFromArr(id, $('.datatable').mytable('getTableData'));
    if (rowData) {
        // cur_id = rowData.id;
        $('#menuname').val(rowData.menuname);
        $('#url').val(rowData.url);
        $('#parentId').val(rowData.parentId);
        $('#iconClass').val(rowData.iconClass);
        $('#type').val(rowData.type);
        $('#sortNo').val(rowData.sortNo);
        // if(rowData.valid==1){
        //     $("#valid").attr('checked','true');
        // }else{
        //     $("#valid").attr('checked','false');
        // }
        showPopup4Common('修改菜单', function() {
            addOrUpdateConstant("/menu/updateMenu/" + id);
        }, '400px');
    }
}

function deleteMenu(id) {
    deleteItem4Common(id, "/menu/deleteMenu/" + id + "", refresh);
}