<template>
  <div class="login-container">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <div class="title-container">
        <h3 class="title">请登录</h3>
      </div>
      <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="username"
          v-model="loginForm.username"
          placeholder="用户名"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="on"
          @blur="getCode"
        />
      </el-form-item>
      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="密码"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon
            :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
          />
        </span>
      </el-form-item>
      <el-form-item prop="imgCode" class="login-code">
        <el-input v-model="loginForm.imgCode" placeholder="验证码" />
        <img :src="imgurl" alt="" @click="getCode" />
      </el-form-item>
      <el-button
        type="primary"
        style="width: 100%; margin-bottom: 30px"
        @click.native.prevent="handleLogin"
      >
        登录
      </el-button>
    </el-form>
    <el-dialog
      title="获取验证码"
      width="450px"
      :visible.sync="codeShow"
      :close-on-press-escape="false"
      :close-on-click-modal="false"
    >
      <div class="getCode">
        <input type="text" v-model="vcode" placeholder="验证码" maxlength="6" />
        <span
          class="code"
          :class="{ code: 1, number: codeState.state }"
          @click="getCheck()"
          >{{ codeState.state ? codeState.time + "s" : "重新获取" }}</span
        >
      </div>
      <div class="loginBtn">
        <el-button
          type="primary"
          style="width: 100%; margin-bottom: 30px"
          @click="btnClick"
        >
          确定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { puk } from "../../api/common";
import urls from "../../api/urls";
export default {
  name: "Login",
  data() {
    return {
      loginForm: {
        username: process.env.NODE_ENV == "development" ? "Lt_ad" : "",
        password: process.env.NODE_ENV == "development" ? "Adgh#$%!13t" : "",
        imgCode: "",
      },
      loginRules: {
        username: [
          {
            required: true,
            trigger: "blur",
            message: "请输入用户名",
          },
        ],
        password: [
          {
            required: true,
            trigger: "blur",
            message: "请输入密码",
          },
        ],
        imgCode: [
          {
            required: true,
            trigger: "blur",
            message: "请输入验证码",
          },
        ],
      },
      passwordType: "password",
      redirect: "/",
      encrypt: null,
      imgurl: "",
      phone: "",
      codeShow: false,
      codeState: {
        state: false,
        time: 60,
        task: null,
      },
      vcode: "",
    };
  },
  watch: {
    // $route: {
    //   handler: function(route) {
    //     this.redirect = route.query && route.query.redirect
    //   },
    //   immediate: true
    // }
  },
  created() {
    puk().then((res) => {
      const encrypt = new JSEncrypt();
      encrypt.setPublicKey(res.data);
      this.encrypt = encrypt;
    });
    if (this.loginForm.username) {
      this.getCode();
    }
  },
  methods: {
    getCode() {
      this.imgurl = `${process.env.VUE_APP_BASE_API}${urls.getCode}?userName=${
        this.loginForm.username
      }&refrech=${this.imgCodeRefrech}&times=${Date.now()}`;
    },
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          const username = this.encrypt.encrypt(this.loginForm.username);
          const password = this.encrypt.encrypt(this.loginForm.password);
          const imgCode = this.loginForm.imgCode;
          this.$post("/sysUser/login", {
            username,
            password,
            imgCode,
          }).then((res) => {
            if (res.code === 10000) {
              if (res.data.phone !== "") {
                this.phone = res.data.phone;
                this.codeShow = true;
                setTimeout(() => {
                  this.getCheck();
                }, 500);
              }
            }
          });
          // this.$store
          //   .dispatch('user/login', { username, password, imgCode })
          //   .then(() => {
          //     this.$router.push({ path: this.redirect || '/' })
          //   })
          //   .catch(() => {

          //   })
        }
      });
    },
    getCheck() {
      this.$post("/sysUser/login/smscode", {
        phone: this.phone,
      }).then((res) => {
        this.codeState.state = true;
        this.codeState.task = setInterval(() => {
          if (this.codeState.time <= 0) {
            clearInterval(this.codeState.task);
            this.codeState.task = null;
            this.codeState.state = false;
            this.codeState.time = 60;
          } else {
            this.codeState.time--;
          }
        }, 1000);
      });
    },
    btnClick() {
      this.$store
        .dispatch("user/login", {
          phone: this.phone,
          vcode: this.vcode,
        })
        .then(() => {
          this.$router.push({ path: this.redirect || "/" });
        });
      // this.$store
      //   .dispatch("/sysUser/login/valismscode", {
      //     phone: this.phone,
      //     vcode: this.vcode,
      //   })
      //   .then(() => {
      //     this.$router.push({ path: this.redirect || "/" });
      //   })
    },
  },
};
</script>
<style lang="scss">
$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }

  .login-code {
    .el-form-item__content {
      display: flex;

      img {
        flex: 0 0 100px;
        margin: 10px;
      }
    }
  }
}
</style>
<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}

.getCode {
  width: 80%;
  height: 45px;
  background: #ffffff;
  border: 1px solid #9e9e9e;
  border-radius: 10px;
  margin: 0 auto;
  // margin-top: 22px;
  box-sizing: border-box;
  padding-left: 31px;
  padding-right: 40px;
  display: flex;
  flex-direction: row;
  justify-content: space-between;

  input {
    margin: 0;
    padding: 0;
    border: none;
    outline: none;
    width: 60%;
    height: 45px;
    line-height: 45px;
    color: #d3d3d3;
    background-color: transparent;
  }

  .code {
    display: block;
    line-height: 45px;
    text-align: center;
    color: #409eff;
    // position: absolute;
    // right: 20px;
    // bottom: 24px;
  }
}

.loginBtn {
  width: 80%;
  margin: 0 auto;
  margin-top: 30px;
}
</style>
