<template>
	<div class="system-add-role-container">
		<el-dialog title="新增角色" v-model="isShowDialog" width="769px" draggable>
			<el-form :model="ruleForm" label-width="90px" ref="ruleFormRef">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="角色名称"  prop="name" :rules="[{ required: true, message: '请输入角色名称', trigger: 'blur' }]">
							<el-input v-model="ruleForm.name" placeholder="请输入角色名称" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="角色别名" prop="name" :rules="[{ required: false, message: '请输入角色别名', trigger: 'blur' }]">
							<el-input v-model="ruleForm.displayName" placeholder="请输入角色别名" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="角色状态" prop="name" :rules="[{ required: false, message: '请输入角色状态', trigger: 'blur' }]">
							<el-switch v-model="ruleForm.enable" inline-prompt active-text="启" inactive-text="禁"></el-switch>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="角色描述" prop="name" :rules="[{ required: false, message: '请输入角色描述', trigger: 'blur' }]">
							<el-input v-model="ruleForm.description" type="textarea" placeholder="请输入角色描述" maxlength="150"></el-input>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel">取 消</el-button>
					<el-button type="primary" @click="onSubmit">确 定</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts">
import { reactive, toRefs, defineComponent,getCurrentInstance } from 'vue';
import { addRole } from '/@/api/system/roles';
import { ElMessage } from 'element-plus';
export default defineComponent({
	name: 'systemAddRole',
	setup(props:any, {emit}:any) {
		const state = reactive<any>({
			isShowDialog: false,
			ruleForm: {
				name: '', // 角色名称
				displayName: '', // 角色标识
				enable: true, // 角色状态
				description: '', // 角色描述
			},
		});
		const { proxy } = <any>getCurrentInstance()
		// 打开弹窗
		const openDialog = () => {
			state.isShowDialog = true;
		};
		// 关闭弹窗
		const closeDialog = () => {
			state.isShowDialog = false;
		};
		// 取消
		const onCancel = () => {
			closeDialog();
		};
		// 新增
		const onSubmit = () => {
			proxy.$refs.ruleFormRef.validate((valid: boolean) => {
				if (valid) {
					addRole(state.ruleForm).then(()=>{
						ElMessage({
							message: '新增成功',
							type: 'success',
						})
						closeDialog();
						emit("updateList")
					}).catch(()=>{ // 新增失败
						closeDialog();
					})
				} else {
					return false;
				}
			});
		};
		return {
			openDialog,
			closeDialog,
			onCancel,
			onSubmit,
			...toRefs(state),
		};
	},
});
</script>

<style scoped lang="scss">
.system-add-role-container {
	.menu-data-tree {
		width: 100%;
		border: 1px solid var(--el-border-color);
		border-radius: var(--el-input-border-radius, var(--el-border-radius-base));
		padding: 5px;
	}
}
</style>
