<template>
	<el-dialog title="编辑违禁词" v-model="state.dialogVisible" width="769px" draggable @close="close">
		<el-form :model="state.ruleForm" ref="ruleFormRef" label-width="100px">
			<el-row :gutter="10">
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<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="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="违禁词分类" prop="classify" :rules="[{ required: true, message: '请选择违禁词分类', trigger: 'change' }]">
						<el-select v-model="state.ruleForm.classify" placeholder="请选择违禁词分类" class="w100">
							<el-option v-for="item in props.prohibitedClassify" :value="item.dicDataValue" :key="item.dicDataValue" :label="item.dicDataName" />
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="违禁词属性" prop="type" :rules="[{ required: true, message: '请选择违禁词属性', trigger: 'change' }]">
						<el-select v-model="state.ruleForm.type" placeholder="请选择违禁词属性" class="w100">
							<el-option v-for="item in props.prohibitedType" :value="item.dicDataValue" :key="item.dicDataValue" :label="item.dicDataName" />
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
					<el-form-item label="违禁同义词" prop="synonym" :rules="[{ required: false, message: '请选择违禁同义词', trigger: 'change' }]">
						<el-tag v-for="tag in dynamicTags" :key="tag" size="large" class="mr10 mb10" closable :disable-transitions="false" @close="handleClose(tag)">
							{{ tag }}
						</el-tag>
						<el-input
							v-if="inputVisible"
							ref="InputRef"
							v-model="inputValue"
							@keyup.enter="handleInputConfirm"
							@blur="handleInputConfirm"
							style="max-width: 200px"
              class="mb10"
						/>
						<el-button v-else @click="showInput" class="mb10"> 添加同义词 </el-button>
					</el-form-item>
				</el-col>
			</el-row>
		</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" name="qualityLexiconEdit">
import { nextTick, reactive, ref } from 'vue';
import { ElMessage, FormInstance } from 'element-plus';
import { lexiconDetail, lexiconUpdate } from '@/api/quality/lexicon';
// 定义子组件向父组件传值/事件
const emit = defineEmits(['updateList']);

const props = defineProps({
	prohibitedClassify: {
		type: Array,
		default: () => [],
	},
	prohibitedType: {
		type: Array,
		default: () => [],
	},
});

const inputValue = ref('');
const dynamicTags = ref<EmptyArrayType>([]);
const inputVisible = ref(false);
const InputRef = ref<InstanceType<any>>();
// 删除同义词
const handleClose = (tag: string) => {
	dynamicTags.value.splice(dynamicTags.value.indexOf(tag), 1);
};
// 展示填写框
const showInput = () => {
	inputVisible.value = true;
	nextTick(() => {
		InputRef.value!.input!.focus();
	});
};
// 确定添加
const handleInputConfirm = () => {
	if (inputValue.value) {
		dynamicTags.value.push(inputValue.value);
	}
	inputVisible.value = false;
	inputValue.value = '';
};

// 定义变量内容
const ruleFormRef = ref<FormInstance>();
const state = reactive<any>({
	dialogVisible: false, // 弹窗显示隐藏
	ruleForm: {
		name: '', // 违禁词
		classify: '', // 违禁词分类
		type: '', // 违禁词属性
	},
	loading: false, // 确定按钮loading
});

// 打开弹窗
const openDialog = async (row: any) => {
	state.loading = true;
	state.dialogVisible = true;
	try {
		const res = await lexiconDetail(row.id);
		state.ruleForm = res.result ?? {};
		dynamicTags.value = res.result.synonym?.split(',') ?? [];
		state.loading = false;
	} catch (e) {
		console.log(e);
		state.loading = false;
	}
};
// 关闭弹窗
const closeDialog = () => {
	state.dialogVisible = false;
};
const close = () => {
	dynamicTags.value = [];
	ruleFormRef.value?.clearValidate();
	ruleFormRef.value?.resetFields();
};
// 新增
const onSubmit = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.validate((valid: boolean) => {
		if (!valid) return;
		state.loading = true;
		const request = {
			...state.ruleForm,
			synonym: dynamicTags.value.join(','),
		};
		lexiconUpdate(request)
			.then(() => {
				emit('updateList');
				closeDialog(); // 关闭弹窗
				ElMessage.success('操作成功');
				state.loading = false;
			})
			.catch(() => {
				emit('updateList');
				state.loading = false;
			});
	});
};
// 暴露变量
defineExpose({
	openDialog,
	closeDialog,
});
</script>
