<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" class="keyword-input" />
		</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>
	<!--	<vxe-toolbar
		ref="toolbarRef"
		:loading="state.loading"
		custom
		:refresh="{
			queryMethod: handleQuery,
		}"
	>
	</vxe-toolbar>-->
	<vxe-table
		border
		:loading="state.loading"
		:data="state.tableData"
		:column-config="{ resizable: true }"
		:row-config="{ isCurrent: true, isHover: true, height: 30, useKey: true }"
		ref="tableRef"
		show-overflow
		:scrollY="{ enabled: true, gt: 100 }"
		:max-height="500"
	>
		<vxe-column field="no" title="工单编码" width="140"></vxe-column>
		<vxe-column field="currentStepName" title="当前节点" width="100"></vxe-column>
		<vxe-column field="isScreen" title="甄别状态" width="100">
			<template #default="{ row }">
				<el-text :type="row.isScreen ? 'danger' : 'info'">{{ row.isScreen ? '已甄别' : '-' }}</el-text>
			</template>
		</vxe-column>
		<vxe-column field="title" title="工单标题" min-width="150">
			<template #default="{ row }">
				<order-detail :order="row" @updateList="searchHistory">{{ row.title }}</order-detail>
			</template>
		</vxe-column>
		<vxe-column field="statusText" title="状态" width="100"></vxe-column>
		<vxe-column field="actualHandleOrgName" title="接办部门" width="140"></vxe-column>
	</vxe-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="orderDetailHistory">
import { reactive, ref, defineAsyncComponent, onMounted } from 'vue';
import { FormInstance } from 'element-plus';
import { throttle } from '@/utils/tools';
import { historyOrder } from '@/api/business/order';
import { useRoute } from 'vue-router';

const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页
const OrderDetail = defineAsyncComponent(() => import('@/components/OrderDetail/index.vue')); // 工单详情

const props = defineProps({
	orderId: {
		type: String,
		default: '',
	},
	ruleForm: {
		// 表单填写的数据
		type: Object,
		default: () => {
			return {};
		},
	},
	readonly: {
		//只读  不允许操作
		type: Boolean,
		default: false,
	},
});
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 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;
		state.loading = false;
	} catch (error) {
		state.loading = false;
	}
}, 300);
/*const toolbarRef = ref<RefType>();
const tableRef = ref<RefType>();
onMounted(() => {
	if (tableRef.value && toolbarRef.value) {
		tableRef.value.connect(toolbarRef.value);
	}
});*/
const route = useRoute();
onMounted(() => {
	if (route.query.createBy || route.query.createBy === 'tel' || route.query.createBy === 'transfer') {
		searchHistory();
	}
});
defineExpose({
	multipleSelection,
	searchHistory,
});
</script>
