let downCount = null;

Component({
    properties: {
        language: {
            type: Object,
            value: {}
        }
    },
    options: {
        addGlobalClass: true
    },
    data: {
        phonenumber: '',
        password: '',
        showText: "获取验证码",
        memory: true,
        focus: false
    },
    lifetimes: {
        attached: function () {
            /* 恢复缓存中保存的账号密码 */
            this.setData({
                ...wx.getStorageSync('loginMsg')
            });
            setTimeout(() => {
                this.allowOrNot();
            }, 100)
        }
    },
    methods: {
        /* 获取验证码 */
        getPassword() {
            this.setData({
                focus: false
            })
            if (this.data.showText != "获取验证码") return;
            if (!require("../../../utils/deleteMark").CheckPhoneNumber(this.data.phonenumber)) return;
            getApp().globalData.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,
                    focus: true
                })
                downCount = setInterval(() => {
                    let showText = this.data.showText;
                    if (showText == 0) {
                        clearInterval(downCount)
                        downCount = null;
                        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() {
            getApp().globalData.http.plogin({
                "phonenumber": this.data.phonenumber,
                "password": require("../../../utils/md5").hexMD5(this.data.password),
                "systemclient": "wechatsaletool"
            }).then(res => {
                getCurrentPages()[0].setData({
                    loading: false
                })
                if (res.code != '1') return wx.showToast({
                    title: res.msg,
                    icon: "none"
                })
                wx.setStorageSync('loginMsg', {
                    phonenumber: (this.data.memory) ? this.data.phonenumber : ''
                })
                require("./login").loginMsg(res);
                getApp().globalData.remindchangepassword = res.remindchangepassword == 1;
            })
        },
        /* 是否记忆密码 */
        isMemory() {
            this.setData({
                memory: !this.data.memory
            })
        },
    }
})