<template>
	<el-dialog
		v-model="state.dialogVisible"
		width="500px"
		draggable
		title="新增数据权限"
		ref="dialogRef"
		append-to-body
		destroy-on-close
		@close="close"
	>
		<el-form :model="state.ruleForm" label-width="90px" ref="ruleFormRef">
			<el-form-item label="数据表" prop="table" :rules="[{ required: true, message: '请选择数据表', trigger: 'change' }]">
				<el-select-v2 v-model="state.ruleForm.table" :options="state.tableData" placeholder="请选择数据表" class="w100" filterable value-key="key" />
			</el-form-item>
			<el-form-item label="可见等级" prop="accessLevel" :rules="[{ required: true, message: '请选择可见等级', trigger: 'change' }]">
				<el-select v-model="state.ruleForm.accessLevel" placeholder="请选择可见等级" class="w100" filterable>
					<el-option v-for="item in state.accessLevelOptions" :key="item.key" :label="item.value" :value="item.key" />
				</el-select>
			</el-form-item>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="closeDialog" class="default-button">取 消</el-button>
				<el-button type="primary" @click="onSubmit(ruleFormRef)" :loading="state.loading">确 定</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref, onMounted } from 'vue';
import { ElMessage, FormInstance } from 'element-plus';
import { useRoute } from 'vue-router';
import { throttle } from '@/utils/tools';
import { dataAuthList, addDataAuth } from '@/api/system/roles';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['updateList']);
// 定义变量内容
const state = reactive<any>({
	dialogVisible: false,
	ruleForm: {
		roleId: '', // 角色id
		roleCode: 0, // 角色code
		accessLevel: '', // 可见等级
		tableName: '', // 数据表
	},
	tableData: [], // 数据表列表
	loading: false, // 加载状态
	accessLevelOptions: [], // 可见等级列表
});
const route = useRoute(); // 路由
// 打开弹窗
const ruleFormRef = ref<RefType>();
const openDialog = async (accessLevelOptions: any) => {
	state.accessLevelOptions = accessLevelOptions;
	const res: any = await dataAuthList();
	state.tableData = res.result ?? [];
	state.tableData = state.tableData.map((item: any) => {
		return {
			value: {
				...item,
			},
			label: item.value,
			key: item.key,
		};
	});
	state.dialogVisible = true;
};
// 关闭弹窗
const closeDialog = () => {
	state.dialogVisible = false;
};
const close = () => {
	ruleFormRef.value?.resetFields();
	ruleFormRef.value?.clearValidate();
};
// 新增
const onSubmit = throttle(async (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	await formEl.validate((valid: boolean) => {
		if (!valid) return;
		state.loading = true;
		const req = {
			roleId: state.ruleForm.roleId, // 角色id
			roleCode: state.ruleForm.roleCode, // 角色code
			accessLevel: state.ruleForm.accessLevel,
			tableName: state.ruleForm.table.key,
			tableDisplay: state.ruleForm.table.value,
			roleDisplay: state.ruleForm.roleDisplay,
		};
		addDataAuth(req)
			.then(() => {
				ElMessage({
					message: '新增成功',
					type: 'success',
				});
				state.loading = false;
				closeDialog();
				emit('updateList');
			})
			.catch(() => {
				// 新增失败
				state.loading = false;
				closeDialog();
			});
	});
}, 1000);
onMounted(() => {
	state.ruleForm.roleId = route.query.id;
	state.ruleForm.roleCode = route.query.code;
	state.ruleForm.roleDisplay = route.query.roleDisplay;
});
// 暴露变量
defineExpose({
	openDialog,
	closeDialog,
});
</script>
<style lang="scss" scoped></style>
