<template>
	<view class="share" v-if="loading">
		<view class="box">
			<view class="share-rule">
				<view class="share-rule-title">
					<image src="https://ct-upimg.yx090.com/ju8hn6/shop/image/2023/08/26/tuFGYtfZXm7M6AhnUR57qAogLf1hojNRXuGJHJaM.png" mode="heightFix"></image>
					<text>分享福利</text>
				</view>
				<view class="title">分享美好，赢会员</view>
				<view class="user">
					<image src="https://ct-upimg.yx090.com/ju8hn6/shop/image/2022/07/01/kSrILtkzL6SPyVE0v9zKslwqRAFsTEVIoXAHnfc7.png" mode="widthFix"></image>
					<text>邀请任意用户下单</text>
				</view>
				<view class="text">
					【 分享有福利啦 】<br />宝子们，速来集合‼ 把我们店铺分享给你的好友，下单成功后双方都可以获得我们的会员卡一张,拥有会员卡下单更优惠哦！
				</view>
				<view class="table">
					<view class="item item1">邀请者福利</view>
					<view class="item">新用户福利</view>
					<view class="item"><text class="day">{{share_inviter_days}}</text>天会员</view>
					<view class="item item4"><text class="day">{{share_invitee_days}}</text>天会员</view>
				</view>
				<view class="btn"><button class="button" @click="handleShare()">立即分享</button></view>
			</view>
		</view>
		<view class="box share-user">
			<view class="share-rule-title">
				<image src="https://ct-upimg.yx090.com/ju8hn6/shop/image/2023/08/26/rGrArcmIQbEc9wZe9cE9ZoVZxj8FVOk9nfm2Vw6n.png" mode="heightFix"></image>
				<text>邀请记录</text>
			</view>
			<view class="list" v-if="inviterList.length > 0">
				<view class="item" v-for="(item, index) in inviterList">
					<text class="index">{{index + 1}}</text>
					<view class="user">
						<image :src="item.invitee.avatar"></image>
						<view class="right">
							<text class="name">{{item.invitee.nickname}}</text>
							<view class="text">
								<text class="time">{{item.created_at}}</text>
								<text>邀请成功</text>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="none" v-if="inviterList.length === 0 && loading">暂无邀请记录</view>
		</view>
	</view>
</template>

<script>
	import { ref, reactive, toRefs } from 'vue'
	import { getShopInfo, uniShare } from '@/components/common.js'
	import { get } from '@/api/request.js'
	export default {
		setup() {
			const data = reactive({
				share_inviter_days: 0,
				share_invitee_days: 0,
				inviterList: [],
				page: 1,
				lastPage: 0,
				loading: false,
				share: {}
			})
			const getVipInfo = async () => {
				let res = await getShopInfo()
				data.share_inviter_days = res.share_inviter_days
				data.share_invitee_days = res.share_invitee_days
				getVipShare()
			}

			const getVipShare = () => {
				get('/api/v1/user/vip/share').then((res) => {
					data.share = res.data
				})
			}

			const getInviterList = () => {
				get('/api/v1/user/vip/invitee', {
					page: data.page
				}).then((res) => {
					data.inviterList = data.inviterList.concat(res.data)
					data.lastPage = res.meta.last_page
				})
			}
			
			function handleShare() {
				let title = data.share.title
				let imageUrl = data.share.image
				let path = data.share.path
				uniShare(title, imageUrl, path)
			}

			return {
				uniShare,
				...toRefs(data),
				getVipInfo,
				getInviterList,
				handleShare
			}
		},
		async onLoad() {
			await this.getVipInfo()
			await this.getInviterList()
			this.loading = true
		},
		onShareAppMessage(options) {
			if (options.from == 'button') {
				return {
					title: this.share.title,
					imageUrl: this.share.image,
					path: this.share.path
				}
			}
		},
		onReachBottom() {
			if (this.page < this.lastPage) {
				this.page++
				this.getInviterList()
			}
		}
	}
</script>

<style lang="scss" scoped>
	.share {
		background: url('https://ct-upimg.yx090.com/ju8hn6/shop/image/2022/02/12/na4SZdAbc2cpIfHS7sixEB9cEKWlT2Md6PCRIb0b.jpg') no-repeat;
		background-size: 100%;
		width: 100%;
		min-height: 100%;
		background-color: #323540;
		padding: 30rpx;
		box-sizing: border-box;

		.box {
			background-color: #fff;
			border-radius: 10rpx;
			padding: 20rpx;
		}

		&-rule {
			border: 2rpx solid #ffa768;
			border-radius: 10rpx;
			padding: 50rpx;

			&-title {
				text-align: center;
				position: relative;

				image {
					position: absolute;
					top: 0;
					transform: translate(-50%, -50%);
					height: 20rpx;
					width: 335rpx;
				}

				text {
					position: absolute;
					top: 0;
					transform: translate(-50%, -50%);
					font-size: 36rpx;
					font-weight: bold;
				}
			}

			.title {
				font-size: 30rpx;
				color: #6c5f4f;
				margin: 50rpx 0 40rpx;
				font-weight: bold;
			}

			.user {
				display: flex;
				align-items: center;
				font-size: 28rpx;
				color: #555555;

				image {
					width: 100rpx;
					margin-right: 20rpx;
				}
			}

			.text {
				font-size: 26rpx;
				color: #98989f;
				margin: 30rpx 0;
				line-height: 45rpx;
			}

			.table {
				display: grid;
				grid-template-columns: 50% 50%;
				grid-template-rows: 80rpx 80rpx;

				.item {
					border: 1rpx solid #dedede;
					align-items: center;
					text-align: center;
					line-height: 80rpx;
					font-size: 26rpx;
					color: #cfcfcf;

					.day {
						color: #ffa768;
					}
				}

				.item1 {
					border-bottom: none;
					border-right: none;
				}

				.item4 {
					border-top: none;
					border-left: none;
				}
			}

			.btn {
				display: flex;
				justify-content: flex-end;

				.button {
					color: #fbe6c3;
					background-color: #353648;
					border-radius: 28rpx;
					margin-top: 40rpx;
					width: 160rpx;
					text-align: center;
					height: 48rpx;
					line-height: 48rpx;
					font-size: 30rpx;
				}
			}
		}

		.share-user {
			margin-top: 25rpx;

			.share-rule-title {
				margin-top: 70rpx;

				text {
					font-size: 30rpx;
					color: #ffa768;
				}
			}

			.list {
				.item {
					display: flex;
					height: 150rpx;
					align-items: center;
					padding: 0 30rpx;
					border-bottom: 1rpx solid #e5e5e5;

					.index {
						margin-right: 40rpx;
						font-size: 30rpx;
					}

					.user {
						display: flex;

						image {
							width: 70rpx;
							height: 70rpx;
							background: #98989f;
							border-radius: 35rpx;
							margin-right: 15rpx;
						}

						.right {
							.name {
								font-size: 26rpx;
							}

							.text {
								font-size: 24rpx;
								color: #b7b7b7;

								.time {
									margin-right: 15rpx;
								}
							}
						}
					}
				}
			}

			.none {
				text-align: center;
				color: #b7b7b7;
				font-size: 24rpx;
				margin-top: 100rpx;
				height: 350rpx;
			}
		}
	}
</style>
