var permission_selectinput;
var resource_selectinput;
var permisiontemp;

function loadPermisionData(nodedata) {
    var pnode = nodedata;
    resetform();

    if (edittype == "add") {
        $("#dept_name").val("")
        $('#addModal .modal-title').html('添加权限');
        $('#permision_add_btn').html('添加');
    }
    if (edittype == "edit") {
        $('#addModal .modal-title').html('修改权限');
        $('#permision_add_btn').html('保存');
        permisiontemp = PermisionMap.get(nodedata[0].id);
        setPermison();
    }

    permission_selectinput = new TreeSelect({
        element: '#permision_select',
        data: zdata,
        valueKey: "id",
        placeholder: "选择父权限",
        selectvalue: pnode.length > 0 ? pnode[0].id : null
    });



    //获取菜单data
    getmumdata();

    $("#resourcetype").change(function() {

        if (this.value == "1") {
            //菜单
            getmumdata();
        }
        if (this.value == "2") {
            //按钮
            getbuttonData()
        }

    });


    $("#permision_add_btn").off("click");
    $("#permision_add_btn").on("click", function() {
        addpermision();
    })

}

function setPermison() {

    $("#permission_name").val(permisiontemp.name);
    $("#permission_des").val(permisiontemp.description);
    $("#resourcetype").val(permisiontemp.type);
}


function addpermision() {

    var per_name = $("#permission_name").val();
    if (!per_name) {
        layer.alert("权限名称不能为空！");
        return;
    }
    var per_des = $("#permission_des").val();

    var per_pid = permission_selectinput.value;
    var resource_type = $("#resourcetype").val();
    var resource_id = resource_selectinput.value;
    var data = {
        "name": per_name,
        "description": per_des,
        "type": resource_type,
        "pid": per_pid,
        "resourceid": resource_id,
        "status": 1
    }

    PermisionAdd(data, function(sus_data) {
        layer.msg('添加成功！', {
            time: 2000, //20s后自动关闭
        }, function() {
            $('#addModal').modal('hide')
            getPermission();
        });
    }, function(error) {

    });



}


function getbuttonData() {
    resource_selectinput = new TreeSelect({
        element: '#resource_select',
        data: [],
        valueKey: "id",
        placeholder: "选择资源",
        selectvalue: null,
        callback: initPerName
    });
    initTextChangeEvent();
}

function getmumdata() {
    var mumdata = [];

    post_common_service('menu/get/all?judge=10', {}, function(data) {
        mumdata = [];
        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);
            mumdata.push(zdataItem);
        }
        resource_selectinput = new TreeSelect({
            element: '#resource_select',
            data: mumdata,
            valueKey: "id",
            placeholder: "选择资源",
            selectvalue: permisiontemp == null ? null : permisiontemp.resourceid,
            callback: initPerName
        });
    }, function(error) {

    });

    function initPerName(name) {
        if (!$("#permission_name").val()) $("#permission_name").val(name);
    }

    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 [];
        }
    }

}


function resetform() {

    $("#permission_name").val("");
    $("#permission_des").val("");
    $("#resourcetype").val(1);
}