<template>
	<view class="wrap f-padding">
		<u-form :model="form" ref="uForm">
			<u-form-item label="原密码" prop="password" left-icon="lock-open" :label-width="labelWidth">
				<u-input v-model="form.oldPwd" placeholder="请输入原密码" />
			</u-form-item>
			<u-form-item label="新密码" prop="newPassword" left-icon="lock-open" :label-width="labelWidth">
				<u-input :password-icon="true" type="password" v-model="form.newPwd" placeholder="请输入新密码" />
			</u-form-item>
			<u-form-item label="确认新密码" prop="secPassword" left-icon="lock-open" :label-width="labelWidth">
				<u-input :password-icon="true" type="password" v-model="form.secPassword" placeholder="请确认新密码" />
			</u-form-item>
			<u-button class="u-m-t-80" type="primary" @click="submit">确认修改</u-button>
		</u-form>
	</view>
</template>

<script>
	import crypto from 'utils/crypto.js'
	export default{
		data(){
			let logPwd2 = (rule, value, callback) => {
				if (value === '') {
					callback(new Error('请再次输入密码'));
				} else if (value !== this.form.newPwd) {
					callback(new Error('两次输入密码不一致!'));
				} else {
					callback();
				}
			};
			return{
				labelWidth:'200',
				form:{},
				rules: {
					oldPwd: [
						{ 
							required: true,
							min: 6, 
							message: '密码不能少于6个字符', 
							// 可以单个或者同时写两个触发验证方式 
							trigger: ['change','blur'],
						}
					],
					newPwd: [
						{
							required: true, 
							min: 6, 
							message: '密码不能少于6个字符', 
							trigger: 'change'
						}
					],
					secPassword:[
						{ required: true,validator: logPwd2, trigger: 'blur' }
					]
				}
			}
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		onLoad(){
			
		},
		onShow(){
			
		},
		methods:{
			submit() {
				// uni.switchTab({
				// 	url: '../index/index'
				// });
				this.$refs.uForm.validate(valid => {
					if (valid) {
						// this.form.password = crypto.Encrypt(this.form.password);
						// this.form.newPassword = crypto.Encrypt(this.form.newPassword);
						// this.form.secPassword = crypto.Encrypt(this.form.secPassword);
						this.$u.api.updatePwd(this.form).then(res=>{
							if(res.code==200){
								//#ifdef H5
								localStorage.setItem('backUrl', '');
								//#endif
								uni.navigateTo({
									url: '../../login/login'
								});
							}else{
								uni.showToast({
									icon:'none',
									title:res.msg
								})
							}
							console.log('res',res);
						}).catch(err=>{
							console.log('err',err);
						})
						// console.log('验证通过');
					} else {
						// console.log('验证失败');
					}
				});
			},
		}
	}
</script>

<style lang="scss" scoped>
	// @import  './resetPass.scss'
</style>
