<template>
	<view class="container">
		<u-cell-group :border="false">
			<u-cell-item title="未注册" value="点击注册" @click="goToRegister()" v-if="!haveAuthorizeCode"></u-cell-item>
			<u-cell-item title="是否拥有授权码" :arrow="false">
				<u-switch v-model="haveAuthorizeCode" active-color="#51A539"></u-switch>
			</u-cell-item>
			<u-field label="授权码" placeholder="请输入授权码" v-model="authorizeCode" v-if="haveAuthorizeCode"></u-field>
		</u-cell-group>
		<view class="form-handle" v-if="haveAuthorizeCode">
			<u-button type="success" shape="circle" :ripple="true" @click="goToSignIn" class="handle-custom" :disabled="!authorizeCode">登录</u-button>
		</view>
		<u-top-tips ref="uTips"></u-top-tips>
	</view>
</template>

<script>
	const NET = require('@/utils/request')
	const API = require('@/config/api')
	export default {
		data() {
			return {
				haveAuthorizeCode: false,
				authorizeCode: '',
			}
		},
		onLoad() {},
		methods: {
			//  注册
			goToRegister() {
				uni.navigateTo({
					url: '/pages/index/register?type=add'
				});
			},
			//  登录
			goToSignIn() {
				NET.request(API.authorizeSignIn, {}, 'POST').then(res => {
					this.$refs.uTips.show({
						title: '授权码登录成功,请重新登录',
						type: 'success',
					})
					setTimeout(() => {
						uni.switchTab({
							url: '/pages/index/index'
						});
					}, 2000)
				}).catch(res => {
					this.$refs.uTips.show({
						title: '授权码登录失败',
						type: 'warning',
					})
				})
			},
		},
	}
</script>

<style lang="less" scoped>
	page {
		width: 100%;
		height: 100%;
	}

	.container {
		width: 100%;
		height: 100%;
		float: left;
		box-sizing: border-box;
		padding-bottom: 70px;
		overflow-y: auto;

		.form-handle {
			width: calc(100% - 30px);
			height: 40px;
			position: fixed;
			bottom: 20px;
			left: 15px;

			.handle-custom {
				background-color: #51A539;
			}

			/deep/.u-btn--success--disabled {
				background-color: #999999 !important;
			}
		}
	}
</style>