<template>
  <div class="login-container" v-if="showLogin">
    <div ref="formContainer" class="form-container">
      <div class="title-container">
        <img class="logo-img" :src="require('@/assets/login-logo.png')" alt="">
        <img class="title-img" :src="require('@/assets/login-title.png')" alt="">
      </div>

      <div class="form-panel">
          <el-form
            ref="loginForm"
            :model="loginForm"
            :rules="loginRules"
            class="login-form"
            auto-complete="on"
            label-position="left"
          >
            <el-form-item class="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="off"
              />
            </el-form-item>

            <el-form-item class="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="off"
                @keyup.enter.native="handleLogin"
              />
            </el-form-item>

            <el-button
              :loading="loading"
              type="primary"
              round
              style="
                font-size: 36px;
                border-radius: 36px;
                height: 74px;
                width: 414px;
                border: none;
                background: linear-gradient(96deg, #ec8081 0%, #ec8081 1%, #d5171f 67%, #d5171f 100%);
                filter: drop-shadow(0px 4px 9px #d5171f66);
              "
              @click.native.prevent="handleLogin"
            >登录</el-button
            >
          </el-form>
        </div>
      </div>
  </div>
  <div v-else class="login-container-w"></div>
</template>

<script>
import drawMixin from "@/views/login/mixin/drawMixin";
import Cookies from "js-cookie";
export default {
  mixins: [drawMixin],
  name: "Login",
  data() {
    const validateUsername = (rule, value, callback) => {
      if (false) {
        callback(new Error("请正确输入登录名"));
      } else {
        callback();
      }
    };
    const validatePassword = (rule, value, callback) => {
      if (false) {
        callback(new Error("请正确输入密码"));
      } else {
        callback();
      }
    };
    return {
      isRemember: false,
      showLogin: false,
      loginForm: {
        username: "",
        password: "",
      },
      loginRules: {
        username: [
          {
            required: true,
            trigger: "blur",
            validator: validateUsername,
          },
        ],
        password: [
          {
            required: true,
            trigger: "blur",
            validator: validatePassword,
          },
        ],
      },
      loading: false,
      passwordType: "password",
      redirect: undefined,
    };
  },
  beforeCreate() {},
  created() {
    var query = this.$route.query;
    if (query.tp) {
      this.showLogin = false;
    } else {
      this.showLogin = true;
    }
    if (query.token && query.userid && query.tp) {
      this.loading = true;
      this.$store
        .dispatch("user/loginskip", {
          token: query.token,
          tp: query.tp,
        })
        .then(() => {
          this.$router.push({
            path: "/",
          });
          this.loading = false;
        })
        .catch((error) => {
          console.log(error);
          this.loading = false;
        });
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  methods: {
    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) {
          this.loading = true;
          this.$store
            .dispatch("user/login", this.loginForm)
            .then(() => {
              console.log(this.redirect);
              this.$router.push({
                path: this.redirect || "/",
              });
              this.loading = false;
            })
            .catch(() => {
              this.loading = false;
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

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

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    width: 70%;
    height: 100%;

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

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

  .el-form-item {
    border-radius: 29px;
    background-color: #fff;
    border: 1px solid #cccccc;
    color: #0003;
    font-size: 16px;
  }
}
</style>

<style lang="scss" scoped>
::v-deep .el-checkbox__inner {}
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container-w {
  min-height: 100%;
  width: 100%;
  background-color: #ffffff;
  overflow: hidden;
}

.login-container {
  min-height: 100%;
  width: 100%;
  background: url("../../assets/bkg.png") no-repeat;
  background-size: cover;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;


  .form-container {

  }

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

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

  .form-panel {
    width: 584px;
    max-width: 100vw;
    height: 327px;
    background: #fff;
    border-radius: 15px;
    filter: drop-shadow(0px 1px 10px rgba(183,70,74,0.48));

    margin-top: 39px;
  }
  .login-form {
    height: 100%;
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-evenly;
  }

  .form-item {
    flex: 0;
    width: 414px;
    height: 59px;

    margin-bottom: 0;

  }
  .svg-container {
    padding: 6px 0;
    color: $dark_gray;
    vertical-align: middle;
    width: 25px;
    transform: scale(1.2,1.2);
    display: inline-block;

    margin-left: 39px;
  }

  .title-container {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    .logo-img {
      width: 124px;
    }
    .title-img {
      width: 577px;

      margin-top: 20px;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
::v-deep .el-button.is-round > span {
  width: 166px;
  display: inline-block;
}
</style>
