var tree;
var iscbdw = "";
var roleCode = $.cookie("code");

var setting = {
    data: {
        key: {
            name: "F_DeptName"
        },
        simpleData: {
            enable: true,
            idKey: "F_DeptId",
            pIdKey: "F_PartentId",
            rootPId: -1
        }
    }
};
$(document).ready(function () {
    if (roleCode === "WLDW") {
        iscbdw = 2;
    } else {
        iscbdw = "";
    }
    bind();
    $(".add").click(function () {
        layer.open({
            type: 2,
            content: "buMenEdit.html", //iframe的url，no代表不显示滚动条
            title: '新增部门',
            area: ['40%', '50%'], //宽高
        });
    })
    $(".change").click(function () {
        var nodes = tree.getSelectedNodes();
        if (nodes.length != 1) {
            layer.confirm('请选择一条记录！', {
                btn: ['确定']
            });
            return;
        }
        else {
            layer.open({
                type: 2,
                content: "buMenEdit.html?id=" + nodes[0].F_DeptId, //iframe的url，no代表不显示滚动条
                title: '修改部门',
                area: ['40%', '50%'], //宽高
            });
        }
    })
    $(".del").click(function () {
        var nodes = tree.getSelectedNodes();
        if (nodes.length != 1) {
            layer.confirm('请选择一条记录！', {
                btn: ['确定']
            });
            return;
        }
        else {
            node = nodes[0];
            $('.delModel').css('display', 'block');
            $('.delName').html(node.F_DeptName);
        }
    })

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

    //确定删除按钮
    $('.sure').click(function () {
        var nodes = tree.getSelectedNodes();
        $.post(huayi.config.callcenter_url + "Department/DelDepts", { id: nodes[0].F_DeptId, "token": $.cookie("token") }, function (result) {
            result = JSON.parse(result);
            if (result.state.toLowerCase() == "success") {
                layer.msg(result.message);
                $('.delModel').css('display', 'none');
                bind();
            }
        })
    })
})

function bind() {
    $.getJSON(huayi.config.callcenter_url + "Department/GetDeptList", { 
        "token": $.cookie("token"),
        iscbdw: iscbdw,
    }, function (result) {
        if (result.state.toLowerCase() == "success") {
            tree = $.fn.zTree.init($("#treeDemo"), setting, result.data);
        }
    })
}