<template>
  <div class="login-box">
    <div class="left">
      <div class="logo">
        <div class="logo-container">
          <img src="@/assets/imgs/login/logo.png" alt="" />
        </div>
        <div class="text">
          <span class="ch"> 贵州博物馆精品展览展示平台管理后台</span>
          <span class="en">
            Management Background Of Guizhou Museum Boutique Exhibition
            Platform</span
          >
        </div>
      </div>
    </div>
    <div class="login-container right">
      <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"
          />
        </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"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon
              :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
            />
          </span>
        </el-form-item>

        <el-row :gutter="10">
          <el-col :span="16">
            <el-form-item prop="captcha">
              <el-input
                ref="captcha"
                v-model="loginForm.captcha"
                placeholder="请输入验证码"
                name="captcha"
                tabindex="3"
                auto-complete="on"
                @keyup.enter.native="handleLogin"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8" class="captcha">
            <img
              v-if="requestCodeSuccess"
              style="margin-top: 2px"
              :src="captchaImgSrc"
              @click="handleGetCaptcha"
            />
            <img
              v-else
              style="margin-top: 2px"
              src="@/assets/404_images/checkcode.png"
              @click="handleGetCaptcha"
            />
          </el-col>
        </el-row>

        <el-button
          :loading="loading"
          type="primary"
          style="width: 100%; margin-bottom: 30px"
          @click.native.prevent="handleLogin"
          >登录</el-button
        >
      </el-form>
      <div class="footer">©贵州省文化和旅游厅版权所有</div>
    </div>
 
  </div>
</template>

<script>
import { getVerify } from "@/api/user";
import md5 from 'js-md5'

export default {
  name: "Login",
  data() {
    return {
      loginForm: {
        username: "",
        password: "",
        captcha: "",
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入用户名" },
        ],
        password: [{ required: true, trigger: "blur", message: "请输入密码" }],
        captcha: [{ required: true, trigger: "blur", message: "请输入验证码" }],
      },
      loading: false,
      passwordType: "password",
      redirect: undefined,
      requestCodeSuccess: false,
      captchaImgSrc: "",
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  created() {
    this.handleGetCaptcha();
  },
  methods: {
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },

    // 获取验证码
    handleGetCaptcha() {
      this.currdatetime = new Date().getTime();
      getVerify()
        .then((res) => {
          this.requestCodeSuccess = true;
          const imgSrc = window.URL.createObjectURL(res);
          this.captchaImgSrc = imgSrc;
        })
        .catch(() => {
          this.requestCodeSuccess = false;
        });
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          const params =JSON.parse(JSON.stringify(this.loginForm))
          params.password = md5(params.password)
          this.$store
            .dispatch("user/login", params)
            .then(async () => {
              // debugger
              // await this.$store.dispatch('user/getMenuList')
              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;
    height: 47px;
    width: 85%;

    input {
      background: transparent !important;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      // color: $light_gray;
      color: #333;
      height: 47px;
      // caret-color: $cursor;
      caret-color: #333;
      &:-webkit-autofill {
        // box-shadow: 0 0 0px 1000px $bg inset !important;
        box-shadow: 0 0 0px 1000px transparent 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: 2px solid #e0e7ff;
    background: transparent;
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
// $dark_gray: #889aa4;
$deep_gray:'#313c64';
$light_gray: #eee;
.login-box {
  display: flex;
  height: 100vh;
  width: 100%;
  overflow: hidden;
  // background-image: url("~@/assets/imgs/login/bg.png");

  box-sizing: border-box;
  position: relative;
  
}

.left,
.right {
  // width: 50%;
  height: 100%;
}

.left {
  width: 60%;
  padding: 80px;
  // background-color: #1d63c3;
  background-image: url("~@/assets/imgs/login/bg.png");
  background-size: 100% 100%;
  background-repeat: no-repeat;
  background-position: center;
  .logo {
    display: flex;
    .logo-container {
      width: 120px;
      height: 120px;
      margin-right: 4px;
      img {
        width: 100%;
      }
    }

    .ch {
      color: #fff;
      font-size: 36px;
      display: inline-block;
      margin: 6px 0 4px;
      // letter-spacing: 2px;
    }
    .en {
      color: #fff;
      font-size: 20px;
      display: inline-block;
      // letter-spacing: 1px;
    }
  }
}

.right{
  background-color: #fff;
  position: relative;
  width: 40%;
}

.login-container {
  min-height: 100%;
  // width: 100%;
  // background-color: $bg;
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;
  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 120px 35px;
    // margin: 0 auto;
    overflow: hidden;
    background-color: #fff;
    // background: #12417b;
    // background: url("~@/assets/imgs/login/login-bg.png") 100% 100% no-repeat;
    // border-radius: 16px;
    // box-shadow: 2px 6px 6px 2px rgba(0, 0, 0, 0.1);
  }

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

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

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

  .title-container {
    position: relative;

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

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

  .captcha {
    // width: 100%;
    height: 100%;
    img {
      display: block;
      width: 100%;
      height: 100%;
    }
  }
}

.el-button--primary {
  background-color: #313c64;
  color: #fff;
  border-color: transparent;
  &:hover {
    background-color: rgba($color: #313c64, $alpha: 0.8);
  }
}

.footer {
  position: absolute;
  bottom: 40px;
  left: 50%;
  transform: translateX(-50%);
}
</style>
