<template>
	<view class="container">
		<uni-forms ref="form" :modelValue="formData" :rules="rules" label-position="top">
			<uni-forms-item label="原密码" name="password">
				<input v-model="formData.password" placeholder="请输入原密码" />
			</uni-forms-item>
			<uni-forms-item label="新密码" name="newPassword">
				<input v-model="formData.newPassword" placeholder="请输入新密码" />
			</uni-forms-item>
		</uni-forms>
		<u-button @click="submit" class="submit" type="primary">提交</u-button>
	</view>
</template>

<script>
	import {
		encrypt
	} from '@/utils/jsencrypt.js'
	export default {
		data() {
			return {
				// 表单数据
				formData: {
					password: null,
					newPassword: null,
				},
				rules: {
					password: {
						rules: [{
							required: true,
							errorMessage: '请输入原密码',
						}, ]
					},
					newPassword: {
						rules: [{
								required: true,
								errorMessage: '请输入新密码',
							},
							{
								validateFunction: function(rule, value, data, callback) {
									if (data.password === data.newPassword) {
										callback('不能和与原密码相同')
									}
									return true
								}
							}
						]
					}
				}
			}
		},
		methods: {
			submit() {
				this.$refs.form.validate().then(res => {
					this.$store.dispatch('user/updatePassword', this.formData).then(req => {
						if (req.data.code === 200) {
							uni.showToast({
								title: req.msg,
								duration: 2000,
								icon: "success"
							});
							uni.redirectTo({
								url: "/pages/login/index"
							})
						}
					})
				}).catch(err => {

				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		padding: 20rpx;
	}

	.submit {
		width: 400rpx;
		margin-top: 60rpx
	}
</style>