<template>
	<div class="system-dept-container layout-padding">
		<el-card shadow="hover" class="layout-padding-auto">
			<div class="system-dept-search mb15">
				<el-input size="default" placeholder="请输入标题" v-model="state.tableData.param.title" class="ml10" style="max-width: 180px"> </el-input>
				<el-input size="default" placeholder="请输入发送者" v-model="state.tableData.param.sendUserName" class="ml10" style="max-width: 180px"> </el-input>
				<el-input size="default" placeholder="请输入接收者" v-model="state.tableData.param.userName" class="ml10" style="max-width: 180px"> </el-input>
				<el-button @click="getTableData" size="default" type="primary" class="ml10">
					<el-icon>
						<ele-Search />
					</el-icon>
					查询
				</el-button>
			</div>
			<el-table :data="state.tableData.data" v-loading="state.tableData.loading" style="width: 100%" @selection-change="handleSelectionChange"
			ref="tableRef">
				<el-table-column type="selection" width="55" />
				<el-table-column prop="title" label="标题" show-overflow-tooltip></el-table-column>
				<el-table-column prop="message" label="内容" show-overflow-tooltip></el-table-column>
				<el-table-column prop="sendUserName" label="发送者" show-overflow-tooltip></el-table-column>
				<el-table-column prop="userName" label="接收者" show-overflow-tooltip></el-table-column>
				<el-table-column prop="createtime" label="创建时间" :formatter="dateFormatter" show-overflow-tooltip></el-table-column>
				<el-table-column label="操作">
					<template #default="scope">
						<el-button size="small" text type="primary" @click="toShowDetail(scope.row.id)">查看详情</el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				@size-change="onHandleSizeChange"
				@current-change="onHandleCurrentChange"
				class="mt15"
				:pager-count="5"
				:page-sizes="[10, 20, 30]"
				v-model:current-page="state.tableData.param.current"
				background
				v-model:page-size="state.tableData.param.size"
				layout="total, sizes, prev, pager, next, jumper"
				:total="state.tableData.total"
			>
			</el-pagination>
		</el-card>
		<MessageDialog ref="messageDialogRef" />
	</div>
</template>

<script setup lang="ts" name="messagePush">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { messagePushApi } from '/@/api/messagePush';
import { ElMessage, TableColumnCtx } from 'element-plus';

// 引入组件
const MessageDialog = defineAsyncComponent(() => import('/@/views/messagePush/component/detail.vue'));
const messageDialogRef = ref();

// 表格数据
const state = reactive({
	tableData: {
		data: [],
		total: 0,
		loading: false,
		param: {
			sendUserName: '',
			userName: '',
			title: '',
			current: 1,
			size: 10,
		},
	},
	selectedRows: [] as any[], // 用于存储选中的消息
});

// 引入 api 请求接口
const mspApi = messagePushApi();

// 获取表格数据
const getTableData = async () => {
	try {
		state.tableData.loading = true;
		let res = await mspApi.getMessagePushList(state.tableData.param);
		if (res?.success) {
			state.tableData.data = res.data.records;
			state.tableData.total = res.data.total;
		} else {
			ElMessage.error('消息列表获取失败！');
		}
	} catch (error) {
		console.log(error)
		ElMessage.error('数据加载失败！');
	} finally {
		state.tableData.loading = false;
	}
};

// 处理多选
const handleSelectionChange = (selection: any[]) => {
	state.selectedRows = selection;
};

// 消息详情
const toShowDetail = (id: any) => {
    messageDialogRef.value.openDialog(id);
}

// 日期格式化
const dateFormatter = (row: any, column: TableColumnCtx<String>) => {
	let date = new Date(row.createtime);
	return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
};

// 分页改变
const onHandleSizeChange = (val: number) => {
	state.tableData.param.size = val;
	getTableData();
};

// 分页改变
const onHandleCurrentChange = (val: number) => {
	state.tableData.param.current = val;
	getTableData();
};

onMounted(() => {
	getTableData();
});
</script>

<style scoped lang="scss">
.system-dept-container {
	:deep(.el-card__body) {
		display: flex;
		flex-direction: column;
		flex: 1;
		overflow: auto;
		.el-table {
			flex: 1;
		}
	}
}
</style>
