<template>
	<el-dialog v-model="state.dialogVisible" draggable title="服务企业查找" ref="dialogRef" append-to-body destroy-on-close>
		<el-form :model="state.queryParams" ref="ruleFormRef" 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(ruleFormRef)" class="default-button" :loading="state.loading">
					<SvgIcon name="ele-Refresh" class="mr5" />重置
				</el-button>
			</el-form-item>
		</el-form>
		<vxe-table
			border
			:loading="state.loading"
			:data="state.tableData"
			:sort-config="{ remote: true }"
			:column-config="{ resizable: true }"
			:row-config="{ isHover: true, height: 30, useKey: true }"
			ref="tableRef"
			max-height="500px"
			show-overflow
			:scrollY="{ enabled: true, gt: 100 }"
			@radio-change="radioChangeEvent"
			:radio-config="{ highlight: true }"
		>
			>
			<vxe-column type="radio" fixed="left" title="请选择" width="90" align="center"></vxe-column>
			<vxe-column field="enterpriseName" title="企业名称"></vxe-column>
			<vxe-column field="specialTeamName" title="专班名称"></vxe-column>
		</vxe-table>
		<pagination
			@pagination="queryList"
			:total="state.total"
			v-model:current-page="state.queryParams.PageIndex"
			v-model:page-size="state.queryParams.PageSize"
			:disabled="state.loading"
		/>
		<template #footer>
			<span class="dialog-footer">
				<el-button class="default-button" @click="state.dialogVisible = false">取 消</el-button>
				<el-button type="primary" @click="selectConfirm" :disabled="!selectRow">确 定</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="tsx" name="orderCompanySearch">
import { defineAsyncComponent, reactive, ref } from 'vue';
import type { FormInstance } from 'element-plus';
import { companyInfo } from '@/api/business/order';

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

// 引入组件
const emit = defineEmits(['selectCompany']);
// 定义变量内容
const state = reactive<any>({
	dialogVisible: false, // 弹窗显示隐藏
	queryParams: {
		PageIndex: 1, // 当前页
		PageSize: 10, // 每页条数
		Keyword: null, // 关键字
	},
	tableData: [], // 表格数据
	total: 0, // 总条数
	loading: false, // 加载状态
});
const ruleFormRef = ref<RefType>(); // 表单ref
// 打开弹窗
const openDialog = () => {
	queryList();
	state.dialogVisible = true;
};
const dialogRef = ref<RefType>();
// 关闭弹窗
const closeDialog = () => {
	state.dialogVisible = false;
};
/** 重置按钮操作 */
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	queryList();
};
// 手动查询，将页码设置为1
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
/** 获取历史工单 */
const queryList = () => {
	state.loading = true;
	companyInfo(state.queryParams)
		.then((response: any) => {
			state.tableData = response?.result.records ?? [];
			state.total = response?.result.total;
			state.loading = false;
		})
		.catch(() => {
			state.loading = false;
		});
};
const tableRef = ref<RefType>();
const selectRow = ref<any>(null);
const radioChangeEvent = ({ row }) => {
	selectRow.value = row;
};
// 确定选择历史工单
const selectConfirm = () => {
	emit('selectCompany', selectRow.value);
	state.dialogVisible = false;
};
// 暴露变量
defineExpose({
	openDialog,
	closeDialog,
});
</script>
