<template>
	<a-form
		:label-col="useUpdateMethod.formLabelCol"
		:model="formState"
		:wrapper-col="useUpdateMethod.formWrapperCol"
		autocomplete="off"
		name="basic"
		@finish="onFinish"
		@finishFailed="useUpdateMethod.formFail"
	>
		<a-form-item :rules="[{ required: true, message: '请输入旧密码' }]" label="旧密码" name="oldPass">
			<a-input-password autofocus v-model:value="formState.oldPass" autoComplete="“true”" />
		</a-form-item>
		<a-form-item :rules="[{ required: true, min: 6, message: '密码不能少于6位数' }]" label="新密码" name="newPass">
			<a-input-password autofocus v-model:value="formState.newPass" autoComplete="“true”" />
		</a-form-item>
		<a-form-item :rules="[{ validator: checkSurePassword, required: true }]" label="确认密码" name="pswCheck">
			<a-input-password autofocus v-model:value="formState.pswCheck" autoComplete="“true”" />
		</a-form-item>
		<a-form-item :wrapper-col="useUpdateMethod.formActionCol">
			<a-button html-type="submit" type="primary">确定</a-button>
			<a-button class="formButton" @click="useUpdateMethod.handleCancel()">取消</a-button>
		</a-form-item>
	</a-form>
</template>

<script lang="ts" setup>
import { updateMethod } from '../../../components/form/method'
import { QueryObjectType } from '../../../http/request-model'
import { reactive } from 'vue'
import { messageMethod } from '../../../utils/message-util'
import requestMethod from '../../../api/account/information'
import { UserInfoFormDTO } from '../../../entity/account/account-user'
const formState = reactive<UserInfoFormDTO>({
	oldPass: '',
	newPass: '',
	pswCheck: ''
})

/**
 * 自定义两次密码验证
 */
const checkSurePassword = (rule: string, value: string) => {
	if (value === '') {
		return Promise.reject('请再次输入密码')
	} else if (value !== formState.newPass) {
		return Promise.reject('两次密码不一致，请检查后重新输入')
	} else {
		return Promise.resolve()
	}
}
const useUpdateMethod = updateMethod()

const onFinish = async (params: UserInfoFormDTO) => {
	const data = {
		...(params as unknown as QueryObjectType),
		version: formState.version || 0
	}
	let { message } = await requestMethod.update({ ...data })
	messageMethod.success(message)
	useUpdateMethod.handleSave()
}
</script>
<style lang="scss" scoped>
.formButton {
	margin-left: 0.625rem;
}
</style>
