<template>
	<a-card>
		<a-form ref="formRef" :model="formState" :rules="rules" layout="vertical">
			<a-form-item label="旧密码：" name="password" has-feedback>
				<a-input
					v-model:value="formState.password"
					placeholder="请输入原密码"
					type="password"
					allow-clear
					autocomplete="off"
				/>
			</a-form-item>
			<a-form-item label="新密码：" name="newPassword" has-feedback>
				<a-input
					v-model:value="formState.newPassword"
					placeholder="请输入新密码"
					type="password"
					allow-clear
					autocomplete="off"
				/>
			</a-form-item>
		</a-form>
		<a-button type="primary" :loading="submitLoading" @click="onSubmit">保存</a-button>
	</a-card>
</template>

<script setup name="updatePassword">
	import { required } from '@/utils/formRules'
	import userCenterApi from '@/api/sys/userCenterApi'

	const formRef = ref()
	// 表单数据
	const formState = ref({})
	const submitLoading = ref(false)

	// 默认要校验的
	const rules = {
		password: [required('请输入原密码')],
		checkPassword: [required('请再次输入原密码')],
		newPassword: [required('请输入新密码')]
	}

	// 提交数据
	const onSubmit = async () => {
		const values = await formRef.value.validateFields()
		submitLoading.value = true
		userCenterApi
			.userUpdatePassword(values)
			.then(() => {
				formRef.value.resetFields()
				visible.value = false
			})
			.finally(() => {
				submitLoading.value = false
			})
	}
</script>
