<template>
    <scroll-view scroll-y class="main-scroll">
        <view class="scrollmain">
            <!-- 未读消息列表 -->
            <view
                class="list-card press-button"
                :class="{ unread: item.isRead === '0' }"
                v-for="(item, index) in unreadMsgList"
                :key="index"
                @click="seeDetail(item)"
            >
                <view class="card-img">
                    <image
                        class="card-img-flame"
                        v-if="item.title === '招聘会预约提醒'"
                        src="/static/icon/msgtype.png"
                    ></image>
                    <image
                        class="card-img-flame"
                        v-else-if="item.title === '职位上新'"
                        src="/static/icon/msgtype2.png"
                    ></image>
                    <image
                        class="card-img-flame"
                        v-else-if="item.title === '系统通知'"
                        src="/static/icon/msgtype3.png"
                    ></image>
                    <image
                        class="card-img-flame card-img-default"
                        v-else
                        src="/static/icon/bell.png"
                    ></image>
                    <view class="subscript" v-if="item.notReadCount">{{ item.notReadCount }}</view>
                </view>
                <view class="card-info">
                    <view class="info-title">
                        <view class="title-left">
                            <view class="unread-dot" v-if="item.isRead === '0'"></view>
                            <text :class="{ 'unread-text': item.isRead === '0' }">{{ item.title }}</text>
                        </view>
                        <view class="card-time">{{ item.date }}</view>
                    </view>
                    <view class="info-text line_2">{{ item.subTitle || '消息' }}</view>
                </view>
            </view>
            
            <!-- 暂无未读消息提示 -->
            <view class="empty-state" v-if="unreadMsgList.length === 0">
                <image class="empty-icon" src="/static/icon/empty.png" mode="aspectFit"></image>
                <text class="empty-text">暂无未读消息</text>
                <text class="empty-desc">您没有未读的消息</text>
            </view>
        </view>
    </scroll-view>
</template>

<script setup>
import { reactive, inject, ref } from 'vue';
const { $api, navTo, debounce } = inject('globalFunction');
import { storeToRefs } from 'pinia';
import { useReadMsg } from '@/stores/useReadMsg';
const { unreadMsgList } = storeToRefs(useReadMsg());
const isLoaded = ref(false);

async function loadData() {
    try {
        if (isLoaded.value) return;
        isLoaded.value = true;
    } catch (err) {
        isLoaded.value = false; // 重置状态允许重试
        throw err;
    }
}

function seeDetail(item) {
    uni.setStorageSync('currentMessageDetail', item);
    navTo('/packageA/pages/msglog/messageDetail');
}

defineExpose({ loadData });
</script>

<style lang="stylus" scoped>
.main-scroll {
    width: 100%
    height: 100%;
}
.scrollmain{
    padding: 28rpx
}
.read{

}
.list-card
    background: #FFFFFF;
    border-radius: 17rpx;
    display: flex;
    justify-content: flex-start;
    align-items: center;
    padding: 26rpx 36rpx;
    margin: 0 0 28rpx 0
    &.unread
        background: linear-gradient(135deg, #FFFAFA 0%, #FFFFFF 100%)
    .card-img
        width: 96rpx;
        height: 96rpx;
        border-radius: 50%
        display: grid;
        place-items: center;
        margin-right: 30rpx;
        position: relative;
        .card-img-flame
            width: 100%;
            height: 100%
        .card-img-default
            width: 60%
            height: 60%
        .subscript
            position: absolute;
            right: 0;
            top: 0;
            min-width: 26rpx;
            height: 26rpx;
            padding: 0 8rpx; /* 横向内边距控制宽度自适应 */
            border-radius: 1000rpx; /* 实现椭圆形/圆角 */
            text-align: center;
            line-height: 26rpx;
            background: #F73636;
            font-weight: 600;
            font-size: 20rpx;
            color: #FFFFFF;
            box-sizing: border-box;
    .card-info
        flex: 1;
        display: flex;
        align-items: flex-start;
        flex-direction: column;
        .info-title
            font-weight: 500;
            font-size: 32rpx;
            color: #333333;
            display: flex;
            justify-content: space-between;
            align-items: center;
            width: 100%
            .title-left
                display: flex
                align-items: center
                min-width: 0
                flex: 1
                .unread-dot
                    width: 14rpx
                    height: 14rpx
                    background: #F73636
                    border-radius: 50%
                    margin-right: 12rpx
                    flex-shrink: 0
                .unread-text
                    font-weight: 600
                text
                    font-family: 'PingFangSC-Medium', 'PingFang SC', 'Helvetica Neue', Helvetica, Arial, 'Microsoft YaHei', sans-serif;
            .card-time
                font-weight: 400;
                font-size: 28rpx;
                color: #AAAAAA;
                height: 100%
                margin: 4rpx;
                flex-shrink: 0
        .info-text
            font-weight: 400;
            font-size: 28rpx;
            color: #6C7282;
            margin-top: 4rpx;

// 空状态样式
.empty-state
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 120rpx 40rpx;
    .empty-icon
        width: 200rpx;
        height: 200rpx;
        margin-bottom: 40rpx;
        opacity: 0.6;
    .empty-text
        font-size: 32rpx;
        color: #999999;
        font-weight: 500;
        margin-bottom: 16rpx;
    .empty-desc
        font-size: 28rpx;
        color: #CCCCCC;
        font-weight: 400;
</style>
