<template>
	<div class="early-manage-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<el-form :model="state.queryParams" ref="ruleFormRef" inline @submit.native.prevent :disabled="state.loading">
				<el-form-item label="预警名称" prop="EarlyWarningName">
					<el-input
						v-model="state.queryParams.EarlyWarningName"
						placeholder="请填写预警名称"
						clearable
						@keyup.enter="handleQuery"
						class="keyword-input"
					/>
				</el-form-item>
				<el-form-item label="预警级别" prop="EarlyWarningLevelValue">
					<el-select v-model="state.queryParams.EarlyWarningLevelValue" placeholder="请选择预警级别" @change="handleQuery" clearable>
						<el-option v-for="item in earlyWarningLevel" :value="item.dicDataValue" :key="item.dicDataValue" :label="item.dicDataName" />
					</el-select>
				</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"> <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, useKey: true }"
					:row-config="{ isCurrent: true, isHover: true, useKey: true, height: 30 }"
					ref="tableRef"
					height="auto"
					auto-resize
					show-overflow
					:scrollY="{ enabled: true, gt: 100 }"
					id="earlyManage"
					:custom-config="{ storage: true }"
					:sort-config="{ remote: true }"
					@sort-change="sortChange"
				>
					<vxe-column field="earlyWarningSetting.earlyWarningName" title="预警名称"></vxe-column>
					<vxe-column field="earlyWarningSetting.earlyWarningLevelName" title="预警级别"></vxe-column>
					<vxe-column field="startTime" title="时间/时间段" min-width="200">
						<template #default="{ row }">
							{{ formatDate(row.startTime, 'YYYY-mm-dd HH:MM:SS') }}-{{ formatDate(row.endTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column field="earlyWarningSetting.earlyWarningTypeName" title="预警类型"></vxe-column>
					<vxe-column field="earlyWarningSetting.frequencyText" title="频率"></vxe-column>
					<vxe-column field="fullAddress" title="预警地点"></vxe-column>
					<vxe-column field="isEnable" title="关联工单">
						<template #default="{ row }">
							{{ row.earlyWarningOrderDetails.length }}
						</template>
					</vxe-column>
					<vxe-column field="lastEarlyTime" title="最新预警时间" width="160">
						<template #default="{ row }">
							{{ formatDate(row.lastEarlyTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column title="操作" fixed="right" width="90" align="center">
						<template #default="{ row }">
							<el-button link type="primary" @click="onDetail(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>
		<!--  详情  -->
		<manage-detail ref="manageDetailRef" />
	</div>
</template>

<script lang="tsx" setup name="earlyManage">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { FormInstance } from 'element-plus';
import { formatDate } from '@/utils/formatTime';
import { getEarlySettingBaseData } from '@/api/early/setting';
import { getEarlyManageList } from '@/api/early/manage';
// 引入组件
const ManageDetail = defineAsyncComponent(() => import('@/views/early/manage/components/Detail.vue')); // 详情
const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页

// 定义变量内容
const state = reactive<any>({
	loading: false, // 加载状态
	queryParams: {
		// 查询参数
		PageIndex: 1,
		PageSize: 20,
		EarlyWarningName: null, // 预警名称
		EarlyWarningLevelValue: null, // 预警级别
		SortField: null,
		SortRule: null,
	},
	total: 0, // 总条数
	tableData: [], // 表格数据
});
const ruleFormRef = ref<any>(null); // 表单ref
/** 搜索按钮操作 */
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
// 获取列表
const queryList = () => {
	state.loading = true;
	getEarlyManageList(state.queryParams)
		.then((res) => {
			state.loading = false;
			state.tableData = res.result.items ?? [];
			state.total = res.result.total ?? 0;
		})
		.finally(() => {
			state.loading = false;
		});
};
// 排序
const sortChange = (val: any) => {
	state.queryParams.SortField = val.order ? val.field : null;
	// 0 升序 1 降序
	state.queryParams.SortRule = val.order ? (val.order == 'desc' ? 1 : 0) : null;
	queryList();
};
// 重置表单
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	queryList();
};
// 查看详情
const manageDetailRef = ref<RefType>();
const onDetail = (row: any) => {
	manageDetailRef.value.openDialog(row.id);
};
const tableRef = ref<RefType>();
const toolbarRef = ref<RefType>();
// 获取基础信息
const earlyWarningLevel = ref<EmptyArrayType>([]); // 预警级别
const queryBaseInfo = () => {
	getEarlySettingBaseData().then((res) => {
		earlyWarningLevel.value = res.result.earlyWarningLevel ?? [];
	});
};
// 页面加载时
onMounted(() => {
	queryList();
	if (tableRef.value && toolbarRef.value) {
		tableRef.value.connect(toolbarRef.value);
	}
	queryBaseInfo();
});
</script>
