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



		<view class="image login-bg">



			<!-- <image :src="imgURL+'login-bg.png'" mode="widthFix"></image> -->
			<!-- <image src="../../../BagStation/static/img/LV.png" mode="widthFix"></image> -->
			<image src="../../../static/img/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">

				</view>

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



					个人账号



				</view>

				<view class="cu-item  flex-sub">

				</view>

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



					企业账号



				</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">



			<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 bg-0 margin-top round  text-sm" @tap="refuseLogin">
				跳过
			</button>

		</view>



		<!-- <loginBottom :check="loginOut" /> -->



	</view>
</template>

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


	export default {
		components: {
			loginBottom
		},
		data() {
			return {
				// imgsrc:require('../../../BagStation/static/img/login-bg.png'),
				userHandle: false,
				TabCur: 0,
				accuntSource: false,
				imgURL: this.global.imgURL,
				loadModal: false,
				active: 0,
				password: "",
				random: '',
				driver: {},
				count: 0,
				loginRes: {},
				sessionKey: '',
				nickName: '',
				loginOut: uni.getStorageSync('loginOut') ? true : false,
				// scanningParams:null
			}
		},
		onLoad(options) {
            let scanningParamsLocale = uni.getStorageSync('scanningParams')
			uni.clearStorageSync()
			if(scanningParamsLocale || options.scanningParams) {
				// 用于扫码进入油站详情页面
				uni.setStorageSync('scanningParams',options.scanningParams || scanningParamsLocale)
				console.log('login scanningParams',uni.getStorageSync('scanningParams'))
			}
			this.refreshLocation()
			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)
							this.autoLogin()

						} 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 => {},
			})
		},
		methods: {
			refuseLogin() {
				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).then(res => {
					if (res.code == 20000) {
						this.loginSuccess(res)
					}
				})
			},
			refuseEmpower() {
				uni.navigateTo({
					url: '/BagAuth/pages/login/oldLogin'
				})
			},



			decryptPhoneNumber(e) {
				if (e.detail.errMsg === 'getPhoneNumber:ok') {
					const data1 = {
						...e.detail,
						sessionKey: this.sessionKey,
						unionId: uni.getStorageSync('unionid'),
						openId: uni.getStorageSync('openid'),
					}
					let url1 = 0 == this.TabCur ? 'bindLoginByPhonePersonal' : 'bindLoginByPhoneCompany'
					this.bindLoginByPhone(data1, url1)
				} else {
					uni.showModal({
						title: '用户拒绝手机号的授权',
						content: `请输入手机号码登录，如有疑问，请联系客服处理`
					})
				}
			},
			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;
			},
			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) => {
							}
						})
					}, 1000);
					let scanningParams = uni.getStorageSync('scanningParams')
					if(scanningParams) {
						uni.redirectTo({
							url: `/BagStation/pages/stationDetail/stationDetail?q=${scanningParams}`
						})
						return 
					}
					//  这里有问题
					uni.switchTab({
						url: '/pages/tabbar/home/home'
					})
				}
			},
		}
	}
</script>

<style>
	.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: #555555;
	}
</style>
