<template>
  <div class="login-container">
    <div id="particles-js">
      <!-- <vue-particles
        v-if="refreshParticles"
        color="#dedede"
        :particle-opacity="0.7"
        :particles-number="80"
        shape-type="circle"
        :particle-size="4"
        lines-color="#dedede"
        :lines-width="1"
        :line-linked="true"
        :line-opacity="0.4"
        :lines-distance="150"
        :move-speed="3"
        :hover-effect="true"
        hover-mode="grab"
        :click-effect="true"
        click-mode="push"
      /> -->
    </div>

    <div id="login" class="login-weaper animated bounceInDown">
      <div class="login-left">
        <div class="login-left-tip pt123">
          <div>
            <div class="left-line" />
          </div>
          <div class="left_tip">WELCOME TO Observable Platform</div>
        </div>
        <div class="login-left-tip pt48">
          <img class="login-logo" src="../../assets/logo/app_logo.png" alt="">
          <div class="login-text">可观测平台</div>
        </div>
        <!-- <div class="login-left-tip pt36">
          <div class="left-line grey"></div>
          <div class="login-tip">
            <div>开创平台+内容+服务</div>
            <div class="login-bt">一站式企培新纪元</div>
          </div>
        </div> -->
      </div>
      <div class="login-border">
        <div class="login-main">
          <div class="login-title">用户登录</div>
          <el-form
            ref="loginForm"
            :model="loginForm"
            :rules="loginRules"
            class="login-form"
            autocomplete="on"
            label-position="left"
          >
            <el-form-item prop="username" style="height: 45px">
              <span class="svg-container">
                <i
                  class="el-icon-user"
                  style="color: #0b73fe; font-size: 18px"
                />
              </span>
              <el-input
                ref="username"
                v-model="loginForm.username"
                placeholder="用户名"
                name="username"
                type="text"
                tabindex="1"
                autocomplete="on"
              />
            </el-form-item>

            <el-tooltip
              v-model="capsTooltip"
              content="Caps lock is On"
              placement="right"
              manual
            >
              <el-form-item
                prop="password"
                style="height: 45px; line-height: 24px"
              >
                <span class="svg-container">
                  <svg-icon
                    icon-class="password"
                    style="color: #0b73fe; font-size: 18px"
                  />
                </span>
                <el-input
                  :key="passwordType"
                  ref="password"
                  v-model="loginForm.password"
                  :type="passwordType"
                  placeholder="密码"
                  name="password"
                  tabindex="2"
                  autocomplete="on"
                  style="height: 40px"
                  @keyup.native="checkCapslock"
                  @blur="capsTooltip = false"
                  @keyup.enter.native="handleLogin"
                />
                <span class="show-pwd" @click="showPwd">
                  <svg-icon
                    :icon-class="
                      passwordType === 'password' ? 'eye' : 'eye-open'
                    "
                  />
                </span>
              </el-form-item>
            </el-tooltip>
            <el-form-item
              prop="code"
              style="width: 66%; float: left; height: 45px"
            >
              <span class="svg-container">
                <!-- <img
                  src="../../assets/logo/va.png"
                  style="width: 16px; height: 18px"
                  alt=""
                  srcset=""
                /> -->
              </span>
              <el-input
                ref="username"
                v-model.trim="loginForm.code"
                placeholder="验证码"
                name="username"
                type="text"
                tabindex="3"
                maxlength="5"
                autocomplete="off"
                style="width: 75%; height: 44px; line-height: 24px"
                @keyup.enter.native="handleLogin"
              />
            </el-form-item>
            <div
              class="login-code"
              style="
                cursor: pointer;
                width: 30%;
                height: 45px;
                float: right;
                background-color: #f0f1f5;
              "
            >
              <img
                style="
                  height: 45px;
                  width: 100%;
                  border: 1px solid rgba(0, 0, 0, 0.1);
                  border-radius: 5px;
                "
                :src="codeUrl"
                @click="getCode"
              >
            </div>

            <el-button
              :loading="loading"
              type="primary"
              style="
                height: 44px;
                width: 100%;
                padding: 12px 20px;
                margin-bottom: 30px;
                background: #fff;
                color: #0b73fe;
                border-radius: 8px;
                font-weight: bold;
              "
              @click.native.prevent="handleLogin"
            >
              <span v-if="!loading">登 录</span>
              <span v-else>登 录 中...</span>
            </el-button>
          </el-form>
        </div>
      </div>
    </div>

    <el-dialog title="Or connect with" :visible.sync="showDialog">
      Can not be simulated on local, so please combine you own business
      simulation! ! !
      <br>
      <br>
      <br>
      <social-sign />
    </el-dialog>
  </div>
</template>

<script>
import JSEncrypt from 'jsencrypt'
import { getCodeImg, getPubkey } from '@/api/login'
import moment from 'moment'
import SocialSign from './components/SocialSignin'

export default {
  name: 'Login',
  components: { SocialSign },
  data() {
    return {
      codeUrl: '',
      cookiePassword: '',
      refreshParticles: true,
      loginForm: {
        username: '',
        password: '',
        rememberMe: false,
        code: '',
        uuid: ''
      },
      loginRules: {
        username: [
          { required: true, trigger: 'blur', message: '用户名不能为空' }
        ],
        password: [
          { required: true, trigger: 'blur', message: '密码不能为空' }
        ],
        code: [
          { required: true, trigger: 'change', message: '验证码不能为空' }
        ]
      },
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {},
      currentTime: null,
      sysInfo: '',
      publicKey: ''// 设置的公钥
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    }
  },
  created() {
    this.getCode()
    // 获取加密公钥
    this.getPublickey()
    // // window.addEventListener('storage', this.afterQRScan)
    // this.getCurrentTime();
    // this.getSystemSetting();
  },
  mounted() {
    if (this.loginForm.username === '') {
      this.$refs.username.focus()
    } else if (this.loginForm.password === '') {
      this.$refs.password.focus()
    }
    window.addEventListener('resize', () => {
      this.refreshParticles = false
      this.$nextTick(() => (this.refreshParticles = true))
    })
  },
  destroyed() {
    clearInterval(this.timer)
    window.removeEventListener('resize', () => {})
    // window.removeEventListener('storage', this.afterQRScan)
  },
  methods: {
    getSystemSetting() {
      this.$store.dispatch('system/settingDetail').then((ret) => {
        this.sysInfo = ret
        // document.title = ret.sys_app_name;
        documetn.title = '可观测平台'
      })
    },
    getCurrentTime() {
      this.timer = setInterval((_) => {
        this.currentTime = moment().format('YYYY-MM-DD HH时mm分ss秒')
      }, 1000)
    },
    getCode() {
      this.loading = false
      getCodeImg().then((res) => {
        if (res !== undefined) {
          this.codeUrl = res.data
          this.loginForm.uuid = res.id
        }
      })
    },
    // 获取加密公钥
    getPublickey() {
      getPubkey().then((res) => {
        if (res && res.code === 200) {
          this.publicKey = res.data
        }
      })
    },
    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.focus()
      })
    },
    // 加密方法
    setRsaCode(str) {
      // 创建 RSA 加密实例
      const encryPt = new JSEncrypt()
      // 设置公钥
      encryPt.setPublicKey(this.publicKey)
      console.log('加密', encryPt.encrypt(str))
      return encryPt.encrypt(str)
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true
          // 加密在此处处理
          // 保留未加密密码
          const originPassword = this.loginForm.password
          const password = this.setRsaCode(this.loginForm.password)
          Object.assign(this.loginForm, { password: password })
          this.$store
            .dispatch('user/login', this.loginForm)
            .then(() => {
              this.loading = false
              // this.$router
              //   .push({ path: this.redirect || "/", query: this.otherQuery })
              //   .catch((e) => {
              //     this.loading = false;
              //   });
              this.$router
                .push({ path: this.redirect || '/', query: this.otherQuery })

                .catch((e) => {
                  this.loading = false
                })
            })
            .catch((e) => {
              // 如果登录失败，密码保持明文显示
              this.loginForm.password = originPassword
              this.getCode()
            })
        } else {
          return false
        }
      })
      //  localStorage.setItem('token','vip')
      //   localStorage.setItem('userName',this.loginForm.username)
      //   this.$router.push({path:'/home'})
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    }
  }
}
</script>

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

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;
.style-i {
  color: #0b73fe;
  width: 16px;
  height: 18px;
}
#color-i {
  color: #0b73fe;
  font-size: 18px;
}
.pt48 {
  padding-top: 48px;
}
.pt36 {
  padding-top: 36px;
}
.pt123 {
  padding-top: 123px;
}
.login-container {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  width: 100%;
  height: 100%;
  margin: 0 auto;
  background: url("../../assets/login.png") no-repeat;
  // background-color: #0e6cff;
  position: relative;
  background-size: cover;
  height: 100vh;
  background-position: 50%;
}

#particles-js {
  z-index: 1;
  width: 100%;
  height: 100%;
  position: absolute;
}

.login-weaper {
  margin: 0 auto;
  width: 1000px;
  z-index: 1000;
}

.login-left {
  border-top-left-radius: 5px;
  border-bottom-left-radius: 5px;
  // -webkit-box-pack: center;
  // -ms-flex-pack: center;
  // justify-content: center;
  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
  -ms-flex-direction: column;
  flex-direction: column;
  // background-color: rgba(64, 158, 255, 0);
  color: #fff;
  float: left;
  width: 50%;
  position: relative;
  min-height: 500px;
  // -webkit-box-align: center;
  // -ms-flex-align: center;
  // align-items: center;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  .login-left-tip {
    align-items: center;
    display: flex;
    font-size: 18px;
    font-family: Source Han Sans CN-Medium, Source Han Sans CN;
    font-weight: 500;
    color: #000000;
    line-height: 21px;
  }
}
.left-line {
  width: 24px;
  height: 2px;
  opacity: 1;
  background: #000000;
  // border: 2px solid #000000;
  border-radius: 2px;
  margin-right: 14px;
}
.login-text {
  font-size: 64px;
  font-family: Source Han Sans CN-Bold, Source Han Sans CN;
  font-weight: bold;
  color: #0b73fe;
  line-height: 55px;
  // -webkit-background-clip: text;
  // -webkit-text-fill-color: transparent;
}
.login-logo {
  width: 74px;
  height: 75px;
  margin-right: 19px;
}
.login-tip {
  align-items: center;
  display: flex;
  font-size: 14px;
  font-family: PingFang SC-Regular, PingFang SC;
  font-weight: 400;
  color: #9f9f9f;
  line-height: 16px;
}
.login-bt {
  color: #0b73fe;
  margin-left: 4px;
  border-bottom: 1px solid #0b73fe;
  padding-bottom: 22px;
  margin-top: 22px;
}

.login-border {
  position: relative;
  min-height: 500px;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  border-left: none;
  border-radius: 20px;
  color: #fff;
  background-color: hsla(0, 0%, 100%, 0.9);
  width: 50%;
  float: left;
  background: -webkit-linear-gradient(
    bottom,
    #5fdaff,
    #0068ff
  ); /* Safari 5.1-6.0 */
  background: -o-linear-gradient(
    bottom,
    #5fdaff,
    #0068ff
  ); /* Opera 11.1-12.0 */
  background: -moz-linear-gradient(
    bottom,
    #5fdaff,
    #0068ff
  ); /* Firefox 3.6-15 */
  background: linear-gradient(to bottom, #5fdaff, #0068ff); /* 标准语法 */
}

.login-main {
  margin: 0 auto;
  width: 70%;
}

.login-title {
  color: #fff;
  margin-bottom: 40px;
  font-weight: 500;
  font-size: 22px;
  text-align: center;
  letter-spacing: 4px;
}
.grey {
  width: 96px;
  height: 3px;
  background: #9f9f9f;
  // border: 3px solid #9f9f9f;
}
@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

::v-deep .el-form-item__error {
  top: 94%;
  left: 17px;
}
::v-deep input::-webkit-input-placeholder {
  color: #000;
  opacity: 0.36;
}
::v-deep input::-moz-input-placeholder {
  color: #000;
  opacity: 0.36;
}
::v-deep input::-ms-input-placeholder {
  color: #000;
  opacity: 0.36;
}
/* reset element-ui css */
.login-container {
  ::v-deep .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
    margin-left:5px;
    input {
      background: transparent !important;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: #333;
      caret-color: #333;

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

  .el-form-item {
    border: 1px solid rgba(0, 0, 0, 0.1);
    background: rgba(255, 255, 255, 0.8);
    border-radius: 5px;
    color: #454545;
  }
}
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  .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;
    color: #0b73fe;
    cursor: pointer;
    user-select: none;
  }

  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
    .login-weaper {
      width: 100%;
      padding: 0 30px;
      box-sizing: border-box;
      box-shadow: none;
    }
    .login-main {
      width: 80%;
    }
    .login-left {
      display: none !important;
    }
    .login-border {
      width: 100%;
    }
  }
}
</style>
