<template>
	<view class="order view">
		<view class="status_bar" :style="navHeight"></view>
		<view class="listview">
			<view class="cell-list">
				<view class="cell-list-block" @click="goMessageList('0')">
					<image class="icon-address" src="@/static/image/message_unread@3x.png" mode="aspectFit"></image>
					<view class="cell-list-block__content">
						<text class="cell-list-block__title">未读消息</text>
						<text class="cell-list-block__desc">最新消息：{{unreadList[0].MSGTITLE}}</text>
					</view>
					<text class="dot" v-if="unreadCount>0">{{unreadCount}}</text>
					<image class="icon-arrow" src="@/static/image/arrow_r@3x.png" mode="aspectFit"></image>
					<view class="line"></view>
				</view>
				<view class="cell-list-block" @click="goMessageList('1')">
					<image class="icon-address" src="@/static/image/message_read@3x.png" mode="aspectFit"></image>
					<view class="cell-list-block__content">
						<text class="cell-list-block__title">已读消息</text>
						<text class="cell-list-block__desc">{{readList[0].MSGTITLE}}</text>
					</view>
					<image class="icon-arrow" src="@/static/image/arrow_r@3x.png" mode="aspectFit"></image>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex'

	import {
		apiFindUnreadMsg
	} from '@/servers/message.js'

	export default {
		data() {
			return {
				readList: [{
					MSGTITLE: '暂无内容'
				}],
				unreadList: [{
					MSGTITLE: '暂无内容'
				}],
				unreadCount: 0
			}
		},
		computed: {
			navHeight() {
				return {
					'height': `${this.sysinfo.safeArea.top + 44}px`
				}
			},
			...mapState(['sysinfo', 'userInfo'])
		},
		onShow() {
			this.getMessageList(0)
			this.getMessageList(1)
		},
		methods: {
			async getMessageList(type) {
				try{
					let params = {
						customer: this.userInfo.code,
						msgType: 'app',
						page: 1,
						pagesize: 1,
						msgStatus: type
					}
					let res = await apiFindUnreadMsg(params)
					if (type === 0) {
						if (res.data.Total - 0 > 0){
							uni.setTabBarBadge({
								index:3,
								text: res.data.Total.toString()
							})
						}else {
							uni.removeTabBarBadge({
								index:3
							})
						}
					}
					if (res.status === 1) {
						if (type - 0 === 0) {
							this.unreadList = res.data.Rows.length > 0 ? res.data.Rows : [{MSGTITLE: '暂无内容'}]
							this.unreadCount = res.data.Total || 0
						} else {
							this.readList = res.data.Rows.length > 0 ? res.data.Rows : [{MSGTITLE: '暂无内容'}]
						}
					}
				}catch(e){
					this.unreadCount =  0
					uni.removeTabBarBadge({
						index:3
					})
				}
			},
			goMessageList(type) {
				uni.navigateTo({
					url: `/pages/message-list/message-list-page?messageType=${type}`
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.view {
		flex-direction: column;
	}

	/* #ifndef APP-PLUS */
	page {
		width: 100%;
		min-height: 100%;
		display: flex;
		background: #fff;
	}

	.status_bar {
		height: var(--status-bar-height);
	}

	/* #endif */

	.order {
		flex: 1;
		background: #f0f4f5;
		overflow-y: scroll;
		padding-top: 16rpx;
	}

	.listview {
		flex: 1;
		flex-direction: column;
	}

	.cell-list {
		flex-direction: column;
	}

	.icon-address {
		width: 60rpx;
		height: 60rpx;
		margin-right: 44rpx;
	}

	.cell-list-block__content {
		display: flex;
		flex: 1;
		flex-direction: column;
	}

	.cell-list-block__title {
		color: #333;
		font-size: 30rpx;
		flex: 1;
		align-items: center;
		margin-bottom: 4rpx;
	}

	.cell-list-block__desc {
		color: #999;
		font-size: 26rpx;
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
	}

	.icon-arrow {
		width: 12rpx;
		height: 20rpx;
	}

	.cell-list-block {
		position: relative;
		overflow: hidden;
		display: flex;
		height: 144rpx;
		background: #fff;
		padding: 0 36rpx 0 40rpx;
		align-items: center;
	}

	.line {
		position: absolute;
		width: 750rpx;
		left: 40rpx;
		bottom: 0;
		z-index: 10;
		height: 2rpx;
		background-color: #eee;
	}

	.dot {
		width: 44rpx;
		height: 36rpx;
		line-height: 36rpx;
		background: #ff0f00;
		border-radius: 18rpx;
		color: #fff;
		font-size: 22rpx;
		margin-right: 28rpx;
		text-align: center;
	}
</style>
