<template>
	<mobile-frame>
		<view class="main">
			<view class="one">
				<scroll-view scroll-y="true" class="scroll-view" @scrolltolower="toPage">
					<view class="list-scroll-view">
						<view class="one_1" @tap="toNotice()"><text
								class="iconfont icon-a-qingchuquanbuyidu"></text>全部已读</view>
						<view class="list" v-for="(item,index) in list" :key="index">
							<uni-badge class="badge" :text="item.num" absolute="leftTop" size="normal">
								<view class="list_1">
									<view class="source">来源：<text>{{item.zhSource||'暂无'}}</text></view>
									<view class="content">内容：<text>{{item.content||'暂无'}}</text></view>
								</view>
								<view class="list_2">
									发送时间：<text>{{item.time||'暂无'}}</text>
								</view>
							</uni-badge>
						</view>
					</view>
				</scroll-view>
			</view>
		</view>
	</mobile-frame>
</template>

<script>
	export default {
		data() {
			return {
				user: {},
				list: [],
				// 状态
				statusList: [],
				// 来源
				sourceList: [],
				total: 0,
				page: 0,
				skip: 0,
				limit: 10,
			};
		},
		onShow: async function(e) {
			const that = this;
			await that.searchOther();
			await that.watchLogin();
		},
		onPullDownRefresh: async function() {
			const that = this;
			that.clearPage();
			await that.search();
			uni.stopPullDownRefresh();
		},
		methods: {
			// 监听用户是否登录
			watchLogin() {
				const that = this;
				uni.getStorage({
					key: 'token',
					success: function(res) {
						let user = that.$jwt(res.data);
						that.$set(that, `user`, user);
						that.search()
					},
					fail: function(err) {
						uni.navigateTo({
							url: `/pages/login/index`
						})
					}
				})
			},
			// 查询列表
			async search() {
				const that = this;
				let info = {
					skip: that.skip,
					limit: that.limit,
					customer: that.user._id
				}
				const res = await that.$api(`/notice`, `GET`, {
					...info,
				})
				if (res.errcode == '0') {
					let list = [...that.list, ...res.data];
					for (let val of list) {
						if (val.status) val.zhStatus = that.searchStatus(val.status)
						if (val.source) val.zhSource = that.searchSource(val.source)
						if (val.status != '1') val.num = 1
					}
					that.$set(that, `list`, list)
					that.$set(that, `total`, res.total)
				}
			},
			// 查询状态
			searchStatus(e) {
				const that = this;
				let data = that.statusList.find((i) => i.value == e);
				if (data) return data.label
				else return '暂无'
			},
			// 查询来源
			searchSource(e) {
				const that = this;
				let data = that.sourceList.find((i) => i.value == e);
				if (data) return data.label
				else return '暂无'
			},
			// 全部已读
			async toNotice() {
				const that = this;
				let list = that.list.filter((i) => i.status != '1');
				for (let val of list) {
					const res = await that.$api(`/notice/${val._id}`, `POST`, {
						status: '1'
					})
					if (res.errcode == '0') {
						that.clearPage();
						that.search();
					}
				}
			},
			// 查询其他信息
			async searchOther() {
				const that = this;
				let res;
				res = await that.$api(`/dictData`, 'GET', {
					code: "notice_status"
				});
				if (res.errcode == '0') that.$set(that, `statusList`, res.data)
				res = await that.$api(`/dictData`, 'GET', {
					code: "notice_source"
				});
				if (res.errcode == '0') that.$set(that, `sourceList`, res.data)
			},
			// 分页
			toPage() {
				const that = this;
				let list = that.list;
				let limit = that.limit;
				if (that.total > list.length) {
					uni.showLoading({
						title: '加载中',
						mask: true
					})
					let page = that.page + 1;
					that.$set(that, `page`, page)
					let skip = page * limit;
					that.$set(that, `skip`, skip)
					that.search();
					uni.hideLoading();
				} else {}
			},
			// 清空列表
			clearPage() {
				const that = this;
				that.$set(that, `list`, [])
				that.$set(that, `skip`, 0)
				that.$set(that, `limit`, 10)
				that.$set(that, `page`, 0)
			}
		},

	}
</script>

<style lang="scss">
	.main {
		.one {
			.one_1 {
				padding: 2vw;
				text-align: center;

				.iconfont {
					color: #707070;
				}

				color:#707070;
			}

			.list {
				margin: 2vw 2vw 0 2vw;
				padding: 2vw;
				border-radius: 5px;
				border: 1px solid #f5f5f5;

				.list_1 {
					padding: 2vw 0 0 0;
					font-size: 14px;

					text {
						color: var(--f85Color);
					}
				}

				.list_2 {
					font-size: 12px;

					text {
						color: var(--f85Color);
					}
				}
			}
		}
	}

	.scroll-view {
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;

		.list-scroll-view {
			display: flex;
			flex-direction: column;
		}
	}
</style>
