<template>
  <view class="callback-container">
	  回调成功{{ceshi.name}}{{ceshi.openid}}
  <!--  <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 { useWeChatAuth } from '@/compontent/useWeChatAuth.js';
import request from '@/request/index.js';
import { reactive,ref } from 'vue';

const { fetchUserInfo, openid, userInfo } = useWeChatAuth();

const ceshi = reactive({
	name:"",
	openid:"",
})
const jumpto = () =>{
	console.log("???")
	uni.navigateTo({
		url: "/pages/pay/index"
	});
}
const getOpenId = (code) =>{
	const url = `https://www.focusnu.com/nursing-unit/weixin/wechat/callback?code=${encodeURIComponent(code)}`;
		
	fetch(url)
	  .then(res => res.json())
	  .then(data => {
	    console.log("✅ 获取用户信息成功:", data);
		ceshi.name = data.nickname
		ceshi.openid = data.openid
		uni.setStorage({
		  key: 'openid',
		  data: {
		    openid: data.openid,
		  }
		});
		getUserMessage()
		
	  })
	  .catch(err => {
	    console.error("❌ 获取用户信息失败:", err);
	  });
}
const getUserMessage = () =>{
	const url = `https://www.focusnu.com/nursing-unit/h5Api/nuBizAdvisoryInfo/queryWeixinInfo?openId=${encodeURIComponent(ceshi.openid)}&wechatName=${encodeURIComponent(ceshi.name)}`;
	fetch(url)
	  .then(res => res.json())
	  .then(data => {
		  console.log("个人信息打印",data)
		  getjigou()
	  } )
}
const jigouArray = ref([]);
const getjigou = () =>{
	const url = `https://www.focusnu.com/nursing-unit/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 = `https://www.focusnu.com/nursing-unit/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>
.callback-container {
  padding: 24px;
}
.avatar {
  width: 80px;
  height: 80px;
  border-radius: 40px;
  margin-top: 12px;
}
</style>