<template>
	<div class="snapshot-statistics-re-audit-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<vxe-grid v-bind="gridOptions" ref="gridRef">
				<template #form>
					<el-form :model="state.queryParams" ref="ruleFormRef" @submit.native.prevent inline :disabled="gridOptions.loading">
						<el-form-item prop="crTime">
							<statistical-time v-model="state.queryParams.crTime" @change="handleQuery" ref="statisticalTimeRef" :disabled="state.loading" />
						</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>
				</template>
				<template #toolbar_buttons>
					<el-popover :width="500" trigger="click">
						<template #reference>
							<el-button type="primary" title="字段说明"><SvgIcon name="ele-QuestionFilled" class="mr5" />字段说明</el-button>
						</template>
						<el-descriptions title="" :column="1" border style="max-height: 400px; overflow: auto">
							<el-descriptions-item label="安全隐患举报（10元"
								>获取行业类型为安全隐患的工单（剔除安全生产工单、电气焊作业举报工单）</el-descriptions-item
							>
							<el-descriptions-item label="安全隐患举报-安全生产（20元"> 行业类型为安全隐患且标记安全生产为【是】的工单 </el-descriptions-item>
							<el-descriptions-item label="城市管理举报（10元）">获取行业类型为城市管理的工单</el-descriptions-item>
							<el-descriptions-item label="环境保护举报（10元）">-获取行业类型为环境保护的工单</el-descriptions-item>
							<el-descriptions-item label="市场监管举报（10元）"> 获取行业类型为市场监管的工单 </el-descriptions-item>
							<el-descriptions-item label="交通管理设施隐患举报（10元）"> 获取行业类型为交通管理设施隐患的工单 </el-descriptions-item>
							<el-descriptions-item label="文化旅游举报（10元）"> 获取行业类型为文化旅游的工单 </el-descriptions-item>
							<el-descriptions-item label="电气焊作业申报（20元）">获取行业类型为电气焊作业申报的工单 </el-descriptions-item>
							<el-descriptions-item label="电气焊作业举报（20元）">获取行业类型为安全隐患，工单标签为动火作业的工单 </el-descriptions-item>
							<el-descriptions-item label="审批同意总金额">时间范围内审批同意的工单总金额</el-descriptions-item>
							<el-descriptions-item label="审批同意总个数">时间范围内审批同意的工单总个数</el-descriptions-item>
							<el-descriptions-item label="发送成功金额">时间范围内在审批同意列表标记【已发红包】的总金额</el-descriptions-item>
							<el-descriptions-item label="发送成功个数">时间范围内在审批同意列表标记【已发红包】的总个数</el-descriptions-item>
							<el-descriptions-item label="发送失败金额">时间范围内在审批同意列表标记【未发】的总金额</el-descriptions-item>
							<el-descriptions-item label="发送失败个数">时间范围内在审批同意列表标记【未发】的总个数</el-descriptions-item>
							<el-descriptions-item label="待发送红包金额">时间范围内审批同意尚未添加备注的工单总金额</el-descriptions-item>
							<el-descriptions-item label="待发送红包个数">时间范围内审批同意尚未添加备注的工单总个数</el-descriptions-item>
							<el-descriptions-item label="补充红包金额">时间范围内补充标记金额的工单总金额</el-descriptions-item>
							<el-descriptions-item label="补充红包个数">时间范围内补充标记金额的工单总个数</el-descriptions-item>
						</el-descriptions>
					</el-popover>
				</template>
				<template #industryName="{ row }">
					{{ row.industryName }}
				</template>
			</vxe-grid>
		</div>
	</div>
</template>

<script lang="tsx" setup name="snapshotStatisticsReAudit">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { FormInstance } from 'element-plus';
import { defaultDate } from '@/utils/constants';
import { getSSPStatisticsCitizenRedEnvelopeApproval, getSSPStatisticsCitizenRedEnvelopeApprovalExport } from '@/api/snapshot/statistics';
import Other from '@/utils/other';
import { useRouter } from 'vue-router';

// 引入组件
const StatisticalTime = defineAsyncComponent(() => import('@/components/StatisticalTime/index.vue')); // 日期类型选择组件

// 定义变量内容
const state = reactive<any>({
	loading: false,
	queryParams: {
		crTime: defaultDate, // 时间默认今天开始到今天结束
		StartTime: null,
		EndTime: null,
		IndustryId: null,
	},
});

const requestParams = ref<EmptyObjectType>({});
const gridOptions = reactive<any>({
	loading: false,
	border: true,
	showOverflow: true,
	columnConfig: {
		resizable: true,
	},
	scrollY: {
		enabled: true,
		gt: 100,
	},
	toolbarConfig: {
		zoom: true,
		custom: true,
		refresh: {
			queryMethod: () => {
				handleQuery();
			},
		},
		slots: {
			buttons: 'toolbar_buttons',
		},
		tools: [{ toolRender: { name: 'exportAll' } }],
	},
	customConfig: {
		storage: true,
	},
	id: 'snapshotStatisticsReAudit',
	rowConfig: { isHover: true, height: 30, isCurrent: true, useKey: true },
	height: 'auto',
	align: 'center',
	columns: [
		{
			field: 'industryName',
			title: '行业类型',
			width: 240,
			slots: { default: 'industryName' },
		},
		{
			title: '审批',
			children: [
				{
					field: 'approvalAmountTxt',
					title: '审批同意总金额',
				},
				{
					field: 'approvalCount',
					title: '审批同意总个数',
					slots: {
						default({ row }) {
							return (
								<el-button type="primary" onClick={() => linkDetail(row, 'approvalCount')} link>
									{row.approvalCount}
								</el-button>
							);
						},
					},
				},
			],
		},
		{
			title: '已发',
			children: [
				{
					field: 'sentAmountTxt',
					title: '发送成功金额',
				},
				{
					field: 'sentCount',
					title: '发送成功个数',
					slots: {
						default({ row }) {
							return (
								<el-button type="primary" onClick={() => linkDetail(row, 'sentCount')} link>
									{row.sentCount}
								</el-button>
							);
						},
					},
				},
				{
					field: 'sendFailAmountTxt',
					title: '发送失败金额',
				},
				{
					field: 'sendFailCount',
					title: '发送失败个数',
					slots: {
						default({ row }) {
							return (
								<el-button type="primary" onClick={() => linkDetail(row, 'sendFailCount')} link>
									{row.sendFailCount}
								</el-button>
							);
						},
					},
				},
			],
		},
		{
			title: '待发',
			children: [
				{
					field: 'pendingAmountTxt',
					title: '待发金额',
				},
				{
					field: 'pendingCount',
					title: '待发个数',
					slots: {
						default({ row }) {
							return (
								<el-button type="primary" onClick={() => linkDetail(row, 'pendingCount')} link>
									{row.pendingCount}
								</el-button>
							);
						},
					},
				},
			],
		},
		{
			title: '补充',
			children: [
				{
					field: 'supplementAmountTxt',
					title: '补充红包金额',
				},
				{
					field: 'supplementCount',
					title: '补充红包数',
					slots: {
						default({ row }) {
							return (
								<el-button type="primary" onClick={() => linkDetail(row, 'supplementCount')} link>
									{row.supplementCount}
								</el-button>
							);
						},
					},
				},
			],
		},
	],
	data: [],
	params: {
		exportMethod: getSSPStatisticsCitizenRedEnvelopeApprovalExport,
		exportParams: requestParams,
	},
});
/** 搜索按钮操作 节流操作 */
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
// 获取参数列表
const queryList = () => {
	state.loading = true;
	gridOptions.loading = true;
	requestParams.value = Other.deepClone(state.queryParams);
	requestParams.value.StartTime = state.queryParams.crTime === null ? null : state.queryParams.crTime[0]; // 受理时间
	requestParams.value.EndTime = state.queryParams.crTime === null ? null : state.queryParams.crTime[1];
	Reflect.deleteProperty(requestParams.value, 'crTime'); // 删除无用的参数
	getSSPStatisticsCitizenRedEnvelopeApproval(requestParams.value)
		.then((res) => {
			state.loading = false;
			gridOptions.data = res.result ?? [];
			gridOptions.loading = false;
		})
		.finally(() => {
			state.loading = false;
			gridOptions.loading = false;
		});
};
// 重置表单
const ruleFormRef = ref<any>(null); // 表单ref
const statisticalTimeRef = ref<RefType>();
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	statisticalTimeRef.value.reset();
	queryList();
};
// 跳转详情
const router = useRouter();
const linkDetail = (row: any, FieldName: string) => {
	router.push({
		path: '/snapshot/statistics/detailReAudit',
		query: {
			IndustryId: row.industryId,
			StartTime: requestParams.value.StartTime,
			EndTime: requestParams.value.EndTime,
			FieldName,
			IndustryType: row.industryType,
			tagsViewName: '市民红包审批统计明细',
		},
	});
};
// 页面加载时
onMounted(() => {
	queryList();
});
</script>
