<template>
    <div>
        <el-form :model="formData" :rules="rules" ref="form" label-width="80px" label-position="right" size="small"
                 style="max-width: 500px;">
            <el-form-item prop="name" label="名称">
                <el-input v-model="formData.name"></el-input>
            </el-form-item>
            <el-form-item prop="enname" label="英文名">
                <el-input v-model="formData.enname"></el-input>
            </el-form-item>
            <el-form-item prop="roleType" label="类型">
                <el-input v-model="formData.roleType"></el-input>
            </el-form-item>
            <el-form-item prop="remarks" label="备注">
                <el-input v-model="formData.remarks"></el-input>
            </el-form-item>
            <el-form-item prop="menuIds" label="菜单权限">
                <el-tree
                    ref="menuTree"
                    :data="menus"
                    :highlight-current="true"
                    :expand-on-click-node="false"
                    node-key="id"
                    show-checkbox
                    default-expand-all>
                </el-tree>
            </el-form-item>
            <el-form-item prop="permission" label="数据权限">
                <el-select v-model="formData.permission" multiple placeholder="请选择" style="width: 100%;">
                    <el-option v-for="item in permissions" :label="item.dataruleName" :value="item.permission"
                               :key="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button @click="onSave" :loading="$store.state.fetchingData" type="primary">保存</el-button>
                <el-button @click="$router.go(-1)">取消</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
    export default {
        created() {
            if (this.$route.query.id) {
                this.$http.get({
                    url: '/sysRole/getOne',
                    data: {
                        id: this.$route.query.id
                    }
                }).then(res => {
                    if (res.success) {
                        if (res.data.permission) {
                            res.data.permission = res.data.permission.split(',');
                        }
                        if (res.data.menuIds) {
                            this.$refs.menuTree.setCheckedKeys(res.data.menuIds.split(','));
                        }
                        this.formData = res.data;
                    }
                })
            }
            this.getMenus();
            this.getPermissions();
        },
        data() {
            return {
                saving: false,
                formData: {},
                rules: {
                    name: [
                        {required: true, message: '请填写名称', trigger: 'blur'},
                    ],
                    enname: [
                        {required: true, message: '请填写英文名', trigger: 'blur'},
                    ]
                },
                menus: [],
                permissions: []
            }
        },
        methods: {
            getMenus() {
                this.$http.get({
                    url: '/sysMenu/menuTree'
                }).then(res => {
                    if (res.success) {
                        const parse = trees => {
                            trees.sort((a, b) => {
                                return a.extra.sort - b.extra.sort;
                            });
                            return trees.map(i => {
                                let t = {
                                    id: i.id,
                                    label: i.name,
                                    parentId: i.parentId,
                                    extra: i.extra
                                };
                                if (i.children instanceof Array) {
                                    t.children = parse(i.children);
                                }
                                return t;
                            });
                        };
                        this.menus = parse(res.data);
                    }
                })
            },
            getPermissions() {
                this.$http.get({
                    url: '/sysDatarule/all'
                }).then(res => {
                    if (res.success) {
                        this.permissions = res.data
                    }
                })
            },
            onSave() {
                this.$refs.form.validate((valid) => {
                    if (valid) {
                        this.submit();
                    } else {
                        return false;
                    }
                });
            },
            submit() {
                this.formData.menuIds = this.$refs.menuTree.getCheckedKeys(true).join();
                this.$http.post({
                    url: this.formData.id ? '/sysRole/update' : '/sysRole/save',
                    data: this.formData
                }).then(res => {
                    if (res.success) {
                        this.$message.success('成功');
                        this.$router.go(-1);
                    } else {
                        this.$message.warning('失败')
                    }
                });
            }
        }
    }
</script>
<style lang="less" scoped>

</style>
