<template>
	<view class="page">
		<view class="list-box">
			<view class="item" v-for="(it,index) in messageList" :key="index" @click="toDetail(it.MessageEntity,it.MessageReceiveEntity.MessageId)">
				<view class="info">
					<view class="title">
						{{it.MessageEntity.Title}}
					</view>
					<view class="desc">
						{{it.MessageEntity.BodyText}}
					</view>
				</view>
				<view class="isRead" style="color: red;" v-if="it.MessageReceiveEntity.IsRead === 0">
					未读
				</view>
				<view class="isRead" style="color: greenyellow;" v-else>
					已读
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import request from '@/utils/request.js'
	export default {
		data() {
			return {
				messageList:[],
				isAdmin:false
			}
		},
		onShow() {
			this.getUser()
			this.getAdminNoticeTell()
		},
		onLoad() {
			this.getUser()
			this.getAdminNoticeTell()
		},
		methods: {
			// 获取用户信息
			getUser() {
				if (uni.getStorageSync('user')) {
					let userCode = uni.getStorageSync("user")
					console.log(userCode)
					if(userCode.userInfo.userId === 'admin'){
						this.isAdmin = true
					}
					console.log('用户已登录！')
				} 
				else {
					uni.showToast({
						title: '请登录',
						icon: 'none',
						duration:1500
					})
					// setTimeout(() => {
					// 	uni.reLaunch({
					// 		url: '/pages/login/index'
					// 	})
					// })
				}
			},
			// 获取消息通知
			getAdminNoticeTell(){
				request({
					url:'/api/extend/demo/GetMessageList',
					method:'get',
					data:{}
				}).then(res=>{
					console.log('通知列表',res)
					if(res.code == 200){
						this.messageList = JSON.parse(JSON.stringify(res.data))
					}
				})
			},
			// 跳转到消息详情
			toDetail(it,id){
				let link = JSON.parse(it.bodyJson)
				// let id = it.Id
				console.log('link',link.link)
				// uni.navigateTo({
				// 	url:link.link + `?data=${JSON.stringify(link.reid)}`
				// })
				// this.API.updateInfo({MessageId:id}).then(res=>{
				// 	console.log("修改已读",res)
				// 	if(res.code == 200){
				// 		console.log('成功修改')
				// 		uni.navigateTo({
				// 			url:link.link + `?data=${JSON.stringify(link.reid)}`
				// 		})
				// 	}
				// })
				uni.navigateTo({
					url:link.link + `?data=${JSON.stringify(link.reid)}`
				})
				// request({
				// 	url:`/api/extend/demo/UpdateIsReadEnd?MessageId=${id}`,
				// 	method:'put',
				// 	data:{}
				// }).then(res=>{
				// 	console.log("修改已读",res)
				// 	if(res.code == 200){
				// 		console.log('成功修改')
				// 		uni.navigateTo({
				// 			url:link.link + `?data=${JSON.stringify(link.reid)}`
				// 		})
				// 	}
				// })
			}
		}
	}
</script>

<style lang="scss" scoped>
	.page{
		width: 100%;
		height: 100vh;
		overflow: scroll;
		background-color: #f3f3f3;
	}
	.list-box{
		width: 96%;
		margin: 0 auto;
		overflow-y: scroll;
	}
	.item{
		width: 100%;
		background-color: white;
		border-radius: 30rpx;
		padding: 30rpx;
		margin-top: 20rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		.info{
			display: flex;
			flex-direction: column;
			// align-items: center;
			.title{
				font-size: 36rpx;
				font-weight: bold;
			}
			.desc{
				// width: 400rpx;
				overflow: hidden;
				white-space: nowrap;
				text-overflow: ellipsis;
				margin-left: 20rpx;
			}
		}
		.isRead{
			color: red;
			margin-right: 40rpx;
		}
	}
</style>
