<template>
	<el-form :model="state.queryParams" ref="queryParamsRef" inline @submit.native.prevent>
		<el-form-item label="关键词" prop="Keyword">
			<el-input v-model="state.queryParams.Keyword" placeholder="工单标题/工单编码" clearable @keyup.enter="handleQuery" />
		</el-form-item>
		<el-form-item>
			<el-button type="primary" @click="handleQuery" :loading="state.loading"> <SvgIcon name="ele-Search" class="mr5" />查询 </el-button>
			<el-button @click="resetQuery(queryParamsRef)" :loading="state.loading" class="default-button">
				<SvgIcon name="ele-Refresh" class="mr5" />重置
			</el-button>
		</el-form-item>
	</el-form>
	<el-table
		:data="state.tableData"
		v-loading="state.loading"
		row-key="id"
		@selection-change="handleSelectionChange"
		:max-height="props.maxHeight"
		ref="multipleTableRef"
	>
		<el-table-column type="selection" width="80" :reserve-selection="true" v-if="props.ruleForm.isRepeat === 'true'" />
		<el-table-column prop="phoneNo" label="工单标题" show-overflow-tooltip width="180">
			<template #default="{ row }">
				{{ row.title }}
			</template>
		</el-table-column>
		<el-table-column prop="hotspotName" label="热点分类" show-overflow-tooltip> </el-table-column>
		<el-table-column prop="no" label="工单编码" show-overflow-tooltip> </el-table-column>
		<el-table-column prop="actualHandleStepName" label="当前环节" show-overflow-tooltip></el-table-column>
		<el-table-column prop="statusText" label="状态" width="100" fixed="right" align="center"></el-table-column>
	</el-table>
  <pagination
    @pagination="searchHistory"
    :total="state.total"
    v-model:current-page="state.queryParams.PageIndex"
    v-model:page-size="state.queryParams.PageSize"
  />
</template>
<script setup lang="ts" name="orderAcceptHistory">
import { reactive, ref, watch,defineAsyncComponent } from 'vue';
import { FormInstance } from 'element-plus';
import { throttle } from '@/utils/tools';
import { historyOrder } from '@/api/business/order';

const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页

const props = defineProps({
	orderId: {
		type: String,
		default: '',
	},
	ruleForm: {
		// 表单填写的数据
		type: Object,
		default: () => {
			return {};
		},
	},
	maxHeight: {
		type: [Number, String],
		default: 300,
	},
	readonly: {
		//只读  不允许操作
		type: Boolean,
		default: false,
	},
});
const emit = defineEmits(['handleSelectionChange']);
const state = reactive<any>({
	tableData: [], // 历史工单
	total: 0, // 历史工单总条数
	queryParams: {
		PageIndex: 1, // 当前页
		PageSize: 10, // 每页条数
		Keyword: null, // 关键字
	},
	loading: false,
});
const queryParamsRef = ref<RefType>(); // 历史工单查询参数
const multipleSelection = ref<any[]>([]); // 重复件表格选中项
const multipleTableRef = ref<RefType>(); // 重复件表格ref
/** 搜索按钮操作 */
const handleQuery = throttle(() => {
	state.queryParams.PageIndex = 1;
	searchHistory();
}, 500);
/** 重置按钮操作 */
const resetQuery = throttle((formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	handleQuery();
}, 300);
/** 获取历史工单 */
const searchHistory = throttle(async () => {
	if (!props.ruleForm.contact) return;
	state.loading = true;
	let request = {
		...state.queryParams,
		PhoneNo: props.ruleForm.contact,
		OrderId: props.orderId, //传入id 排除重复工单选择自己
	};
	try {
		const response = await historyOrder(request);
		state.tableData = response?.result.items ?? [];
		state.total = response?.result.total;
		if (props.ruleForm.id) {
			if (props.ruleForm.duplicateIds && props.ruleForm.duplicateIds.length) {
				for (let i of props.ruleForm.duplicateIds) {
					for (let j of state.tableData) {
						if (i === j.id) {
							setTimeout(() => {
								multipleTableRef.value!.toggleRowSelection(j, true);
								multipleSelection.value.push(j);
							}, 100);
						}
					}
				}
			}
		}
		state.loading = false;
	} catch (error) {
		state.loading = false;
	}
}, 300);
// 右边表格选中重复件
const handleSelectionChange = (row: any) => {
	if (row && row.length) {
		multipleSelection.value = row;
	} else {
		multipleSelection.value = [];
	}
	emit('handleSelectionChange', multipleSelection.value);
};
// 弹窗选择历史工单（保持选择一致）
const dialogConfirmRepeat = (val: any) => {
	multipleSelection.value = val;
	for (let i of multipleSelection.value) {
		for (let j of state.tableData) {
			if (i.id === j.id) {
				setTimeout(() => {
					multipleTableRef.value!.toggleRowSelection(j, true);
				}, 0);
			}
		}
	}
};
// 清除重复选择
const clearRepeat = () => {
	multipleTableRef.value!.clearSelection();
};
watch(
	() => props.ruleForm.contact,
	(newVal: any) => {
		if (newVal) {
			searchHistory();
		}
	},
	{ immediate: true }
);
defineExpose({
	multipleSelection,
	multipleTableRef,
	searchHistory,
	dialogConfirmRepeat,
	clearRepeat,
});
</script>
