<template>
	<el-dialog v-model="state.dialogVisible" width="30%" draggable title="修改标注" destroy-on-close append-to-body @close="close">
		<el-form :model="state.ruleForm" label-width="90px" ref="ruleFormRef" v-loading="loading">
			<el-form-item label="工单标题">{{ orderTitle }}</el-form-item>
			<el-form-item label="工单标记" prop="snapshotLabel" :rules="[{ required: true, message: '请选择工单标记', trigger: 'change' }]">
				<el-select
					v-model="state.ruleForm.snapshotLabel"
					multiple
					collapse-tags
					collapse-tags-tooltip
					:max-collapse-tags="3"
					placeholder="请选择工单标记"
					value-key="dicDataValue"
					clearable
					@change="handleSnapshotLabel"
				>
					<el-option v-for="item in snapshotLabel" :key="item.dicDataValue" :label="item.dicDataName" :value="item" class="w100" />
				</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="loading">确 定</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue';
import { ElMessage, FormInstance } from 'element-plus';
import { throttle } from '@/utils/tools';
import { centerMarkDetail, centerMarkUpdate } from '@/api/snapshot/centerMark';
// 定义子组件向父组件传值/事件
const emit = defineEmits(['updateList']);

// 定义变量内容
const state = reactive<any>({
	dialogVisible: false,
	ruleForm: {
		snapshotLabel: [],
	},
});
let loading = ref<boolean>(false); // 加载状态
// 打开弹窗
const ruleFormRef = ref<RefType>();
const snapshotLabel = ref<EmptyArrayType>([]);
const orderTitle = ref<string>('');
const openDialog = (val: any) => {
	state.dialogVisible = true;
	getDetail(val.orderId);
};
// 获取详情
const getDetail = async (id: string) => {
	loading.value = true;
	try {
		const { result } = await centerMarkDetail(id);
		snapshotLabel.value = result.labelsBaseData;
		orderTitle.value = result.title;
		state.ruleForm = {
			snapshotLabel: result.lables?.map((item: any) => {
				return {
					dicDataValue: item.key,
					dicDataName: item.value,
				};
			}),
			id: result.id,
		};
		loading.value = false;
	} catch (e) {
		console.log(e);
		loading.value = false;
	}
};
// 关闭弹窗
const closeDialog = () => {
	state.dialogVisible = false;
};
const close = () => {
	ruleFormRef.value?.resetFields();
	ruleFormRef.value?.resetFields();
};
// 选择工单标记、
const handleSnapshotLabel = (val: any) => {
	state.ruleForm.snapshotLabels = val.map((item: any) => {
		return {
			key: item.dicDataValue,
			value: item.dicDataName,
		};
	});
};
// 新增
const onSubmit = throttle(async (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	await formEl.validate((valid: boolean) => {
		if (!valid) return;
		loading.value = true;
		centerMarkUpdate(state.ruleForm)
			.then(() => {
				ElMessage({
					message: '修改成功',
					type: 'success',
				});
				emit('updateList');
			})
			.catch(() => {})
			.finally(() => {
				loading.value = false;
				closeDialog();
			});
	});
}, 300);
// 暴露变量
defineExpose({
	openDialog,
	closeDialog,
});
</script>
