let loginMsg = require("./login"),
    md5 = require("../../../utils/md5"),
    _Http = getApp().globalData.http,
    Check = require("../../../utils/Check"),
    downCount = null;

Component({
    properties: {},
    options: {
        addGlobalClass: true
    },
    data: {
        phonenumber: '',
        password: '',
        showText: "获取验证码",
        memory: true
    },
    lifetimes: {
        attached: function () {
            /* 恢复缓存中保存的账号密码 */
            this.setData({
                ...wx.getStorageSync('loginMsg')
            });
            setTimeout(this.allowOrNot, 300)
        }
    },
    methods: {
        /* 获取验证码 */
        getPassword() {
            if (this.data.showText != "获取验证码") return;
            if (!Check.CheckPhoneNumber(this.data.phonenumber)) return;
            _Http.getpassword({
                "phonenumber": this.data.phonenumber,
                "systemclient": "wechatsaletool"
            }).then(res => {
                wx.showToast({
                    title: res.msg,
                    icon: "none",
                    duration: 3000
                })
                if (res.code != 1) return;
                this.setData({
                    showText: 30
                })
                downCount = setInterval(() => {
                    let showText = this.data.showText;
                    if (showText == 0) {
                        clearInterval(downCount)
                        showText = '获取验证码'
                    } else {
                        showText--
                    }
                    this.setData({
                        showText
                    })
                }, 1000)
            })
        },
        /* input输入 */
        inputChange(e) {
            this.setData({
                [e.target.dataset.name]: e.detail.value.trim()
            })
            this.allowOrNot();
        },
        /* 验证是否允许登录 */
        allowOrNot() {
            getCurrentPages().forEach(v => {
                if (['pages/login/phone'].includes(v.__route__)) {
                    v.setData({
                        disabled: this.data.phonenumber.length == 0 || this.data.password.length == 0
                    })
                }
            })
        },
        /* 处理登录 */
        handleLogin() {
            _Http.plogin({
                "phonenumber": this.data.phonenumber,
                "password": md5.hexMD5(this.data.password),
                "systemclient": "wechatsaletool"
            }).then(res => {
                getCurrentPages()[0].setData({
                    loading: false
                })
                if (res.msg != '成功') return wx.showToast({
                    title: res.msg,
                    icon: "none"
                })
                wx.setStorageSync('loginMsg', {
                    phonenumber: (this.data.memory) ? this.data.phonenumber : ''
                })
                loginMsg.loginMsg(res);
            })
        },
        /* 是否记忆密码 */
        isMemory() {
            this.setData({
                memory: !this.data.memory
            })
        },
    }
})