<template>
    <view :loading="loading">
        <view class="message">
            <view class="message-nav" @click="jump('run')">
                <image mode="aspectFill" class="message-nav-icon" src="/static/message3.png" />
                <view class="message-nav-label">运行事件</view>
                <uni-badge :text="messageCount.runCount"></uni-badge>
                <uni-icons type="forward" color="#aaa" size="20"></uni-icons>
            </view>
            <view class="message-nav" @click="jump('transient')">
                <image mode="aspectFill" class="message-nav-icon" src="/static/incident.png" />
                <view class="message-nav-label">暂态事件</view>
                <uni-badge :text="messageCount.eventCount"></uni-badge>
                <uni-icons type="forward" color="#aaa" size="20"></uni-icons>
            </view>
            <view class="message-nav" @click="jump('steady')">
                <image mode="aspectFill" class="message-nav-icon" src="/static/report.png" />
                <view class="message-nav-label">稳态事件</view>
                <uni-badge :text="messageCount.harmonicCount"></uni-badge>
                <uni-icons type="forward" color="#aaa" size="20"></uni-icons>
            </view>
            <view class="message-nav" @click="jump('report')">
                <image mode="aspectFill" class="message-nav-icon" src="/static/term.png" />
                <view class="message-nav-label">设备告警</view>
                <uni-badge :text="messageCount.alarmCount"></uni-badge>
                <uni-icons type="forward" color="#aaa" size="20"></uni-icons>
            </view>
        </view>
    </view>
</template>

<script>
import { getDevCount } from '../../common/api/device.js'
export default {
    data() {
        return {
            loading: false,
            userInfo: {},
            messageCount: {},
            timer: null,
            devCount: {},
        }
    },

    methods: {
        init() {
            this.userInfo = uni.getStorageSync('userInfo')
        },
        upgrade(code) {
            console.log(code)
            uni.showToast({
                title: '升级成功',
                icon: 'none',
            })
        },
        jump(type) {
            switch (type) {
                case 'run':
                    uni.navigateTo({
                        url: `/pages/message/message?type=${2}`,
                    })
                    break
                case 'transient':
                    uni.navigateTo({
                        url: `/pages/message/message?type=${0}`,
                    })
                    break
                case 'steady':
                    uni.navigateTo({
                        url: `/pages/message/message?type=${1}`,
                    })
                    break
                case 'report':
                    uni.navigateTo({
                        url: `/pages/message/message?type=${3}`,
                    })
                    break
                default:
                    break
            }
        },
        getDevCount() {
            if (uni.getStorageSync('projectList')[1] != undefined) {
                getDevCount(uni.getStorageSync('projectList')[1].engineeringId).then((res) => {
                    this.devCount = res.data
                    this.devCount.currentOffLineDevs.forEach((item) => {
                        item.runStatus = 1
                    })
                    this.devCount.offLineDevs.forEach((item) => {
                        item.runStatus = 1
                    })
                    uni.setStorage({
                        key: this.$cacheKey.messageCount,
                        data: this.devCount,
                    })
                    let messagePage =
                        this.devCount.runCount +
                        this.devCount.eventCount +
                        this.devCount.alarmCount +
                        this.devCount.harmonicCount
                    let minePage = this.devCount.feedBackCount

                    if (messagePage) {
                        uni.setTabBarBadge({
                            index: 1,
                             text:messagePage ? (messagePage > 99 ? '99+' : messagePage + '') : '',
                        })
                    } else {
                        uni.removeTabBarBadge({
                            index: 1,
                        })
                    }
                    if (minePage) {
                        uni.setTabBarBadge({
                            index: 2,
                            text: minePage + '',
                        })
                    } else {
                        uni.removeTabBarBadge({
                            index: 2,
                        })
                    }
                    // #ifdef APP-PLUS
                    plus.runtime.setBadgeNumber(messagePage + minePage)
                    // #endif
                })
            }
        },
    },
    onLoad(options) {},
    onShow() {
        this.init()
        this.getDevCount()
        this.messageCount = uni.getStorageSync(this.$cacheKey.messageCount) || {}
        this.timer = setInterval(() => {
            this.messageCount = uni.getStorageSync(this.$cacheKey.messageCount) || {}
        }, 1000) // 定时请求
    },
    onHide() {
        clearInterval(this.timer)
    },
}
</script>

<style lang="scss">
.message {
    padding-top: 20rpx;

    .message-header {
        padding: 200rpx 34rpx 34rpx;
        display: flex;
        align-items: center;
        background: $uni-theme-white;
        margin-bottom: 20rpx;
        box-shadow: 0 4rpx 8rpx #e7e7e74c;

        .message-header-head {
            margin-right: 30rpx;
            height: 128rpx;
            width: 128rpx;
            border-radius: $uni-theme-radius;
            overflow: hidden;
        }

        .message-header-name {
            margin-right: 30rpx;
            flex: 1;
            font-size: 36rpx;
            color: #111;
            font-weight: 700;

            .tag {
                margin-top: 10rpx;
                font-size: 24rpx;
                color: #aaa;
            }
        }
    }

    .message-nav {
        padding: 34rpx;
        display: flex;
        align-items: center;
        background: $uni-theme-white;
        border-bottom: 1rpx solid #e8e8e8;

        &-icon {
            margin-right: 30rpx;
            height: 44rpx;
            width: 44rpx;
            border-radius: $uni-theme-radius;
            overflow: hidden;
        }

        &-label {
            margin-right: 30rpx;
            flex: 1;
            font-size: 28rpx;
            color: #111;
        }
    }
}
</style>
