<template>
	<div class="">
		<el-dialog v-model="state.isShowDialog" draggable title="历史工单" ref="dialogRef" width="60%" append-to-body>
			<div>
				<el-form :model="state.queryParams" ref="ruleFormRef" :inline="true" @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" v-waves>
							<SvgIcon name="ele-Search" class="mr5" />查询
						</el-button>
						<el-button @click="resetQuery(ruleFormRef)" v-waves class="default-button"> <SvgIcon name="ele-Refresh" class="mr5" />重置 </el-button>
					</el-form-item>
				</el-form>
				<el-table :data="state.tableData" @current-change="handleSelectionChange">
					<el-table-column prop="phoneNo" label="重复件" width="70">
						<template #default="scope">
							<el-radio v-model="state.tableRadio" :label="scope.row.id" @change="handleRowChange(scope.row)">&nbsp;</el-radio>
						</template>
					</el-table-column>
					<el-table-column prop="phoneNo" label="工单标题" show-overflow-tooltip>
						<template #default="scope">
							<span style="color: var(--el-color-primary)">{{ scope.row.title }}</span>
						</template>
					</el-table-column>
					<el-table-column prop="hotspot" label="热点分类" show-overflow-tooltip> </el-table-column>
					<el-table-column prop="no" label="工单编码" show-overflow-tooltip> </el-table-column>
					<el-table-column prop="currentStepName" label="当前环节" show-overflow-tooltip></el-table-column>
					<el-table-column prop="statusText" label="状态" width="70" fixed="right" align="center">
						<template #default="scope">
							<span style="color: var(--el-color-primary)">{{ scope.row.statusText }}</span>
						</template>
					</el-table-column>
					<template #empty>
						<Empty />
					</template>
				</el-table>
				<!-- 分页 -->
				<pagination
					:total="state.total"
					v-model:page="state.queryParams.PageIndex"
					v-model:limit="state.queryParams.PageSize"
					@pagination="getList"
				/>
			</div>
			<template #footer>
				<span class="dialog-footer">
					<el-button class="default-button" @click="state.isShowDialog = false">关闭</el-button>
					<el-button type="primary" @click="selecConfrim" :disabled="!state.tableRadio">确 定</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="orderHistory">
import { reactive, ref } from 'vue';
import type { FormInstance } from 'element-plus';
import { historyOrder } from '/@/api/business/order';
import { useRoute } from 'vue-router';
// import { ElMessage } from 'element-plus';
const emit = defineEmits(['saveSlect']);
// 定义变量内容
const state = reactive<any>({
	isShowDialog: false,
	queryParams: {
		PageIndex: 1,
		PageSize: 10,
		Keyword: '',
	},
	tableData: [],
	total: 0,
	loading: false,
	tableRadio: null as any,
	selectRow: null as any,
	ruleForm: {},
});
const ruleFormRef = ref();
const route = useRoute();
// 打开弹窗
const openDialog = (row: any) => {
	state.ruleForm = row;
	state.tableRadio = row.duplicateId ?? '';
	getList();
	state.isShowDialog = true;
};
const dialogRef = ref();
// 关闭弹窗
const closeDialog = () => {
	state.isShowDialog = false;
};
/** 搜索按钮操作 */
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	getList();
};
/** 重置按钮操作 */
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	handleQuery();
};
/** 获取历史工单 */
const getList = () => {
	state.loading = true;
	let request = {
		...state.queryParams,
		PhoneNo: state.ruleForm.fromPhone,
		OrderId: route.params.id ?? '', //传入id 排除重复工单选择自己
	};
	historyOrder(request)
		.then((response: any) => {
			state.tableData = response?.result.items ?? [];
			state.total = response?.result.total;
			state.loading = false;
		})
		.catch(() => {
			state.loading = false;
		});
};
const handleSelectionChange = (row: any) => {
	if (row) {
		state.tableRadio = row.id;
		state.selectRow = row;
	}
};
const handleRowChange = (row: any) => {
	state.tableRadio = row.id;
	state.selectRow = row;
};
// 确定选择历史工单
const selecConfrim = () => {
	emit('saveSlect', state.selectRow);
};
// 暴露变量
defineExpose({
	openDialog,
	closeDialog,
});
</script>
<style lang="scss" scoped></style>
