<template>
	<view class="login-container">
		<image class="imge" src="/static/index/nu.png" />
		<view class="font">
			页面跳转中，请稍后...
		</view>
		
		<!-- 底部的栏，为啥这样写，是因为要做左右拉动 -->
		<!-- <view class="botton-view">
			<view v-for="(item,index) in itemArray" class="array-father">
				<view :class="itemTarget===index ? `bottom-button-target` : `bottom-button`" @click="itemTarget=index">
					<view class="">
						{{item}}
					</view>
				</view>
				<view v-show="itemTarget===index" class="blue-heng"></view>
			</view>
		</view> -->
		
	</view>
	<!-- <view class="callback-container">
		回调成功{{ceshi.name}}{{ceshi.openid}} -->
		<!-- {{look}} -->
		<!--  <text v-if="!userInfo">授权中...</text>
    <view v-else>
      <text>OpenID: {{ openid }}</text>
      <text v-if="userInfo.nickname">昵称: {{ userInfo.nickname }}</text>
      <image v-if="userInfo.headimgurl" :src="userInfo.headimgurl" class="avatar" />
    </view> -->
		<!-- <view v-for="(item,index) in jigouArray" :key="index">
			<view style="font-size: 30rpx;margin-top: 10rpx;font-weight: 700;" @click="jigouClick(item)">
				{{item.departName}}
			</view>
			<image style="width: 60rpx;height: 60rpx;"
				:src="`https://www.focusnu.com/nursing-unit/sys/common/static/${item.picUrl}`" />
		</view>
		<view v-for="(item,index) in secondArray" :key="index" @click="jumpto">
			<view style="font-size: 30rpx;margin-top: 10rpx;font-weight: 700;">
				{{item.nuName}}
			</view>
		</view> 
	</view> -->
</template>

<script setup>
	import {
		onLoad
	} from '@dcloudio/uni-app';
	import { base_url } from '@/request/index.js'
	import request from '@/request/index.js';
	import {
		reactive,
		ref
	} from 'vue';
	import {getList} from "@/api/loginApi.js"
	
	const itemArray = ["NU", "动态", "我的"];
	const itemTarget = ref(0);
	// const {
	// 	fetchUserInfo,
	// 	openid,
	// 	userInfo
	// } = useWeChatAuth();

	const ceshi = reactive({
		name: "",
		openid: "",
		accessToken: ""
	})
	// const jumpto = () => {
	// 	console.log("???")
	// 	uni.navigateTo({
	// 		url: "/pages/pay/index"
	// 	});
	// }
	const getOpenId = (code) => {
		const url = `${base_url}/weixin/wechat/callback?code=${encodeURIComponent(code)}`;

		fetch(url)
			.then(res => res.json())
			.then(data => {	
				ceshi.name = data.data.nickname
				ceshi.openid = data.data.openid
				ceshi.accessToken = data.accessToken
				uni.setStorage({
					key: 'openid',
					data: {
						openid: data.data.openid,
						accessToken: data.accessToken,
					}
				});
				getUserMessage()

			})
			.catch(err => {
				console.error("❌ 获取用户信息失败:", err);
			});
	}
	// const serve = ref("")
	const look = ref("")
	const getUserMessage = () => {
		const url =
			`${base_url}/h5Api/nuBizAdvisoryInfo/queryWeixinInfo?openId=${encodeURIComponent(ceshi.openid)}`;
		fetch(url)
			.then(res => res.json())
			.then(data => {
				console.log("个人信息打印", data)
				uni.setStorageSync('token', data.result.token);
				uni.setStorageSync('serverUrl', data.result.serverUrl);
				console.log("???token存储",data.result.token)
				// const post = `${uni.getStorageSync('serverUrl')}/weiXinPay/getJsApiInfo`;
				// const pay = {
				// 	access_token: ceshi.accessToken,
				// };
				// fetch(post, {
				// 		method: 'POST',
				// 		headers: {
				// 			'Content-Type': 'application/json'
				// 		},
				// 		body: JSON.stringify(pay)
				// 	})
				// 	.then(res => res.json())
				// 	.then(data => {
				// 		// secondArray.value = [...data.result];
				// 		console.log("???调取微信", data)
						
				// 	})
				// 	.catch(err => {
				// 		console.error('请求失败:', err);
				// 	});
				// const urlpost = `${data.result.serverUrl}/weiXinPay/getUserInfo`;
				// const payload = {
				// 	openid: ceshi.openid,
				// 	access_token: ceshi.accessToken,
				// 	// serverUrl: serve.value
				// };
				// console.log("???/", payload)
				// fetch(urlpost, {
				// 		method: 'POST',
				// 		headers: {
				// 			'Content-Type': 'application/json'
				// 		},
				// 		body: JSON.stringify(payload)
				// 	})
				// 	.then(res => res.json())
				// 	.then(data => {
				// 		// secondArray.value = [...data.result];
				// 		look.value = data
				// 		console.log("！！！！！！！！！！！！！！！！！！！！！！！", data)
				// 	})
				// 	.catch(err => {
				// 		console.error('请求失败:', err);
				// 	});

				if(!data.result.tel){
					uni.redirectTo({
						url: `/pages/login/phonebumber`
					});
				}else{
					uni.redirectTo({
						url: `/pages/login/threeselectone`
					});
					uni.setStorageSync('tel', data.result.tel);
				}
				getjigou()
			})

	}
	const jigouArray = ref([]);
	const getjigou = () => {
		const url = `${base_url}/sys/sysDepart/queryInstitutionsList`;
		fetch(url)
			.then(res => res.json())
			.then(data => {
				jigouArray.value = [...data]
				console.log("机构打印", jigouArray.value)
			})
	}
	const secondArray = ref([]);
	const jigouClick = (element) => {
		const url = `${element.serverUrl}/h5Api/nuBaseInfo/list`;
		fetch(url)
			.then(res => res.json())
			.then(data => {
				secondArray.value = [...data.result]
			})
		uni.setStorage({
			key: 'serverUrl',
			data: {
				url: element.serverUrl,
			}
		});
		const urlpost = `${base_url}/h5Api/nuBizAdvisoryInfo/editNuBizAdvisoryInfo`;
		const payload = {
			openId: ceshi.openid,
			serverUrl: element.serverUrl
		};
		console.log("???/", payload)
		fetch(urlpost, {
				method: 'POST',
				headers: {
					'Content-Type': 'application/json'
				},
				body: JSON.stringify(payload)
			})
			.then(res => res.json())
			.then(data => {
				// secondArray.value = [...data.result];
				console.log("???", data)
			})
			.catch(err => {
				console.error('请求失败:', err);
			});
	}
	onLoad(() => {
		const href = window.location.href;
		const queryString = href.split('?')[1]?.split('#')[0]; // 提取 ? 和 # 之间的参数
		const query = {};

		if (queryString) {
			queryString.split('&').forEach(pair => {
				const [key, value] = pair.split('=');
				query[key] = decodeURIComponent(value);
			});
		}

		console.log('解析到的 query 参数:', query);

		if (query.code) {
			getOpenId(query.code)
			// 调用你的方法，比如 fetchUserInfo(query.code)
		}
	});
</script>

<style scoped lang="scss">
	.callback-container {
		padding: 24px;
	}

	.avatar {
		width: 80px;
		height: 80px;
		border-radius: 40px;
		margin-top: 12px;
	}
	.login-container {
		display: flex;
		flex-direction: column;
		min-height: 100vh;
		width: 100%;
		background-color: rgb(239, 241, 252);
		position: relative;
		justify-content: center;
		align-items: center;
	}
	
	.array-father {
		width: 33%;
		position: relative;
	}
	
	.botton-view {
		position: fixed;
		bottom: 0;
		left: 0;
		height: 200rpx;
		width: 100%;
		// background-color: greenyellow;
		display: flex;
		justify-content: space-between;
		font-weight: 500;
		
		.bottom-button {
			width: 100%;
			height: 100%;
			display: flex;
			justify-content: center;
			align-items: center;
		}
	
		.bottom-button-target {
			width: 100%;
			height: 100%;
			display: flex;
			justify-content: center;
			align-items: center;
			color: rgb(42, 133, 235);
		}
	
		.blue-heng {
			height: 6rpx;
			width: 150rpx;
			background-color: rgb(42, 133, 235);
			position: absolute;
			bottom: 55rpx;
			left: 50%;
			/* 左边缘到父容器左边的距离占父宽度 50% */
			transform: translateX(-50%);
		}
	}
	.imge{
		width: 240rpx;
		height: 240rpx;
		margin-bottom: 50rpx;
	}
	.font{
		font-weight: 600;
		font-size: 35rpx;
		// margin-bottom: 100rpx;
	}
</style>