<template>
	<view class="container">
		<image class="cbg" :src="base.imgHost('common_bg.png')"></image>
		<view class="wrap">
			<u-form :model="form" :label-width="140" ref="uForm" :label-align="'left'" :error-type="['message']">
				<u-form-item label="姓名" prop="username" required>
					<u-input v-model="form.username" placeholder="请输入姓名" />
				</u-form-item>
				<u-form-item label="性别" prop="sex" required>
					<u-input @click="showSex = true" placeholder="请选择性别" v-model="form.sex" type="select" />
				</u-form-item>
				<u-form-item label="联系方式" prop="mobile" required>
					<!-- <u-input type="number" placeholder="请输入联系方式" v-model="form.mobile" /> -->
					<view style="display: flex;justify-content: space-between;align-items: center;">
						<u-input type="number" placeholder="联系方式" v-model="form.mobile" />
						<!-- <view>{{form.mobile}}</view> -->
						<!-- <u-button size="mini" type="primary" @click="addMobile">绑定</u-button> -->
					</view>
				</u-form-item>
				<u-form-item label="出生日期" prop="birthday">
					<u-input @click="dateShow=true" placeholder="请选择出生日期" v-model="form.birthday"
						type="select" /></u-form-item>
				<u-form-item label="邮箱" prop="email">
					<u-input v-model="form.email" placeholder="请输入邮箱" /></u-form-item>
				<u-form-item label="公司名称" prop="company_name">
					<u-input v-model="form.company_name" placeholder="请输入公司名称" />
				</u-form-item>
				<u-form-item label="职务" prop="company_position">
					<u-input v-model="form.company_position" type="text" placeholder="请输入职务" /></u-form-item>
			</u-form>
		</view>
		<view class="form-btn">
			<view @click="saveUser">注册</view>
		</view>
		
		<!-- 修改手机号 -->
		<view class="modal">
			<u-popup v-model="showMobile" mode="bottom">
				<view class="modal-tip">联系方式</view>
				<view class="modal-content" style="height:400rpx">
					<view class="login-form">
						<view>
							<u-input :custom-style="inputStyle" :clearable="false" v-model="myMobile"
								placeholder="联系方式"></u-input>
						</view>
						<view class="sendmsg">
							<u-input v-model="myCode" :custom-style="inputSendStyle" :clearable="false"
								placeholder="验证码"></u-input>
							<view @click="getSmsCode" class="send" :class="{'hasSend':hasSend}">
								{{hasSend?'已发送':'获取验证码'}}<text v-if="hasSend">({{count}}s)</text>
							</view>
						</view>
					</view>
				</view>
				<view class="form-btn">
					<view @click="changeMobile" type="primary">确定</view>
				</view>
			</u-popup>
		</view>
		
		
		<u-picker @confirm="selectSex" v-model="showSex" :range="sexList" range-key="value" mode="selector"></u-picker>
		<u-picker @confirm="dateConfirm" mode="time" v-model="dateShow" :params="dateParams"></u-picker>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				showMobile: false,
				isDonate:false,
				myMobile:'',
				myCode: '',
				hasSend: false,
				sendTimer: null,
				count: 60,
				inputStyle: {
					'padding': '0rpx 30rpx',
					'height': '80rpx',
					'border': '1rpx solid #dad8d8;',
					'border-radius': '20rpx'
				},
				inputSendStyle: {
					'padding': '0rpx 30rpx',
					'height': '80rpx',
					'border': '1rpx solid #dad8d8;',
					'border-radius': '20rpx 0 0 20rpx'
				},
				
				
				
				course_id: null,
				showSex: false,
				dateShow: false,
				dateParams: {
					year: true,
					month: true,
					day: true,
					hour: false,
					minute: false,
					second: false
				},
				form: {
					username: '',
					sex: ''
				},
				sexList: [{
					label: '男',
					value: '男'
				}, {
					label: '女',
					value: '女'
				}],
				rules: {
					username: [{
						required: true,
						message: '请输入姓名',
						trigger: ['blur'],
					}],
					sex: [{
						required: true,
						message: '请选择性别',
						trigger: ['change', 'blur'],
					}],
					mobile: [{
						required: true,
						message: '请输入联系方式',
						trigger: ['blur'],
					}, {
						validator: (rule, value, callback) => {
							return this.$u.test.mobile(value);
						},
						message: '手机号码不正确',
						trigger: ['blur'],
					}],
					// email: [{
					// 	validator: (rule, value, callback) => {
					// 		return this.$u.test.email(value);
					// 	},
					// 	message: '邮箱不正确',
					// 	trigger: ['blur'],
					// }],

				}
			}
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		onLoad(options) {
			this.course_id = options.id ? options.id : null
			this.isDonate = options.isDonate?options.isDonate:false
		},
		onUnload() {
			if (this.sendTimer) {
				clearInterval(this.sendTimer);
				this.sendTimer = null
			}
		},
		onHide() {
			if (this.sendTimer) {
				clearInterval(this.sendTimer);
				this.sendTimer = null
			}
		},
		methods: {
			addMobile() {
				// this.myMobile = this.form.mobile
				this.showMobile = true
			},
			getSmsCode() {
				if (this.hasSend) {
					return
				}
				if (this.base.isNull(this.myMobile)) {
					this.base.toast('手机号码不能为空')
					return
				}
				if (!this.base.isMobile(this.myMobile)) {
					this.base.toast('手机号码错误')
					return
				}
				let that = this
				this.$u.api.sendSms({
					mobile: this.myMobile
				}).then(res => {
					this.base.toast("发送成功")
					this.hasSend = true
					this.sendTimer = setInterval(function() {
						if (that.count > 1) {
							that.count--
						} else {
							clearInterval(that.sendTimer);
							that.sendTimer = null
							that.count = 60
							that.hasSend = false;
						}
					}, 1000)
				})
			},
			changeMobile() {
				if (this.base.isNull(this.myMobile)) {
					this.base.toast('请输入手机号')
					return
				}
				if (this.base.isNull(this.myCode)) {
					this.base.toast('请输入验证码')
					return
				}
				let that = this
				this.$u.api.bindMobile({
					mobile: this.myMobile,
					code: this.myCode,
					is_bind: 1
				}).then(res => {
					this.base.toast('绑定成功')
					this.form.mobile = this.myMobile
					// that.myMobile = ''
					this.myCode = ''
					this.showMobile = false
		
				})
			},
			selectSex(e) {
				this.form.sex = this.sexList[e[0]]['value']
			},
			// 日期
			dateConfirm(e) {
				this.form.birthday = e.year + '-' + e.month + '-' + e.day
			},

			saveUser() {
				let that = this
				this.$refs.uForm.validate(valid => {
					if (valid) {
						this.form.name = this.form.username
						this.$u.api.saveUser(this.form).then(res => {
							this.$u.api.user().then(res => {
								this.$u.vuex('vuex_user', res.user)
								this.base.toast("注册成功", 2000, function() {
									console.log("that.course_id", that.course_id)
									if (that.course_id) {
										uni.redirectTo({
											url: '/packages/course/detail?id=' +
												that.course_id
										})
										// uni.redirectTo({
										// 	url:'/pages/course/index'
										// })
									}else if(that.isDonate){
										uni.redirectTo({
											url: '/packages/donate/index'
										})
									} else {
										uni.switchTab({
											url: '/pages/me/index'
										})
									}
								})


							})
						})
					} else {
						console.log('验证失败');
						// this.base.toast("注册失败")
					}
				});

			},
		}
	}
</script>

<style scoped lang="scss">
	.container {
		padding: 30rpx;
		height: 100vh;
		overflow: scroll;

		.cbg {
			position: absolute;
			top: 0;
			left: 0;
			width: 100%;
			height: 100vh;
		}

		.wrap {
			background: #fff;
			position: relative;
			padding: 35rpx;
			border-radius: 30rpx;
			padding-top: 0;
		}

		.form-btn {
			width: 100%;
			position: relative;
			padding: 60rpx 0;

			&>view {
				width: 70%;
				text-align: center;
				margin: 0 auto;
				color: #fff;
				background: linear-gradient(to right, #5e5fbc, #0d0398);
				border-radius: 30rpx;
				padding: 20rpx;
			}
		}
		.modal {
			::v-deep .u-drawer-bottom {
				border-radius: 40rpx;
			}
		
			&-tip {
				text-align: center;
				padding: 30rpx;
				font-size: 32rpx;
			}
		
			&-content {
				// height: 400rpx;
				padding: 0 30rpx;
			}
		}
		
		.login-form {
			margin: 100rpx 50rpx;
		
			&>view {
				border-radius: 20rpx;
		
				&:first-child {
					margin-bottom: 50rpx;
				}
			}
		
			.sendmsg {
				display: flex;
				justify-content: space-between;
				align-items: center;
		
				u-input {
					width: calc(100% - 200rpx);
		
				}
		
				.send {
					background: #c69c6d;
					color: #fff;
					height: 80rpx;
					line-height: 80rpx;
					border-radius: 0 20rpx 20rpx 0;
					width: 200rpx;
					text-align: center;
					border: 1px solid #c69c6d;
					box-sizing: content-box;
				}
		
				.hasSend {
					background: #dad8d8;
					color: #333;
					border: 1px solid #dad8d8;
				}
			}
		}

	}
</style>