<template>
	<el-dialog v-model="state.dialogVisible" draggable title="线索核实" width="500px" append-to-body destroy-on-close @close="close">
		<div class="collapse-container">
			<el-form label-width="110px" ref="ruleFormRef" :model="state.ruleForm">
				<el-form-item label="线索是否属实" prop="isTheClueTrue" :rules="[{ required: true, message: '请选择线索是否属实', trigger: 'change' }]">
					<el-radio-group v-model="state.ruleForm.isTheClueTrue" @change="change">
						<el-radio :value="true">是</el-radio>
						<el-radio :value="false">否</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item
					label="事项类型"
					prop="eventTypeId"
					:rules="[{ required: true, message: '请选择事项类型', trigger: 'change' }]"
					v-if="state.ruleForm.isTheClueTrue"
				>
					<el-tree-select
						v-model="state.ruleForm.eventTypeId"
						:data="treeList"
						:props="{
							label: 'eventTypeName',
							value: 'id',
							children: 'children',
						}"
						class="w100"
						ref="treeSelectRef"
						@change="changeTree"
						filterable
						default-expand-all
						clearable
						node-key="id"
					/>
				</el-form-item>
			</el-form>
		</div>
		<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="SpecialDialog">
import { reactive, ref } from 'vue';
import { ElMessage, FormInstance } from 'element-plus';
import { getCluesBaseData, getClues } from '@/api/judicial';
import { isArray } from '@/utils/tools';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['updateList']);
// 定义变量内容
const state = reactive<any>({
	dialogVisible: false, // 是否显示弹窗
	loading: false, // 是否显示加载
	ruleForm: {
		isTheClueTrue: null, // 线索是否属实
		eventTypeId: null, // 事项类型
		eventTypeName: null, // 事项类型名称
		eventTypeSpliceName: null, // 事项类型全名
	},
});
const ruleFormRef = ref<RefType>();
const treeList = ref<EmptyArrayType>([]); // 处理对象
const ids = ref<EmptyArrayType>([]); //
/**
 * @description 打开弹窗
 * @param {object} val 工单详情
 */
const openDialog = async (val: any) => {
	if (isArray(val)) {
		ids.value = val.map((item: any) => item.id);
	} else {
		ids.value = [val.id];
		if (val.eventTypeName !== null) {
			state.ruleForm.isTheClueTrue = val.isTheClueTrue;
			if (val.isTheClueTrue) {
				state.ruleForm.eventTypeId = val.eventTypeId;
				state.ruleForm.eventTypeName = val.eventTypeName;
				state.ruleForm.eventTypeSpliceName = val.eventTypeSpliceName;
			}
		} else {
			state.ruleForm.isTheClueTrue = null;
			state.ruleForm.eventTypeId = null;
			state.ruleForm.eventTypeName = null;
		}
	}
	state.loading = true;
	try {
		const baseRes = await getCluesBaseData();
		treeList.value = baseRes.result ?? [];
		state.dialogVisible = true;
	} catch (e) {
		console.log(e);
	} finally {
		state.loading = false;
	}
};
// 关闭弹窗
const closeDialog = () => {
	state.dialogVisible = false;
};
const close = () => {
	ruleFormRef.value?.clearValidate();
	ruleFormRef.value?.resetFields();
};
const change = (val) => {
	if (!val) {
		ruleFormRef.value.resetFields('eventTypeId');
		state.ruleForm.eventTypeId = null;
		state.ruleForm.eventTypeName = null;
	}
};
const treeSelectRef = ref<RefType>();
const changeTree = () => {
	const nodes = treeSelectRef.value?.getCurrentNode();
	if (nodes) {
		state.ruleForm.eventTypeName = nodes.eventTypeName;
		state.ruleForm.eventTypeSpliceName = nodes.eventTypeFullName;
	}
};
// 提交
const onSubmit = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.validate((valid: boolean) => {
		if (!valid) return;
		state.loading = true;
		const request = {
			orderIds: ids.value,
			isTheClueTrue: state.ruleForm.isTheClueTrue,
			eventTypeId: state.ruleForm.eventTypeId,
			eventTypeName: state.ruleForm.eventTypeName,
			eventTypeSpliceName: state.ruleForm.eventTypeSpliceName,
		};
		getClues(request)
			.then(() => {
				state.loading = false;
				closeDialog();
				emit('updateList');
				ElMessage.success('线索核实成功');
			})
			.catch(() => {
				state.loading = false;
			});
	});
};
defineExpose({
	openDialog,
	closeDialog,
});
</script>
