<template>
	<el-dialog v-model="state.dialogVisible" draggable title="服务企业查找" ref="dialogRef" width="800px" append-to-body destroy-on-close>
		<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"> <SvgIcon name="ele-Search" class="mr5" />查询 </el-button>
				<el-button @click="resetQuery(ruleFormRef)" 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 label="选择" width="70">
				<template #default="{ row }">
					<el-radio v-model="tableRadio" :label="row.id" @change="handleRowChange(row)">&nbsp;</el-radio>
				</template>
			</el-table-column>
			<el-table-column prop="phoneNo" label="企业名称" show-overflow-tooltip></el-table-column>
			<el-table-column prop="hotspotName" label="专班名称" show-overflow-tooltip> </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="queryList" />
		<template #footer>
			<span class="dialog-footer">
				<el-button class="default-button" @click="state.dialogVisible = false">取 消</el-button>
				<el-button type="primary" @click="selectConfirm" :disabled="!tableRadio">确 定</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts" name="orderCompanySearch">
import {reactive, ref} from 'vue';
import type {FormInstance} from 'element-plus';
import {historyOrder} from '/@/api/business/order';
import {useRoute} from 'vue-router';
// 引入组件
const emit = defineEmits(['saveSelect']);
// 定义变量内容
const state = reactive<any>({
	dialogVisible: false, // 弹窗显示隐藏
	queryParams: {
		PageIndex: 1, // 当前页
		PageSize: 10, // 每页条数
		Keyword: null, // 关键字
	},
	tableData: [], // 表格数据
	total: 0, // 总条数
	loading: false, // 加载状态
});
const ruleFormRef = ref<RefType>(); // 表单ref
const tableRadio = ref<String>(''); // 选择的单选框
const selectRow = ref<EmptyObjectType>({}); // 选择的行
const route = useRoute(); // 路由
// 打开弹窗
const openDialog = (row: any) => {
	// tableRadio.value = row.duplicateId ?? '';
	// queryList();
	state.dialogVisible = true;
};
const dialogRef = ref<RefType>();
// 关闭弹窗
const closeDialog = () => {
	state.dialogVisible = false;
};
/** 搜索按钮操作 */
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	// queryList();
};
/** 重置按钮操作 */
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	handleQuery();
};
/** 获取历史工单 */
const queryList = () => {
	state.loading = true;
	let request = {
		...state.queryParams,
		PhoneNo: state.ruleForm.contact,
		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) {
		tableRadio.value = row.id;
		selectRow.value = row;
	}
};
const handleRowChange = (row: any) => {
	tableRadio.value = row.id;
	selectRow.value = row;
};

// 确定选择历史工单
const selectConfirm = () => {
	emit('saveSelect', state.selectRow);
};
// 暴露变量
defineExpose({
	openDialog,
	closeDialog,
});
</script>