<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>
		<SwitchEnterprises @logout='logout'  @submit='goHome'  :is-show.sync="isSwitchEnterprises"></SwitchEnterprises>
	</view>
</template>

<script>
	import SwitchEnterprises from '@/components/SwitchEnterprises.vue'
	import loginBottom from './components/loginBottom.vue'
	import oilIdentityApi from '@/api/oil-identity'
	import accountApi from '@/api/account.js'
	export default {
		components: {
			loginBottom,
			SwitchEnterprises
		},
		data() {
			return { 
				isSwitchEnterprises:false,
				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) {  
			this.init(option);
			console.log('onLoad')
		},
		onShow() {
			console.log('show'); 
			
		},
		watch:{
			isSwitchEnterprises(n,o){ }
		},
		methods: { 
			onpwd(){
				uni.$on('')
			},
			init(option){
				if(option){
					if(option.companyId === '1460872936910831616'){
						this.otherUser = true  
						uni.clearStorageSync() 
						this.weChatLogin(option)
						console.log('当前为卡力用户')
						return
					}  
				}
				let IsOtherUser = uni.getStorageSync('isOther') 
				if(IsOtherUser){ 
					uni.showToast({
						title:'卡力用户仅支持跳转登录',
						icon:'none'
					}) 
					this.otherUser = true
					return
				} else{
					this.weChatLogin()
				} 
			},
			logout(){
				let that = this
				accountApi.logout('authSystem').then(res => {
					if (res.code == '20000') {
						uni.showToast({
							title: res.msg
						})
						uni.clearStorageSync(); 
						this.isSwitchEnterprises = false;
						this.init()
					}
				})
			},
			goHome(){
				uni.switchTab({
					url: '/pages/tabbar/home/home'
				})
			},
			weChatLogin(option){
				let isJump = uni.getStorageSync('isJump');
				uni.clearStorageSync();
				if(isJump){
					uni.setStorageSync('isJump',isJump)
				}
				this.refreshLocation()
				let _that = this
				uni.login({
					provider: 'weixin',
					success: (loginRes) => {
						const code = loginRes.code
						this.onceCode = code

						oilIdentityApi.sendCode(this.onceCode).then(res => { 
							if (res.code === 20000) {

								this.sessionKey = res.data.sessionKey
								uni.setStorageSync('openid', res.data.openId)
								uni.setStorageSync('unionid', res.data.unionId) 
								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.clearStorageSync()
				uni.setStorageSync('refuseLogin','true')
				uni.switchTab({
					url: '/pages/tabbar/home/home'
				})
			},
			refreshLocation() {
				uni.getLocation({
					type: 'wgs84',
					success: function(res) {
						uni.setStorageSync('location', res)
					}
				});
			},
			autoLogin() {
				let url = 'loginCompany'

				// let url = 'loginPersonal'
				oilIdentityApi.autoLogin({
					unionId: uni.getStorageSync('unionid')
				}, url,{closePrompt:true}).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,{closePrompt:true}).then(res => {
						if (res.code == 20000) { 
							this.loginSuccess(res)
						}
					})
				}

			},

			tabSelect(e) {
				this.TabCur = e;
			},
			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(() => { 
					}, 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) 
					this.isSwitchEnterprises = true
					// //  这里有问题
					// 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>
