<template>
<el-dialog title="角色设置" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
	<el-tabs tab-position="top">
		<el-tab-pane label="角色选择">
			<div class="treeMain">
				<el-tree ref="role" node-key="id" :data="role.list" :default-checked-keys="role.checked" :props="role.props" check-strictly default-expand-all show-checkbox></el-tree>
			</div>
		</el-tab-pane>
	</el-tabs>
	<template #footer>
		<el-button @click="visible=false" >取 消</el-button>
		<el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
	</template>
</el-dialog>
</template>

<script>
export default {
	data(){
		return {
			visible: false,
			isSaveing: false,
			role: {
				list: [],
				checked: [],
				props: {
					label: (data)=>{
						return data.title
					}
				}
			},
			form: {uid: '',roles: []}
		}
	},
	mounted(){
		this.getRole();
	},
	methods:{
		open(){
			this.visible = true;
			return this;
		},
		async submit(){
			this.isSaveing = true;
			let roles = this.$refs.role.getCheckedNodes();
			this.form.roles = [];
			roles.map(item => {
				this.form.roles.push(item.id);
			})
			this.isSaveing = true;
			var res = await this.$API.auth.users.uprole.post(this.form);

			this.isSaveing = false;
			if(res.code == 1){
				this.$emit('success', this.form)
				this.visible = false;
				this.$message.success("操作成功")
			}else{
				this.$message.error(res.message)
			}
		},
		async getRole(){
			let res = await this.$API.auth.role.list.get({is_tree: 1});
			this.role.list = res.data;
		},
		setData(data){
			data.roles.map(item => {
				this.role.checked.push(item.id)
			})
			this.form.uid = data.uid;
		}
	}
}
</script>

<style scoped>
.treeMain {height:280px;overflow: auto;border: 1px solid #dcdfe6;margin-bottom: 10px;}
</style>
