<template>
	<view class="content">
		<image v-if="isBind" src="https://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/app/newImages/3da41caccf8346b188809fac656ba834.jpg" mode="widthFix"></image>
		<view v-if="!isBind" class="bindbox">{{msg}}</view>
		<!-- <view class="bindbox">
			<image :src="baseUrl + '/images/bindComUser.png'" mode="aspectFill"></image>
			<view>立即绑定，享受更多权益</view>
		</view>
		<button class="bind-btn" :loading="btnLodaing" :disabled="btnLodaing" @click="handleBind">立即绑定</button> -->
	</view>
</template>

<script>
	import {getUserInfo} from '@/api/user.js'
	import { bindCompanyFsUser } from '@/api/companyUser.js'
	export default {
		data() {
			return {
				isBind: false,
				msg: '绑定中...',
				baseUrl:uni.getStorageSync('requestPath'),
				btnLodaing: false,
				companyUserId: '',
			}
		},
		onLoad(option) {
			let obj = {}
			if(option.q) {
				let url = decodeURIComponent(option.q) // 获取到二维码原始链接内容
				// // 对url中携带的参数提取处理
				obj = this.$urlToObj(url)
			}
			this.companyUserId = obj.companyUserId || option.companyUserId
		},
		onShow() {
			if(!this.isBind) {
				const historyApp = uni.getStorageSync('historyApp') || '';
				if(historyApp) {
					this.handleBind()
				} else {
					this.getUser()
				}
			} else {
				this.msg = '已绑定或已经存在绑定关系'
			}
		},
		methods: {
			getUser() {
				uni.showLoading({
					title: '绑定中...'
				})
				getUserInfo().then(
					res => {
						if(res.code==200){
							if(res.user!=null){
								if(res.user.historyApp) {
									uni.setStorageSync('historyApp',res.user.historyApp)
									this.handleBind()
								} else {
									uni.hideLoading()
									uni.redirectTo({
										url: '/pages_user/downApp'
									})
								}
							}
							else{
								uni.hideLoading()
								this.utils.loginOut();
							}
							
						}else{
							uni.hideLoading()
							uni.showToast({
								icon:'none',
								title: "请求失败",
							});
						}
					},
					rej => {}
				).catch(()=>{
					uni.hideLoading()
				});
			},
			handleBind() {
				uni.showLoading({
					title: '绑定中...'
				})
				this.msg = '绑定中...'
				this.$isLogin().then(
					res => {
						if(res){
							this.submit();
						}
						else{
							uni.hideLoading()
							uni.navigateTo({
								url:'/pages/auth/login'
							})
						}
					}
				).catch(()=>{
					this.msg = '绑定失败'
					uni.hideLoading()
				});
			},
			submit() {
				this.btnLodaing = true
				uni.showLoading({
					title: '绑定中...'
				})
				bindCompanyFsUser(this.companyUserId).then(res=>{
					this.btnLodaing = false
					uni.hideLoading()
					if(res.code == 200) {
						this.isBind = true
						this.msg = '绑定成功'
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}else {
						if(res.msg=="已存在绑定关系") {
							this.isBind = true
							this.msg = '绑定成功'
							uni.showToast({
								title: '绑定成功',
								icon: 'none'
							})
						} else {
							this.msg = res.msg
							this.isBind = false
							uni.showToast({
								title: res.msg,
								icon: 'none'
							})
						}
					}
				}).catch(()=>{
					this.msg = '绑定失败'
					this.isBind = false
					this.btnLodaing = false
					uni.hideLoading()
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.bindbox {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding-top: 20vh;
		font-family: PingFang SC, PingFang SC;
		font-weight: 400;
		font-size: 32rpx;
		color: #757575;
		image {
			height: 150rpx;
			width: 150rpx;
			margin-bottom: 50rpx;
		}
	}
	.content {
		width: 100%;
		display: flex;
		flex-direction: column;
		align-items: center;
		
	}
	.bind-btn {
		margin: 20vh auto 20px auto;
		width: 300px;
		height: 44px;
		line-height: 44px;
		text-align: center;
		background: #FF5C03 !important;
		color: #fff !important;
		font-size: 16px;
		font-weight: 500;
		border-radius: 22px;
		display: block;
		&:after {
			border: none;
		}
	}
</style>
