<template>
	<view class="page">
		<view :style="{'marginTop':'10rpx'}">
			<u--form labelPosition="left" :model="form" :rules="rules" ref="uForm">

				<u-form-item :labelWidth="80" label="手机号：" prop="username" ref="item1">
					<u--input v-model="form.username"  placeholder="请输入手机号"></u--input>
				</u-form-item>

				<u-form-item :labelWidth="80" label="原密码：" prop="oldPassword" ref="item1">
					<u--input v-model="form.oldPassword" placeholder="请输入原密码" type="password"></u--input>
				</u-form-item>

				<u-form-item :labelWidth="80" label="新密码：" prop="newPassword" ref="item1">
					<u--input v-model="form.newPassword" placeholder="请输入新密码" type="password"></u--input>
				</u-form-item>

			</u--form>

			<view :style="{'marginTop':'100rpx'}">
				<button class="customStyle"  @click="updatePassword">提交</button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				form:{
					username:'',
					oldPassword:'',
					newPassword:''
				},
				rules: {
					'username': {
						type: 'string',
						required: true,
						min:11,
						max: 11,
						pattern:this.$phonePattern,
						message: '请输入正确的电话号码',
						trigger: ['blur', 'change']
					},
					'oldPassword': {
						type: 'string',
						min:6,
						max: 16,
						required: true,
						message: '请输入6-18位老密码',
						trigger: ['blur', 'change']
					},
					'newPassword': {
						type: 'string',
						min:6,
						max: 16,
						required: true,
						message: '请输入6-18位新密码',
						trigger: ['blur', 'change']
					}
				},
			}
		},
		methods: {
			back(){
				uni.navigateBack({
					delta: 1
				});
			},
			updatePassword(){
				console.log("+++++++++++++++++++")
				this.$refs.uForm.validate().then(res => {
					this.$api.login.updatePassword(this.form).then(res=>{
						uni.showToast({
							icon: 'success',
							duration: 2000,
							title: '修改成功，即将跳转登录页'
						});
						setTimeout(()=>{
							this.logout()
						},2000)

					})
				})
			},
			logout(){
				this.$api.login.logout().then(res=>{
					uni.clearStorageSync();
					uni.navigateTo({
						url:'/pages/login/login'
					})
				})
			}

		}
	}
</script>

<style>
	@import './index.rpx.css';
</style>
