<template>
	<view class="u-p-l-20 u-p-r-20">
		<u-form :error-type="['toast']" ref="uForm" label-width="100" :model="form">
			<!-- 	<u-form-item label="账号" prop="name">
				<u-input v-model="form.name" />
			</u-form-item>
			<u-form-item label="验证码" prop="vertion">
				<u-input v-model="form.vertion" />
				<view slot="right">
					<u-verification-code :seconds="seconds" @end="end" @start="start" ref="uCode" @change="codeChange">
					</u-verification-code>
					<view @tap="getCode" class="vertion-info">{{tips}}</view>
				</view>
			</u-form-item> -->
			<u-form-item label="旧密码" prop="oldPassword">
				<u-input v-model="form.oldPassword" />
			</u-form-item>
			<u-form-item label="新密码" prop="newPassword">
				<u-input v-model="form.newPassword" />
			</u-form-item>
			<button class="sumit" @click="sumit()">确认修改</button>
		</u-form>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	import md5 from 'js-md5';
	import {
		updatapwsele
	} from '@/api/index.js'
	export default {
		data() {
			return {
				form: {
					oldPassword: undefined,
					newPassword: undefined
				},
				rules: {
					oldPassword: [{
						required: true,
						message: '请输入旧密码',
						// 可以单个或者同时写两个触发验证方式
						trigger: 'blur,change'
					}],
					newPassword: [{
						required: true,
						message: '请输入新密码',
						// 可以单个或者同时写两个触发验证方式
						trigger: 'blur,change'
					}],
					vertion: [{
						required: true,
						message: '请输入验证码',
						trigger: 'blur,change'
					}],
					password: [{
						required: true,
						message: '请输入新的密码',
						trigger: 'blur,change'
					}]
				},
				seconds: 10,
				tips: ''
			};
		},
		// 必须要在onReady生命周期，因为onLoad生命周期组件可能尚未创建完毕
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		methods: {
			sumit() {
				let {
					oldPassword,
					newPassword
				} = this.form
				oldPassword = md5(`${oldPassword}`)
				newPassword = md5(`${newPassword}`)
				oldPassword = String(oldPassword)
				newPassword = String(newPassword)
				this.$refs.uForm.validate(valid => {
					if (valid) {
						updatapwsele({
								passwordOld: oldPassword,
								password: newPassword
							})
							.then(({
								code,
								message
							}) => {
								if (code == "200") {
									this.$refs.uToast.show({
										title: "修改成功",
										type: 'success',
										callback: () => {
											this.form = {
												oldPassword: undefined,
												newPassword: undefined
											}
											uni.redirectTo({
												url: '../login/index'
											})
										}
									})
								} else {
									this.$u.toast(message);
								}
							})
					} else {
						this.$refs.uToast.show({
							title: "输入格式错误",
							type: 'warning',
							})
					}
				});
			},
			codeChange(text) {
				this.tips = text;
			},
			getCode() {
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					setTimeout(() => {
						uni.hideLoading();
						// 这里此提示会被this.start()方法中的提示覆盖
						this.$u.toast('验证码已发送');
						// 通知验证码组件内部开始倒计时
						this.$refs.uCode.start();
					}, 2000);
				} else {
					this.$u.toast('倒计时结束后再发送');
				}
			},
			end() {
				//this.$u.toast('倒计时结束');
			},
			start() {
				//this.$u.toast('倒计时开始');
			}
		}
	};
</script>

<style lang="scss" scoped>
	.vertion-info {
		color: rgba(0, 186, 173, 1);
		font-size: 28rpx;
		border-left: 2rpx solid rgba(229, 229, 229, 1);
		text-align: left;
		padding-left: 46rpx;
	}

	.sumit {
		width: 704rpx;
		height: 88rpx;
		margin-top: 98rpx;
		color: rgba(255, 255, 255, 1);
		background-color: rgba(42, 130, 228, 1);
		border-radius: 5px;
		font-size: 16px;
		line-height: 88rpx;
		text-align: center;
	}
</style>
