<template>
	<view class="sharepage_container" @click="handlePageClick">
		<view class="background_image_container">
			<!-- 背景图片 -->
			<image class="background_image" :src="$baseUrl + 'homepage/shareImg_jf1.jpg'" mode="aspectFit" />
			
			<!-- 一键领取按钮 -->
			<view class="claim_button" @click="handleClaim">
				<text class="claim_text">一键领取</text>
			</view>
		</view>
		
		<!-- 分享登录弹窗 -->
		<!-- <sharepoplogin 
			:visible="showSharePopLogin" 
			@close="handleCloseSharePopLogin"
			@viewBenefits="handleViewBenefits"
		/> -->
		
		<!-- 分享未登录弹窗 -->
		<!-- <sharenpopnologin 
			:visible="showShareNPopNoLogin" 
			@close="handleCloseShareNPopNoLogin"
			@viewBenefits="handleViewBenefits"
		/> -->
	</view>
</template>

<script setup>
import { onMounted, ref, computed } from 'vue';
import { onShareAppMessage, onShow } from '@dcloudio/uni-app';
import sharepoplogin from '@/components/integralArea/sharepoplogin.vue';
import sharenpopnologin from '@/components/integralArea/sharenpopnologin.vue';
import { jump, JumpType } from '../../utils';
import { useGlobalStore } from '@/stores/global';
import { useUserStore } from '@/stores/user';
import { useIntegralStore } from '@/stores/integral';

// 分享登录弹窗状态
const showSharePopLogin = ref(false);

// 分享未登录弹窗状态
const showShareNPopNoLogin = ref(true);

const handlePageClick = () => {
	console.log('handlePageClick');
	if(!isLogin.value) {
		needReinit.value = true;
		jump({
			type: JumpType.INNER,
			url: "/pages/activity/register",
		})
	}
};

// 获取store实例
const userStore = useUserStore();
const globalStore = useGlobalStore();
const integralStore = useIntegralStore();

const isLogin = computed(()=>{
  //return false
  return userStore.isLogin;
})

const isnew = ref(false);

const inviteCode = ref('');
const unionId = ref('oQ9KswkD0qMVwPHQGslkHaJ0QxnE' || '');	
onMounted(() => {
	// 页面加载时显示分享未登录弹窗
	// showShareNPopNoLogin.value = true;

	// 获取邀请码参数
	const pages = getCurrentPages();
	const currentPage = pages[pages.length - 1];
	const options = currentPage.options || {};
	const inviteCode1 = options.inviteCode || '';
	

	inviteCode.value = inviteCode1;

	// 如果有邀请码，调用助力接口
	if (inviteCode.value && isLogin.value) {
		callAssistInvite(inviteCode1,unionId.value);
	}
});

// 调用助力接口
const callAssistInvite = async (ivtCode,unionId) => {
	try {
		// 获取用户unionId
		const wxUnionId = userStore.unionId || '';
		
		const res = await integralStore.assistInvite(ivtCode, unionId, true);
		console.log('助力接口返回:', res);
	} catch (error) {
		console.error('调用助力接口失败:', error);
	}
};

onShow(() => {
	if(isLogin.value) {
		if(isnew.value) {
			globalStore.invateUseType = 'new';
			//新用户注册登录后调用助力接口
			gotoIntegral();


			// 如果有邀请码，调用助力接口
			if (inviteCode.value) {
				callAssistInvite(ivtCode,unionId.value);
			}
			isnew.value = false;
		}
	}
});

// // 关闭分享登录弹窗
// const handleCloseSharePopLogin = () => {
// 	showSharePopLogin.value = false;
// };

// // 关闭分享未登录弹窗
// const handleCloseShareNPopNoLogin = () => {
// 	showShareNPopNoLogin.value = false;
// };

// // 查看权益按钮点击
// const handleViewBenefits = () => {
// 	console.log('查看我的权益');
// 	// 这里可以添加跳转到权益页面的逻辑
// 	uni.showToast({
// 		title: '跳转到权益页面',
// 		icon: 'none'
// 	});
// };

// 一键领取按钮点击事件
const handleClaim = () => {
	if(!isLogin.value) {
		needReinit.value = true;
		jump({
			type: JumpType.INNER,
			url: "/pages/activity/register",
		})
		
		isnew.value = true;
	}else{
		// 设置邀请用户类型
		if(isnew.value) {
			globalStore.invateUseType = 'new';
		} else {
			globalStore.invateUseType = 'old';
		}

		gotoIntegral();
	}
};

const gotoIntegral = () => {
	jump({
		url: '/pages/index/index?pageType=integral',
		type: JumpType.INNER
	});
};

// // 分享配置
// onShareAppMessage(() => {
// 	return {
// 		title: '积分分享',
// 		path: '/pages/invate/sharepage',
// 		imageUrl: ''
// 	};
// });
</script>

<style lang="less" scoped>
@import './sharepage.less';
</style>
