<template>
	<layout>
		<!-- #ifdef MP-WEIXIN -->
		<uv-navbar
		  :fixed="false"
		  :title="title"
		  left-arrow
		  @leftClick="$onClickLeft"
		/>
		<!-- #endif -->
		<view style="background-color: #ffffff; padding: 0rpx 30rpx 30rpx 30rpx;">
			<uv-form labelPosition="left" :model="model1" :rules="rules" ref="uForm" labelWidth="70" :errorType="errorType" >
				<uv-form-item label="旧密码" prop="oldPassword" borderBottom required>
					<uv-input v-model="model1.oldPassword" type="password" border="none">
					</uv-input>
				</uv-form-item>
				<uv-form-item label="新密码" prop="newPassword" borderBottom required>
					<uv-input v-model="model1.newPassword" type="password"  border="none">
					</uv-input>
				</uv-form-item>
				<uv-form-item label="确认密码" prop="confirmPassword" borderBottom required>
					<uv-input v-model="model1.confirmPassword" type="password"  border="none">
					</uv-input>
				</uv-form-item>
				<uv-button  text="保存" customStyle="margin-top: 10px" color="#09b4f1" @click="submit"></uv-button>
			</uv-form>
		</view>
	</layout>
</template>

<script setup>
import {
  ref
} from 'vue'
import { onLoad,onShow} from '@dcloudio/uni-app'
import {
  updateUserProfilePassword
} from '@/api/user'
import {
  userLogout
} from '@/api/auth'
const title = ref('修改密码')
const errorType = ref('message')
const rules = ref({
		'oldPassword': {
			type: 'string',
			required: true,
			message: '请填写旧密码',
			trigger: ['blur', 'change']
		},
		'newPassword': {
			type: 'string',
			required: true,
			message: '请填写新密码',
			trigger: ['blur', 'change']
		},
		'confirmPassword': {
			type: 'string',
			required: true,
			message: '请确认密码',
			trigger: ['blur', 'change']
		},
})

const model1 = ref({
	oldPassword: '',
	newPassword: '',
	confirmPassword: '',
})


onLoad((option) => {

})	

const uForm = ref()
const submit = () => {
	uForm.value.validate().then(res => {
		onSubmit()
	}).catch(errors => {
		uni.showToast({
			icon: 'error',
			title: '校验失败'
		})
	})
	
}

const onSubmit = async() => {
	if(model1.value.newPassword != model1.value.confirmPassword){
		uni.showToast({
			icon: 'error',
			title: '输入密码不一致'
		})
		return
	}
	let data = await updateUserProfilePassword(model1.value)
	if (data) {
		uni.showToast({
			title: "保存成功",
			icon: 'success',
			duration: 2000
		})
		
		logout()
		
	}
}

const logout = async() => {
	await userLogout()
	setTimeout(() => {
		gopage('/pages/components/pages/login/login')
	}, 1500);

}

const gopage = (url) => {
	uni.navigateTo({
		url
	})
}

</script>

<style lang="scss" scoped>
	
</style>
