<template>
	<view class="container">
		<view class="bottom-sign">
			<image v-if="theme" :src="`/static/public/login_bg_${theme}.png`"></image>
		</view>
		<view class="back-btn" @click="navBack">
			<image src="@/static/icons/left_back@3x.png"></image>
		</view>
		<view class="wrapper">
			<view class="tips">
				<view class="tips_title">{{tips.title}}</view>
				<view class="tips_text">{{tips.text}}</view>
			</view>
			<view class="input_box">
				<view class="area_select" @click="isShowAreaCheck = true">
					<view class="area">{{checkData.checkedCode}}</view>
					<image class="icon_down" src="@/static/icons/icon_down.png"></image>
				</view>
				<input type="number" maxlength="11" v-model="mobile" placeholder="输入手机号" />
				<image v-if="mobile" class="icon_clear" src="@/static/icons/icon_clear.png" @click="mobile=''"></image>
			</view>
			<button :class="{'confirm-btn':true,disbled:!canSend}" :disabled="!canSend"
				@click="getCode">获取短信验证码</button>
			<view class="checkbox_box">
				<label class="radio" @click="isRead = !isRead">
					<radio value="" :checked="isRead" />已阅读并同意 <text class="textBtn"
						@click.stop="toRead(0)">《用户协议》</text>、
					<text class="textBtn" @click.stop="toRead(1)">《隐私政策》</text>
				</label>
			</view>
			<view class="login_icon">
				<image class="wechat" v-if="useWechatLogin" @click="loginByWechat" src="@/static/icons/icon_wechat.png"></image>
				<image class="apple" v-if="useAppleLogin" @click="loginByApple" src="@/static/icons/icon_apple.png"></image>
			</view>
		</view>
		<check-code :data="showData" @closeModal="closeModal" @login="doLogin" @regetCode="regetCode" v-if="isShowCode"
			class="modal-box" />
		<check-area :data="checkData" @closeModal="closeModal" @checkedCode="checkedCode" v-if="isShowAreaCheck" />
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	import {
		sendsms,
		appLogin,
		wxIdLogin,
		memberInfo,
		supportedCountryCode,
	} from '@/api/member.js';
	import CheckCode from './components/checkCode.vue'
	import CheckArea from './components/checkArea.vue'
	import rule from '@/utils/rule.js'
	import { USE_WECHAT_LOGIN, USE_APPLE_LOGIN } from '@/utils/appConfig.js';

	export default {
		data() {
			return {
				tips: {
					title: '注册/登录',
					text: '输入手机号，开启雀乐；未注册手机，将自动注册'
				},
				// 地区编号
				area: '+86',
				// 手机号
				mobile: '',
				// 验证码
				mobileCheckCode: '',
				// 是否同意隐私策略
				isRead: false,
				// 是否显示验证码输入
				isShowCode: true,
				// 登录中禁用按钮
				isLogining: false,
				// 传给验证码输入组件
				showData: {
					area: '',
					mobile: '',
					deviceId: '',
					mobileCheckCode: '',
					deviceBrand: ''
				},
				// 是否显示区号选择页面
				isShowAreaCheck: false,
				checkData: {
					areaList: [],
					checkedCode: '+86'
				},
				// 微信登录(暂未实现)
				useWechatLogin: USE_WECHAT_LOGIN,
				// 苹果登录(暂未实现)
				useAppleLogin: USE_APPLE_LOGIN,
			}
		},
		components: {
			CheckCode,
			CheckArea
		},
		computed: {
			...mapState(['theme', 'deviceId', 'deviceBrand']),
			canSend() {
				return this.mobile.length === 11 && !this.isLogining
			}
		},
		onLoad() {
			this.getCountryCode()
		},
		methods: {
			...mapMutations(['login']),
			navBack() {
				uni.navigateBack();
			},
			getCode() {
				if (!rule.mobile(this.mobile)) {
					this.$api.msg('请输入正确的手机号码');
					return;
				}
				if (!this.isRead) {
					this.$api.msg('请先同意隐私政策');
					return;
				}
				this.regetCode()
			},
			closeModal(type) {
				console.log('close', type)
				if (type === 'code') {
					this.isShowCode = false
				}
				if (type === 'area') {
					this.isShowAreaCheck = false
				}
			},
			async doLogin(data) {
				uni.showLoading({
					title: '登录中……',
					mask: true
				})
				appLogin(data)
					.then(res => {
						if (res.code === 200 && res.data) {
							uni.setStorageSync('token', res.data);
							memberInfo().then(response => {
								this.login(response.data);
								uni.navigateBack();
							});
						}
					})
					.catch(err => {
						console.log('err', err)
						uni.showToast({
							title: '登录失败',
							icon: 'error'
						})
					})
					.finally(
						() => {
							uni.hideLoading()
						}
					)
			},
			regetCode() {
				this.isLogining = true
				sendsms({
						mobile: this.mobile,
						deviceId: this.deviceId
					})
					.then(res => {
						if (res.code === 200) {
							this.showData = {
								area: this.area,
								mobile: this.mobile,
								deviceId: this.deviceId,
								deviceBrand: this.deviceBrand,
								mobileCheckCode: '',
							}
							this.isShowCode = true
						}
					})
					.catch(
						err => {
							console.log(err)
							uni.showToast({
								duration: 3000,
								title: err
							})
						})
					.finally(() => {
						this.isLogining = false
					})
			},
			toRead(index) {
				const item = [{
						url: 'https://m.indie.cn/agreement/registrationAgreement.html',
						webviewStyles: {
							progress: true
						}
					},
					{
						url: 'https://m.indie.cn/agreement/privacyPolicy.html',
						webviewStyles: {
							progress: true
						}
					}
				]
				uni.navigateTo({
					url: '/pages/webview/webview?item=' + encodeURIComponent(JSON.stringify(item[index]))
				})
			},
			getCountryCode() {
				supportedCountryCode()
				.then(res => {
					console.log(res)
					if(res.code === 200 && res.data) {
						this.checkData = {
							areaList: res.data,
							checkedCode: '+86' 
						}
					}
				})
				.catch(err => {
					console.log(err)
				})
			},
			checkedCode(item) {
				this.checkData.checkedCode = item.countryCode
				this.isShowAreaCheck = false
			},
			loginByWechat() {
				uni.showToast({
					title: '功能开发中……',
					icon: 'none'
				})
			},
			loginByApple() {
				uni.showToast({
					title: '功能开发中……',
					icon: 'none'
				})
			},

		},

	}
</script>

<style lang='scss' scoped>
	.container {
		position: relative;
		width: 100vw;
		height: 100vh;
		overflow: hidden;

		.bottom-sign {
			position: fixed;
			left: 0;
			bottom: 0;
			width: 100vw;
			height: 100vw;

			image {
				width: 100%;
				height: 100%;
			}
		}

		.back-btn {
			position: absolute;
			left: 40upx;
			padding-top: var(--status-bar-height);
			top: 40upx;
			font-size: 40upx;
			color: $font-color-dark;
			z-index: 9;

			image {
				width: 40upx;
				height: 40upx;
			}
		}

		.wrapper {
			position: relative;
			padding-top: 200upx;

			.tips {
				width: 100%;
				height: 136upx;
				padding: 0 48upx;

				.tips_title {
					font-size: 64upx;
					margin-bottom: 26upx;
				}

				.tips_text {
					font-size: 28upx;
				}
			}

			.input_box {
				display: flex;
				align-items: center;
				padding: 88upx 48upx;
				position: relative;

				.area_select {
					display: flex;
					align-items: center;

					.area {
						font-size: 34upx;
					}

					.icon_down {
						width: 48upx;
						height: 48upx;
						margin: 0 23upx 0 14upx;
					}
				}

				input {
					font-size: 34upx;
					border-left: 2upx solid rgba(0, 0, 0, 0.4);
					padding-left: 54upx;
				}

				.icon_clear {
					width: 48upx;
					height: 48upx;
					position: absolute;
					right: 48upx;
					top: 50%;
					margin-top: -24upx;
				}

			}

			.confirm-btn {
				width: 566upx;
				height: 88upx;
				line-height: 88upx;
				border-radius: 999px;
				background: #C43737;
				color: #fff;
				font-size: $font-lg;

				&:after {
					border-radius: 100px;
				}
			}

			.confirm-btn.disbled {
				background-color: #ccc;
			}

			.checkbox_box {
				padding: 42upx;

				.radio {
					font-size: 24upx;
					vertical-align: middle;

					.textBtn {
						display: inline-block;
						height: 24upx;
						line-height: 24upx;
						font-size: 24upx;
						margin: 0;
						padding: 0;
					}
				}

				radio {
					transform: scale(0.7)
				}
			}

			.login_icon {
				padding-top: 32upx;
				display: flex;
				justify-content: center;

				image {
					width: 92upx;
					height: 92upx;
				}

				image.wechat {
					margin-right: 24upx;
				}

				image.apple {
					margin-left: 24upx;
				}
			}
		}

		.modal-box {
			position: absolute;
			top: 0;
			left: 0;
			height: 100vh;
			width: 100vw;
			background: #fff;
			z-index: 9;
		}
	}

	/* 暗黑模式 */
	@media (prefers-color-scheme: dark) {
		.container {
			.wrapper .input_box input {
				border-left: 2upx solid #fff;
			}

			.back-btn {
				color: #fff;
			}
		}
	}
</style>