var formData = {};
layui.use(['form'], function () {
    var form = layui.form;
    //资源类型数据获取
    setTypeOption();
    formData = parent.getMenuData();

    if (formData.name == '') {
        $("#parentSelect").append("<option value='" + formData.guid + "'>" + formData.parentName + "</option>");
    } else {
        $("#name").val(formData.name);
        $("#path").val(formData.path);
        $("#des").val(formData.des);
        setOption(formData);
        $("#typeSelect").val(formData.menuType);
    }

    form.render('select');

    form.on('submit(addMenu)', function (data) {
        submitData(data.field);
        return false;
    });
});

function setTypeOption() {
    $.ajax({
        url: "/pqs9900/sysset/menuTypeList",
        type: "post",
        async: false,
        dataType: "json",
        success: function (data) {
            for (var i = 0; i < data.body.length; i++) {
                for (var i = 0; i < data.body.length; i++) {
                    $("#typeSelect").append("<option value='" + data.body[i].menuType + "'>" + data.body[i].menuName + "</option>");
                }
            }
        }
    });
}

function setOption(value) {
    $.ajax({
        url: "/pqs9900/sysset/parentMenuList",
        type: "post",
        async: false,
        dataType: "json",
        success: function (data) {
            if (data.code == 200 && data.body.length > 0) {
                for (var i = 0; i < data.body.length; i++) {
                    if (value.id != data.body[i].parentNode) {
                        $("#parentSelect").append("<option value='" + data.body[i].parentNode + "'>" + data.body[i].parentName + "</option>");
                    }
                }
            }

            $("#parentSelect").append("<option value='0'>菜单资源</option>");
            $("#parentSelect").val(value.parentGuid);
        }
    });
}

function submitData(data) {
    //xss过滤
    var name = data.name;
    name = name.replace(/(^\s*)|(\s*$)/g, '');
    name = filterXSS(name);
    var re = cenz;// 只能输入中文
    if (!re.test(name)) {
        layer.tips('名称只由中文、英文、数字组成', '#name', {
            tips: [1, 'red'],
            time: 2000
        });
        $("#name").val("");
    } else {
        var path = data.path;

        var re = url;
        path = filterXSS(path);
        if (!re.test(path)) {
            layer.tips('资源路径只能包含/字母或#',
                '#path', {
                    tips: [1, 'red'],
                    time: 2000
                });
            $("#path").val("");
        } else {
            var des = data.menudes;
            des = filterXSS(des);
            var parentSelect = data.parentName;
            var typeSelect = data.typeName;

            $.ajax({
                url: "/pqs9900/sysset/addMenu",
                type: "post",
                async: false,
                dataType: "json",
                data: {
                    id: formData.id,
                    name: name,
                    path: path,
                    parentSelect: parentSelect,
                    typeSelect: typeSelect,
                    des: des
                },
                success: function (data) {
                    if (data.code == 200) {
                        layer.msg("操作成功", function () {
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                            parent.reloadTable();
                        });
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        }
    }
}