<template>
  <el-row style="padding-top: 45px;">
    <el-col :span="11" :offset="1">
      <img class="TT-logo" src="../../../../static/img/login/logo_login.png" alt="">
    </el-col>
    <el-col :span="8" :offset="1">
      <h3 class="title">
        <span>登录</span>
        <span>/Login</span>
      </h3>
      <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
               status-icon>
        <el-form-item prop="userPhone">
          <el-input type="tel" v-model="dataForm.userPhone" placeholder="帐号"></el-input>
        </el-form-item>
        <el-form-item prop="userPassword">
          <el-input v-model="dataForm.userPassword" type="password" placeholder="密码"></el-input>
        </el-form-item>
        <el-form-item prop="captcha" v-if="false">
          <el-row :gutter="20">
            <el-col :span="17">
              <el-input v-model="dataForm.captcha" placeholder="图形验证码">
              </el-input>
            </el-col>
            <el-col :span="7" class="captcha">
              <img :src="captchaPath" @click="getCaptcha()" alt="">
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item>
          <el-button class="login-btn-submit" type="primary" @click="dataFormSubmit()"
                     style="width:100%;background: #332C2B;color: #00EEFF;border: none;">登录
          </el-button>
          <p class="login" @click="gotoRegister">暂无账号？立即注册</p>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<script>
import { getUUID } from '@/utils'
import Crypto from '@/utils/crypto'

export default {
  data () {
    return {
      dataForm: {
        userPhone: '',
        userPassword: '',
        uuid: '',
        captcha: 'aaaa'
      },
      dataRule: {
        userPhone: [
          { required: true, message: '帐号不能为空', trigger: 'blur' }
        ],
        userPassword: [
          { required: true, message: '密码不能为空', trigger: 'blur' }
        ],
        captcha: [
          { required: true, message: '验证码不能为空', trigger: 'blur' }
        ]
      },
      captchaPath: '',
      isDisabled: false
    }
  },
  created () {
    this.getCaptcha()
  },
  methods: {
    // 提交表单
    dataFormSubmit () {
      const { userPhone, userPassword, uuid, captcha } = this.dataForm
      const postData = {
        'userPhone': userPhone,
        'userPassword': userPassword,
        'uuid': uuid,
        'captcha': captcha
      }
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          if (this.isDisabled) {
            return
          }
          this.isDisabled = true
          this.$http({
            url: this.$http.adornUrl('/user/login'),
            method: 'post',
            data: this.$http.adornData(postData)
          }).then(({ data }) => {
            this.isDisabled = false
            if (data.status) {
              const { token } = data.data
              const { next } = this.$route.query
              this.$store.commit('user/updateToken', token)
              this.$store.commit('user/updateUserPhone', Crypto.encrypt(userPhone))
              this.$cookie.set('token', token)
              this.$cookie.set('userPhone', Crypto.encrypt(userPhone))
              if (next) {
                location.replace(decodeURIComponent(next))
              } else {
                this.$router.replace({ name: 'home' })
              }
              return
            }
            this.$message.error(data.msg)
          }).catch(() => {
            this.isDisabled = false
          })
        }
      })
    },
    // 获取验证码
    getCaptcha () {
      this.dataForm.uuid = getUUID()
      this.captchaPath = this.$http.adornUrl(`/api/captcha?uuid=${this.dataForm.uuid}`)
    },
    gotoRegister () {
      this.$router.push({
        name: 'register'
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.TT-logo {
  display: block;
  width: 401px;
  height: 315px;
  margin: 0 auto;
}

.title {
  text-align: right;
  padding-right: 66px;
  margin-bottom: 37px;

  span {
    &:nth-of-type(1) {
      line-height: 30px;
      font-size: 22px;
      color: #332C2B;
    }

    &:nth-of-type(2) {
      line-height: 26px;
      font-size: 18px;
      font-weight: 400;
      color: #DCDFE6;
    }
  }
}

.el-form {
  width: 100%;
  margin: 0 auto;
}

.captcha {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  overflow: hidden;

  > img {
    height: 36px;
    cursor: pointer;
  }
}

.login-btn-submit {
  width: 100%;
}

.login {
  margin-top: 10px;
  font-size: 14px;
  line-height: 22px;
  color: #1ab2ff;
  cursor: pointer;
  text-align: left;
  text-indent: 8px;
  width: 160px;
}
</style>
