<template>
	<view class="wrap">
		<view class="box" v-for="item in messageList" :key="item.messageId">
			<view class="title">
				{{item.createTime}}
			</view>
			<view class="content">
				{{item.messageContent}}
			</view>
			<u-line color="rgba(238, 238, 238, 1)"></u-line>
			<view class="bottom" @click="msgDetail(item.messageId)">
				<text class="detail">{{ this.$t('homePage.mine.lookDetail') }}</text>
				<u-icon name="arrow-right" color="rgba(0, 156, 119, 1)" size="20"></u-icon>
			</view>
		</view>
		<u-empty v-if="!messageList.length" :text="$t('homePage.mine.noMessage')" mode="messageList" style="height: 100vh;"></u-empty>
		<u-modal v-model="show" @confirm="confirm" ref="uModal" :show-cancel-button="true" :confirm-text="$t('homePage.mine.sureRead')"
			:cancel-text="$t('homePage.mine.cancel')" :async-close="true" :title="$t('homePage.mine.messageDetail')">
			<view style="padding: 20rpx;">
				<u-input v-model="contentValue" type="textarea" :border="false" disabled :height="100"
					:auto-height="true" />
			</view>

		</u-modal>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				stationId: null,
				userId: null,
				messageList: [],
				show: false,
				contentValue: '',
				messageId: null,
				formModel: {},
				timer: null
			}
		},
		computed: {
			language(){
				return this.vuex_language 
			},
			currentStation() {
				return this.vuex_currentStation;
			},
			userData() {
				return this.vuex_user
			},
		},
		watch: {
			language: {
				immediate: true,
				deep:true,
				handler(val) {
					uni.setNavigationBarTitle({
						title: this.$t('homePage.mine.messageAlerts')
					});
				}
			},
			currentStation: {
				handler(val) {
					if (val && val.id) {
						if (this.timer) {
							clearInterval(this.timer)
						}
						this.userId = this.userData.userId
						this.stationId = val.id
						if (this.stationId && this.userId) {
							this.getMessage()
						}

					}
				},
				deep: true,
				immediate: true
			},
		},
		onShow() {
			this.timer = setInterval(() => {
				this.getMessage()
			}, 3000)
		},
		onUnload() {
			if (this.timer) {
				clearInterval(this.timer)
			}
		},
		methods: {
			confirm() {
				if (this.formModel.readStatus === 0) {
					this.$u.api.UpdateMessageStatus({
						messageId: this.formModel.messageId,
						stationId: this.stationId,
						receivingUser: this.userId
					}).then(() => {
						this.$u.toast(this.$t('homePage.mine.operateSuccess'));
						this.getMessage()
						this.show = false
					})
				} else {
					this.$u.toast(this.$t('homePage.mine.messageReaded'))
				}
			},
			msgDetail(id) {
				this.show = true
				this.$u.api.GetMessage({
					receivingUser: this.userId,
					stationId: this.stationId,
					messageId:id
				}).then((res) => {
					this.formModel = res.data[0]
					this.contentValue = this.formModel.messageContent
				})
			},
			getMessage() {
				this.$u.api.GetMessage({
					receivingUser: this.userId,
					stationId: this.stationId,
					createstatus: 3,
					readStatus: 0
				}).then((res) => {
					this.messageList = res.data
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	page {
		background: #f5f5f5;
	}

	/deep/ .u-model__title {
		padding-top: 20rpx;
		margin-bottom: 10rpx;
	}

	.wrap {
		padding: 10px;
		height: 100vh;
		overflow: auto;
		.box {
			height: 160rpx;
			width: 100%;
			border-radius: 10rpx;
			box-shadow: 0rpx 4rpx 16rpx 0rpx rgba(0, 0, 0, 0.2);
			padding: 16rpx 30rpx;
			margin-bottom: 10rpx;

			.title {
				color: rgba(153, 153, 153, 1);
				font-size: 20rpx;
			}

			.content {
				margin-top: 8rpx;
				margin-bottom: 12rpx;
				font-size: 24rpx;
				overflow: hidden;
				white-space: nowrap;
				text-overflow: ellipsis;
			}

			.bottom {
				margin-top: 20rpx;
				display: flex;
				justify-content: space-between;

				.detail {
					color: rgba(0, 156, 119, 1);
					font-size: 20rpx;
				}
			}
		}
	}
</style>