
<script>

export default {
  name: "Login",
  data: function () {
    return {
      loginForm: {
        account: "",
        password: "",
        hospitalname:"",
      },
      loginRules: {
        account: [
          {
            required: true,
            trigger: "blur",
            message: this.$t("login.accountTip"),
          },
        ],
        password: [
          {
            required: true,
            trigger: "blur",
            message: this.$t("login.passwordTip"),
          },
        ],
      },
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {},
    };
  },
  computed: {
    loginLoading() {
      return this.$store.state.user.loginLoading;
    },
  },
  watch: {
    loginLoading(val) {
      if (!val) this.loading = false;
    },
    $route: {
      handler: function (route) {
        const query = route.query;
        if (query) {
          this.redirect = query.redirect;
          // this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true,
    },
  },
  created() {
    const _this = this;
    
    // const _this = this;
    // document.onkeydown = function (e) {
    //   const { keyCode } = e;
    //   if (keyCode === 13) {
    //     _this.handleLogin();
    //   }
    // };
  },
  mounted() {
    this.handlemyLogin();
    // if (this.loginForm.account == "") {
    //   this.$refs.account.focus();
    // } else if (this.loginForm.password == "") {
    //   this.$refs.password.focus();
    // }
    // this.$store.commit("user/SET_LOGIN_LOADING", false);
  },
  destroyed() {
    document.onkeydown = function (e) {
      const { keyCode } = e;
      if (keyCode === 13) {
      }
    };
  },
  methods: {
    checkCapslock(e) {
      const { key } = e;
      this.capsTooltip = key && key.length === 1 && key >= "A" && key <= "Z";
    },
    handleLogin() {
      if (this.loading) return;
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$store.commit("user/SET_LOGIN_LOADING", true);
          this.$store
            .dispatch("user/login", this.loginForm)
            .then(() => {
              this.$router.push({
                path: this.redirect || "/home",
                query: this.otherQuery,
              });
            })
            .catch(() => {
              this.$store.commit("user/SET_LOGIN_LOADING", false);
            });
        } else {
          return false;
        }
      });
    },
    handlemyLogin() {
    //this.$store.dispatch('user/logout')
    console.log("开始执行单点登录!");
    if (this.loading) return;
    let username = this.$route.query.username;
    let hospitalname=this.$route.query.hospitalname;
    console.log("username:"+username);
    if (username != null && username != "") {
      this.loginForm.account = username;
      this.loginForm.hospitalname =hospitalname;
      this.loading = true;
      this.$store.commit("user/SET_LOGIN_LOADING", true);
      this.$store
        .dispatch("user/pislogin", this.loginForm)
        .then(() => {
          this.$router.push({
            path: this.redirect || "/home",
            query: this.otherQuery,
          });
        })
        .catch(() => {
          this.$store.commit("user/SET_LOGIN_LOADING", false);
        });
    }
  },
  },
  
  getOtherQuery(query) {
    return Object.keys(query).reduce((acc, cur) => {
      if (cur !== "redirect") {
        acc[cur] = query[cur];
      }
      return acc;
    }, {});
  },
};
</script>

