<template>
	<div class="business-visit-smart-container layout-pd">
		<el-card shadow="never">
			<el-form :model="state.queryParams" ref="ruleFormRef" inline @submit.native.prevent>
				<el-form-item label="关键字" prop="Name">
					<el-input v-model="state.queryParams.Name" placeholder="回访任务名称" clearable @keyup.enter="queryList" class="keyword-input" />
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="queryList" :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-card>
		<el-card shadow="never">
			<ProTable
				ref="proTableRef"
				:columns="columns"
				:data="state.tableData"
				@updateTable="queryList"
				:loading="state.loading"
				:total="state.total"
				v-model:page-index="state.queryParams.PageIndex"
				v-model:page-size="state.queryParams.PageSize"
			>
				<template #tableHeader="scope">
					<el-button type="primary" @click="onAddVisit" v-auth="'business:visit:smart:add'"> <SvgIcon name="ele-Plus" class="mr5" />新增 </el-button>
				</template>
				<!-- 表格操作 -->
				<template #operation="{ row }">
					<el-button link type="primary" @click="onDetail(row)" title="回访明细"> 回访明细 </el-button>
					<!--            <el-button link type="primary" @click="onTemplateEdit(row)" title="终止回访任务" v-auth="'smart:visit:end'"> 终止 </el-button>-->
				</template>
			</ProTable>
		</el-card>
		<!--  选择需要智能回访的工单  -->
		<smart-visit-add ref="smartVisitAddRef" @updateList="queryList" />
		<!--  回访明细  -->
		<smart-visit-detail ref="smartVisitDetailRef" @updateList="queryList" />
	</div>
</template>

<script lang="tsx" setup name="businessVisitSmart">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { ElButton, FormInstance } from 'element-plus';
import { formatDate } from '@/utils/formatTime';
import { getSmartVisitList } from '@/api/smartVisit';

// 引入组件
const SmartVisitAdd = defineAsyncComponent(() => import('@/views/business/visit/component/Smart-visit-add.vue')); // 选择需要智能回访的工单
const SmartVisitDetail = defineAsyncComponent(() => import('@/views/business/visit/component/Smart-visit-Detail.vue')); // 回访明细

const proTableRef = ref<RefType>(); // 表格ref
// 表格配置项
const columns = ref<any[]>([
	{ type: 'selection', fixed: 'left', width: 55 },
	{ prop: 'name', label: '回访任务名称' },
	{ prop: 'hasVisitCount', label: '回访任务总数量' },
	{ prop: 'visitedCount', label: '回访任务成功数量' },
	{ prop: 'visitedFailCount', label: '回访任务失败数量' },
	{ prop: 'taskStateText', label: '任务完成状态' },
	{ prop: 'creatorName', label: '创建人' },
	{
		prop: 'creationTime',
		label: '创建时间',
		width: 170,
		render: (scope: any) => {
			return <span>{formatDate(scope.row.creationTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
	{ prop: 'operation', label: '操作', fixed: 'right', width: 140, align: 'center' },
]);
// 定义变量内容
const state = reactive({
	loading: false, // 加载状态
	queryParams: {
		// 查询参数
		PageIndex: 1,
		PageSize: 10,
		Grouping: null,
		Name: null,
	},
	total: 0, // 总条数
	tableData: [], // 表格数据
});
const ruleFormRef = ref<RefType>(null); // 表单ref
// 获取参数列表
const queryList = () => {
	state.loading = true;
	getSmartVisitList(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 smartVisitAddRef = ref<RefType>();
const onAddVisit = () => {
	smartVisitAddRef.value.openDialog();
};
// 回访明细
const smartVisitDetailRef = ref<RefType>();
const onDetail = (row: any) => {
	smartVisitDetailRef.value.openDialog(row);
};
// 页面加载时
onMounted(() => {
	queryList();
});
</script>

<style lang="scss" scoped>
.quality-template-container {
}
</style>
