<template>
	<div class="statistics-center-report-manage-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<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" :disabled="state.loading" />
				</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>
			<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="statisticsCenterReportManage"
					:custom-config="{ storage: true }"
					showHeaderOverflow
				>
					<vxe-column field="analysisName" title="报告名称"></vxe-column>
					<vxe-column field="remark" title="备注"></vxe-column>
					<vxe-column field="generatedTime" title="生成时间" width="160">
						<template #default="{ row }">
							{{ formatDate(row.generatedTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column fixed="right" title="操作" width="240" align="center">
						<template #default="{ 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>
					</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>
		<el-dialog title="预警事件明细" v-model="state.eventDialogVisible" draggable destroy-on-close append-to-body>
			<vxe-table
				border
				:loading="state.eventLoading"
				:data="state.eventTableData"
				:column-config="{ resizable: true }"
				:row-config="{ isCurrent: true, isHover: true, height: 30, useKey: true }"
				show-overflow
				:scrollY="{ enabled: true, gt: 100 }"
				max-height="500px"
			>
				<vxe-column field="no" title="工单编码" width="140"></vxe-column>
				<vxe-column field="title" title="工单标题" min-width="200">
					<template #default="{ row }">
						<order-detail :order="row" @updateList="queryList">{{ row.title }}</order-detail>
					</template>
				</vxe-column>
				<vxe-column field="acceptType" title="受理类型"></vxe-column>
				<vxe-column field="hotspotName" title="热点分类"></vxe-column>
				<vxe-column field="fullAddress" title="事发地址"></vxe-column>
			</vxe-table>
			<pagination
				@pagination="queryEventList"
				:total="state.eventTotal"
				v-model:current-page="state.queryEvent.PageIndex"
				v-model:page-size="state.queryEvent.PageSize"
				:disabled="state.eventLoading"
			/>
		</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 { useRouter } from 'vue-router';

// 引入组件
const OrderDetail = defineAsyncComponent(() => import('@/components/OrderDetail/index.vue')); // 工单详情
const StatisticalTime = defineAsyncComponent(() => import('@/components/StatisticalTime/index.vue')); // 日期类型选择组件
const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页
// 定义变量内容
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 router = useRouter();
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(() => {});
};
const toolbarRef = ref<RefType>();
const tableRef = ref<RefType>();
onMounted(() => {
	queryList();
	if (tableRef.value && toolbarRef.value) {
		tableRef.value.connect(toolbarRef.value);
	}
});
</script>
