<template>
	<view class="phone-login-page">
		<view class="login-wrap-box">
			<view class="bjcx-head-box">
				<icon class="bjcx-logo-box"></icon>
				<view class="bjcx-logo-title">家政学</view>
			</view>
			<!-- 合同分享下 -->
			<view v-if="fromAppShare" class="wechat-auth-box">
				<!-- <view class="auth-tips">欢迎使用小程序</view> -->
				<view class="agreement-checkbox-box">
					<checkbox-group @change="handleChange">
						<checkbox class="agreement-checkbox-input" color="#3fd2a1" value="agree" :checked="isAgreed"
							style="transform:scale(0.7)" />
					</checkbox-group>
					<view class="agreement-text-box mt-big">
						在使用当前小程序服务之前，请仔细阅读<view class="agreement-text" @click="agreeBtn('yhxy')">《诚祥学用户协议》</view>和<view
							@click="agreeBtn('ystk')" class="agreement-text">《诚祥学隐私政策》</view>，如您同意，请勾选后开始使用
					</view>
				</view>
				<button class="phone-green-btn wechat-auth-btn" type="default" open-type="getPhoneNumber" @getphonenumber="onWechatAuth">
					微信一键登录
				</button>
				<view class="qtdl-btn"><text @click="handleChangeFangshi">其他登录</text></view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import cacheManager from '@/utils/cacheManager.js'
	import * as httpApi from "@/api/login.js"
	import {
		ref,
		onMounted
	} from "vue"
	import {
		toast
	} from "@/utils/common";
	import {
		useIsCanBack
	} from "@/store/isCanBack.js"
	import {
		onLoad
	} from "@dcloudio/uni-app";

	// 原有变量
	const userName = ref('')
	const password = ref('')
	const showPassword = ref(true)
	const clearTelIcon = ref(false)
	const clearPwIcon = ref(false)
	const isAgreed = ref(false)
	const id = ref('')
	const code = ref('')

	const fromPage = ref('');
	const openId = ref('');

	// 新增变量
	const fromAppShare = ref(true) // 是否来自APP分享

	const store = useIsCanBack();

	onLoad((options) => {
		if (options.from === 'appcx') {
			id.value = options.id
			fromPage.value = options.from;
		}
		getAllImg();
	})

	function handleChange() {
		isAgreed.value = !isAgreed.value
	}

	function agreeBtn(code) {
		if (code === 'yhxy') {
			uni.navigateTo({
				url: "/pages/client/my/xieyi"
			})
		} else {
			uni.navigateTo({
				url: "/pages/client/my/zhengce"
			})
		}
	}

	function handleChangeFangshi() {
		let str = ``;
		if (id.value) {
			str = str + `id=${id.value}`
		}
		if (id.value && fromPage.value) {
			str = str + `&from=${fromPage.value}`
		} else if (!id.value && fromPage.value) {
			str = str + `from=${fromPage.value}`
		}

		if (str) {
			uni.redirectTo({
				url: `/pages/Login/test?${str}`
			})
		} else {
			uni.redirectTo({
				url: `/pages/Login/test`
			})
		}
	}
	// 微信授权登录
	function onWechatAuth(e) {
		if (!isAgreed.value) {
			toast('请先阅读并同意用户协议和隐私政策')
			return // 直接返回，不执行后面的授权逻辑
		}
		if (e.detail.errMsg.includes('fail')) {
			toast('授权失败')
			return
		}
		uni.showLoading({
			title: '登录中'
		})
		try {
			uni.login().then(res1 => {
				console.log('res2222222', res1);
				code.value = res1.code
				httpApi.loginApplet({
					code: res1.code,
				}).then(result => {
					console.log('result2222', result);
					openId.value = result.data.openId
					if (result.data.bind) {
						// 保存用户信息
						if (result.data.loginVo.type == 4) {
							cacheManager.set('auth', result.data.loginVo)
							store.setIsCanBack(false)
							gotoPage();
						} else if (result.data.loginVo.type == 6) {
							cacheManager.set('auth', result.data.loginVo)
							store.setIsCanBack(false)
							gotoPage2();
						} else {
							toast('登录失败，您的身份有误，请联系管理员。')
							return
						}
					} else {
						console.log('1231');
						//.value = true
						httpApi.bindApplet({
							code: e.detail.code,
							openId: openId.value,
						}).then(result => {
							if (result.data.type == 4) {
								cacheManager.set('auth', result.data)
								store.setIsCanBack(false)
								gotoPage();
							} else if (result.data.type == 6) {
								cacheManager.set('auth', result.data)
								store.setIsCanBack(false)
								gotoPage2();
							} else {
								toast('登录失败，您的身份有误，请联系管理员。')
							}
						})
					}
				})
			})
		} catch (error) {
			toast('bindApplet失败')
		} finally {
			uni.hideLoading()
		}
	}

	function gotoPage() {
		console.log('ididid', id.value);
		if (id.value) {
			uni.redirectTo({
				url: `/pages/client/hetong/hetongInfo?id=` + id.value + `&from=${fromPage.value}`
			})
		} else {
			uni.redirectTo({
				url: `/pages/client/ShouYe/shouye`
			})
		}
	}

	function gotoPage2() {
		console.log('ididid222222', id.value);
		if (id.value) {
			uni.redirectTo({
				url: `/pages/kehu/hetong/hetongInfo?id=` + id.value + `&from=${fromPage.value}`
			})
		} else {
			uni.redirectTo({
				url: `/pages/kehu/shouYe/shouye`
			})
		}
	}

	function getAllImg() {
		httpApi.getAllImgList({}).then(res => {
			cacheManager.set('projectImg', res.data)
		});
	}
</script>
