<template>
    <div class="system-dept-container layout-padding">
		<el-card shadow="hover" class="layout-padding-auto">
			<div class="system-dept-search mb15">
                <el-select size="default" v-model="state.tableData.param.type" placeholder="请选择类型" clearable class="ml10" style="max-width: 180px">
					<el-option v-for="(item,index) in typeList" :key="index" :label="item" :value="item"></el-option>
                </el-select>
				<el-button @click="getTableData" size="default" type="primary" class="ml10">
					<el-icon>
						<ele-Search />
					</el-icon>
					查询
				</el-button>
                <el-button @click="reset" size="default" type="primary" class="ml10">
					<el-icon>
						<ele-RefreshRight />
					</el-icon>
					重置
				</el-button>
			</div>
			<el-table :data="state.tableData.data" v-loading="state.tableData.loading" style="width: 100%">
				<el-table-column type="index" label="序号"  width="100"/>
				<el-table-column prop="name" label="姓名" show-overflow-tooltip></el-table-column>
				<el-table-column prop="phone" label="联系方式" show-overflow-tooltip></el-table-column>
				<el-table-column prop="type" label="类型" show-overflow-tooltip></el-table-column>
				<el-table-column prop="message" 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="操作" width="200">
					<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="messageIndex">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { messageApi } from '/@/api/message';
import { ElMessage, TableColumnCtx } from 'element-plus';

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

// 表格数据
const state = reactive({
	tableData: {
		data: [],
		total: 0,
		loading: false,
		param: {
			type: '',// 类型
			current: 1,
			size: 10,
		},
	},
});

// 类型列表
const typeList = ref(["场地咨询","政策咨询","人才服务","赛事咨询","其他"]);

// 引入 api 请求接口
const mesApi = messageApi();

// 获取表格数据
const getTableData = async() => {
	try {
		state.tableData.loading = true;
		let res = await mesApi.getMessageList(state.tableData.param);
		if(res?.success){
			state.tableData.data = res.data.records;
			state.tableData.total = res.data.total;
		}else{
			ElMessage.error('文章列表获取失败！');
		}
	} catch (error) {
	} finally {
		state.tableData.loading = false;
	}
};

// 重置
const reset = () =>{
	state.tableData.param = {
		type: '',
		current: 1,
		size: 10,
	}
	getTableData();
}

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

// 日期格式化
const dateFormatter = (row: any, column: TableColumnCtx<String>) => {
    if(row.createtime === null) return '暂无';
	let date = row.createtime;
  	return `${date[0]}-${date[1]}-${date[2]} ${date[3]}:${date[4]}:${date[5]}`;
}

// 分页改变
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>