var tps = $(".tps");
var id = ""; //当前节点id
var name = ""; //当前节点name
var pid; //当前节点父ID
var pidName; //
var xlName; //添加弹出框内下拉框内节点name
var token = $.cookie("token");
tree();
departmentTree(); //部门名称数据

function tree() {
    $.get(
        huayi.config.callcenter_url + "Dictionary/GetZTreeList",
        {
            token: $.cookie("token"),
            //			"pid": pid
        },
        function (result) {
            result = $.parseJSON(result);
            $.fn.zTree.init($("#treeDemo"), setting1, result.data); //实例化树形图
        }
    );
}

var setting1 = {
    data: {
        key: {
            name: "text",
        },
        simpleData: {
            enable: true,
            idKey: "id",
            rootPId: 0,
        },
    },
    callback: {
        onClick: zTreeOnClick,
    },
};

function zTreeOnClick(event, treeId, treeNode) {
    id = treeNode.id;
    name = treeNode.text;
    var pidnode = treeNode.getParentNode();
    if (pidnode) {
        pidName = pidnode.text;
        pid = pidnode.id;
    } else {
        pidName = "顶级分类";
        pid = 0;
    }
}
var setting2 = {
    data: {
        key: {
            name: "text",
        },
        simpleData: {
            enable: true,
            idKey: "id",
            rootPId: 0,
        },
    },
    callback: {
        onClick: addTreeClick,
    },
};

function addTreeClick(event, treeId, treeNode) {
    addPid = treeNode.id;
    console.log(addPid);
    if (addPid == 38) {
        $(".addkey").show();
    } else {
        $(".addkey").hide();
    }
    xlName = treeNode.text;
    $(".inps1").val(xlName);
}
//删除按钮
$(".del").click(function () {
    if (id == "null" || id == "") {
        layer.confirm("没有要删除的分类！", {
            btn: ["确定"],
        });
        return;
    } else {
        $(".delModel").css("display", "block");
        $(".delName").html(name);
    }
});
//关闭按钮
$(".delBtr").click(function () {
    $(".delModel").css("display", "none");
});
//确定删除按钮
$(".sure").click(function () {
    $.ajax({
        type: "post",
        url: huayi.config.callcenter_url + "Dictionary/DelDicValue",
        async: true,
        dataType: "json",
        data: {
            ids: id,
            token: token,
        },
        success: function (data) {
            if (data.state == "success") {
                layer.msg("删除成功！");
                tree();
            }
        },
    });
    $(".delModel").css("display", "none");
});
//取消删除按钮
$(".return").click(function () {
    $(".delModel").css("display", "none");
});

//添加按钮
$(".add").click(function () {
    $(".addModel").css("display", "block");
    if (name) {
        if (name == "关键字") {
            $(".addkey").show();
        } else {
            $(".addkey").hide();
        }
        tps.html(name);
        $(".inps1").val(name);
        addPid = id;
    } else {
        tps.html("顶级分类");
        $(".inps1").val("顶级分类");
        addPid = 0;
    }
    treeCont();
});
//添加关闭按钮
$(".addBtr").click(function () {
    $(".addModel").css("display", "none");
    $(".addTree3").css("display", "none");
});
//添加内容下拉
$(".inps1").click(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() {
    $.get(
        huayi.config.callcenter_url + "Dictionary/GetZTreeList",
        {
            token: $.cookie("token"),
            //			"pid": pid
        },
        function (result) {
            result = $.parseJSON(result);
            $.fn.zTree.init($("#addTreeDemo"), setting2, result.data); //实例化树形图
        }
    );
}
$(".addCun").click(function () {
    console.log(addPid);
    if ($(".inps2").val() == "") {
        if (addPid.length <= 0) {
            layer.confirm("所添加内容不能为空！", {
                btn: ["确定"],
            });
            return;
        }
    } else {
        addDeptname = $(".inps2").val();
        $(".addModel").css("display", "none");
        $.ajax({
            type: "post",
            url: huayi.config.callcenter_url + "Dictionary/AddDicValue",
            dataType: "json",
            async: true,
            data: {
                pid: addPid, //当前选择节点ID
                name: addDeptname, //部门名称
                deptid: $("#Dpment").val(), //部门名称id
                remark: $("#addRemarks").val(), //备注
                token: token,
            },
            success: function (data) {
                //					console.log(data)
                //					console.log(data.state);
                if (data.state == "success") {
                    layer.msg("添加成功！");
                    tree();
                    $(".inps1").val("");
                    $(".inps2").val("");
                    $(".inps3").val("");
                    $("#addRemarks").val("");
                }
            },
        });
    }
});

//修改按钮
var chanPid, //修改弹出框内 下拉框父节点ID
    chanId, //修改弹出框内 下拉框当前节点ID
    changeName, //修改弹出框内 下拉框当前节点name
    chbcName, //保存的部门名称
    chbcSort, //保存的排序号
    chbcPid; //保存的所属部门ID
$(".change").click(function () {
    if (id == "null" || id == "") {
        layer.confirm("没有选择要修改的项！", {
            btn: ["确定"],
        });
        return;
    } else {
        // if (String(pid) === "38") {
            $(".editkey").show();
        // } else {
        //     $(".editkey").hide();
        // }
        changeAjax(id);
        $(".changeModel").css("display", "block");
        $(".chtps").html(name);
        $(".inps4").val(pidName);
        chbcPid = pid;
        changeTreeCont();
    }
});
//关闭按钮
$(".changeBtr").click(function () {
    $(".changeModel").css("display", "none");
    $(".addTree3").css("display", "none");
});
$(".inps4").click(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: {
        key: {
            name: "text",
        },
        simpleData: {
            enable: true,
            idKey: "id",
            rootPId: 0,
        },
    },
    callback: {
        onClick: changeTreeClick,
    },
};

function changeTreeClick(event, treeId, treeNode) {
    //		chanPid = treeNode.pId;
    chanId = treeNode.id;
    if (chanId == 38) {
        $(".editkey").show();
    } else {
        $(".editkey").hide();
    }
    changeName = treeNode.text;
    $(".inps4").val(changeName);
    var pidnode = treeNode.getParentNode();
    if (pidnode) {
        chanPid = pidnode.id;
    } else {
        chanPid = 0;
    }
    chbcPid = chanId;
}

//修改弹出框内 所属部门下拉框内数据
function changeTreeCont() {
    $.get(
        huayi.config.callcenter_url + "Dictionary/GetZTreeList",
        {
            token: $.cookie("token"),
            //			"pid": pid
        },
        function (result) {
            result = $.parseJSON(result);
            $.fn.zTree.init($("#changeTreeDemo"), setting3, result.data); //实例化树形图
        }
    );
}

function changeAjax(id) {
    $.ajax({
        type: "get",
        url: huayi.config.callcenter_url + "Dictionary/GetDicValue",
        dataType: "json",
        async: true,
        data: {
            id: id,
            token: token,
        },
        success: function (data) {
            var chanCon = data.data;
            if (pid == 0) {
                $(".inps4").val("顶级分类");
                chbcPid = pid;
            } else {
                $(".inps4").val(pidName);
                chbcPid = pid;
            }
            $(".inps5").val(chanCon.F_Value);
            $(".inps3").val(chanCon.F_DeptName);
            $("#Dpment").val(chanCon.F_Deptid);
            $("#changeRemarks").val(chanCon.F_Remark);
            //				$('.inps6').val(chanCon.sort);
            //				chbcSort = chanCon.sort;
            chbcName = chanCon.F_Value;
        },
    });
}
//修改的保存按钮功能
function changeBaoCunAjax(id, chbcPid, chbcName) {
    $.ajax({
        type: "post",
        url: huayi.config.callcenter_url + "Dictionary/AddDicValue",
        async: true,
        dataType: "json",
        data: {
            id: id,
            pid: chbcPid,
            name: chbcName,
            deptid: $("#Dpment").val(), //部门名称id
            remark: $("#changeRemarks").val(), //备注
            token: token,
        },
        success: function (data) {
            if (data.state == "success") {
                layer.msg("修改成功！");
                tree();
            }
        },
    });
}

$(".changeCun").click(function () {
    if ($(".inps4").val() == "" || $(".inps5").val() == "") {
        layer.confirm("所修改内容不允许为空！", {
            btn: ["确定"],
        });
        return;
    } else {
        $(".changeModel").css("display", "none");
        console.log(
            "部门id" +
                id +
                "所属部门id" +
                chbcPid +
                "部门名称" +
                chbcName +
                "排序" +
                chbcSort
        );
        chbcPid;
        chbcName = $(".inps5").val();
        //			chbcSort = $('.inps6').val();
        changeBaoCunAjax(id, chbcPid, chbcName);
    }
});
//部门名称数据
function departmentTree() {
    $.get(
        huayi.config.callcenter_url + "Department/GetDeptList",
        {
            token: $.cookie("token"),
            //		"pid": pid
        },
        function (result) {
            result = $.parseJSON(result);
            $.fn.zTree.init($("#addTreeDemo3"), setting4, result.data); //实例化树形图
            $.fn.zTree.init($("#addTreeDemo6"), setting4, result.data); //实例化树形图
        }
    );
}
var setting4 = {
    data: {
        key: {
            name: "F_DeptName",
        },
        simpleData: {
            enable: true,
            idKey: "F_DeptId",
            pIdKey: "F_PartentId",
            rootPId: 0,
        },
    },
    callback: {
        onClick: zTreeOnClick3,
    },
};
function zTreeOnClick3(event, treeId, treeNode) {
    // if (treeNode.level >= 2) {
        $(".inps3").val(treeNode.F_DeptName);
        $("#PID").val(treeNode.F_DeptId);
        $("#Dpment").val(treeNode.F_DeptId);
        $(".Cleans").show();
    // }
}
$(".inps3").focus(function () {
    $(".xlAdd3").css("display", "block");
});
$(".xl3").click(function () {
    if ($(".xlAdd3").css("display") == "block") {
        $(".xlAdd3").css("display", "none");
    } else {
        $(".xlAdd3").css("display", "block");
    }
});
$(".addTree3").mouseleave(function () {
    $(this).css("display", "none");
});
//清除
$(".Cleans ").click(function () {
    $(".inps3").val("");
    $("#PID").val("");
    $("#Dpment").val("");
});
