<template>
	<view class="reg">
		<view class="title">
			<u-icon name="arrow-left" size="34" @click="back"></u-icon>
			<view class="text">修改密码</view>
		</view>
		<u-form :model="form" ref="uForm" label-width="160" class="form">
			<!-- 第一个密码框 -->
			<u-form-item :leftIconStyle="leftIconStyle"  :class="{focusCode: iscFocusPsw, error: isPswError}"  v-if="showPsw.one" class="psw"
				:label-position="labelPosition" label-width="150">
				<u-input :border="false" :password-icon="false"  @blur="inputBlur('psw')" @focus="inputFocus('psw')" :clearable="false" v-model="form.password"
					placeholder="输入原登录密码"></u-input>
				<u-icon name="eye" size="36" @click="showPsw.one = false"></u-icon>
			</u-form-item>

			<u-form-item :leftIconStyle="leftIconStyle"  :class="{focusCode: iscFocusPsw, error: isPswError}"  v-else class="psw"
				:label-position="labelPosition" label-width="150">
				<u-input :border="false" type="password"  @blur="inputBlur('psw')" @focus="inputFocus('psw')" :password-icon="false" :clearable="false"
					v-model="form.password" placeholder="输入原登录密码"></u-input>
				<u-icon name="eye-off" size="36" @click="showPsw.one = true"></u-icon>
			</u-form-item>

			<!-- 第二个密码框 -->
			<u-form-item :leftIconStyle="leftIconStyle"   :class="{focusCode: iscFocusRePsw, error: isRePswError}"   v-if="showPsw.two" class="psw"
				:label-position="labelPosition" label-width="150">
				<u-input :border="false"   @blur="inputBlur('repsw')" @focus="inputFocus('repsw')"  :password-icon="false" :clearable="false" v-model="form.rePassword"
					placeholder="设置新登录密码"></u-input>
				<u-icon name="eye" size="36" @click="showPsw.two = false"></u-icon>
			</u-form-item>

			<u-form-item :leftIconStyle="leftIconStyle" :class="{focusCode: iscFocusRePsw, error: isRePswError}"  v-else class="psw"
				:label-position="labelPosition" label-width="150">
				<u-input :border="false" type="password"   @blur="inputBlur('repsw')" @focus="inputFocus('repsw')"  :password-icon="false" :clearable="false"
					v-model="form.rePassword" placeholder="设置新登录密码"></u-input>
				<u-icon name="eye-off" size="36" @click="showPsw.two = true"></u-icon>
			</u-form-item>
		</u-form>
		<view class="submit " @click="submit" :class="{active: isActive}">
			确定
		</view>
	
	</view>
</template>

<script>
	import config from '../../../config/config'
	import rulesMixin from '../../../config/rulesMixin.js'
	
	export default {
		mixins: [rulesMixin],
		data() {
			return {
				form: {
					mobile: '',
					password: '',
					rePassword: '',
					code: ''
				},
				codeSrc: '',
				phoneHome: '86',
				resCode: 3322,
				show: false,
				codeText: '发送验证码',
				leftIconStyle: {
					color: '#888',
					fontSize: '36rpx'
				},
				labelPosition: 'left',
				showPsw: {
					one: false,
					two: false
				},
			}
		},
		methods: {
			getCodeSrc() {
				if (this.user.userInfo.mobile) {
					this.codeSrc = config.baseUrl + '/home/imgCode/' + this.user.userInfo.mobile + '?' + Date.now()
					console.log((this.codeSrc, 999))
				}
			},
			back() {
				uni.navigateBack()
			},
			confirm(item) {
				this.phoneHome = item[0].value
				console.log(item)
			},
			codeTime() {
				let time = 60
				let timer = null
				this.codeText = time + ' s'
				uni.showToast({
					title: '3322',
					duration: 2000,
					icon: 'none'
				});
				timer = setInterval(() => {
					time = time - 1
					this.codeText = time + ' s'
					if (this.codeText < 1) {
						clearInterval(timer)
					}
				}, 1000)
			},
			toLogin() {
				uni.navigateTo({
					url: './login'
				})
			},
			async submit() {
				let res = await this.$u.api.user.updatePassword({
					oldPassword: this.form.password,
					password: this.form.rePassword
				})
				this.$u.toast('修改成功');
				setTimeout(() => {
					uni.navigateBack()
				},700)
			}
		},
		computed: {
			isActive() {
				let {
					phone,
					password,
					code
				} = this.form
				return phone !== '' && password !== '' 
			}
		},
		onLoad() {
			this.getCodeSrc()
		}
	}
</script>

<style lang="scss" scoped>
	.title {
		padding-top: 96rpx;
		font-size: 48rpx;
		color: #303133;
		font-weight: 500;
		line-height: 58px;
		display: flex;
		padding-left: 30rpx;

		.text {
			margin-left: 10rpx;
			vertical-align: middle;
			font-size: 48rpx;
			font-weight: 500;
		}
	}

	.form {
		margin: 0 auto;
		margin-top: 82rpx;
		width: 654rpx;

		.text {
			padding-left: 24rpx;
			font-size: 28rpx;
			color: #000000;
			border-left: 1px solid #EDEFF2;

		}

		.item {
			text-align: center;

		}

		.code {
			color: #cacaca;
		}


		/deep/ .u-form-item__message {
			padding-left: 0 !important;
		}
	}

	.submit {
		font-size: 32rpx;
		color: #FFFFFF;
		background-color: #dfe0e5;
		width: 654rpx;
		height: 100rpx;
		background: #DFE0E5;
		border-radius: 16rpx;
		line-height: 100rpx;
		text-align: center;
		margin: 0 auto;
		margin-top: 120rpx;
	}

	.active {
		background-color: #e67d29;
	}

	.creation {
		margin-top: 568rpx;
		padding-bottom: 52rpx;
		color: #d3d3d3;
		font-size: 28rpx;
		text-align: center;

		.link {
			color: #708bc5;
			font-size: 28rpx;
			margin-left: 20rpx;
		}
	}
</style>
