<template>
	<el-dialog v-model="state.dialogVisible" width="800px" draggable title="编辑知识库关键词" @close="close">
		<el-form :model="state.ruleForm" label-width="110px" ref="ruleFormRef">
			<el-row :gutter="10">
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="关键词" prop="tag" :rules="[{ required: true, message: '请填写关键词', trigger: 'blur' }]">
						<el-input v-model="state.ruleForm.tag" 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="classifyArray" :rules="[{ required: true, message: '请选择分类', trigger: 'change' }]">
						<el-select v-model="state.ruleForm.classifyArray" placeholder="请选择分类" class="w100" multiple collapse-tags collapse-tags-tooltip>
							<el-option v-for="item in knowledgeWordClassify" :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="isEnable" :rules="[{ required: false, message: '请选择是否启用', trigger: 'change' }]">
						<el-switch v-model="state.ruleForm.isEnable" inline-prompt active-text="启用" inactive-text="禁用" :active-value="0" :inactive-value="1" />
					</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" class="mr10 mb10" size="large" closable @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"
							placeholder="请填写标签近义词"
						/>
						<el-button v-else @click="showInput" class="mb10"> 添加近义词 </el-button>
					</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
					<el-form-item label="备注说明" prop="remark" :rules="[{ required: false, message: '请填写备注说明', trigger: 'blur' }]">
						<el-input v-model="state.ruleForm.remark" placeholder="请填写备注说明" type="textarea" :autosize="{ minRows: 4, maxRows: 8 }"></el-input>
					</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="loading">确 定</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts" name="knowledgeLexiconEdit">
import { nextTick, reactive, ref } from 'vue';
import { ElMessage, FormInstance } from 'element-plus';
import { throttle } from '@/utils/tools';
import { knowledgeLexiconUpdate, knowledgeLexiconDetail } from '@/api/auxiliary/knowledgeLexicon';

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

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 state = reactive<any>({
	dialogVisible: false,
	ruleForm: {
		tag: '', // 关键词
		classifyArray: [], // 标签分类
		isEnable: 0, // 是否启用
		synonym: '', // 违禁同义词
		remark: '', // 备注说明
	},
	isDisabled: false, // 是否禁用
});
let loading = ref<boolean>(false); // 加载状态
// 打开弹窗
const ruleFormRef = ref<RefType>();
const openDialog = async (id: string) => {
	state.dialogVisible = true;
	try {
		const res = await knowledgeLexiconDetail(id);
		state.ruleForm = {
			tag: res.result?.tag, // 工单关键词
			classifyArray: res.result?.classify.split(','), // 标签分类
			isEnable: res.result?.isEnable, // 是否启用
			synonym: res.result?.synonym.split(','), // 违禁同义词
			remark: res.result?.remark, // 备注说明
			...res.result,
		};
	} catch (error) {
		console.log(error);
	}
};
// 关闭弹窗
const closeDialog = () => {
	state.dialogVisible = false;
};
const close = () => {
	ruleFormRef.value?.resetFields();
	ruleFormRef.value?.clearValidate();
  dynamicTags.value = [];
};
// 新增
const onSubmit = throttle(async (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	await formEl.validate((valid: boolean) => {
		if (!valid) return;
		loading.value = true;
		const request = {
			...state.ruleForm,
			synonym: dynamicTags.value.join(','),
			classify: state.ruleForm.classifyArray.join(','),
		};
		knowledgeLexiconUpdate(request)
			.then(() => {
				ElMessage({
					message: '操作成功',
					type: 'success',
				});
				loading.value = false;
				closeDialog();
				emit('updateList');
			})
			.catch(() => {
				// 新增失败
				loading.value = false;
				closeDialog();
			});
	});
}, 300);
// 暴露变量
defineExpose({
	openDialog,
	closeDialog,
});
</script>
