<template>
	<view class="login-content">
		<view class="login-content-info">
			<view class="login-bgm":style="{backgroundImage:`url(${staticUrl}/img/tuan-index-bg.png)`}">
				<image class="login-logo" :src="staticUrl+'/img/logo.png'" mode="scaleToFill"></image>
				<text>{{ title }}</text>
			</view>
			<view class="login-info">
				<view class="login-info-box">
					<text class="login-info-title">账号密码登录</text>
					<view class="login-info-form">
						<u--form 
						labelWidth="0" 
						:borderBottom="false"
						:model="form" 
						:rules="rules" 
						ref="uForm">
							<u-form-item prop="mobile">
								<u--input 
								v-model="form.mobile" 
								placeholder="请输入账号"
								border="surround"
								shape="circle"
								prefixIcon="account-fill"
								prefixIconStyle="font-size: 22px;color: #909399"
								></u--input>
							</u-form-item>
							<u-form-item prop="password">
								<u--input
								v-model="form.password" 
								placeholder="请输入账号密码"
								border="surround"
								shape="circle"
								prefixIcon="lock-fill"
								:password="true"
								prefixIconStyle="font-size: 22px;color: #909399"
								></u--input>
							</u-form-item>
						</u--form>
					</view>
					<view class="login-info-submit">
						<u-button 
						class="login-info-submit-but"
						@click="submit()"
						:loading="loading"
						loadingText="登录中..."
						>登录</u-button>
					</view>
					<view class="login-info-tip">暂不支持自行注册</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				code:'',
				staticUrl:this.$commonConfig.staticUrl,
				title: '《伟大转折》剧目团购系统',
				logoUrl: this.$commonConfig.staticUrl + "login/logo.png",
				loading: false,
				backUrl:null,
				form: {
					mobile: '',
					password: '',
				},
				rules: {
					'mobile': {
						type: 'string',
						required: true,
						message: '请填写账号',
						trigger: ['blur', 'change']
					},
					'password': {
						type: 'string',
						required: true,
						message: '请填写密码',
						trigger: ['blur', 'change']
					},
				},
			}
		},
		onReady() {
			//如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则。
		    this.$refs.uForm.setRules(this.rules)
		},
		onLoad(e) {
			let that = this;
			uni.getStorage({
				key: 'backUrl',
				success: function (res) {
					// console.log('getStorage',res);
				},
				complete(res) {
					if(res.data){
						that.backUrl = '/'+res.data;
					}else{
						that.backUrl = '/pages/index/index';
					}
					console.log('backUrl',that.backUrl);
				}
			});
			uni.getStorage({
				key:'mobile',
				success: (res) => {
					that.form.mobile = res.data
				}
			})
			// console.log('accessToken=====',this.vuex_user_info.accessToken);
			let accessToken = this.vuex_user_info?.accessToken;
			if(accessToken){
				this.$u.api.teamLoginCheck().then(res=>{
					if(this.backUrl.includes('login/login')||this.backUrl.includes('main/index')){
						 this.backUrl = '/pages/index/index'
					}
					uni.reLaunch({url: this.backUrl});				
				}).catch(err=>{
					console.log('teamLoginCheck',err);
					uni.$u.vuex('vuex_member_info', {});
					uni.$u.vuex('vuex_user_info',  {});
					this.$u.toast(err.msg)
					this.redirectToAuth()
				})
			}else{
				if(!e.code) { // 微信第三方登录失败
					this.redirectToAuth()
				}else {
					this.code = e.code
				}
			}
			
			// 测试环境填充用户名密码
			if(process.env.NODE_ENV=='development'){
				this.form.mobile = '13682277062';
				this.form.password = '123456';
			}
		},
		onShow() {

		},
		methods: {
			/**  公众号 微信授权登录  */
			redirectToAuth() {
				try{
					window.location.href = this.$commonConfig.authUrl;
				}catch(e){
					alert(`redirectToAuth e:${e}`)
				}
			    
			},
			/**
			 * 
			 * 提交登录
			 * 
			 */
			async submit(e) {
				let _this = this;
				let wxinfo = null
				let data = {}
				wxinfo = await this.$u.api.wxinfoH5({code:this.code});
				let { openid } = wxinfo.data;
				this.form.h5OpenId = openid;
				
				this.loading = true
				this.$refs.uForm.validate().then(res => {
					this.$u.toast('校验通过');
					uni.setStorage({
						key:'mobile',
						data:this.form.mobile
					});
					this.$u.api.teamLogin(this.form).then(res=>{
						this.loading = false
						// console.log('res',res.data);
						this.$u.vuex('vuex_user_info', res.data);
						uni.reLaunch({url: this.backUrl});
					}).catch(err=>{
						console.log('login',err);
						this.$u.toast(err.msg);
						this.loading = false;
						setTimeout(()=>{
							this.redirectToAuth();
						},1500)
					})
				}).catch(errors => {
					this.$u.toast('校验失败')
					this.loading = false
				})
			}
		}
	}
</script>
<style lang="scss" scoped>
	.login-content {
		width: 100%;
		height: 100vh;
		position: relative;
		box-sizing: border-box;
		--bgm-height: 630rpx;
	}

	/**  背景  */
	.login-content-box {
		width: 100%;
		height: 100%;
		box-sizing: border-box;
	}

	.login-bgm {
		width: 100%;
		height: 630rpx;
		// background-image: url("#{$image-beas-url}login/bgm.png");
		background-size: 100% auto;
		background-repeat: no-repeat;
		box-sizing: border-box;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;

		.login-logo {
			width: 240rpx;
			height: 172rpx;
			padding: 50rpx 0;
		}

		>text {
			font-size: 32rpx;
			font-family: SourceHanSansCN, SourceHanSansCN;
			font-weight: bold;
			color: #FFD788;
		}
	}

	.login-info {
		width: 100%;
		height: calc(100% - var(--bgm-height) + 40rpx);
		background-color: #fff;
		border-radius: 28rpx 28rpx 0rpx 0rpx;
		position: absolute;
		bottom: 0;
		left: 0;
		z-index: 22;
		box-sizing: border-box;
		padding: 80rpx;
		.login-info-box {
			width: 100%;
			height: 100%;
			display: flex;
			flex-direction: column;
			box-sizing: border-box;
			.login-info-title {
				font-size: 32rpx;
				font-family: SourceHanSansCN, SourceHanSansCN;
				font-weight: bold;
				color: #2D2D2D;
				padding-bottom: 40rpx;
			}
			.login-info-form {
				
			}
			.login-info-submit {
				width: 100%;
				height: 80rpx;
				padding: 40rpx 0 20rpx;
				flex-shrink: 0;
				.login-info-submit-but {
					display: flex;
					justify-content: center;
					align-items: center;
					border-radius: 40rpx;
					width: 100% !important;
					height: 100% !important;
					background-color: #ed0000;
					color: #fff;
				}
			}
			.login-info-tip {
				width: 100%;
				text-align: center;
				font-size: 24rpx;
				font-family: SourceHanSansCN, SourceHanSansCN;
				font-weight: 400;
				color: #C2C2C2;
			}
		}
	}

</style>