var tps = $('.tps');
var id = ''; //当前节点id
var name; //当前节点name
var pid; //当前节点父id
var pname;//当前节点父name
var xlName; //添加弹出框内下拉框内节点name
var zNodes = [];
//通用ajax
$.ajax({
    type: "get",
    url: "http://localhost:63662/ModuleInfo/GetTreelist",
    dataType: 'json',
    async: false,
    data: {
        pid: pid,
        token: $.cookie("token")
    },
    success: function (res) {
        var content = res.data;
        for (var i = 0, l = content.length; i < l; i++) {
            content[i].name = content[i].text;
            delete content[i].text;
            var second = content[i].children
            if (second != null) {
                for (var j = 0; j < second.length; j++) {
                    second[j].name = second[j].text;
                    delete second[j].text;
                    var thired = second[j].children;
                    if (thired != null) {
                        for (var g = 0; g < thired.length; g++) {
                            thired[g].name = thired[g].text;
                            delete thired[g].text;
                            var fours = thired[g].children;
                            if (fours != null) {
                                for (var k = 0; k < fours.length ; k++) {
                                    fours[k].name = fours[k].text;
                                    delete fours[k].text;
                                }
                            }
                        }
                    }
                }
            }
            zNodes = content;
        }

    }

});
$(document).ready(function () {
    $.fn.zTree.init($("#treeDemo"), setting1, zNodes); //实例化树形图
});

var setting1 = {
    data: {
        simpleData: {
            enable: true
        }
    },
    callback: {
        onClick: zTreeOnClick
    }
};
function zTreeOnClick(event, treeId, treeNode) {
    id = treeNode.id;
    name = treeNode.name
    var p = treeNode.getParentNode();
    pid = p.id
    pname = p.name;
};
var setting2 = {
    data: {
        simpleData: {
            enable: true
        }
    },
    callback: {
        onClick: addTreeClick
    }
}
function addTreeClick(event, treeId, treeNode) {
    addPid = treeNode.id;
    console.log(addPid);
    xlName = treeNode.name;
    $('.inps1').val(xlName);

};
//删除按钮
$('.del').click(function () {
    if (id == 'null' || id == '') {
        alert('没有要删除的分类！')
    } else {
        $('.delModel').css('display', 'block');
        $('.delName').html(name);
    }
})
//关闭按钮
$('.delBtr').click(function () {
    $('.delModel').css('display', 'none');
})
//确定删除按钮
$('.sure').click(function () {
    $.ajax({
        type: "post",
        url: "http://localhost:63662/ModuleInfo/DelModule",
        async: true,
        dataType: 'json',
        data: {
            ids: id,
            token: $.cookie("token")
        },
        success: function (data) {
            if (data.state == "success") {
                alert("删除成功！")
                window.location.reload();
            }
        }
    });
    $('.delModel').css('display', 'none');

})
//取消删除按钮
$('.return').click(function () {
    $('.delModel').css('display', 'none');
})


//添加按钮
$('.add').click(function () {
    $('.addModel').css('display', 'block');
    if (name == null || name == '') {
        tps.html('顶级分类');
        $('.inps1').val('顶级分类');
        addPid == 0;
    } else {
        tps.html(name);
        $('.inps1').val(name);
        addPid = id;
    }
    treeCont();

})
//添加关闭按钮
$('.addBtr').click(function () {
    $('.addModel').css('display', 'none');
})
//添加内容下拉
$('.inps1').focus(function () {
    $('.xlAdd').css('display', 'block')
})
$('.xl_one').click(function () {
    if ($('.xlAdd').css('display') == 'block') {
        $('.xlAdd').css('display', 'none')
    } else {
        $('.xlAdd').css('display', 'block')
    }
})
$('.addTree').mouseleave(function () {
    $(this).css('display', 'none')
})
//保存添加按钮
var addPid, addDeptname, addSort;
//添加弹出框内 所属部门下拉框内数据
function treeCont() {
    $.fn.zTree.init($("#addTreeDemo"), setting2, zNodes); //实例化树形图
    var addTreeObj = $.fn.zTree.getZTreeObj("addTreeDemo");
    var addNodes = addTreeObj.getSelectedNodes();

}
$('.addCun').click(function () {
    console.log(addPid);
    if ($('.inps2').val() == "" || $('.inps3').val() == "") {
        alert('所添加内容不能为空！')
    } else {

        addType = $('#selecttype').val();
        addName = $('.inps3').val();
        addCode = $('.inps4').val();
        addController = $('.inps5').val();
        addAction = $('.inps6').val();
        addState = $('.inps7').prop('checked');
        addRemark = $('.inps8').val();
        addSort = $('.inps9').val();

        $('.addModel').css('display', 'none');
        $.ajax({
            type: "post",
            url: "http://localhost:63662/ModuleInfo/AddModule",
            dataType: 'json',
            async: true,
            data: {
                parentid: id,//当前选择节点ID
                code: addCode,
                name: addName,//部门名称
                controller: addController,
                action: addAction,
                flag: addState,
                remark: addRemark,
                sort: addSort,//排列序号
                type: addType
            },
            success: function (data) {
                console.log(data)
                console.log(data.state);
                if (data.state == "success") {
                    alert('添加部门成功！')
                    window.location.reload();

                }
            }

        });

    }


})
//修改按钮
var chanPid,//修改弹出框内 下拉框父节点ID
    chanId,//修改弹出框内 下拉框当前节点ID
changeName,//修改弹出框内 下拉框当前节点name
  chbcName,//保存的部门名称
  chbcSort,//保存的排序号
   chbcPid;//保存的所属部门ID
$('.change').click(function () {
    if (id == 'null' || id == '') {
        alert('没有选择要修改的菜单！')

    } else {
        changeAjax(id);
        $('.changeModel').css('display', 'block');
        $('.chtps').html(name);
        changeTreeCont();
    }


})
$('.changeBtr').click(function () {
    $('.changeModel').css('display', 'none');
})
$('.inps4').focus(function () {
    $('.xlChange').css('display', 'block')
})
//下拉按钮功能
$('.xl_two').click(function () {
    if ($('.xlChange').css('display') == 'block') {
        $('.xlChange').css('display', 'none')
    } else {
        $('.xlChange').css('display', 'block')
    }

})
//修改弹出框内 下拉树形图参数配置项
var setting3 = {
    data: {
        simpleData: {
            enable: true
        }
    },
    callback: {
        onClick: changeTreeClick
    }
}

function changeTreeClick(event, treeId, treeNode) {
    chanPid = treeNode.pId;
    chanId = treeNode.id;
    changeName = treeNode.name;
    $('.inps1').val(changeName);

};

//修改弹出框内 所属部门下拉框内数据
function changeTreeCont() {
    $.fn.zTree.init($("#changeTreeDemo"), setting3, zNodes); //实例化树形图
}
function changeAjax(id) {
    $.ajax({
        type: "get",
        url: "http://localhost:63662/ModuleInfo/GetModule",
        dataType: 'json',
        async: true,
        data: {
            mid: id
        },
        success: function (res) {
            var data = res.data.model;
            if (pid == null) {
                pid == 0;
                $('.inps1_chg').val('顶级分类');
                chbcPid == 0;
            } else {
                $('.inps1_chg').val(pname);
                chbcPid = pid;
            }
            $('.inps2_chg').val(data.type);
            $('.inps3_chg').val(data.name);
            $('.inps4_chg').val(data.code);
            $('.inps5_chg').val(data.controller);
            $('.inps6_chg').val(data.action);
            if (data.enable == 1) { $('.inps7_chg').prop("checked", "checked"); }
            $('.inps8_chg').val(data.remark);
            $('.inps9_chg').val(data.sort);
        }
    });
}
//修改的保存按钮功能
function changeBaoCunAjax(id, chbcPid, chbcName, chbcSort) {
    $.ajax({
        type: "post",
        url: "http://localhost:63662/ModuleInfo/EditModule",
        async: true,
        dataType: 'json',
        data: {
            id: id,
            parentid: id,//当前选择节点ID
            code: addCode,
            name: addName,//部门名称
            controller: addController,
            action: addAction,
            flag: addState,
            remark: addRemark,
            sort: addSort,//排列序号
            type: addType
        },
        success: function (data) {
            if (data.state == "success") {
                alert('修改部门成功！')
                window.location.reload();
            }
        }
    });
}

$('.changeCun').click(function () {
    //if ($('.inps4').val() == "" || $('.inps5').val() == "" || $('.inps6').val() == "") {
    //    alert('所修改内容不允许为空！')
    //} else {
    $('.changeModel').css('display', 'none');
    console.log('部门id' + id + '所属部门id' + chbcPid + '部门名称' + chbcName + '排序' + chbcSort);
    chbcPid = chanId;
    chbcName = $('.inps5').val();
    chbcSort = $('.inps6').val();
    changeBaoCunAjax(id, chbcPid, chbcName, chbcSort);
    //}

})



