<template>
	<mobile-frame>
		<view class="main">
			<scroll-view scroll-y="true" class="scroll-view" @scrolltolower="toPage" @scroll="toScroll">
				<view class="list-scroll-view">
					<discount :Style="Style" :couponList="list" :total="total" @toReceive="toReceive"></discount>
					<view class="is_bottom" v-if="is_bottom">
						<text>{{config.bottom_title}}</text>
					</view>
				</view>
			</scroll-view>
		</view>
	</mobile-frame>
</template>

<script>
	import discount from '@/components/discount/index.vue';
	export default {
		components: {
			discount
		},
		data() {
			return {
				config: {},
				user: {},
				Style: {
					receive: true
				},
				list: [],
				total: 0,
				skip: 0,
				limit: 10,
				page: 0,
				// 数据是否触底
				is_bottom: false,
				scrollTop: 0
			};
		},
		onLoad: function(e) {
			const that = this;
			// 监听用户是否登录
			that.watchLogin();
			// 下拉刷新

		},
		onShow: function() {
			const that = this;
			that.searchConfig();
		},
		onPullDownRefresh: async function() {
			const that = this;
			that.clearPage();
			await that.search();
			uni.stopPullDownRefresh();
		},
		methods: {
			// 查询基本设置
			searchConfig() {
				const that = this;
				uni.getStorage({
					key: 'config',
					success: function(res) {
						if (res.data) that.$set(that, `config`, res.data)
					},
				})
			},
			watchLogin() {
				const that = this;
				uni.getStorage({
					key: 'token',
					success: function(res) {
						let user = that.$jwt(res.data);
						if (user) {
							that.$set(that, `user`, user);
							that.search();
						}
					},
					fail: function(err) {
						console.log(err);
					}
				})
			},
			// 查询列表
			async search() {
				const that = this;
				let info = {
					skip: that.skip,
					limit: that.limit,
				}
				const res = await that.$api(`/coupon/userView`, '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,
					});
				}
			},
			// 分页
			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 that.$set(that, `is_bottom`, true)
			},
			toScroll(e) {
				const that = this;
				let up = that.scrollTop;
				that.$set(that, `scrollTop`, e.detail.scrollTop);
				let num = Math.sign(up - e.detail.scrollTop);
				if (num == 1) that.$set(that, `is_bottom`, false);
			},
			async toReceive(e) {
				const that = this;
				uni.showModal({
					title: '提示',
					content: '确定领取该优惠券?',
					success: async function(res) {
						if (res.confirm) {
							const arr = await that.$api(`/userCoupon/getCoupon/${e._id}`, 'POST');
							if (arr.errcode == '0') {
								if (arr.data) {
									uni.showToast({
										title: arr.data.msg,
										icon: 'none'
									});
								} else {
									uni.showToast({
										title: `领取成功`,
										icon: 'none'
									})
									that.clearPage();
									that.search();
								}
							} else {
								uni.showToast({
									title: arr.errmsg,
									icon: 'none',
								});
							}
						}
					}
				});
			},
			// 清空列表
			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 {
		background-color: var(--f5Color);
	}

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

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

	.is_bottom {
		text-align: center;

		text {
			padding: 2vw 0;
			display: inline-block;
			color: #858585;
			font-size: 14px;
		}
	}
</style>
