<template>
  <div class="container">
    <div>
      <div class="login-box">
        <div class="login-box-lf">
          <img src="../../../static/login/img/goelia-bg.png" alt="goelia" />
        </div>

        <el-form
          ref="loginForm"
          :model="loginForm"
          :rules="loginRules"
          class="login-form"
          auto-complete="on"
          label-position="left"
        >
          <div class="title-container">
            <h3 class="title">歌莉娅AI营销平台</h3>
          </div>
          <el-form-item prop="username" class="input_outer">
            <span class="u_user"></span>
            <input
              class="text"
              ref="username"
              name="username"
              v-model="loginForm.username"
              placeholder="请输入您的账号"
              type="text"
            />
          </el-form-item>

          <el-form-item prop="password" class="input_outer">
            <span class="us_uer"></span>
            <input
              v-model="loginForm.password"
              class="text"
              rel="password"
              name="password"
              autocomplete="on"
              :type="passwordType"
              placeholder="请输入您的密码"
              @keyup.enter="handleLogin"
            />

            <span
              :class="[
                'show-pwd',
                passwordType === 'password' ? 'eye' : 'eye-open'
              ]"
              @click="showPwd"
            >
              <!-- <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" /> -->
            </span>
          </el-form-item>

          <el-button
            type="primary"
            class="login-btn"
            :loading="loading"
            @click.native.prevent="handleLogin"
            >登录</el-button
          >
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { validUsername } from "@/utils/validate";

export default {
  name: "Login",
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error("请输入账号！"));
      } else {
        callback();
      }
    };
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error("请输入密码！"));
      } else {
        callback();
      }
    };
    return {
      loginForm: {
        username: "",
        password: ""
      },
      loginRules: {
        username: [
          {
            required: true,
            trigger: "blur",
            validator: validateUsername
          }
        ],
        password: [
          {
            required: true,
            trigger: "blur",
            validator: validatePassword
          }
        ]
      },
      passwordType: "password",
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {}
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        const query = route.query;
        if (query) {
          this.redirect = query.redirect;
          this.otherQuery = this.getOtherQuery(query);
        }
      },
      immediate: true
    }
  },
  mounted() {
    if (this.loginForm.username === "") {
      this.$refs.username.focus();
    } else if (this.loginForm.password === "") {
      this.$refs.password.focus();
    }
  },
  methods: {
    checkCapslock({ shiftKey, key } = {}) {
      if (key && key.length === 1) {
        if (
          (shiftKey && (key >= "a" && key <= "z")) ||
          (!shiftKey && (key >= "A" && key <= "Z"))
        ) {
          this.capsTooltip = true;
        } else {
          this.capsTooltip = false;
        }
      }
      if (key === "CapsLock" && this.capsTooltip === true) {
        this.capsTooltip = false;
      }
    },
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password && this.$refs.password.focus();
      });
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          this.$store
            .dispatch("user/login", this.loginForm)
            .then(() => {
              this.$router.push({
                path: this.redirect || "/",
                query: this.otherQuery
              });
              this.loading = false;
            })
            .catch(() => {
              this.loading = false;
            });
        } else {
          return false;
        }
      });
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== "redirect") {
          acc[cur] = query[cur];
        }
        return acc;
      }, {});
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
@import "../../../static/login/css/normalize.css";
@import "../../../static/login/css/component.css";
input:-webkit-autofill {
  // -webkit-box-shadow: 0 0 0 50px #fffc inset; /* Change the color to your own background color */
  -webkit-text-fill-color: #3d3d3d !important;
}
input:-internal-autofill-previewed,
input:-internal-autofill-selected {
  -webkit-text-fill-color: 3d3d3d !important;
  transition: background-color 50000s ease-out 0.5s;
  color: 3d3d3d !important;
}
input::input-placeholder {
  color: #767676;
}
input::-webkit-input-placeholder {
  color: #767676;
}
.container {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  // background: url("../../../static/login/img/goelia-bg.png") no-repeat 40%;
  // background-size: cover;
  position: relative;

  &::before {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: url("../../../static/login/img/goelia-bg.png") no-repeat 50% 9%;
    background-size: cover;
    opacity: 0.25; // 调整这个值来控制透明度，0.6 = 60%不透明
    z-index: -1;
  }
}
.title-container {
  .title {
    margin: 0px auto 50px auto;
    text-align: center;
    font-size: 32px;
    line-height: 50px;
    color: #000;
  }
}
.login-box {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
  width: 954px;
  height: 513px;
  margin: auto;
  .login-box-lf {
    width: 400px;
    height: 513px;
    img {
      width: 100%;
      height: 100%;
      display: block;
      object-fit: cover;
      object-position: 41%;
      border-radius: 10px 0px 0px 10px;
      box-shadow: -5px 5px 10px rgba(0, 0, 0, 0.4);
    }
  }
  .login-form {
    padding: 65px 60px;
    width: 554px;
    height: 513px;
    background: rgba(255, 255, 255, 0.8);
    border-radius: 0px 10px 10px 0px;
    box-shadow: 5px 5px 10px rgba(0, 0, 0, 0.4);
  }
  .login-btn {
    width: 100%;
    height: 50px;
    border-radius: 10px;
    margin-top: 30px;
    font-size: 18px;
    background-color: #000;
    &:hover {
      background-color: #333;
    }
  }
}
</style>
