<template>
  <div class="wrapper" style="min-width:1300px;">
    <!-- 左侧图片 -->

    <div class="img-block">
      <img src="../../assets/image/jz-logo.png" />
    </div>
    <div class="right-block">
      <div class="login-container">
        <div class="login-title">
          <p>欢迎登录</p>
          <p>鲸致生活-<span style="color:#20B6EB;">门户系统</span></p>
          <p class="slogan">因为有你 所以鲸致 JINGZHI LIFE</p>
        </div>
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on"
          label-position="left">


          <el-form-item prop="username">
            <span class="svg-container">
              <svg-icon icon-class="user" />
            </span>
            <el-input v-model="loginForm.username" name="username" type="text" tabindex="1" auto-complete="on"
              placeholder="请输入登录账号" clearable />
          </el-form-item>

          <el-form-item prop="password">
            <span class="svg-container">
              <svg-icon icon-class="password" />
            </span>
            <el-input v-model="loginForm.password" :type="passwordType" name="password" auto-complete="on" tabindex="2"
              show-password placeholder="请输入登录密码" @keyup.enter.native="handleLogin" clearable />
          </el-form-item>
          <el-form-item prop="verifyCode">
            <div style="display: flex;flex-direction: row;">
              <el-input v-model="loginForm.verifyCode" name="verifyCode" type="text" tabindex="3" auto-complete="on"
                placeholder="请输入验证码" clearable />
              <img id="code" width="130px" height="48px" alt="加载中" @click="getCaptcha" />
              <span class="svg-container">
                <svg-icon icon-class="verifyCode" />
              </span>
            </div>
          </el-form-item>
          <!-- <p style="margin:10px 0">没有加入吗？<span style="text-decoration: underline; color:#20B6EB">联系总部招商4009218787</span></p> -->
          <el-button :loading="loading" type="primary" style="width:100%;margin:20px 0;"
            @click.native.prevent="handleLogin">登录</el-button>
        </el-form>

        <div class="bottom-company">
          <div class="line"></div>
          <div class="compangy-name">上海宅易丞鲸致网络科技有限公司</div>
          <div class="line"></div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {getToken, hasLogin} from '@/utils/auth'
export default {
  name: "Login",
  data() {
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error("账号密码长度应大于6"));
      } else {
        callback();
      }
    };
    return {
      logo: require("../../icons/star.jpg"),
      loginForm: {
        username: "",
        password: "",
        verifyCode: ""
      },
      loginRules: {
        username: [
          { required: true, message: "登录账号不能为空", trigger: "blur" }
        ],
        password: [
          { required: true, message: "登录密码不能为空", trigger: "blur" },
          { validator: validatePassword, trigger: "blur" }
        ],
        verifyCode: [
          { required: true, message: "验证码不能为空", trigger: "blur" }
        ]
      },
      passwordType: "password",
      loading: false
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        // this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    console.log(this.$store.state.user);
    if (getToken()&&hasLogin()&&this.$store.state.user.user) {
    this.$router.push({ path: "/dashboard" });
    }
    this.getCaptcha();
    // window.addEventListener('hashchange', this.afterQRScan)
  },
  destroyed() {
    // window.removeEventListener('hashchange', this.afterQRScan)
  },
  methods: {
    getCaptcha() {
      this.$store.dispatch('verifyCode').then(res => {
        let blob = new Blob([res.data], { type: res.data.type });
        document.getElementById('code').src = URL.createObjectURL(blob);
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid && !this.loading) {
          this.loading = true;
          this.$store
            .dispatch("LoginByUsername", this.loginForm)
            .then(() => {
              console.log('登陆成功');
              this.loading = false;
              if (this.loginForm.password == "123456" || this.loginForm.password == "654321") {
                this.$router.push({ path: "/profile/password" });
                this.$confirm(
                  "您的密码是初始密码，请修改；否则下次将无法登录",
                  "提示",
                  {
                    confirmButtonText: "立即修改",
                    type: "warning"
                  }
                );
              }

                else this.$router.push({ path: "/dashboard" });
              // else this.$router.push({ path: this.redirect || "/" });
            })
            .catch(() => {
              this.loading = false;
              this.getCaptcha();
            });
        } else {
          return false;
        }
      });
    }
  }
};
</script>

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

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

@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: 92%;

    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 {
        background: #fff;
        // box-shadow: 0 0 0px 1000px $bg inset !important;
        box-shadow: 0 0 0px 1000px #fff inset !important;
        // -webkit-text-fill-color: $cursor !important;
      }
    }
  }

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

  .el-form-item:hover {
    border: 1px solid #09afff;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
// $light_gray: #eee;
$light_gray: #333;

.right-block {
  flex: 2;
  background: url('../../assets/image/login-right.png') no-repeat center center;
  background-size: cover;
  min-height: 600px;
  height: 100%;
  display: flex;
  align-items: center;
}

.login-container {

  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px 80px 40px;

  .login-form {
    position: relative;
    width: 454px;
    // 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;
    color: #888;
  }

  .logo-form {
    border: 0px;
    text-align: center;
  }

  .logo {
    width: 100px;
  }

  .title-container {
    position: relative;

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

  .copyright {
    font-size: 12px;
    color: #fff;
    position: absolute;
    bottom: 0;
    left: 50%;
    transform: translate(-50%, -50%);
    margin-bottom: 20px;
    letter-spacing: 0.6px;

    a {
      font-weight: bold;
      border-bottom: 1px solid #fff;
      font-family: "PingFangSC-Semibold", sans-serif;
    }
  }
}
</style>
<style scoped>
.wrapper {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  min-height: 600px;
}

.img-block {
  flex: 3;
  background: url('../../assets/image/login-left.png') no-repeat center center;
  background-size: cover;
  height: 100%;
  min-height: 480px;
  padding: 88px 0 0 88px;
  min-width: 800px;
}

.img-block img {
  width: 200px;
  /* height:60px; */
}

.login-title {
  width: 454px;
}

.login-title p {
  color: #000;
  font-size: 24px;
  font-weight: bold;
}

.login-title p.slogan {
  font-size: 14px;
  color: #B3B3B3;
  margin: 20px 0 40px;
}

.el-button--primary {
  background-color: #20B6EB;
  border-color: #20B6EB;
  height: 48px;
  font-size: 18px;
}

.wrapper input:-internal-autofill-selected {
  /* background: #fff!important; */
}

.bottom-company {
  height: 20px;
  display: flex;
  justify-content: center;
  align-items: center;
  color: #AFBCCC;
  font-size: 13px;
  margin-top: 40px;
}

.bottom-company .compangy-name {
  margin: 0 20px;
}

.bottom-company .line {
  width: 100px;
  height: 1px;
  background: #AFBCCC;

}
</style>
