<style scoped>
	.pages{
        min-height: 100vh;
		background-color: #f6f6f6;
    }
	.addChild-header{
        flex-direction: column;
        align-items: center;
        background: #ffffff;
    }
    .addChild-header image{
        padding: 54rpx 0 31rpx 0;
    }
    .addChild-header text{
        font-size: 34rpx;
        font-weight: 800;
        color: #111111;
        padding: 0 0 69rpx 0;
    }
    .title{
        font-size: 34rpx;
        font-weight: 500;
        color: #282828;
        text-align: center;
        padding: 123rpx 0 50rpx 0;
    }
    .addChild-buttom{
        width: 450rpx;
        margin: auto;
        background: #C91727;
        border-radius: 44px;
        line-height: 88rpx;
        color: #ffffff;
        font-weight: 500;
        font-size: 30rpx;
        z-index: 999;
        text-align: center;
    }
    .addChild-buttom.active{
        background: rgba(201, 23, 39, .5);
    }
</style>
<template>
	<view class="addChild pages">
        <view class="addChild-header flex">
            <image src="https://viitto-1301420277.cos.ap-chengdu.myqcloud.com/Test/Upload/Goods/1664160541000_21.png"
            style="width: 352rpx;height: 218rpx;"></image>
            <text>{{AccountName?AccountName:'**'}}的亲友团</text>
        </view>
        <view class="title">
            {{nickName?nickName:'**'}}邀请您加入亲友团
        </view>
        <view v-if="loading" class="addChild-buttom" @click="getUserProfile">
            立即加入
        </view>
        <view v-else class="addChild-buttom active">
            立即加入
        </view>
	</view>
</template>

<script>
	import {
		ref,
		reactive,
		toRefs,
		toRef,
		getCurrentInstance,
		watch,
		computed,
		onMounted,
		inject,
	} from "vue";
	import Loadmore from "../../components/loadmore.vue";
	import bottom from "../../components/bottom";
	import {
		getWeChatOpenId,loginByUnionId,getLoginAccounts,SetFriend
	} from "../../api/erp";
	export default {
		components: {
			bottom,
			Loadmore,
		},
		setup(props, context) {
			let {
				proxy
			} = getCurrentInstance();
			let data = reactive({
				obj: {
					Name: '',
					Photo: '',
				},
				nickName:'',
				AccountName:'',
				OldUnionId:'',
                loading:true,
				form:{
					OldUnionId:'',
					NewUnionId:''
				},
            });
			const pages = getCurrentPages() // 获取栈实例
			const currentRoute = pages[pages.length - 1].route; // 获取当前页面路由
			const methods = {
				getUserProfile() {
					//新的获取用户资料
					var that = this;
					wx.getUserProfile({
						desc: "用于完善资料", // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
						success: (info) => {
							uni.setStorageSync('userWxInfo',info)
							wx.login({
							  //成功放回
							  success:(res)=>{
								uni.showLoading({
									title:'尝试授权登陆'
								})
								that.loginByOpenCode(res.code)
							  }
							})
						},
						fail: () => {
							uni.showToast({
								title: "微信授权失败",
								icon: "none",
							});
						},
					});
				},
				loginByOpenCode(Code){
					getWeChatOpenId({Code}).then(r=>{
						uni.setStorageSync('wxUserAuthInfo',r.Data)
						data.userAuth=r.Data
						if(r.Data.unionid){
							that.addFriend(r)
						}
					}).catch(e=>{})
				},
				addFriend(r){
					data.form = {
						OldUnionId: data.OldUnionId,
						NewUnionId: uni.getStorageSync('wxUserAuthInfo').unionid
					}
					data.loading = false
					uni.showLoading()
					let res = SetFriend(data.form);
					if (res.Code==1) {
						uni.hideLoading()
						data.loading = true
						that.setUserLoginByUnionId(r.Data.unionid)
					}else{
						uni.hideLoading()
						data.loading = true
						uni.showToast({
							title: res.Message,
							icon: "none",
						});
					}
                },
				setUserLoginByUnionId(uid){
					loginByUnionId({uid}).then(r=>{
						if(r.Data && r.Data.length>0){
							uni.setStorageSync("ErpStus",r.Data)
							let account=[]
							r.Data.forEach(x=>{
								account.push(x.ClassInAccount)
							})
							that.setAccountsLogin(`'${account.join("','")}'`)
						}
					}).catch(x=>{
						uni.hideLoading()
						data.loading = true
					})
				},
				setAccountsLogin(Accounts){
					getLoginAccounts({Accounts}).then(r=>{
						let erpStus=uni.getStorageSync('ErpStus')
						uni.hideLoading()
						data.loading = true
						if(r.Data && r.Data.length>0){
							
							if(erpStus){
								erpStus.forEach(x=>{
									if(x.ClassInAccount==r.Data[0].Account){
										uni.setStorageSync('erpUserInfo',x)
									}
								})
							}
							uni.setStorageSync("userInfo", r.Data[0]);
							uni.setStorageSync("users",r.Data)
							console.log(r.Data && r.Data.length>0)
							uni.reLaunch({
								url:'/pages/index/index'
							})
						}else if(erpStus){
							uni.setStorageSync('erpUserInfo',x[0])
							uni.reLaunch({
								url:'/pages/index/index'
							})
						}
						
					}).catch(e=>{
						uni.hideLoading()
						data.loading = true
					})
				},
				
			};
			let that = methods;
			onMounted(() => {
				
			});
			return {
				...toRefs(data),
				...methods
			};
		},
		onLoad(options) {
			this.OldUnionId = options.unionid
			this.AccountName = options.AccountName
			this.nickName = options.nickName
		},
        onShow(){

        },
		onReachBottom() {
			
		}
	};
</script>
