<template>
	<el-dialog title="新增角色" v-model="state.dialogVisible" width="769px" draggable append-to-body destroy-on-close @close="close">
		<el-form :model="state.ruleForm" label-width="90px" ref="ruleFormRef">
			<el-row :gutter="35">
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="角色名称" prop="displayName" :rules="[{ required: true, message: '请填写角色名称', trigger: 'blur' }]">
						<el-input v-model="state.ruleForm.displayName" placeholder="请填写角色名称" clearable></el-input>
					</el-form-item>
				</el-col>
        <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
          <el-form-item label="角色类型" prop="roleType" :rules="[{ required: true, message: '请选择角色类型', trigger: 'change' }]">
            <el-select v-model="state.ruleForm.roleType" placeholder="请选择角色类型" class="w100">
              <el-option v-for="item in props.baseData.roleType" :key="item.key" :label="item.value" :value="item.key" />
            </el-select>
          </el-form-item>
        </el-col>
				<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
					<el-form-item label="角色编码" prop="name" :rules="[{ required: true, message: '请填写角色编码', trigger: 'blur' }]">
						<el-input v-model="state.ruleForm.name" placeholder="请填写角色编码" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
					<el-form-item label="角色说明" prop="description" :rules="[{ required: false, message: '请填写角色说明', trigger: 'blur' }]">
						<el-input v-model="state.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="closeDialog">取 消</el-button>
				<el-button type="primary" @click="onSubmit(ruleFormRef)">确 定</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts" name="systemAddRole">
import { reactive, ref } from 'vue';
import { addRole } from '@/api/system/roles';
import { ElMessage, FormInstance } from 'element-plus';
import mittBus from '@/utils/mitt';

const props = defineProps({
	baseData: {
		type: Object,
		default: () => {
			return {};
		},
	},
});
// 定义子组件向父组件传值/事件
const emit = defineEmits(['updateList']);

// 定义变量内容
const state = reactive<any>({
	dialogVisible: false, // 弹窗显示隐藏
	ruleForm: {
		name: null, // 角色名称
		displayName: null, // 角色标识
		description: null, // 角色描述
    roleType: null, // 角色类型
	},
});

// 打开弹窗
const ruleFormRef = ref<RefType>();
const openDialog = () => {
	state.dialogVisible = true;
};
// 关闭弹窗
const closeDialog = () => {
	state.dialogVisible = false;
};
const close = ()=>{
  ruleFormRef.value?.resetFields();
  ruleFormRef.value?.clearValidate();
}
// 新增
const onSubmit = async (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	await formEl.validate((valid: boolean) => {
		if (!valid) return;
		addRole(state.ruleForm)
			.then(() => {
				ElMessage({
					message: '新增成功',
					type: 'success',
				});
				closeDialog();
				emit('updateList');
				mittBus.emit('clearCache', 'systemUser');
			})
			.catch(() => {
				// 新增失败
				closeDialog();
			});
	});
};
// 暴露变量
defineExpose({
	openDialog,
	closeDialog,
});
</script>

<style scoped lang="scss">
.system-role-add-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>
