<template>
	<view>
		<view class="form-area">
			<uni-forms 
				ref="pwdForm" 
				:modelValue="pwdFormData" 
				:rules="pwdFormRules"
				label-position="top"
			>
				<uni-forms-item name="oldPassword" required label="旧密码" label-width="100">
					<uni-easyinput type="password" placeholder="请输入" maxlength="60"
						v-model="pwdFormData.oldPassword"></uni-easyinput>
				</uni-forms-item>
				<uni-forms-item name="newPassword" required label="新密码" label-width="100">
					<uni-easyinput type="password" placeholder="请输入" maxlength="60"
						v-model="pwdFormData.newPassword"></uni-easyinput>
				</uni-forms-item>
				<uni-forms-item name="confirmNewPassword" required label="确认新密码" label-width="100">
					<uni-easyinput type="password" placeholder="请输入" maxlength="60"
						v-model="pwdFormData.confirmNewPassword"></uni-easyinput>
				</uni-forms-item>
				<view 
					class="sub-btn"
					@click="submitForm"
				>
					保存
				</view>
			</uni-forms>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				pwdFormData: {
					oldPassword: '',
					newPassword: '',
					confirmNewPassword: ''
				},
				pwdFormRules: {
					oldPassword: {
						rules: [
							{
								required: true,
								errorMessage: '请输入旧密码！'
							}
						]
					},
					newPassword: {
						rules: [
							{
								required: true,
								errorMessage: '请输入新密码！'
							}
						]
					},
					confirmNewPassword: {
						rules: [
							{
								required: true,
								errorMessage: '请确认新密码！'
							}
						]
					}
				}
			}
		},
		methods: {
			submitForm() {
				let that = this
				that.$refs.pwdForm.validate().then(formData => {
					if (formData.newPassword != formData.confirmNewPassword) {
						uni.showToast({
							icon: 'none',
							title: '确认密码与新密码不一致！'
						})
					} else {
						console.log(formData);
						that.$api.user.fixPwd({
							oldPassword: formData.oldPassword,
							newPassword: formData.newPassword
						}).then(res => {
							console.log(res);
							if (res.data.code == 200) {
								uni.showToast({
									icon: 'none',
									title: '修改成功！',
									success: () => {
										uni.navigateBack()
									}
								})
							} else {
								uni.showToast({
									icon: 'none',
									title: res.data.msg
								})
							}
						}).catch(err => {
							console.log(err);
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.form-area{
		width: 90%;
		margin: 40rpx auto;
		.sub-btn{
			width: 40%;
			margin: 100rpx auto;
			box-sizing: border-box;
			padding: 15rpx 30rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			border-radius: 15rpx;
			background-color: #3888FF;
			color: #fff;
		}
	}
</style>
