<template>
	<div class="dataShare-pushedTask-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<el-tabs v-model="state.queryParams.SelectType" @tab-change="handleQuery">
				<el-tab-pane name="1" label="服务工单受理" :disabled="state.loading"></el-tab-pane>
				<el-tab-pane name="2" label="电话记录" :disabled="state.loading"></el-tab-pane>
				<el-tab-pane name="3" label="服务工单处理过程" :disabled="state.loading"></el-tab-pane>
				<el-tab-pane name="4" label="服务工单处理结果" :disabled="state.loading"></el-tab-pane>
				<el-tab-pane name="5" label="省平台推送其他" :disabled="state.loading"></el-tab-pane>
				<el-tab-pane name="6" label="其他平台" :disabled="state.loading"></el-tab-pane>
			</el-tabs>
			<el-form :model="state.queryParams" ref="ruleFormRef" @submit.native.prevent inline>
				<el-form-item label="省本地编码" prop="CaseSerial">
					<el-input v-model="state.queryParams.CaseSerial" placeholder="省本地编码" clearable @keyup.enter="handleQuery" class="keyword-input" />
				</el-form-item>
				<el-form-item label="推送状态" prop="IsSuccess">
					<el-select v-model="state.queryParams.IsSuccess" placeholder="请选择推送状态" clearable @change="handleQuery">
						<el-option :value="true" label="推送成功" />
						<el-option :value="false" label="推送失败" />
					</el-select>
				</el-form-item>
				<el-form-item label="平台名称" prop="PlatformSource" v-if="['6'].includes(state.queryParams.SelectType)">
					<el-select v-model="state.queryParams.PlatformSource" placeholder="请选择平台名称" clearable @change="handleQuery">
						<el-option :value="item.key" :label="item.value" v-for="item in ePlatformSource" />
					</el-select>
				</el-form-item>
				<el-form-item label="任务类型" prop="type" v-if="['5'].includes(state.queryParams.SelectType)">
					<el-cascader v-model="state.queryParams.type" :options="options" clearable placeholder="请选择任务类型" @change="selectPath" filterable />
				</el-form-item>
				<el-form-item label="时间段" prop="crTime">
					<el-date-picker
						v-model="state.queryParams.crTime"
						type="datetimerange"
						unlink-panels
						range-separator="至"
						start-placeholder="开始时间"
						end-placeholder="结束时间"
						:shortcuts="shortcuts"
						@change="handleQuery"
						value-format="YYYY-MM-DD[T]HH:mm:ss"
						:default-time="defaultTimeStartEnd"
						:clearable="false"
					/>
				</el-form-item>
				<el-form-item label-width="0">
					<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-toolbar
				ref="toolbarRef"
				:loading="state.loading"
				custom
				:refresh="{
					queryMethod: handleQuery,
				}"
			>
			</vxe-toolbar>
			<div style="overflow: hidden; width: 100%; height: 100%; flex: 1">
				<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"
					height="auto"
					auto-resize
					show-overflow
					:scrollY="{ enabled: true, gt: 100 }"
					id="dataSharePushedTask"
					:custom-config="{ storage: true }"
				>
					<vxe-column field="provinceNo" title="省本地编码" min-width="220"></vxe-column>
					<template v-if="['1', '4'].includes(state.queryParams.SelectType)">
						<vxe-column field="expiredTime" title="期满时间" width="160">
							<template #default="{ row }">
								{{ formatDate(row.expiredTime, 'YYYY-mm-dd HH:MM:SS') }}
							</template>
						</vxe-column>
					</template>
					<template v-if="['4'].includes(state.queryParams.SelectType)">
						<vxe-column field="filedTime" title="办结时间" width="160">
							<template #default="{ row }">
								{{ formatDate(row.filedTime, 'YYYY-mm-dd HH:MM:SS') }}
							</template>
						</vxe-column>
						<vxe-column field="isOverdueText" title="是否超期" min-width="110"></vxe-column>
					</template>
					<vxe-column field="firstTime" title="初次推送时间" width="160">
						<template #default="{ row }">
							{{ formatDate(row.firstTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column field="lastTime" title="最近一次推送时间" width="160">
						<template #default="{ row }">
							{{ formatDate(row.lastTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column field="sendTimes" title="推送次数" width="100"></vxe-column>
					<vxe-column field="isSuccesseText" title="推送状态" width="100"></vxe-column>
					<vxe-column field="platformSourceText" title="平台名称" width="100"></vxe-column>
					<vxe-column field="pathText" title="任务类型" width="200"></vxe-column>
					<vxe-column field="request" title="请求参数" min-width="200"></vxe-column>
					<vxe-column field="generationTime" title="生成时间" width="160">
						<template #default="{ row }">
							{{ formatDate(row.generationTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column field="creationTime" title="创建时间" width="160">
						<template #default="{ row }">
							{{ formatDate(row.creationTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column title="操作" width="140" fixed="right" :show-overflow="false">
						<template #default="{ row }">
							<el-button link type="primary" @click="onDetail(row)" title="查看任务明细"> 任务明细 </el-button>
							<el-button link type="primary" @click="onRePush(row)" title="重新推送"> 重推 </el-button>
						</template>
					</vxe-column>
				</vxe-table>
			</div>
			<pagination
				@pagination="queryList"
				:total="state.total"
				v-model:current-page="state.queryParams.PageIndex"
				v-model:page-size="state.queryParams.PageSize"
				:disabled="state.loading"
			/>
		</div>
	</div>
</template>
<script setup lang="tsx" name="dataSharePushedTask">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { ElMessage, ElMessageBox, FormInstance } from 'element-plus';
import { defaultDateTime, defaultTimeStartEnd, shortcuts } from '@/utils/constants';
import { getDataShareBaseData, getPushedTask, rePushNew } from '@/api/dataShare';
import { formatDate } from '@/utils/formatTime';
import { useRouter } from 'vue-router';
import other from '@/utils/other';

const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页
// 定义变量内容
const ruleFormRef = ref<RefType>(); // 表单ref
const state = reactive<any>({
	queryParams: {
		// 查询条件
		PageIndex: 1,
		PageSize: 20,
		Id: null, //
		Keyword: null,
		IsSuccess: null,
		crTime: defaultDateTime,
		SelectType: '1',
		StartTime: null,
		EndTime: null,
		CaseSerial: null,
		Path: null,
		PlatformSource: null,
		type: null,
	},
	tableData: [], //表单
	loading: false, // 加载
	total: 0, // 总数
});
/** 搜索按钮操作 */
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
/** 获取列表 */
const queryList = () => {
	state.loading = true;
	let request = other.deepClone(state.queryParams);
	request.StartTime = state.queryParams.crTime === null ? null : state.queryParams.crTime[0];
	request.EndTime = state.queryParams.crTime === null ? null : state.queryParams.crTime[1];
	Reflect.deleteProperty(request, 'crTime');
	Reflect.deleteProperty(request, 'type');
	getPushedTask(request)
		.then((res: any) => {
			state.tableData = res.result?.items ?? [];
			state.total = res.result?.total ?? 0;
			state.loading = false;
		})
		.catch(() => {
			state.loading = false;
		});
};
const options = [
	{
		value: 'ywhj',
		label: '业务汇聚',
		children: [
			{
				value: 'submit_case_info',
				label: '服务工单受理接口',
			},
			{
				value: 'submit_case_record',
				label: '电话记录',
			},
			{
				value: 'submit_case_process',
				label: '服务工单处理过程接口',
			},
			{
				value: 'submit_case_result',
				label: '服务工单处理结果接口',
			},
			{
				value: 'submit_visit_info',
				label: '服务工单回访评价接口',
			},
			{
				value: 'submit_case_extends',
				label: '服务工单拓展信息接口',
			},
			{
				value: 'get_case_material_info',
				label: '材料接口',
			},
			{
				value: 'submit_case_total',
				label: '统计数据接口',
			},
			{
				value: 'sendrealtimestatus',
				label: '实时服务上报接口',
			},
		],
	},
	{
		value: 'ywxt',
		label: '业务协同',
		children: [
			{
				value: 'get_case_back_apply',
				label: '退回申请接口',
			},
			{
				value: 'send_supervise_process_info',
				label: '服务工单督办过程接口',
			},
			{
				value: 'send_supervise_result_info',
				label: '服务工单督办结果接口',
			},
			{
				value: 'screen_case_info_send',
				label: '工单发起甄别接口',
			},
			{
				value: 'screen_case_info_send',
				label: '工单发起甄别接口',
			},
			{
				value: 'delay_case_info_send',
				label: '服务工单申请延时接口',
			},
			{
				value: 'get_case_process_receive',
				label: '服务工单交办处理过程接口',
			},
			{
				value: 'supply_case_info_send',
				label: '上报补充诉求接口',
			},
			{
				value: 'revoke_case_info',
				label: '服务工单撤单接口',
			},
			{
				value: 'remind_case_info',
				label: '服务工单催单接口',
			},
			{
				value: 'get_case_result_receive',
				label: '服务工单交办处理结果接口',
			},
			{
				value: 'zmhd_case_info_public',
				label: '政民互动提交公开',
			},
			{
				value: 'get_knowledge_info_send',
				label: '知识库上报接口',
			},
			{
				value: 'get_knowledge_info_update',
				label: '知识库变更接口',
			},
			{
				value: 'get_knowledge_info_abandon',
				label: '知识库下架接口',
			},
			{
				value: 'get_visit_info_receive',
				label: '服务工单交办评价接口',
			},
			{
				value: 'send_case_info',
				label: '服务工单上报',
			},
		],
	},
];
/** 重置按钮操作 */
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	queryList();
};
// 查看任务明细
const router = useRouter();
const onDetail = (row: any) => {
	router.push({
		name: 'dataShareTaskDetail',
		params: {
			id: row.id,
		},
	});
};
// 重推
const onRePush = (row: any) => {
	ElMessageBox.confirm(`确定重新推送省本地编码为【${row.provinceNo}】吗？`, '提示', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning',
		draggable: true,
	})
		.then(() => {
			rePushNew({ id: row.id, Type: state.queryParams.SelectType })
				.then(() => {
					ElMessage({
						message: '操作成功',
						type: 'success',
					});
					queryList();
				})
				.catch(() => {});
		})
		.catch(() => {});
};
// 获取基础数据
const ePlatformSource = ref<EmptyArrayType>([]); // 基础数据
const getBaseData = async () => {
	try {
		const { result } = await getDataShareBaseData();
		ePlatformSource.value = result?.ePlatformSource ?? [];
	} catch (e) {
		console.log(e);
	}
};
// 选择任务类型
const selectPath = (val: any) => {
	if (val) {
		state.queryParams.path = val[1];
		handleQuery();
	} else {
		state.queryParams.path = null;
		handleQuery();
	}
};
const toolbarRef = ref<RefType>();
const tableRef = ref<RefType>();
onMounted(() => {
	getBaseData();
	if (tableRef.value && toolbarRef.value) {
		tableRef.value.connect(toolbarRef.value);
	}
	queryList();
});
</script>
