<template>
  <div>
    <yd-navbar title="登录">
    </yd-navbar>
    <yd-cell-group>
      <yd-cell-item>
        <span slot="left">账号：</span>
        <yd-input
          required
          slot="right"
          v-model="username"
          placeholder="请输入账号"
          ref="username"
          autocomplete="on"
        ></yd-input>
      </yd-cell-item>
      <yd-cell-item>
        <span slot="left">密码：</span>
        <yd-input
          required
          slot="right"
          type="password"
          v-model="password"
          placeholder="请输入密码"
          ref="password"
        ></yd-input>
      </yd-cell-item>
    </yd-cell-group>
    <yd-button
      type="primary"
      shape="circle"
      bgcolor="#10AEFF"
      color="#FFF"
      style="width:70%;height: 35px;"
      @click.native="onLogin"
    >登录</yd-button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      openid: null, 
      username: "",
      password: "",
    };
  },
  created() {
    let openid = localStorage.getItem("openid");
    if (!openid) {
      openid = this.$route.query.openid;
      if (!openid) {
        this.$messagebox.confirm('微信授权认证失败，请您重新点击菜单进入并授权', {
          title: '温馨提示',
          showCancelButton: false,
        })
        return;
      }
      localStorage.setItem('openid', openid);
    }
    let username = localStorage.getItem('username')
    let userid = localStorage.getItem('userid')
    if (username && userid) {
      this.$router.push('/yys')
    }
    this.openid = openid;
  },
  methods: {
    onLogin() {
      if (this.$refs.username.iserror) {
        this.$dialog.toast({
          mes: '账号' + this.$refs.username.errorMsg,
        });
        return;
      }
      if (this.$refs.password.iserror) {
        this.$dialog.toast({
          mes: '密码' + this.$refs.password.errorMsg,
        });
        return;
      }
      
      let username = this.$refs.username.value.trim();
      let password = this.$refs.password.value.trim();
      let formData = {
        username: username,
        password: password,
        openid: this.openid,
      }
      this.$http.post('yysLogin', JSON.stringify(formData))
                .then(resp => {
                  if (!resp.data.success) {
                    this.$notify({ message: resp.data.message });
                    return;
                  }
                  localStorage.setItem('userid', resp.data.data)
                  localStorage.setItem('username', username)
                  this.$notify({ type: 'success', message: resp.data.message });
                  this.$router.push({path: '/yys'})
                })
                .catch(resp => {
                  this.$notify({ type: 'warning', message: '出错了，请稍后重试' });
                  console.log(resp);
                })

    },
    saveLoginInfo(userid){
            //缓存手机号
            localStorage.setItem('userid', userid)
            localStorage.setItem("username",this.username);
            
    }
  },
};
</script>
<style scoped>
.bottom-text{
  margin: 10px;
}
</style>