const loginMsg = require("./login"),
    md5 = require("../../../utils/md5"),
    _Http = getApp().globalData.http;

Component({
    properties: {},
    options: {
        addGlobalClass: true
    },
    data: {
        accountno: "",
        password: "",
        inputType: "password", //密码输入框类型
        memory: true, //记忆
        focus: false,
    },
    lifetimes: {
        attached: function () {
            /* 恢复缓存中保存的账号密码 */
            this.setData({
                ...wx.getStorageSync('loginMsg')
            });
            this.allowOrNot();
        }
    },
    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 > 0 && this.data.password.length > 0) ? false : true
            })
        },
        /* 改变密码输入框类型 */
        changePasswordType() {
            this.setData({
                inputType: this.data.inputType == "text" ? 'password' : 'text'
            })
        },
        /* 是否记忆密码 */
        isMemory() {
            this.setData({
                memory: !this.data.memory
            })
        },
        /* 处理登录 */
        handleLogin() {
            _Http.login({
                "accountno": this.data.accountno,
                "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', {
                    accountno: this.data.accountno,
                    password: (this.data.memory) ? this.data.password : ''
                })
                loginMsg.loginMsg(res);
            })
        }
    }
})