Component({
    properties: {
        language: {
            type: Object,
            value: {}
        }
    },
    options: {
        addGlobalClass: true
    },
    data: {
        accountno: "",
        password: "",
        inputType: "password", //密码输入框类型
        memory: true, //记忆
        focus: false,
    },
    lifetimes: {
        attached: function () {
            /* 恢复缓存中保存的账号密码 */
            this.setData({
                ...wx.getStorageSync('loginMsg')
            });
            setTimeout(() => {
                this.allowOrNot();
            }, 100)
        }
    },
    methods: {
        /* input输入 */
        inputChange(e) {
            this.setData({
                [e.target.dataset.name]: e.detail.value.trim()
            })
            this.allowOrNot()
        },
        /* 验证是否允许登录 */
        allowOrNot() {
            getCurrentPages()[0].setData({
                disabled: (this.data.accountno.length && this.data.password.length) ? false : true
            })
        },
        /* 改变密码输入框类型 */
        changePasswordType() {
            this.setData({
                inputType: this.data.inputType == "text" ? 'password' : 'text'
            })
        },
        /* 是否记忆密码 */
        isMemory() {
            this.setData({
                memory: !this.data.memory
            })
        },
        /* 处理登录 */
        handleLogin() {
            getApp().globalData.http.login({
                "accountno": this.data.accountno,
                "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"
                })
                let loginMsg = {
                    accountno: this.data.accountno,
                    baseUrl: getApp().globalData.http.baseUrl,
                    password: (this.data.memory) ? this.data.password : ''
                }
                let list = wx.getStorageSync('logins') || [],
                    index = list.findIndex(v => v.accountno == loginMsg.accountno && v.baseUrl == loginMsg.baseUrl);

                if (index != -1) {
                    list[index] = loginMsg
                } else {
                    list.push(loginMsg)
                }
                wx.setStorageSync('logins', list)
                wx.setStorageSync('loginMsg', loginMsg)
                require("./login").loginMsg(res);
                getApp().globalData.remindchangepassword = res.remindchangepassword == 1;
            })
        }
    }
})