<template>
	<div class="statistics-center-report-manage-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<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 #table-search>
					<el-form :model="state.queryParams" ref="ruleFormRef" @submit.native.prevent inline>
						<el-form-item prop="crTime">
							<statistical-time v-model="state.queryParams.crTime" @change="handleQuery" ref="statisticalTimeRef"/>
						</el-form-item>
						<el-form-item label="报告名称" prop="AnalysisName">
							<el-input v-model="state.queryParams.AnalysisName" placeholder="报告名称" clearable @keyup.enter="handleQuery" class="keyword-input" />
						</el-form-item>
						<el-form-item label="">
							<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 #title="{ row }">
					<order-detail :order="row" @updateList="queryList">{{ row.title }}</order-detail>
				</template>
				<template #operation="{ row }">
					<el-button type="primary" link @click="viewEvent(row)">查看预警事件</el-button>
					<el-button type="primary" link @click="viewReport(row)">查看报告</el-button>
					<el-button type="primary" link @click="deleteReport(row)">删除</el-button>
				</template>
			</ProTable>
		</div>
		<el-dialog title="预警事件明细" v-model="state.eventDialogVisible" draggable destroy-on-close append-to-body>
			<ProTable
				:columns="eventColumns"
				:data="state.eventTableData"
				:toolButton="false"
				:loading="state.eventLoading"
				:total="state.eventTotal"
				v-model:page-index="state.queryEvent.PageIndex"
				v-model:page-size="state.queryEvent.PageSize"
				max-height="400px"
				@updateTable="queryEventList"
			>
				<template #title="{ row }">
					<order-detail :order="row" @updateList="queryList">{{ row.title }}</order-detail>
				</template>
			</ProTable>
		</el-dialog>
	</div>
</template>
<script setup lang="tsx" name="statisticsCenterReportManage">
import { onMounted, reactive, ref, defineAsyncComponent } from 'vue';
import { ElMessageBox, FormInstance, ElMessage } from 'element-plus';
import { centerAnalysisReportDetail, centerAnalysisReportList, centerDeleteAnalysisReport } from '@/api/statistics/center';
import { defaultDate } from '@/utils/constants';
import Other from '@/utils/other';
import { formatDate } from '@/utils/formatTime';
import router from '@/router';

// 引入组件
const OrderDetail = defineAsyncComponent(() => import('@/components/OrderDetail/index.vue')); // 工单详情
const StatisticalTime = defineAsyncComponent(() => import('@/components/StatisticalTime/index.vue')); // 日期类型选择组件
// 表格配置项
const columns = ref<any[]>([
	{ prop: 'analysisName', label: '报告名称', minWidth: 140 },
	{ prop: 'remark', label: '备注', minWidth: 200 },
	{
		prop: 'generatedTime',
		label: '生成时间',
		minWidth: 160,
		render: (scope) => {
			return <span>{formatDate(scope.row?.generatedTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
	{ prop: 'operation', label: '操作', fixed: 'right', width: 240, align: 'center' },
]);
//
const eventColumns = ref<any[]>([
	{ prop: 'no', label: '工单编码' },
	{ prop: 'title', label: '工单标题', minWidth: 200 },
	{ prop: 'acceptType', label: '受理类型' },
	{ prop: 'hotspotName', label: '热点分类' },
	{ prop: 'fullAddress', label: '事发地址' },
]);
// 定义变量内容
const ruleFormRef = ref<RefType>(); // 表单ref
const state = reactive<any>({
	queryParams: {
		// 查询条件
		PageIndex: 1,
		PageSize: 20,
		AnalysisName: null, // 关键词
		crTime: defaultDate, // 生成时间
		GeneratedStartTime: null,
		GeneratedEndTime: null,
	},
	tableData: [], //表单
	loading: false, // 加载
	total: 0, // 总数
	eventDialogVisible: false,
	eventLoading: false,
	eventTotal: 0,
	queryEvent: {
		PageIndex: 1,
		PageSize: 10,
		AnalysisId: null,
	},
	eventTableData: [],
});
/** 搜索按钮操作 */
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
/** 获取列表 */
const requestParams = ref<EmptyObjectType>({});
const queryList = () => {
	state.loading = true;
	requestParams.value = Other.deepClone(state.queryParams);
	requestParams.value.GeneratedStartTime = state.queryParams.crTime === null ? null : state.queryParams.crTime[0];
	requestParams.value.GeneratedEndTime = state.queryParams.crTime === null ? null : state.queryParams.crTime[1];
	Reflect.deleteProperty(requestParams.value, 'crTime');
	centerAnalysisReportList(requestParams.value)
		.then((res: any) => {
			state.tableData = res.result?.items ?? [];
			state.total = res.result?.total ?? 0;
			state.loading = false;
		})
		.catch(() => {
			state.loading = false;
		});
};
/** 重置按钮操作 */
const statisticalTimeRef = ref<RefType>();
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	statisticalTimeRef.value.reset();
	queryList();
};
// 查看预警事件
const viewEvent = (row: any) => {
	state.queryEvent.AnalysisId = row.analysisId;
	queryEventList();
};
const queryEventList = () => {
	state.eventDialogVisible = true;
	state.eventLoading = true;
	centerAnalysisReportDetail(state.queryEvent)
		.then((res: any) => {
			state.eventTableData = res.result?.items ?? [];
			state.eventTotal = res.result?.total ?? 0;
			state.eventLoading = false;
		})
		.catch(() => {
			state.eventLoading = false;
		});
};
// 查看报告
const viewReport = (row: any) => {
	router.push({
		name: 'statisticsCenterDetailReport',
		params: {
			id: row.analysisId
		},
	});
};
// 删除报告
const deleteReport = (row: any) => {
	ElMessageBox.confirm('确定删除该报告吗？', '提示', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(() => {
			centerDeleteAnalysisReport({ analysisIds: [row.analysisId] })
				.then(() => {
					ElMessage({
						type: 'success',
						message: '删除成功',
					});
					queryList();
				})
				.catch(() => {
					queryList();
				});
		})
		.catch(() => {});
};
onMounted(() => {
	queryList();
});
</script>