<template>
	<view>
		<view>
			<uni-card v-for="item in datas" @click="toDetail(item)">
				<view class="uni-flex uni-row" style="align-items: center;">
					<text style="flex: 1;">{{item.content}}</text>
					<text
						:style="'padding-left: 8px;'+(item.status==='未读'?'color:#01D393':'color:#999999')">{{item.status}}</text>
				</view>
			</uni-card>
			<uni-load-more :status="loadStatus" @clickLoadMore="loadMore" :content-text="contentText" />
		</view>
	</view>
</template>

<script>
	import helper from '../../common/helper';
	export default {
		data() {
			return {
				datas: [],
				token: '',
				loadStatus: 'loading',
				pageNum: 1,
				contentText: {
					contentdown: '查看更多',
					contentrefresh: '加载中',
					contentnomore: '没有更多了'
				}
			}
		},
		onNavigationBarButtonTap(e) {
			if (e.index === 0) {
				this.readAll();
			}
		},
		onShow(e) {
			this.token = helper.tempToken;
			this.datas = [];
			this.requestData();
		},
		methods: {
			readAll() {
				uni.showModal({
					content: '确定将所有消息标记为已读？',
					title: '提示',
					success: (res) => {
						if (res.confirm) {
							uni.request({
								header: {
									Authorization: `Bearer ${this.token}`
								},
								method: 'PUT',
								url: `${helper.serverUrl}/message/readAll`,
								success: (resp) => {
									console.log(resp)
									this.datas = [];
									this.pageNum = 1;
									this.requestData();
								}
							})
						} else if (res.cancel) {}
					}
				})
			},
			toDetail(item) {
				uni.request({
					header: {
						Authorization: `Bearer ${this.token}`
					},
					method: 'PUT',
					url: `${helper.serverUrl}/message/read/${item.id}`
				})
				const pages = getCurrentPages();
				const prevPage = pages[pages.length - 2];
				uni.navigateBack({
					success: () => {
						prevPage.$vm.showFfzy(item.sourceId)
					}
				})
			},
			loadMore(e) {
				if (e.detail.status !== 'no-more') {
					this.pageNum = this.pageNum + 1;
					this.requestData();
				}
			},
			requestData() {
				this.loadStatus = 'loading';
				uni.request({
					url: `${helper.serverUrl}/message/list`,
					data: {
						pageNum: this.pageNum,
						pageSize: 10
					},
					header: {
						Authorization: `Bearer ${this.token}`
					},
					success: (resp) => {
						console.log(resp)
						if (resp.data.rows) {
							this.datas = [...this.datas, ...resp.data.rows];
							if (this.datas.length >= resp.data.totalCount) {
								this.loadStatus = 'no-more'
							} else {
								this.loadStatus = 'more'
							}
						}
					}
				})
			}

		}
	}
</script>

<style scoped>
</style>