<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" draggable destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="90px" label-position="right">
			<el-form-item label="角色名称" prop="role_name">
				<el-input v-model="form.role_name" placeholder="请填写角色名称" clearable></el-input>
			</el-form-item>
			<el-form-item label="状态" prop="active_status">
				<el-switch v-model="form.active_status" :active-value="true" :inactive-value="false"></el-switch>
			</el-form-item>
			<el-form-item label="备注" prop="remark">
				<el-input v-model="form.remark" clearable type="textarea"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSave" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增',
					edit: '编辑',
					show: '查看'
				},
				visible: false,
				isSave: false,
				//表单数据
				form: {
					id:"",
					role_name: "",
					active_status: true,
					remark: ""
				},
				//验证规则
				rules: {
					role_name: [
						{required: true, message: '请输入角色名称'}
					],
					active_status: [
						{required: true,}
					]
				}
			}
		},
		mounted() {

		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSave = true;
						const res = await this.$API.system.role.add.post(this.form);
						this.isSave = false;
						if(res.code == 200){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						}
					}
				})
			},
			//表单注入数据
			setData(data){
				this.form.id = data.id
				this.form.role_name = data.role_name
				this.form.active_status = data.active_status
				this.form.remark = data.remark
			}
		}
	}
</script>

<style>
</style>
