import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt } from '@/utils/jsencrypt'
import store from "../../store"; // 公钥,私钥都声明在里面
export default {
  components: {
  },
  data () {
    return {
      isSmsLogin: false,
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: "",
        uuid: "",
        mobile: "",
        smsCode: "",
      },
      loading: false,
      codeUrl: "",
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "账号不能为空" },
        ],
        mobile: [
          { required: true, trigger: "blur", message: "手机号不能为空" },
        ],
        password: [
          { required: true, trigger: "blur", message: "口令不能为空" },
        ],
        code: [{ required: true, trigger: "blur", message: "验证码不能为空" }],
        // smsCode: [{ required: true, trigger: "blur", message: "验证码不能为空" }]
      },
      redirect: undefined,
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
        console.log(route)
        if (route.query && route.query.code && route.query.exiturl) {
          let obj = {
            code: route.query.code,
          };
          this.singleLoading = true;
          this.$store
              .dispatch("SingleLogin", obj)
              .then((res) => {
                Cookies.set("singleUrl", route.query.exiturl, { expires: 30 });
                this.singleLoading = false;
                if (this.viewChecked == true) {
                  this.$router.push({ path: "/viewPages/profile" });
                } else {
                  if (res && res.code && res.code == 200) {
                    let requireUpdatePassword = res.requireUpdatePassword;
                    if (requireUpdatePassword) {
                      //重置密码
                      this.$set(this.resetPwd, "open", true);
                    } else {
                      this.$router
                          .push({ path: this.redirect || "/" })
                          .catch(() => {});
                    }
                  }
                }
              })
              .catch((error) => {
                const errorString = error + "";
                this.loading = false;
                if (errorString.indexOf("证书无效") > -1) {
                  loginError().then((res) => {
                    this.upload.open = true;
                    this.upload.loginJson = res.msg;
                  });
                }
                this.singleLoading = false;
              });
        }
      },
      immediate: true,
    },
  },
  computed: {
  },
  created () {
    this.getCode();
    store.dispatch('systemTitle').then(() => {
      document.title = store.getters.browserName == '' ? '集体资产监管一张图' : store.getters.browserName
    })
  },
  mounted () {
  },
  methods: {
    getCode () {
      getCodeImg().then((res) => {
        this.codeUrl = "data:image/gif;base64," + res.img;
        this.loginForm.uuid = res.uuid;
      });
    },
    handleLogin () {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          if (this.isSmsLogin) {
            this.loading = true;
            if (this.loginForm.rememberMe) {
              Cookies.set("mobile", this.loginForm.mobile, { expires: 30 });
              Cookies.set("rememberMe", this.loginForm.rememberMe, {
                expires: 30,
              });
            } else {
              Cookies.remove("mobile");
              Cookies.remove("rememberMe");
            }
            if (this.viewChecked == true) {
              Cookies.set("cookieViewChecked", this.viewChecked, {
                expires: 30,
              });
            } else {
              Cookies.remove("cookieViewChecked");
            }
            this.$store
              .dispatch("SmsLogin", this.loginForm)
              .then((res) => {
                Cookies.remove("viewPagesMonophylyId");
                Cookies.remove("viewPagesMonophyly");
                if (this.viewChecked == true) {
                  this.$router.push({ path: "/nav" });
                } else {
                  if (res && res.code && res.code == 200) {
                    let requireUpdatePassword = res.requireUpdatePassword;
                    if (requireUpdatePassword) {
                      //重置密码
                      this.$set(this.resetPwd, "open", true);
                    } else {
                      this.$router
                        .push({ path: this.redirect || "/" })
                        .catch(() => { });
                    }
                  }
                }
              })
              .catch((error) => {
                const errorString = error + "";
                this.loading = false;
                if (errorString.indexOf("证书无效") > -1) {
                  loginError().then((res) => {
                    this.upload.open = true;
                    this.upload.loginJson = res.msg;
                  });
                }
                this.getCode();
              });
          } else {
            this.loading = true;
            if (this.loginForm.rememberMe) {
              Cookies.set("username", this.loginForm.username, { expires: 30 });
              Cookies.set("password", encrypt(this.loginForm.password), {
                expires: 30,
              });
              Cookies.set("rememberMe", this.loginForm.rememberMe, {
                expires: 30,
              });
            } else {
              Cookies.remove("username");
              Cookies.remove("password");
              Cookies.remove("rememberMe");
            }
            if (this.viewChecked == true) {
              Cookies.set("cookieViewChecked", this.viewChecked, {
                expires: 30,
              });
            } else {
              Cookies.remove("cookieViewChecked");
            }
            this.$store
              .dispatch("Login", this.loginForm)
              .then((res) => {
                Cookies.remove("viewPagesMonophylyId");
                Cookies.remove("viewPagesMonophyly");
                if (this.viewChecked == true) {
                  this.$router.push({ path: "/nav" });
                } else {
                  if (res && res.code && res.code == 200) {
                    let requireUpdatePassword = res.requireUpdatePassword;
                    if (requireUpdatePassword) {
                      //重置密码
                      this.$set(this.resetPwd, "open", true);
                    } else {
                      this.$router.push({ path: this.redirect || "/nav" })
                    }
                  }
                }
              })
              .catch((error) => {
                //console.log(error);
                const errorString = error + "";
                this.loading = false;
                if (errorString.indexOf("证书无效") > -1) {
                  loginError().then((res) => {
                    this.upload.open = true;
                    this.upload.loginJson = res.msg;
                  });
                }
                this.getCode();
              });
          }
        }
      });
    },
  }
};
