new Vue({
    el: "#main",
    data: {
        type: 1, //记录选择的登录方式，1-手机号登录，2-验证码登录
        mobile: '',
        password: '',
        captcha: '',
        eyeIsOpen: false,
        infoText: '获取验证码',
        countdown: false //是否在倒计时
    },
    methods: {
        changeType: function(val){
            this.type = val;
        },
        getCaptcha: function(){
            //先校验手机号码是否正确
            var isMobile = isMobilePhone(this.mobile);
            if(!isMobile){
                tip('请输入正确的手机号码！');
                return false;
            }
            setTimer(this);
            //发送请求
            sendToGetCaptcha(this);
        },
        login: function(){
            if(this.type == 1){
                var validation = $("#form1").valid();
                if(validation){
                    getPublicKey(this);
                }
            }else{
                var validation = $("#form2").valid();
                if(validation){
                    login({
                        mobile: this.mobile,
                        captcha: this.captcha,
                        platform: 4
                    })
                }
            }
        },
    }
});

//验证码定时
function setTimer(vm) {
    var seconds = 59;
    vm.countdown = true;
    timer = setInterval(function() {
        if(seconds == 0) {
            clearInterval(timer);
            timer = null;
            seconds = 59;
            vm.infoText = "获取验证码";
            vm.countdown = false;
            return ;
        }
        seconds --;
        vm.infoText = seconds+"s后重新获取";
    }, 1000)
}

function sendToGetCaptcha(vm){
    var url = "common/captcha",
        params = {
            mobile: vm.mobile,
            type: 5,
            captchaToken: 5
        };
    httpRequest.post(url, {data: params}).then(function(res){
        if(res.status == 200){
            tip('发送成功','add');
        }else{
            tip('获取验证码失败');
        }
    });
}

function getPublicKey(vm){
    var url = "login/public_key";
    httpRequest.post(url).then(function(res){
        if(res.status){
            var mod = res.data.modulus;
            var exp = res.data.exponent;
            key = RSAUtils.getKeyPair(exp, "", mod);
            if (key) {
                encryedPwd = RSAUtils.encryStr(key, vm.password);
                login({
                    mobile: vm.mobile,
                    password: encryedPwd,
                    platform: 4
                })
            }else {
               tip("获取数据失败")
               $submit.removeAttr('disabled')
            }
        }else{
            tip(res.msg);
        }
    })
}

function login(data) {
    httpRequest.post('login/doctor', {data: data}).then(function(res){
        if(res.status == 200){
            var docInfo = res.data;
            localStorage.setItem(httpRequest.agentName,JSON.stringify({
                id: docInfo.id,
                uid: docInfo.uid,
                token: docInfo.token,
                imei: localStorage.getItem('WLYY_IMEI'),
                platform: 4
            }));
            localStorage.setItem("app_storage", JSON.stringify({
                'api_login_doctor': docInfo,
                'IMEI': localStorage.getItem('WLYY_IMEI')
            }))
            //将用户的角色信息单独存储在localstorage中
            localStorage.setItem("userRole", JSON.stringify(docInfo.userRole));
            sessionStorage.setItem("userRole", JSON.stringify(docInfo.userRole));
            window.location.href = "../home/html/index.html";
        }else{
            tip(res.msg);
        }
    });
}
