<template>
  <div class="login-container" id="particlesId">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      autocomplete="on"
      label-position="left"
    >
      <div class="title-container">
        <div>
          <img src="@/assets/404_images/404.png" alt="AI智能客服系统" />
        </div>
        <div class="title">AI智能客服系统</div>
      </div>

      <el-form-item prop="username" class="border-b">
        <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"
          autocomplete="on"
        />
      </el-form-item>
      <el-tooltip v-model="capsTooltip" content="Caps lock is On" placement="right" manual>
        <el-form-item prop="password" class="border-b">
          <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"
            autocomplete="on"
            @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="remenber" class="remenber-box">
        <el-row>
          <el-col :span="12">
            <el-checkbox v-model="loginForm.remenber">记住密码</el-checkbox>
          </el-col>
          <el-col :span="12" class="forget-pwd">
            <a>忘记密码?</a>
          </el-col>
        </el-row>
      </el-form-item>
      <el-button :loading="loading" class="login-btn" @click.native.prevent="handleLogin">登录</el-button>
      <el-form-item prop="readAgrement" class="read-box">
        <el-checkbox v-model="loginForm.readAgrement">
          <span>我已阅读并同意AI智能客服</span>
        </el-checkbox>
        <a class="agreement-txt">用户协议</a>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
// eslint-disable-next-line no-unused-vars
import particles from 'particles.js'
export default {
  name: 'Login',
  data() {
    // const validateName = (rule, value, callback) => {
    // if (/^([a-zA-Z]|[0-9])+@[a-zA-Z0-9]+\.([a-zA-Z]{1,})$/.test(value)) {
    //   callback()
    // } else {
    //   callback(new Error('请输入正确的邮箱格式'))
    // }
    // if (/^[1][0-9]{10}$/.test(value)) {
    //   callback()
    // } else {
    //   callback(new Error('请输入11位手机号'))
    // }
    // }
    return {
      loginForm: {
        remenber: false,
        readAgrement: false,
        // username: '786270744@qq.com',
        username: 'yxl001',
        password: '123456'
        // username: '',
        // password: ''
      },
      loginRules: {
        username: [
          {
            required: true,
            // validator: validateName,
            trigger: 'blur'
          }
        ],
        password: [
          {
            required: true,
            trigger: 'blur',
            max: 20,
            message: '密码输入字符最长为20位'
          }
        ]
      },
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      redirect: undefined,
      otherQuery: {}
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    }
  },
  mounted() {
    if (localStorage.getItem('aicremenberpwd') === 'yes') {
      this.loginForm.remenber = true
      this.loginForm.username = this.$publicTools.decrypt(
        localStorage.getItem('aicusername')
      )
      this.loginForm.password = this.$publicTools.decrypt(
        localStorage.getItem('aicuserpwd')
      )
    }
    // eslint-disable-next-line no-undef
    particlesJS('particlesId', {
      particles: {
        number: {
          value: 80,
          density: {
            enable: true,
            value_area: 800
          }
        },
        color: {
          value: '#666'
        },
        shape: {
          type: 'circle',
          stroke: {
            width: 0,
            color: '#666'
          },
          polygon: {
            nb_sides: 5
          },
          image: {
            src: 'img/github.svg',
            width: 100,
            height: 100
          }
        },
        opacity: {
          value: 0.5,
          random: false,
          anim: {
            enable: false,
            speed: 1,
            opacity_min: 0.1,
            sync: false
          }
        },
        size: {
          value: 5,
          random: true,
          anim: {
            enable: false,
            speed: 40,
            size_min: 0.1,
            sync: false
          }
        },
        line_linked: {
          enable: true,
          distance: 150,
          color: '#666',
          opacity: 0.4,
          width: 1
        },
        move: {
          enable: true,
          speed: 6,
          direction: 'none',
          random: false,
          straight: false,
          out_mode: 'out',
          attract: {
            enable: false,
            rotateX: 600,
            rotateY: 1200
          }
        }
      },
      interactivity: {
        detect_on: 'canvas',
        events: {
          onhover: {
            enable: true,
            mode: 'repulse'
          },
          onclick: {
            enable: true,
            mode: 'push'
          },
          resize: true
        },
        modes: {
          grab: {
            distance: 400,
            line_linked: {
              opacity: 1
            }
          },
          bubble: {
            distance: 400,
            size: 40,
            duration: 2,
            opacity: 8,
            speed: 3
          },
          repulse: {
            distance: 200
          },
          push: {
            particles_nb: 4
          },
          remove: {
            particles_nb: 2
          }
        }
      },
      retina_detect: true,
      config_demo: {
        hide_card: false,
        background_color: '#000',
        background_image: '',
        background_position: '50% 50%',
        background_repeat: 'no-repeat',
        background_size: 'cover'
      }
    })
    if (this.loginForm.username === '') {
      this.$refs.username.focus()
    } else if (this.loginForm.password === '') {
      this.$refs.password.focus()
    }
  },
  methods: {
    checkCapslock(e) {
      const { key } = e
      this.capsTooltip = key && key.length === 1 && key >= 'A' && key <= 'Z'
    },
    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) {
          if (!this.loginForm.readAgrement) {
            this.$message({
              message: '请阅读并勾选用户协议',
              type: 'warning'
            })
            return false
          }
          if (this.loginForm.remenber) {
            localStorage.setItem('aicremenberpwd', 'yes')
            localStorage.setItem(
              'aicusername',
              this.$publicTools.encrypt(this.loginForm.username)
            )
            localStorage.setItem(
              'aicuserpwd',
              this.$publicTools.encrypt(this.loginForm.password)
            )
          } else {
            localStorage.removeItem('aicremenberpwd')
            localStorage.removeItem('aicusername')
            localStorage.removeItem('aicuserpwd')
          }
          this.loading = true
          let { username, password } = this.loginForm
          // console.log(this.$publicTools.encrypt('欢迎登陆')) // 加密
          // console.log(this.$publicTools.decrypt('BSC3Xq5Kxw0ICjur0TDQTQ==')) // 解密
          // 本地存储用了aes加解密 登陆暂未用
          this.$store
            .dispatch('user/login', { username, password })
            .then(() => {
              this.$router.push({
                path: this.redirect || '/',
                query: this.otherQuery
              })
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          return false
        }
      })
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    }
  }
}
</script>

<style lang="scss">
$bg: #283443;
$light_gray: black;
$cursor: black;

@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;
      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-radius: 5px;
    color: #454545;
  }
  .el-form-item.border-b {
    border-bottom: 1px solid #ddd;
  }
}
</style>

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

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

  .login-form {
    top: calc(50% - 280px);
    position: absolute;
    left: calc(50% - 260px);
    width: 520px;
    max-width: 100%;
    padding: 30px;
    margin: 0 auto;
    overflow: hidden;
    border: 1px solid #ddd;
    border-radius: 8px;
  }

  .tips {
    font-size: 14px;
    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;
    text-align: center;
    img {
      width: 100px;
      height: 100px;
    }
    .title {
      font-size: 26px;
      color: #369dff;
      margin: 20px auto;
      text-align: center;
    }
  }

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

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

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }
  .read-box {
    text-align: center;
  }
  .remenber-box {
    padding-left: 15px;
  }
  .forget-pwd {
    text-align: right;
    padding-right: 10px;
    color: #3690ff;
  }
  .agreement-txt {
    color: #3690ff;
    text-decoration: underline;
  }
  .login-btn {
    width: 100%;
    margin-bottom: 20px;
    background-color: #a4cdff;
    color: #fff;
    padding: 15px;
  }
}
</style>
