<template>
	<div class="quality-template-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="width300"/>
				</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">
			<div class="mb20">
				<el-button type="primary" @click="onAddVisit" v-auth="'smart:visit:add'"> <SvgIcon name="ele-Plus" class="mr5" />新增 </el-button>
			</div>
			<!-- 表格 -->
			<el-table :data="state.tableData" v-loading="state.loading" row-key="id" ref="multipleTableRef" @selection-change="handleSelectionChange">
				<el-table-column type="selection" width="55" />
				<el-table-column prop="name" label="回访任务名称" show-overflow-toolti></el-table-column>
				<el-table-column prop="hasVisitCount" label="回访任务总数量" show-overflow-tooltip> </el-table-column>
				<el-table-column prop="visitedCount" label="回访任务成功数量" show-overflow-tooltip></el-table-column>
				<el-table-column prop="visitedFailCount" label="回访任务失败数量" show-overflow-tooltip></el-table-column>
				<el-table-column prop="taskStateText" label="任务完成状态" show-overflow-tooltip></el-table-column>
				<el-table-column prop="creatorName" label="创建人" show-overflow-tooltip></el-table-column>
				<el-table-column prop="creationTime" label="创建时间" show-overflow-tooltip width="170">
					<template #default="{ row }">
						<span>{{ formatDate(row.creationTime, 'YYYY-mm-dd HH:MM:SS') }}</span>
					</template>
				</el-table-column>
				<el-table-column label="操作" width="140" fixed="right" align="center">
					<template #default="{ 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>
				</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"
			/>
		</el-card>
		<!--  选择需要智能回访的工单  -->
		<smart-visit-add ref="smartVisitAddRef" @updateList="queryList" />
		<!--  回访明细  -->
		<smart-visit-detail ref="smartVisitDetailRef" @updateList="queryList" />
	</div>
</template>

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

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

// 定义变量内容
const state = reactive<any>({
	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;
	if (!auth('quality:template:query')) ElMessage.error('抱歉，您没有权限获取质检模板列表！');
	else {
		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 multipleTableRef = ref<RefType>();
const multipleSelection = ref<any>([]);
const handleSelectionChange = (val: any[]) => {
	multipleSelection.value = val;
};
// 新增回访任务
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>
