<template>
	<mobile-frame :frameStyle="frameStyle" @toPath="toPath">
		<view class="main">
			<view class="one">
				<uni-forms ref="form" :modelValue="form" :rules="rules" label-width="auto">
					<uni-forms-item label="账号" name="phone">
						<uni-easyinput type="text" v-model="form.phone" placeholder="请输入账号" disabled />
					</uni-forms-item>
					<uni-forms-item label="新密码" name="password">
						<uni-easyinput type="password" v-model="form.password" placeholder="请输入新密码" />
					</uni-forms-item>
					<uni-forms-item label="确认密码" name="is_password">
						<uni-easyinput type="is_password" v-model="form.is_password" placeholder="请输入确认密码" />
					</uni-forms-item>
				</uni-forms>
				<view class="btn">
					<button type="primary" @click="onSubmit('form')" size="small">提交</button>
					<view class="name">提示：为了您的账户安全，建议定期修改密码</view>
				</view>
			</view>
		</view>

	</mobile-frame>
</template>

<script>
	export default {
		data() {
			return {
				frameStyle: {
					useBar: false
				},
				form: {},
				rules: {
					password: {
						rules: [{
							required: true,
							errorMessage: '请输入新密码',
						}]
					},
				},
			};
		},
		onLoad: function(e) {
			const that = this;
			that.watchLogin();
		},
		methods: {
			watchLogin() {
				const that = this;
				uni.getStorage({
					key: 'token',
					success: (res) => {
						let user = that.$jwt(res.data);
						if (user) {
							that.$set(that, `form`, {
								id: user.id,
								phone: user.phone
							})
						}
					},
					fail: (err) => {}
				})
			},
			toPath(e) {
				if (e && e.route) uni.redirectTo({
					url: `/${e.route}`
				})
			},

			// 提交保存
			onSubmit(ref) {
				const that = this;
				that.$refs[ref].validate().then(async params => {
					if (params.password == params.is_password) {
						const arr = await that.$api(`/user/resetPwd/${that.form.id}`, 'POST', params);
						if (arr.errcode == '0') {
							uni.showToast({
								title: `密码修改成功`,
								icon: 'success',
							});
							uni.clearStorage();
							uni.redirectTo({
								url: '/pages/login/index'
							})
						} else {
							uni.showToast({
								title: arr.errmsg,
							})
						}
					} else {
						uni.showToast({
							title: `密码不一致`,
						});
					}
				})
			},
		}
	}
</script>

<style lang="scss">
	.main {
		display: flex;
		flex-direction: column;
		width: 100vw;
		height: 100vh;

		.one {
			padding: 2vw;

			.uni-input {
				border: #f1f1ff 1px solid;
				padding: 2vw 2vw;
				border-radius: 1vw;
			}

			.btn {
				text-align: center;

				button {
					margin: 0 2vw 2vw 2vw;
					background-color:var(--fFB1Color);
					color: var(--fffColor);
				}

				.name {
					color: var(--f85Color);
					font-size: var(--font14Size);
				}
			}
		}
	}

	.uni-forms-item {
		margin-bottom: 6vw !important;
		display: flex;
		flex-direction: row;
	}
</style>
