<template>
	<view class="template-edit tn-safe-area-inset-bottom">
		<tn-nav-bar :isBack="false" backTitle="" :bottomShadow="false" backgroundColor="#FFFFFF">
			<view class="custom-nav tn-flex tn-flex-col-center tn-flex-row-left">
				<view style="padding-left: 15rpx;" @click="goBack()">
					<text class="tn-icon-left" style="font-size: 40rpx;"></text>
				</view>
				<view style="width: 83%;font-weight: bold;">
					邀请会员
				</view>
			</view>
		</tn-nav-bar>

		<view class="tn-safe-area-inset-bottom" :style="{ paddingTop: vuex_custom_bar_height + 'px' }">
			<!-- 顶部卡片 -->
			<view class="invite-card tn-shadow-blur">
				<view class="card-stats">
					<view class="stat-item">
						<text class="number">{{ inviteCount}}</text>
						<text class="label">已邀请</text>
					</view>
				</view>
				<!-- 按钮组 -->
				<view class="button-group">
					<!-- <tn-button @click="openQrcode" class="invite-btn" backgroundColor="#FFFFFF" height="80rpx"
						:shadow="true" fontColor="#5C8FFF">
						<text class="tn-icon-qr-code"></text> 生成二维码
					</tn-button> -->
					<tn-button open-type="share" width="100%" class="invite-btn" backgroundColor="#FFFFFF"
						height="80rpx" :shadow="true" fontColor="#5C8FFF">
						<text class="tn-icon-share"></text> 邀请好友
					</tn-button>
				</view>
			</view>

			<!-- 邀请列表 -->
			<view class="invite-list">
				<view class="list-header">
					<text class="title">邀请记录</text>
					<text class="count">共{{ inviteCount }}人</text>
				</view>

				<view class="list-content">
					<view v-for="(item, index) in inviteList" :key="index" class="list-item tn-flex tn-flex-row">
						<image v-if="item.photo_image!=''&&item.photo_image!=null" :src="apiImgUrl+item.photo_image"
							class="avatar" mode="aspectFill">
						</image>
						<image v-if="item.photo_image==''||item.photo_image==null" src="/static/def.png" class="avatar"
							mode="aspectFill">
						</image>
						<view class="info">
							<view class="name">{{ item.nikename || '匿名用户' }}</view>
							<view class="time">{{ item.createtime || '未知时间' }}</view>
						</view>
						<view class="status" :class="item.if_xianshi === 1 ? 'success' : 'pending'">
							{{ item.if_xianshi === 1 ? '已入会' : '未入会' }}
						</view>
					</view>
				</view>
			</view>
		</view>
		<tn-popup :closeBtn="true" v-model="show" mode="center" width="500rpx" height="700rpx">
			<div style="text-align: center;padding: 30rpx;font-size: 32rpx;font-weight: 600;">使用二维码邀请入会</div>
			<view style="text-align: center;margin-top: 40rpx;">
				<image :src="qrcode_url" style="width: 400rpx;height: 400rpx;"></image>
			</view>
			<view style="text-align: center;margin-top: 40rpx;">
				<tn-button shape="round" backgroundColor="#82B2FF" fontColor="#ffffff"
					@click="saveBase64">保存图片</tn-button>
			</view>
		</tn-popup>
		<view class='tn-tabbar-height'></view>
	</view>
</template>

<script>
	import {
		getUserIndex,
		getQrcode,
		getrecommendationList
	} from "@/util/api";
	import store from "@/store";
	export default {
		data() {
			return {
				apiImgUrl: this.$store.state.imgUrl,
				apiUpUrl: this.$store.state.apiUrl,
				show: false,
				topCurrent: 0,
				uid: 0,
				userInfo: {},
				qrcode_url: '',
				inviteList: [],
				inviteCount: 0,
				page: 1
			}
		},
		onShareAppMessage() {
			return {
				title: this.userInfo.nikename + '邀请你加入【' + this.userInfo.association_name + '】',
				path: '/pages/index/index?id=' + this.uid,
				imageUrl: this.apiImgUrl + this.userInfo.association_image,
			}
		},
		onShareTimeline() { // 分享到朋友圈
			return {
				title: this.userInfo.nikename + '邀请你加入【' + this.userInfo.association_name + '】',
				path: '/pages/index/index?id=' + this.uid,
				imageUrl: this.apiImgUrl + this.userInfo.association_image,
			}
		},
		onLoad() {
			var u = uni.getStorageSync('uid');
			this.uid = u;
			this.getUserInfo();
			this.getList();
		},
		onReachBottom() {
			this.page += 1;
			this.getList();
		},
		methods: {
			saveBase64() {
				wx.saveImageToPhotosAlbum({
					filePath: this.qrcode_url,
					success: function(res) {
						wx.showToast({
							title: '保存成功',
						})
					},
					fail: function(err) {
						console.log(err, '失败')
					}
				})
			},
			openQrcode() {
				uni.showLoading({
					title: '生成中',
					mask: true,
				});
				var that = this;
				uni.request({
					url: store.state.apiUrl + '/move/login/getUnlimitedQRCode',
					method: 'POST',
					data: {
						path: 'pages/index/apply_in',
						scene: "id=" + this.uid + "&association_id=" + this.userInfo.association_id
					},
					responseType: 'arraybuffer',
					arraybuffer: true,
					success: (result) => {
						const url = 'data:image/png;base64,' + uni.arrayBufferToBase64(result.data);
						that.base64ToImage(url);
					}
				})
			},
			base64ToImage(base64Data) {
				var that = this;
				const fs = uni.getFileSystemManager();
				var filePath = wx.env.USER_DATA_PATH + '/qrcode.jpg';
				const base64 = base64Data.split(',')[1]; // 获取base64字符串部分
				fs.writeFile({
					filePath: filePath,
					data: base64,
					encoding: 'base64',
					success: (res) => {
						that.qrcode_url = filePath;
						console.log('图片保存成功', filePath);
						// 成功回调
						that.show = true;
						uni.hideLoading()
					},
					fail: (err) => {
						console.error('图片保存失败', err);
						uni.hideLoading()
						// 失败回调
					}
				});
			},
			getUserInfo() {
				getUserIndex({
						member_b_id: this.uid,
						member_id: this.uid
					})
					.then(res => {
						console.log(res);
						if (res.code == 1) {
							this.userInfo = res.data;
						}
					})
					.catch(error => {
						uni.showToast({
							title: error,
							icon: 'none',
							duration: 2000
						});
					})
			},
			getList() {
				console.log(1);
				getrecommendationList({
						page: this.page,
						size: 10
					})
					.then(res => {
						console.log(res);
						if (res.code == 1) {
							this.inviteList.push(...res.data.ret);
							this.inviteCount = res.data.count;
						}
					})
					.catch(error => {
						uni.showToast({
							title: error,
							icon: 'none',
							duration: 2000
						});
					})
			},
			// 跳转
			tn(e) {
				uni.navigateTo({
					url: e,
				});
			},
			goBack() {
				if (getCurrentPages().length > 1) {
					uni.navigateBack()
				} else {
					uni.redirectTo({
						url: '/pages/index/index'
					})

				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	/* 底部悬浮按钮 start*/
	.tn-tabbar-height {
		min-height: 100rpx;
		height: calc(120rpx + env(safe-area-inset-bottom) / 2);
	}

	.tn-footerfixed {
		position: fixed;
		width: 100%;
		bottom: calc(30rpx + env(safe-area-inset-bottom));
		z-index: 1024;
		box-shadow: 0 1rpx 6rpx rgba(0, 0, 0, 0);

	}

	/* 底部悬浮按钮 end*/

	/* 标签内容 start*/
	.tn-tag-content {
		&__item {
			display: inline-block;
			line-height: 45rpx;
			padding: 10rpx 30rpx;
			margin: 20rpx 20rpx 5rpx 0rpx;

			&--prefix {
				padding-right: 10rpx;
			}
		}
	}

	/* 标签内容 end*/

	/* 邀请卡片 */
	.invite-card {
		margin: 30rpx;
		padding: 40rpx;
		background: linear-gradient(45deg, #5C8FFF, #82B2FF);
		border-radius: 24rpx;
		color: #FFFFFF;

		.card-title {
			font-size: 32rpx;
			font-weight: bold;
			margin-bottom: 20rpx;
		}

		.card-content {
			.invite-code {
				font-size: 48rpx;
				font-weight: bold;
				letter-spacing: 4rpx;
			}
		}

		.card-stats {
			margin: 30rpx 0;

			.stat-item {
				text-align: center;

				.number {
					font-size: 80rpx;
					font-weight: bold;
					display: block;
				}

				.label {
					font-size: 24rpx;
					opacity: 0.8;
				}
			}
		}

		.button-group {
			display: flex;
			gap: 20rpx;

			.invite-btn {
				width: 100%;
				flex: 1;
				border-radius: 40rpx;

				.tn-icon-qr-code,
				.tn-icon-share {
					margin-right: 10rpx;
				}
			}
		}
	}

	/* 邀请列表 */
	.invite-list {
		margin: 30rpx;
		background-color: #FFFFFF;
		border-radius: 24rpx;
		padding: 30rpx;

		.list-header {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-bottom: 30rpx;

			.title {
				font-size: 32rpx;
				font-weight: bold;
				color: #333333;
			}

			.count {
				font-size: 28rpx;
				color: #999999;
			}
		}

		.list-content {
			.list-item {
				display: flex;
				align-items: center;
				padding: 20rpx 0;
				border-bottom: 1rpx solid #F5F5F5;

				&:last-child {
					border-bottom: none;
				}

				.avatar {
					width: 80rpx;
					height: 80rpx;
					border-radius: 50%;
					margin-right: 20rpx;
				}

				.info {
					flex: 1;

					.name {
						font-size: 28rpx;
						color: #333333;
						margin-bottom: 6rpx;
					}

					.time {
						font-size: 24rpx;
						color: #999999;
					}
				}

				.status {
					font-size: 24rpx;
					padding: 4rpx 16rpx;
					border-radius: 20rpx;

					&.success {
						background-color: #E8F3FF;
						color: #5C8FFF;
					}

					&.pending {
						background-color: #FFF7E6;
						color: #FFA726;
					}
				}
			}
		}
	}
</style>