<template>
	<view class="web_box">
		<view class="title">
			新用户注册
		</view>
		<view class="form">
			<u--form ref="form1">
				<u-form-item borderBottom>
					<u--input border="none" v-model="userInfo.name" placeholder="请输入用户名"></u--input>
				</u-form-item>
				<u-form-item borderBottom>
					<picker @change="bindSexChange" :value="index" :range="sexList">
						<u--input border="none" v-model="sexList[userInfo.sex]" disabled placeholder="请选择性别" suffixIcon="arrow-down"></u--input>
					</picker>
				</u-form-item>
				<!-- <u-form-item borderBottom>
					<picker mode="region" @change="changeAddress" :value="index" :range="address">
						<u--input border="none" v-model="region" disabled placeholder="请选择地区" suffixIcon="arrow-down"></u--input>
					</picker>
				</u-form-item> -->
				<u-form-item borderBottom>
					<u--input border="none" v-model="userInfo.employer" placeholder="请输入工作单位"></u--input>
				</u-form-item>
				<u-form-item borderBottom>
					<u--input border="none" v-model="userInfo.realName" placeholder="请输入真实姓名"></u--input>
				</u-form-item>
				<u-form-item borderBottom>
					<u--input border="none" v-model="userInfo.IDnumber" placeholder="请输入身份证号"></u--input>
				</u-form-item>
				<u-form-item borderBottom>
					<u--input border="none" v-model="userInfo.phoneNumber" placeholder="请输入手机号"></u--input>
				</u-form-item>
				<u-form-item borderBottom>
					<u-input border="none" v-model="userInfo.code" placeholder="请输入验证码">
						<template slot="suffix">
							<u-code ref="uCode" @change="codeChange" seconds="60" changeText="X秒重新获取"></u-code>
							<view @click="getCode" class="code">{{tips}}</view>
						</template>
					</u-input>
				</u-form-item>
				<u-form-item borderBottom>
					<u--input border="none" type="password" v-model="userInfo.password" placeholder="请输入6-18位字符"></u--input>
					
				</u-form-item>
				<view class="topic">请输入6-18位字符</view>
			</u--form>
		</view>
		<view class="hflex acenter">
			<u-checkbox-group @change="checkboxChange">
				<u-checkbox v-model="agree" shape="circle"></u-checkbox>
			</u-checkbox-group>
			<view class="text">我已阅读并同意<span class="read" @click="open(0)">《用户协议》</span>、<span class="read" @click="open(1)">《隐私协议》</span></view>
		</view>
		<view class="bottom">
			<u-button @click="submit">注册并登录</u-button>
			<view class="text bottom_text">
				已有账户，<span class="read" @click="toLogin">去登录</span>
			</view>
		</view>
		<!-- 弹出层 -->
		<u-modal :show="showProtocol" :title="protocolTitle" confirmColor="#2988FE" @confirm="showProtocol = false" confirmText="我已阅读">
			<view class="slot-content">
				<rich-text :nodes="protocolContent"></rich-text>
			</view>
		</u-modal>
	</view>
</template>

<script>
	import $api from '@/static/js/api.js'
	
	export default {
		data() {
			return {
				userInfo: {
					name: '',
					sex: '',
					province: '',
					city: '',
					area: '',
					employer: '',
					realName: '',
					IDnumber: '',
					phoneNumber: '',
					code: '',
					password: '',
				},
				region: '',
				sexList: ['保密','男','女'],
				address: [],
				tips: '',
				agree: false,
				showProtocol: false,
				protocolTitle: '',
				protocolContent: '',
				code: '',
			}
		},
		onLoad(options) {
			this.code = decodeURIComponent(options.scene)
			console.log(this.code);
			if (this.code == "undefined") {
				this.code = ""
				console.log(this.code);
			}
		},
		methods: {
			// 选择性别
			bindSexChange: function(e) {
				var index = e.detail.value
				this.userInfo.sex = index
			},
			// 选择城市
			changeAddress(e){
				var index = e.detail.value
				this.userInfo.province = index[0]
				this.userInfo.city = index[1]
				this.userInfo.area = index[2]
				this.region = this.userInfo.province + '-' + this.userInfo.city + '-' + this.userInfo.area
			},
			// 验证码文字改变
			codeChange(text) {
				this.tips = text;
			},
			// 获得验证码
			getCode() {
				var that = this
				if (this.$refs.uCode.canGetCode) {
				  uni.showLoading({
					title: '正在获取验证码'
				  })
				  
				  setTimeout(() => {
					uni.hideLoading();
					uni.$u.toast('验证码已发送');
					$api.req({
						url: '/api/Sms/send',
						data: {
							phone: that.userInfo.phoneNumber,
							event: 'register'
						}
					},function(res) {
						if (res.code == 1) {
							// $api.info("发送成功")
						}
					})
					this.$refs.uCode.start();
				  }, 2000);
				} else {
				  uni.$u.toast('倒计时结束后再发送');
				}
			},
			// 阅读并同意
			checkboxChange(n) {
				this.agree = !this.agree
			},
			// 打开弹出层
			open(index) {
				var that = this
				if(index == 0) {
					$api.req({
						url: '/api/Publics/config_info'
					}, function(res) {
						if (res.code == 1) {
							that.protocolTitle = '用户协议'
							that.protocolContent = res.data.xieyi
						}
					})
					
				} else {
					that.protocolTitle = '隐私政策'
					that.protocolContent = ''
				}
				that.showProtocol = true
			},
			// 去登录
			toLogin() {
				uni.navigateTo({
					url: '/pages/login/login'
				})
			},
			// 注册并登录
			submit() {
				var that = this
				var userInfo = that.userInfo
				if ($api.formCheck(userInfo.name,"required") && $api.formCheck(userInfo.sex,"required") && $api.formCheck(userInfo.region,"required") && $api.formCheck(userInfo.employer,"required")
				 && $api.formCheck(userInfo.IDnumber,"identity") && $api.formCheck(userInfo.phoneNumber,"phone") && $api.formCheck(userInfo.code,"code6") && $api.formCheck(userInfo.password,"required")) {
					if (that.agree) {
						$api.req({
							url: '/api/Login/Register',
							data: {
								user_name: userInfo.name,
								phone: userInfo.phoneNumber,
								sex: userInfo.sex,
								// province: userInfo.province,
								// city: userInfo.city,
								// area: userInfo.area,
								real_name: userInfo.realName,
								work_units: userInfo.employer,
								id_card: userInfo.IDnumber,
								ver_code: userInfo.code,
								password: userInfo.password,
								from: 2,
								code: that.code
							}
						},function(res) {
							if (res.code == 1) {
								$api.info(res.msg)
								uni.setStorageSync("token",res.data)
								uni.getSetting({
								   success(res) {
								      if (res.authSetting['scope.userInfo']) {
										  uni.login({
											provider: 'weixin',
											success: function(res2) {
												if(res2.code) {
													$api.req({
														url: '/api/Login/bind_openid',
														data: {
															code: res2.code
														}
													}, function(res2) {
														if (res2.code == 1) {
															$api.jump("/pages/mine/mine",3)
														}
														
													})
												}
											}
										  })
									  }
								   }
								})
							} else if(res.code == 0) {
								$api.info(res.msg)
							}
						})
					} else {
						$api.info("请先阅读并同意用户协议和隐私协议")
					}
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.web_box::v-deep {
		background: url('@/static/images/login/choose-bg.png') no-repeat;
		background-size: 100%;
		padding: 226rpx 66rpx 0;
		
		.title {
			font-size: 40rpx;
			font-weight: 500;
			color: #333333;
		}
		.form {
			width: 100%;
			box-sizing: border-box;
			padding: 80rpx 0 0;
			margin-bottom: 20rpx;
		}
		.u-input {
			background-color: #fff !important;
		}
		.code {
			font-size: 28rpx;
			font-weight: 400;
			color: #2988FE;
		}
		.text {
			font-size: 24rpx;
			color: #999999;
		}
		.read {
			color: #2988FE;
		}
		.bottom {
			margin-top: 80rpx;
		}
		.u-button {
			width: 578rpx;
			height: 80rpx;
			background: linear-gradient(135deg, #53BDFF 0%, #57BBF0 29%, #3B86FE 72%, #2988FE 100%);
			border-radius: 40rpx;
			font-size: 28rpx;
			font-weight: 500;
			color: #FFFFFF;
		}
		.bottom_text {
			width: 100%;
			text-align: right;
			margin: 14rpx 0 80rpx;
		}
		.topic {
			padding: 10rpx 0;
			font-size: 24rpx;
			color: #888;
		}
	}
</style>