<template>
	<view class="container main">
		<view class="one" v-if="total>0">
			<scroll-view :scroll-top="scrollTop" scroll-y="true" class="scroll-view" @scrolltolower="toPage"
				@scroll="toScroll">
				<view class="list-scroll-view">
					<view class="list" v-for="(item, index) in list" :key="index">
						<view class="value">
							<view class="title">内容：</view>
							<view class="label"> <text :user-select="true">{{item.content||'暂无'}}</text> </view>
						</view>
						<view class="value">
							<view class="title">通知类型：</view>
							<view class="label">{{getDict(item.type,'type')||'暂无'}}</view>
						</view>
						<view class="value">
							<view class="title">是否已读：</view>
							<view class="label">{{getDict(item,'is_read')||'暂无'}}</view>
						</view>
						<view class="value">
							<view class="title">发送时间：</view>
							<view class="label">{{item.created_time||'暂无'}}</view>
						</view>
						<view class="bottom">
							<button v-if="getDict(item, 'is_read') == '未读'" class="button button_2" type="default"
								size="mini" @tap.stop="toRead(item)">已读</button>
						</view>
					</view>
					<view class="is_bottom" v-if="is_bottom">
						<text>{{config.bottom_title||'到底了！'}}</text>
					</view>
				</view>
			</scroll-view>
		</view>
		<o-empty v-else />
	</view>
</template>

<script>
	export default {
		data() {
			return {
				user: {},
				config: {},
				list: [],
				total: 0,
				skip: 0,
				limit: 5,
				page: 0,
				// 数据是否触底
				is_bottom: false,
				scrollTop: 0,
				// 禁止滚动穿透
				show: false,
				readList: [],
				typeList: []
			}
		},
		onLoad: async function(e) {
			const that = this;
			await that.searchToken();
			await that.searchOther();
			await that.searchConfig();
		},
		onShow: async function() {
			const that = this;
			that.clearPage();
			await that.search();
		},
		onPullDownRefresh: async function() {
			const that = this;
			that.clearPage();
			await that.search();
			uni.stopPullDownRefresh();
		},
		methods: {
			// 禁止滚动穿透
			change(e) {
				const that = this;
				that.show = e.show
			},
			// 用户信息
			searchToken() {
				const that = this;
				try {
					const res = uni.getStorageSync('token');
					if (res) {
						const user = that.$jwt(res);
						that.$set(that, `user`, user);
					}
				} catch (e) {}
			},
			searchConfig() {
				const that = this;
				try {
					const res = uni.getStorageSync('config');
					if (res) that.$set(that, `config`, res);
				} catch (e) {}
			},
			// 查询其他信息
			async searchOther() {
				const that = this;
				let res;
				// 查询类型
				res = await that.$api(`/dictData`, 'GET', {
					code: 'messageIsRead',
					is_use: '0',
				})
				if (res.errcode == '0') that.$set(that, `readList`, res.data);
				// 通知类型
				res = await that.$api(`/dictData`, 'GET', {
					code: 'messageType',
					is_use: '0',
				})
				if (res.errcode == '0') that.$set(that, `typeList`, res.data)
			},
			// 查询
			async search() {
				const that = this;
				let info = {
					skip: that.skip,
					limit: that.limit,
					'to.user': that.user.id
				}
				const res = await that.$api(`/message`, 'GET', info)
				if (res.errcode == '0') {
					let list = [...that.list, ...res.data];
					that.$set(that, `list`, list)
					that.$set(that, `total`, res.total)
				} else {
					uni.showToast({
						title: res.errmsg,
						icon: 'none'
					});
				}
			},
			// 处理字典表
			getDict(item, model) {
				if(item){
					const that = this;
					let res
					if (model == 'is_read') {
						//理论上,to中应该只有这个数据,但是还要处理
						const userid = that.user.id
						if (!userid) return
						const to = item.to || []
						const is_readData = to.find((f) => f.user === userid)
						if (!is_readData) return
						const is_read = is_readData.is_read || '0'
						res = that.readList.find((f) => f.value === is_read)
					} else if (model == 'type') res = that.typeList.find(i => i.value == item)
					if (res) return res.label
					else return '暂无'
				}
			},
			// 已读未读
			toRead(item) {
				const that = this;
				uni.showModal({
					title: '提示',
					content: '您确认已读该数据？',
					success: async function(res) {
						if (res.confirm) {
							const data = JSON.parse(JSON.stringify(item));
							const userid = that.user.id
							const to = data.to || []
							for (const val of to) {
								if (val.user == userid) val.is_read = '1'
							}
							const res = await that.$api(`/message/${item.id}`, 'POST', {
								id: item.id,
								to
							})
							if (res.errcode == '0') {
								uni.showToast({
									title: "已读成功！",
									icon: 'none'
								});
								that.clearPage();
								that.search();
							} else {
								uni.showToast({
									title: res.errmsg,
									icon: 'none'
								});
							}
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			// 分页
			toPage(e) {
				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 that.$set(that, `is_bottom`, true)
			},
			toScroll(e) {
				const that = this;
				let up = that.scrollTop;
				let num = Math.sign(up - e.detail.scrollTop);
				if (num == 1) that.$set(that, `is_bottom`, false);
				// 检查滚动位置是否达到div显示的条件
				if (e.detail.scrollTop >= 300 && !that.showDiv) {
					that.showDiv = true;
				} else if (e.detail.scrollTop < 300 && that.showDiv) {
					that.showDiv = false;
				}
			},
			// 清空列表
			clearPage() {
				const that = this;
				that.$set(that, `list`, [])
				that.$set(that, `skip`, 0)
				that.$set(that, `limit`, 5)
				that.$set(that, `page`, 0)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.main {
		.one {
			position: relative;
			flex-grow: 1;
			background-color: var(--f1Color);

			.list {
				background-color: var(--mainColor);
				border-bottom: 1px solid var(--f5Color);
				margin: 2vw 2vw 0 2vw;
				border-radius: 4px;
				padding: 3vw;


				.value {
					display: flex;
					align-items: center;

					.title {
						min-width: 70px;
						font-size: var(--font14Size);
						font-weight: bold;
					}

					.label {
						font-size: var(--font12Size);
						color: var(--f85Color);
					}
				}

				.bottom {
					margin: 2vw 0 0 0;
					text-align: center;

					.button {
						color: var(--mainColor);
						font-size: var(--font14Size);
						border-radius: 2vw;
					}

					.button_1 {
						margin: 0 2vw 0 0;
						background-color: var(--f3CColor);
					}

					.button_2 {
						background-color: var(--fF0Color);
					}
				}
			}
		}
	}

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

		.list-scroll-view {
			display: flex;
			flex-direction: column;
		}
	}

	.is_bottom {
		width: 100%;
		text-align: center;

		text {
			padding: 2vw 0;
			display: inline-block;
			color: var(--f85Color);
			font-size: var(--font14Size);
		}
	}
</style>