import ApiConstant from "@/api/ApiConstant";

export default {
  computed: {
    user() {
      return this.$store.state.auth.user?.user || {};
    },
    systemConfig() {
      return this.$store.state.app.systemConfig || {};
    },
    instance() {
      if (window) {
        return window.$nuxt;
      }
      return null;
    },
  },
  methods: {
    handleLogin() {
      return new Promise((resolve, reject) => {
        // 缓存上一次登录用户id
        this.$store.commit("account/SET_USER_ID", this.user.id);
        this.jumpReset()
          .then(() => {
            const $axios = this.$axios || this.instance?.$axios || null;
            if ($axios) {
              this.$store
                .dispatch("fetchSysInfoByToken", $axios)
                .then(() => {
                  resolve();
                })
                .catch(() => {
                  reject(new Error("请查看用户菜单接口响应"));
                });
            } else {
              reject(new Error("请查看 Vue 实例对象"));
            }
          })
          .catch(() => {
            this.$store.commit("account/SET_IS_RESET", true);
            const $router = this.$router || this.instance?.$router || null;
            $router && $router.push("/reset");
          });
      });
    },
    // 判断重置
    jumpReset() {
      return new Promise((resolve, reject) => {
        if (this.systemConfig?.IS_FORCE_MODIFY_PWD === "Y") {
          if (this.user?.pwdModifyTime === this.user?.createTime) {
            reject();
          } else if (this.systemConfig?.PWD_EXPIRE_DAYS) {
            const date = new Date(this.user.pwdModifyTime);
            const dateFormat = `${date.getFullYear()}-${
              date.getMonth() + 1
            }-${date.getDate()}`;
            if (
              this.dateFun(
                dateFormat,
                Number(this.systemConfig?.PWD_EXPIRE_DAYS)
              ) === true
            ) {
              reject();
            } else {
              resolve();
            }
          } else {
            resolve();
          }
        } else if (this.systemConfig?.IS_FORCE_MODIFY_PWD === "N") {
          if (this.systemConfig?.PWD_EXPIRE_DAYS) {
            if (this.user?.pwdModifyTime !== null) {
              const date = new Date(this.user?.pwdModifyTime);
              const dateFormat = `${date.getFullYear()}-${
                date.getMonth() + 1
              }-${date.getDate()}`;
              if (
                this.dateFun(
                  dateFormat,
                  Number(this.systemConfig?.PWD_EXPIRE_DAYS)
                ) === true
              ) {
                reject();
              } else {
                resolve();
              }
            } else {
              const date = new Date(this.user?.createTime);
              const dateFormat = `${date.getFullYear()}-${
                date.getMonth() + 1
              }-${date.getDate()}`;
              if (
                this.dateFun(
                  dateFormat,
                  Number(this.systemConfig?.PWD_EXPIRE_DAYS)
                ) === true
              ) {
                reject();
              } else {
                resolve();
              }
            }
          } else {
            resolve();
          }
        } else {
          resolve();
        }
      });
    },
    dateFun(val, m) {
      const sDate = new Date(val);
      const eDate = new Date();
      if (eDate.getFullYear() - sDate.getFullYear() > Math.floor(m / 12)) {
        // 先比较年
        return true;
      }
      if (eDate.getMonth() - sDate.getMonth() > m) {
        // 再比较月
        return true;
      }
      if (eDate.getMonth() - sDate.getMonth() === m) {
        if (eDate.getDate() - sDate.getDate() >= 1) {
          return true;
        }
      }
      return false;
    },
  },
};
