<template>
	<view class="login-page bg-white">



		<view class="image login-bg">



			<image :src="yellowURL+'login-bg.png'" mode="widthFix"></image>



		</view>



		<scroll-view scroll-x class=" bg-white nav padding">



			<view class="flex text-center padding-bottom">



				<!-- 		<view class="cu-item  flex-sub" :class="0==TabCur?'text-red cur':''" @tap="tabSelect(0)">



					个人账号



				</view>
 -->
				<view class="cu-item text-bold text-lg flex-sub">

				</view>

				<view class="cu-item  flex-sub" :class="1==TabCur?'text-red cur':''" @tap="tabSelect(1)">
					企业账号
				</view>
				<view class="cu-item text-bold text-lg flex-sub">

				</view>
			</view>
		</scroll-view>
		<view class="cu-load load-modal" v-if="loadModal">
			<view class="gray-text">加载中...</view>
		</view>
		<view class="margin-lg flex flex-direction text-center" v-if="!otherUser">
			<button class="cu-btn round  bg-main-oil  lg text-lg" open-type="getPhoneNumber"
				@getphonenumber="decryptPhoneNumber">
				<text class="cuIcon-weixin padding-right-xs"> </text>
				<text>
					微信用户一键登录
				</text>
			</button>
			<button class="cu-btn margin-top round lg" @tap="refuseEmpower">
				输入手机号码登录
			</button>
			<button class="cu-btn margin-top bg-0 round lg" @tap="refuseLogin">
				跳过
			</button>
		</view> 
		<view class="isKLUser"  v-if='otherUser'> 
			<view class="isKLUser">卡力用户仅支持跳转登录</view> 
		</view>
	</view>
</template>

<script>
	import loginBottom from './components/loginBottom.vue'
	import oilIdentityApi from '@/api/oil-identity'

	export default {
		components: {
			loginBottom
		},
		data() {
			return { 
				value:'123',
				userHandle: false,
				TabCur: 1,
				otherUser:false,
				accuntSource: false,
				imgURL: this.global.imgURL,
				yellowURL: this.global.yellowURL,
				loadModal: false,
				active: 0,
				password: "",
				random: '',
				driver: {},
				count: 0,
				loginRes: {},
				sessionKey: '',
				nickName: '',
				loginOut: uni.getStorageSync('loginOut') ? true : false
			}
		},
		onLoad(option) {  
			if(option){
				if(option.companyId === '1460872936910831616'){
					this.otherUser = true  
					uni.clearStorageSync() 
					this.weChatLogin(option)
					console.log('当前为卡力用户')
					return
				} 
				
			}
			let IsOtherUser = uni.getStorageSync('isOther')
			console.log('这里是onload',IsOtherUser)
			if(IsOtherUser){ 
				uni.showToast({
					title:'卡力用户仅支持跳转登录',
					icon:'none'
				}) 
				this.otherUser = true
				return
			} else{
				this.weChatLogin()
			}
			console.log('小程序跳转参数接收onLoad----')
			console.log('姓名:',option.driverName) 
			console.log('公司ID:',option.companyId) 
			console.log('车牌号:',option.plateNumber) 
			console.log('油站Id:',option.siteId) 
			console.log('--------------------------') 
			console.log('这里是登录') 
		},
		methods: {
			weChatLogin(option){
				uni.clearStorageSync()
				this.refreshLocation()
				let _that = this
				uni.login({
					provider: 'weixin',
					success: (loginRes) => {
						const code = loginRes.code
						this.onceCode = code
						console.log('这里是code')
						console.log(this.onceCode)
						oilIdentityApi.sendCode(this.onceCode).then(res => { 
							if (res.code === 20000) {
								console.log('这里是sendCode',res)
								this.sessionKey = res.data.sessionKey
								uni.setStorageSync('openid', res.data.openId)
								uni.setStorageSync('unionid', res.data.unionId)
								console.log('这里是213','++++++')
								if(option){
									if(option.companyId == '1460872936910831616'){
									let sysCustomerInfos = {
										userName :option.driverName,
										companyId:option.companyId,
										plateNumber:option.plateNumber,
										siteId:option.siteId,
										phone:option.phone 
									} ,data = {
										tokenInfoDTO:{
											sysCustomerInfo:sysCustomerInfos
										},
										unionId:res.data.unionId
									}
									uni.setStorageSync('isOther',_that.otherUser)
									uni.setStorageSync('otherUserInfo',sysCustomerInfos)
									console.log('此处为data参数：',data)
									console.log('当前为卡利用户：companyId 为',sysCustomerInfos.companyId)
									oilIdentityApi.wechatLogin(data).then(res=>{
										console.log('这里是wechatLogin',res)
										this.OtherloginSuccess(res)
										console.log(res)
									})
									return
								}
								}else{
									console.log('这里是213','----')
									this.autoLogin()
								}
								// uni.setStorageSync('isOther',_that.otherUser)
								
				
							} else if (res.code == 1002) {
								uni.showModal({
									title: '微信登录失败提醒',
									content: `${res.msg}如有疑问，请联系客服处理`
								})
							} else {
								// console.log('res', res.code) 
							} 
						}).catch(err => {
							uni.reLaunch({
								url: '/BagAuth/pages/login/boforeLogin/boforeLogin',
								fail: (err) => {
									// console.log(err)
								}
							})
						})
				
					},
					fail: err => {},
				})
			},
			refuseLogin(){
				uni.switchTab({
					url: '/pages/tabbar/home/home'
				})
			},
			refreshLocation() {
				uni.getLocation({
					type: 'wgs84',
					success: function(res) {
						uni.setStorageSync('location', res)
					}
				});
			},
			autoLogin() {
				let url = 'loginCompany'
				console.log('这里是自动登录')
				// let url = 'loginPersonal'
				oilIdentityApi.autoLogin({
					unionId: uni.getStorageSync('unionid')
				}, url).then(res => {
					if (res.code == 20000) {
						this.loginSuccess(res)
					}
				})
			},
			refuseEmpower() {
				uni.navigateTo({
					url: '/BagAuth/pages/login/oldLogin'
				})
			},



			decryptPhoneNumber(e) {
				// console.log(e.detail)
				if (e.detail.errMsg === 'getPhoneNumber:ok') {
					const data1 = {
						...e.detail,
						sessionKey: this.sessionKey,
						unionId: uni.getStorageSync('unionid'),
						openId: uni.getStorageSync('openid'),
						appId: 'wx7a939c19b270cc3d'
					}
					let url1 = 0 == this.TabCur ? 'bindLoginByPhonePersonal' : 'bindLoginByPhoneCompany'
					this.bindLoginByPhone(data1, url1)
				} else {
					uni.showModal({
						title: '您已拒绝授权手机号'
					})
				}
			},
			bindLoginByPhone(data, url) {
				if (this.count < 1) {
					oilIdentityApi.bindLoginByPhone(data, url).then(res => {
						if (res.code == 20000) {
							
							this.loginSuccess(res)
						}
					})
				}

			},

			tabSelect(e) {
				this.TabCur = e;
			},
			OtherloginSuccess(res){
				console.log('这里是OtherloginSuccess',res)
				let resData = res.data
				if (resData.authTokenDTO.loginFlag) {
					uni.setStorageSync('Authorization', resData.authTokenDTO.accessToken)
					uni.setStorageSync('accountStatus', resData.isCompanyAccount) 
					let user = resData.authTokenDTO.loginUser
					uni.setStorageSync('user', user) 
					setTimeout(() => {
						uni.showToast({
							title: res.msg,
							icon: 'none',
							complete: (err) => {
								// console.log(err)
							}
						})
					}, 1000);
					//  这里有问题
					uni.redirectTo({
						url: '/BagStation/pages/SeparateProcess/stationDetail'
					}) 
				}
			},
			loginSuccess(res) {
				let resData = res.data
				if (resData.authTokenDTO.loginFlag) {
					uni.setStorageSync('Authorization', resData.authTokenDTO.accessToken)
					uni.setStorageSync('accountStatus', resData.isCompanyAccount) 
					let user = resData.authTokenDTO.loginUser
					uni.setStorageSync('user', user) 
					setTimeout(() => {
						uni.showToast({
							title: res.msg,
							icon: 'none',
							complete: (err) => {
								// console.log(err)
							}
						})
					}, 1000);
					//  这里有问题
					uni.switchTab({
						url: '/pages/tabbar/home/home'
					})
				}
			},
		}
	}
</script>

<style>
	.isKLUser{
		font-size: 40rpx;
		font-weight: 700;
		color: #888888;
		text-align: center;
		padding: 2% 0s;
	}
	.login-bg {
		width: 100%;
	}

	.login-bg image {
		width: 750upx;
		vertical-align: bottom;
	}

	.login-page {
		height: 750rpx;
		background-color: #fff;
	}
	.bg-0{
		background-color: transparent;
		color: #555;
	}
</style>
