<template>
	<div class="notice-container">
		<el-card shadow="hover" :body-style="{ padding: '5px 5px 0 5px', display: 'flex', width: '100%', height: '100%', alignItems: 'start' }">
			<el-form :model="state.queryParams" ref="queryForm" :show-message="false" :inlineMessage="true" label-width="auto" style="flex: 1 1 0%">
				<el-row :gutter="10">
					<el-col class="mb5" :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
						<el-form-item label="标题" prop="title">
							<el-input v-model="state.queryParams.title" placeholder="标题" clearable @keyup.enter.native="handleQuery(true)" />
						</el-form-item>
					</el-col>
					<el-col class="mb5" :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
						<el-form-item label="类型" prop="type">
							<el-select v-model="state.queryParams.type" placeholder="类型" clearable @clear="state.queryParams.type = undefined">
								<el-option label="通知" :value="1" />
								<el-option label="公告" :value="2" />
							</el-select>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>

			<el-divider style="height: calc(100% - 5px); margin: 0 10px" direction="vertical" />

			<el-row>
				<el-col>
					<el-button-group>
						<el-button type="primary" icon="ele-Search" @click="handleQuery(true)" :loading="options.loading"> 查询 </el-button>
						<el-button icon="ele-Refresh" @click="resetQuery" :loading="options.loading"> 重置 </el-button>
					</el-button-group>
				</el-col>
			</el-row>
		</el-card>

		<el-card class="full-table" shadow="hover" style="margin-top: 5px">
			<vxe-grid ref="xGrid" class="xGrid-style" v-bind="options" @cell-dblclick="({ row }: any) => handleView(row)" @sort-change="sortChange">
				<template #toolbar_buttons> </template>
				<template #toolbar_tools> </template>
				<template #empty>
					<el-empty :image-size="200" />
				</template>
				<template #row_type="{ row }">
					<el-tag v-if="row.sysNotice.type === 1" type="success">通知</el-tag>
					<el-tag v-else type="warning">公告</el-tag>
				</template>
				<template #row_readStatus="{ row }">
					<el-tag v-if="row.readStatus === 1" type="info">已读</el-tag>
					<el-tag v-else type="danger">未读</el-tag>
				</template>
				<template #row_buttons="{ row }">
					<el-tooltip content="详情" placement="top">
						<el-button icon="ele-InfoFilled" text type="primary" @click="handleView(row)"> </el-button>
					</el-tooltip>
				</template>
				<template #pager>
					<vxe-pager
						:loading="options.loading"
						v-model:current-page="state.tableParams.page"
						v-model:page-size="state.tableParams.pageSize"
						:total="state.tableParams.total"
						@page-change="pageChange"
					/>
				</template>
			</vxe-grid>
		</el-card>

		<el-dialog v-model="state.isShowDialog" draggable width="769px">
			<template #header>
				<div style="color: #fff">
					<el-icon size="16" style="margin-right: 3px; display: inline; vertical-align: middle"> <ele-Bell /> </el-icon>
					<span> 消息详情 </span>
				</div>
			</template>
			<div class="w-e-text-container">
				<div v-html="state.content" data-slate-editor></div>
			</div>
			<template #footer>
				<span class="dialog-footer">
					<el-button type="primary" @click="state.isShowDialog = false">确认</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="notice">
import { onMounted, reactive, ref } from 'vue';
import commonFunction from '/@/utils/commonFunction';
import '@wangeditor/editor/dist/css/style.css';
import { VxeGridInstance, VxePagerEvents, VxePagerDefines } from 'vxe-table';
import { useVxeTable } from '/@/hooks/vxeTableOptionsHook';

import { getAPI } from '/@/utils/axios-utils';
import { SysNoticeApi } from '/@/api-services/api';
import { SysNoticeUser } from '/@/api-services/models';

const xGrid = ref<VxeGridInstance>();
const { removeHtml } = commonFunction();
const state = reactive({
	queryParams: {
		title: undefined,
		type: undefined,
	},
	tableParams: {
		page: 1,
		pageSize: 50,
		field: 'id', // 默认的排序字段
		order: 'desc', // 排序方向
		descStr: 'desc', // 降序排序的关键字符
		total: 0 as any,
	},
	isShowDialog: false,
	title: '',
	content: '',
});

// 表格参数配置
const options = useVxeTable<SysNoticeUser>({
	id: 'sysNoticeUser',
	name: '消息',
	columns: [
		// { type: 'checkbox', width: 40, fixed: 'left' },
		{ type: 'seq', title: '序号', width: 60, fixed: 'left' },
		{ field: 'sysNotice.title', title: '标题', minWidth: 200, showOverflow: 'tooltip' },
		{ field: 'sysNotice.content', title: '内容', minWidth: 180, showOverflow: 'tooltip', slots: { default: (scope: any) => removeHtml(scope.row.sysNotice.content) } },
		{ field: 'sysNotice.type', title: '类型', minWidth: 100, showOverflow: 'tooltip', slots: { default: 'row_type' } },
		{ field: 'sysNotice.createTime', title: '创建时间', minWidth: 150, showOverflow: 'tooltip' },
		{ field: 'readStatus', title: '阅读状态', minWidth: 100, showOverflow: 'tooltip', slots: { default: 'row_readStatus' } },
		{ field: 'sysNotice.publicUserName', title: '发布者', minWidth: 120, showOverflow: 'tooltip' },
		{ field: 'sysNotice.publicTime', title: '发布时间', minWidth: 150, showOverflow: 'tooltip' },
		{ title: '操作', fixed: 'right', width: 100, showOverflow: true, slots: { default: 'row_buttons' } },
	],
	searchCallback: () => handleQuery(),
	queryAllCallback: () => fetchData({ pageSize: 99999 }),
});

// 页面初始化
onMounted(async () => {
	await handleQuery();
});

// 查询操作
const handleQuery = async (reset = false) => {
	options.loading = true;
	if (reset) state.tableParams.page = 1;
	var res = await fetchData(null);
	xGrid.value?.loadData(res.data.result?.items ?? []);
	state.tableParams.total = res.data.result?.total;
	options.loading = false;
};

// 获取数据
const fetchData = async (tableParams: any) => {
	let params = Object.assign(state.queryParams, state.tableParams, tableParams);
	return getAPI(SysNoticeApi).apiSysNoticePageReceivedGet(params);
};

// 重置操作
const resetQuery = () => {
	state.queryParams.title = undefined;
	state.queryParams.type = undefined;
	handleQuery(true);
};

// 改变页码序号或页面容量
const pageChange: VxePagerEvents.PageChange = ({ currentPage, pageSize }: VxePagerDefines.PageChangeEventParams) => {
	state.tableParams.page = currentPage;
	state.tableParams.pageSize = pageSize;
	handleQuery();
};

// 列排序
const sortChange = (options: any) => {
	state.tableParams.field = options.field;
	state.tableParams.order = options.order;
	handleQuery();
};

// 查看详情
const handleView = async (row: any) => {
	state.content = row.sysNotice.content;
	state.isShowDialog = true;
	row.readStatus = 1;
	// mittBus.emit('noticeRead', row.sysNotice.id);
	await getAPI(SysNoticeApi).apiSysNoticeSetReadPost({ id: row.sysNotice.id });
};
</script>

<style lang="scss" scoped>
:deep(.el-dialog__body) {
	min-height: 600px;
}
</style>
