<template>
	<el-dialog v-model="state.dialogVisible" draggable title="外呼明细" destroy-on-close append-to-body @close="close">
		<el-form :model="state.queryParams" ref="ruleFormRef" @submit.native.prevent inline>
			<el-form-item label="市民姓名" prop="Name">
				<el-input v-model="state.queryParams.Name" placeholder="请填写市民姓名" clearable @keyup.enter="handleQuery" class="keyword-input"/>
			</el-form-item>
			<el-form-item label="联系电话" prop="OuterNo">
				<el-input v-model="state.queryParams.OuterNo" placeholder="请填写市民联系电话" clearable @keyup.enter="handleQuery"  class="keyword-input"/>
			</el-form-item>
			<el-form-item label="外呼状态" prop="AiCallOutState">
				<el-select v-model="state.queryParams.AiCallOutState" placeholder="请选择外呼状态" @change="handleQuery" class="w100">
					<el-option v-for="item in aiCallOutStateArr" :value="item.key" :key="item.key" :label="item.value" />
				</el-select>
			</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>
		<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="500px"
		>
			<vxe-column field="name" title="市民姓名"></vxe-column>
			<vxe-column field="outerNo" title="联系电话"></vxe-column>
			<vxe-column field="aiCallOutStateText" 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 @click="closeDialog" class="default-button">关 闭</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="tsx">
import { defineAsyncComponent, reactive, ref } from 'vue';
import { FormInstance } from 'element-plus';
import { smartCallOutTaskBaseData, smartCallOutTaskDetailList } from '@/api/auxiliary/smartCallOut';

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

// 定义变量内容
const state = reactive<any>({
	dialogVisible: false,
	queryParams: {
		// 查询参数
		PageIndex: 1,
		PageSize: 10,
		OuterNo: null,
		Name: null,
		id: null,
		AiCallOutState: null,
	},
	total:0,
	tableData: [],
});
const close = () => {
	ruleFormRef.value?.clearValidate();
	ruleFormRef.value?.resetFields();
};
const ruleFormRef = ref<RefType>(null); // 表单ref
/** 搜索按钮操作 */
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
// 获取列表
const queryList = () => {
	state.loading = true;
	smartCallOutTaskDetailList(state.queryParams)
		.then((res) => {
			state.loading = false;
			state.tableData = res.result.items ?? [];
			state.total = res.result.total ?? 0;
		})
		.finally(() => {
			state.loading = false;
		});
};
// 重置表单
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	queryList();
};
// 获取基础数据
const aiCallOutStateArr = ref<EmptyArrayType>([]);
const getBaseData = async () => {
	try {
		const { result } = await smartCallOutTaskBaseData();
		aiCallOutStateArr.value = result.aiCallOutState;
	} catch (e) {
		console.log(e);
	}
};
// 打开弹窗
const openDialog = async (row: any) => {
	state.queryParams.id = row.id;
	state.dialogVisible = true;
	queryList();
	await getBaseData();
};
// 关闭弹窗
const closeDialog = () => {
	state.dialogVisible = false;
};
// 暴露变量
defineExpose({
	openDialog,
	closeDialog,
});
</script>
