<template>
	<view>
		<CustomPopup v-model="show" mode="center" width="700rpx" bgColor="transparent" :maskClosable="true">
			<view class="share-popup-content">
				<!-- Background Image -->
				<image class="bg-image" src="/pages-sell/static/goods/icon-share-bg.png" mode="widthFix"></image>

				<!-- Close Button -->
				<view class="close-btn" @click="close">
					<image src="/pages-sell/static/goods/icon-close.png" mode="widthFix"></image>
				</view>

				<!-- Main Content -->
				<view class="main-box">
					<view class="title">分享</view>
					<view class="share-options">
						<view class="share-item">
							<button class="share-btn" open-type="share">
								<image src="/pages-sell/static/goods/icon-wx.png" mode="widthFix"></image>
								<text>微信好友</text>
							</button>
						</view>
						<view class="share-item" @click="onGeneratePoster">
							<image src="/pages-sell/static/goods/icon-pic.png" mode="widthFix"></image>
							<text>生成海报</text>
						</view>
					</view>
				</view>
			</view>
		</CustomPopup>

		<!-- Poster Popup -->
		<PosterPopup ref="posterPopup" :product="product" :userInfo="userInfo" :qrcodeUrl="qrcodeUrl"></PosterPopup>
	</view>
</template>

<script>
import CustomPopup from '@/components/custom-popup.vue';
import PosterPopup from './poster-popup.vue';
import { mapState } from 'vuex';

export default {
	name: 'SharePopup',
	components: {
		CustomPopup,
		PosterPopup
	},
	props: {
		product: {
			type: Object,
			default: () => ({})
		}
	},
	data() {
		return {
			show: false,
			userInfo: {},
			qrcodeUrl: ''
		};
	},
	methods: {
		open() {
			this.show = true;
			this.userInfo = uni.getStorageSync('userInfo') || {};
			this.$u.api.getIsbnQrcodeAjax(this.product.isbn).then(res => {
				if (res.code == 200) {
					console.log(res);
					this.qrcodeUrl = res.data;
				}
			});
		},
		close() {
			this.show = false;
		},
		onGeneratePoster() {
			this.close();
			// Wait for close animation or immediate?
			// Usually immediate is fine, but if mask fades out, it might look weird.
			// But CustomPopup v-model controls visibility.
			this.$nextTick(() => {
				this.$refs.posterPopup.open();
			});
		}
	}
};
</script>

<style lang="scss" scoped>
.share-popup-content {
	position: relative;
	width: 100%;
	// Remove padding-top as background image defines the shape
}

.bg-image {
	position: absolute;
	top: 0;
	left: 0;
	width: 100%;
	height: 100%;
	z-index: 0;
}

.close-btn {
	position: absolute;
	top: -40rpx; // Position outside the card
	right: 20rpx;
	width: 80rpx;
	height: 80rpx;
	z-index: 10;
	display: flex;
	align-items: center;
	justify-content: center;

	image {
		width: 60rpx;
	}
}

.main-box {
	// Remove background and border-radius as they are provided by image
	position: relative;
	z-index: 1;
	padding: 80rpx 40rpx 60rpx; // Adjust padding to fit content within the background image design

	.title {
		font-size: 36rpx;
		font-weight: bold;
		color: #308740;
		margin-bottom: 60rpx;
		padding-left: 30rpx;
	}

	.share-options {
		display: flex;
		justify-content: space-around;
		padding: 0 40rpx;
		padding-top: 60rpx;

		.share-item {
			display: flex;
			flex-direction: column;
			align-items: center;

			.share-btn {
				display: flex;
				flex-direction: column;
				align-items: center;
				background: none;
				padding: 0;
				margin: 0;
				line-height: 1.5;

				&::after {
					border: none;
				}
			}

			image {
				width: 100rpx;
				height: 100rpx;
				margin-bottom: 20rpx;
			}

			text {
				font-size: 28rpx;
				color: #333;
			}
		}
	}
}
</style>
