const md5 = require("../../../../utils/md5");
const _Http = getApp().globalData.http;
import Toast from '@vant/weapp/toast/toast';
Page({
    data: {
        from: {
            password: "", //原密码
            newPassword: "", //新密码
            verifyNewPassword: "" //确认密码
        },
        disabled: true,
        loading: false,
        confirmPassword: "",
    },
    onLoad(options) {
        getApp().globalData.Language.getLanguagePackage(this, '修改密码')
    },
    /* 修改密码 */
    changePassword() {
        if (this.data.disabled || this.data.loading) return;
        if (this.data.confirmPassword != true) return Toast({
            message: getApp().globalData.Language.getMapText("两次输入的密码不一致"),
            position: 'bottom'
        });
        let from = this.data.from;
        this.setData({
            loading: true
        })
        _Http.basic({
            "classname": "common.usercenter.usercenter",
            "method": "changePassWord",
            "content": {
                "password": md5.hexMD5(from.password),
                "newpassword": md5.hexMD5(from.newPassword)
            }
        }).then(res => {
            console.log(res)
            this.setData({
                loading: false
            })
            if (res.code != '1') return Toast({
                message: res.msg,
                position: 'bottom'
            });
            this.setData({
                disabled: true
            })
            wx.showToast({
                title: getApp().globalData.Language.getMapText("修改成功") + '!',
            })
            setTimeout(() => {
                wx.navigateBack({
                    delta: 0
                })
            }, 300)
        })
    },
    /* 表单输入 */
    formInput(e) {
        let v = e.detail.value.trim(),
            name = e.currentTarget.dataset.name;
        this.setData({
            [`from.${name}`]: v
        });
        let disabled = false,
            from = this.data.from;
        for (let i in from) {
            if (from[i] == '') disabled = true;
        }
        this.setData({
            disabled
        })
    },
    /* 验证确认密码 */
    passwordBlur() {
        let {
            from
        } = this.data,
            confirmPassword = from.newPassword == from.verifyNewPassword;
        if (from.newPassword == '' || from.verifyNewPassword == '') confirmPassword = ""
        this.setData({
            confirmPassword
        })
    },
    /* 清除确认密码 */
    clearNewPassword() {
        this.setData({
            ['from.verifyNewPassword']: '',
            confirmPassword: ""
        })
    },
})