<template>
    <div>
        <van-form @submit="onSubmit">
            <div style="margin-top: 20px">
                <van-field v-model="oldPwd" label="旧密码" placeholder="请输入旧密码"
                           :rules="[{ required: true, message: '' }]"></van-field>
            </div>
            <div style="margin-top: 20px">
                <van-field v-model="newPwd" label="新密码" placeholder="请输入新密码"
                           :rules="[{ required: true, message: '' }]"></van-field>
            </div>
            <div style="margin-top: 20px">
                <van-field v-model="newPwds" label="确认密码" placeholder="请确认密码"
                           :rules="[{ required: true, message: '' }]"></van-field>
            </div>
            <div style="margin: 40px 16px;">
                <van-button block type="info" native-type="submit">
                    保存
                </van-button>
            </div>
        </van-form>
    </div>
</template>

<script>
    import {sysUser} from '@/api'

    export default {
        name: "pwd-edit",
        data() {
            return {
                title: '修改密码',
                oldPwd: '',
                newPwd: '',
                newPwds: '',
            }
        },
        methods: {
            onSubmit() {
                if (this.newPwds !== this.newPwd) {
                    this.$toast('两次填写的密码不一致');
                } else {
                    this._save()
                }
            },
            _save: async function () {
                let data = {
                    oldPwd: this.oldPwd,
                    newPwd: this.newPwd
                };
                let result = await sysUser.editPsd(data)
                if (result) {
                    let that = this;
                    this.$dialog.confirm({
                        title: '提示',
                        message: '登录密码已被修改，请重新登录！',
                    }).then(() => {
                        that._exit(); //退出重新登录
                    }).catch(() => {
                        // on cancel
                    });

                }
            },
            //退出
            _exit() {
                this.$global.logout();
                localStorage.removeItem('password'); //清空密码
                // this.$navigation.cleanRoutes();
                this.$router.replace('/login');
            },
        }
    }
</script>
