<template>
	<view class="pwd-retrieve-container">
		<uni-forms ref="form" :value="user" labelWidth="80px">
			<uni-forms-item  name="oldPassword" :label="i18('旧密码')">
				<uni-easyinput type="password" :styles="styles" :placeholderStyle="placeholderStyle"
					v-model="user.oldPassword" :placeholder="i18('请输入旧密码')" />
			</uni-forms-item>
			<uni-forms-item name="newPassword" :label="i18('新密码')">
				<uni-easyinput type="password" v-model="user.newPassword" :styles="styles" :placeholderStyle="placeholderStyle" :placeholder="i18('请输入新密码')" />
			</uni-forms-item>
			<uni-forms-item name="confirmPassword" :label="i18('确认密码')">
				<uni-easyinput type="password" v-model="user.confirmPassword" :styles="styles" :placeholderStyle="placeholderStyle" :placeholder="i18('请确认新密码')" />
			</uni-forms-item>
			<button type="primary" style="background: #57B03D;color: white" @click="submit">{{i18('提交')}}</button>

			<view style="bottom: 80px;position: absolute;left: 10px;right:10px">
				<button type="warn" @click="" style="background: #57B03D;color: white"
					@click="deleteUser">{{i18('注销账号')}}</button>
			</view>
			<!-- <view style="bottom: 10px;position: absolute;left: 10px;right:10px">
        <button type="warn"  @click="" style="background: #57B03D;color: white" @click="handleLogout">{{i18('退出登录')}}</button>
      </view> -->
		</uni-forms>
	</view>
</template>

<script>
	import {
		updateUserPwd
	} from "@/api/system/user"
	import i18 from '@/utils/i18.js'
	export default {
		data() {
			return {
				placeholderStyle: "color:#bababa;font-size:14px",
				styles: {
					color: '#ffffff',
					borderColor: '#ffffff'
				},
				user: {
					oldPassword: undefined,
					newPassword: undefined,
					confirmPassword: undefined,

				},
				rules: {
					oldPassword: {
						rules: [{
							required: true,
							errorMessage: i18('旧密码不能为空')
						}]
					},
					newPassword: {
						rules: [{
								required: true,
								errorMessage: i18('新密码不能为空'),
							},
							{
								minLength: 6,
								maxLength: 20,
								errorMessage: i18('长度在 6 到 20 个字符')
							}
						]
					},
					confirmPassword: {
						rules: [{
							required: true,
							errorMessage: i18('确认密码不能为空')
						}, {
							validateFunction: (rule, value, data) => data.newPassword === value,
							errorMessage: i18('两次输入的密码不一致')
						}]
					}
				}
			}
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		onShow() {
			uni.setNavigationBarTitle({
				title: this.$t('page.modifypwd')
			})
		},
		methods: {
			i18(text) {
				return i18(text)
			},
			submit() {
				this.$refs.form.validate().then(res => {
					updateUserPwd(this.user.oldPassword, this.user.newPassword).then(response => {
						this.$modal.msgSuccess("修改成功")
					})
				})
			},
			handleLogout() {
				this.$modal.confirm('确定注销并退出系统吗？').then(() => {
					this.$store.dispatch('LogOut').then(() => {
						this.$tab.reLaunch('/pages/index')
					})
				})
			},
			deleteUser() {
				this.$modal.confirm('确定删除账号吗？').then(() => {
					this.$store.dispatch('DeleteUser').then(() => {
						this.$tab.reLaunch('/pages/index')
					})
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #2b2b2b;
	}

	.pwd-retrieve-container {
		padding-top: 36rpx;
		padding: 15px;
	}
</style>