const _Http = getApp().globalData.http;
import Toast from '@vant/weapp/toast/toast';
const md5 = require('../../utils/md5');
let passwordDowm = null;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        from: {
            accountno: "", //账户名
            password: "", //验证码
            newPassword: "", //新密码
            verifyNewPassword: "", //确认密码
        },
        disabled: true, //修改按钮禁用
        loading: false,
        confirmPassword: "", //两次密码是否一致
        countDown: "", //倒计时
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },
    /* 获取验证码 */
    getPassword() {
        let accountno = this.data.from.accountno;
        if (!accountno) return Toast({
            message: '您还未填写账户名称',
            position: 'bottom'
        });
        if (this.data.countDown != '') return Toast({
            message: '您已发送验证码，请勿重复获取',
            position: 'bottom'
        });
        _Http.base({
            "classname": "common.usercenter.usercenter",
            "method": "forgetPassword_getPassWord",
            "content": {
                accountno
            }
        }, false).then(res => {
            if (res.code != 1) return Toast({
                message: res.data,
                position: 'bottom'
            });
            this.setData({
                countDown: 30
            })
            passwordDowm = setInterval(() => {
                let countDown = (this.data.countDown == 0) ? '' : this.data.countDown - 1;
                this.setData({
                    countDown
                })
                if (countDown == '') clearInterval(passwordDowm)
            }, 1000);
            Toast({
                message: res.msg,
                position: 'bottom'
            });
        })
    },
    /* 表单输入 */
    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
        })
        this.passwordBlur();
    },
    /* 验证确认密码 */
    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: ""
        })
    },
    /* 修改密码 */
    changePassword() {
        if (this.data.disabled || this.data.loading) return;
        if (this.data.confirmPassword != true) return Toast({
            message: '请检查新密码与确认密码',
            position: 'bottom'
        });
        let from = this.data.from;
        this.setData({
            loading: true
        })
        _Http.base({
            "classname": "common.usercenter.usercenter",
            "method": "forgetPassword_changePassWord",
            "content": {
                "password": md5.hexMD5(from.password),
                "accountno": from.accountno,
                "newpassword": md5.hexMD5(from.newPassword)
            }
        }).then(res => {
            this.setData({
                loading: false
            })
            if (res.msg != '成功') return Toast({
                message: res.msg,
                position: 'bottom'
            });
            this.setData({
                disabled: true
            })
            wx.showToast({
                title: '修改成功!',
            })
            let pages = getCurrentPages(),
                prevPage = pages[pages.length - 2];
            prevPage.setData({
                password: "" // 需要传递的值
            })
            wx.setStorageSync('loginMsg', {
                "accountno": this.data.from.accountno,
                "password": this.data.from.newPassword
            })
            setTimeout(() => {
                wx.navigateBack({
                    delta: 0
                })
            }, 300)
        })
    },
    onShareAppMessage() {}
})