<template>
    <div class="container">
        <el-table :data="tableData" style="width: 100%" :show-header="false" @row-click="handleRowClick">
            <el-table-column>
                <template slot-scope="scope" width="80">
                    <img :src="getIcon(scope.$index)" alt="icon" style="width: 2.19rem; height: 1.25rem;">
                </template>
            </el-table-column>
            <el-table-column prop="name" width="300">
                <template slot-scope="scope">
                    <span :style="{ color: scope.row.isRead === 2 ? '#ccc' : 'inherit' }">
                        {{ scope.row.name }}
                    </span>
                </template>
            </el-table-column>
            <el-table-column prop="alertTime">
                <template slot-scope="scope">
                    <span :style="{ color: scope.row.isRead === 2 ? '#ccc' : 'inherit' }">
                    {{ formatDate(scope.row.alertTime) }}
                    </span>
                </template>
            </el-table-column>
        </el-table>

        <!-- 详情弹窗 -->
        <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="40%">
            <div>
                <p><strong>内容:</strong> {{ selectedMessage.name }}</p>
                <p><strong>时间:</strong> {{ selectedMessage.alertTime}}</p>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="handleConfirm">已读</el-button>
                <el-button @click="dialogVisible = false">关闭</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { getAllMessages } from '@/api/ManageApi/index';
import { markSmartReminderAsRead } from '@/api/ManageApi/index';

export default {
    name: 'MessageTable',
    data() {
        return {
            tableData: [],
            dialogVisible: false,
            selectedMessage: {
                id: '',
                name: '',
                alertTime: ''
            },
            dialogTitle: '消息详情'
        };
    },
    mounted() {
        this.getData();
    },
    methods: {
        async getData() {
            const response = await getAllMessages();
            if (response && response.code === 200 && response.data) {
                this.tableData = this.processData(response.data);
                this.tableData.reverse(); // 直接倒序排序
            }
        },
        processData(data) {
            return data.map(item => ({
                id: item.id,
                name: item.content,
                alertTime: item.alertTime,
                isRead: item.isRead // 确保包含 isRead 字段
            }));
        },
        getIcon(index) {
            return index === 0 ? require('@/assets/images/new@2x.png') : require('@/assets/images/other.png');
        },
        formatDate(dateString) {
            if (!dateString) {
                return '';
            }
            const date = new Date(dateString);
            if (isNaN(date.getTime())) {
                return '';
            }
            const year = date.getFullYear();
            const month = String(date.getMonth() + 1).padStart(2, '0');
            const day = String(date.getDate()).padStart(2, '0');
            return `${year}-${month}-${day}`;
        },
        handleRowClick(row) {
            this.selectedMessage = row;
            this.dialogVisible = true;
        },
        handleConfirm() {
            if (this.selectedMessage.id) {
                markSmartReminderAsRead(this.selectedMessage.id)
                    .then(response => {
                        if (response && response.code === 200) {
                            this.$message.success('消息已标记为已读');
                            this.dialogVisible = false;
                            this.getData(); // 刷新数据
                        } else {
                            this.$message.error('标记为已读失败');
                        }
                    })
                    .catch(error => {
                        console.error('标记为已读失败:', error);
                        this.$message.error('标记为已读失败');
                    });
            } else {
                this.$message.error('消息 ID 不存在');
                this.dialogVisible = false;
            }
        }
    }
};
</script>

<style scoped>
.container {
    width: 98%;
    height: 11.5rem;
    /* background-color: lightblue; */
}
.no-data {
  color: gray;
}
</style>