<template>
  <div class="login_body">
    <div class="bg" />
    <div class="box">
      <div class="title">短信中心云平台登录</div>
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item
          label="账号"
          prop="username"
        >
          <el-input v-model="form.username" />
        </el-form-item>

        <el-form-item
          label="密码"
          prop="password"
        >
          <el-input v-model="form.password" type="password" />
        </el-form-item>

        <!-- 验证码 -->
        <el-form-item prop="verify" label="验证码">
          <el-row :gutter="20">
            <el-col :span="14">
              <el-input v-model="form.verify" />
            </el-col>
            <el-col :span="10">
              <el-link :underline="false" @click="onChangeGetVerify"><el-image :src="image" fit="contain ">123</el-image></el-link>
            </el-col>
          </el-row>
        </el-form-item>

        <el-form-item>
          <el-checkbox v-model="remember">记住我</el-checkbox>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" class="login_btn" @click="onSubmit">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { getVerifyAPI } from '@/api/user'

export default {
  name: 'Login',

  data() {
    return {
      image: '',
      uuid: '',
      form: {
        username: '',
        password: '',
        verify: '',
        uuid: ''
      },
      remember: false,

      rules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        verify: [
          { required: true, message: '请输入验证码', trigger: 'blur' }
        ]
      }

    }
  },

  created() {
    this.onGetVerify()

    const username = localStorage.getItem('username')
    const password = localStorage.getItem('password')
    if (username && password) {
      this.form.username = username
      this.form.password = password
    }
  },

  methods: {
    // 获取验证码
    async onGetVerify() {
      const res = await getVerifyAPI()
      this.image = res.data.image
      this.uuid = res.data.uuid
      this.form.uuid = res.data.uuid
    },

    // 登录
    onSubmit() {
      this.$refs.form.validate(async flag => {
        if (flag) {
          await this.$store.dispatch('user/asyncSetToken', this.form)
          this.$router.push('/')
          this.$message({
            message: '登录成功',
            type: 'success'
          })
          if (this.remember) {
            localStorage.setItem('username', this.form.username)
            localStorage.setItem('password', this.form.password)
          } else {
            localStorage.removeItem('username')
            localStorage.removeItem('password')
          }
          this.form = {}
        }
      })
    },

    onChangeGetVerify() {
      this.onGetVerify()
    }
  }
}

</script>

<style scoped lang="scss">
  .login_body {
    display: flex;
  }
  .bg {
    width: 60vw;
    height: 100vh;
    background: url('~@/assets/login-bg.svg') no-repeat;
    background-position: right top;
    background-size: cover;
  }
  .box {
    margin: 200px 10% 0;
    flex: 1;
    .title {
      padding-bottom: 76px;
      font-size: 26px;
      font-weight: 500;
      color: #1e2023;
    }
    ::v-deep() {
      .ant-form-item {
        display: flex;
        margin-bottom: 62px;
        flex-direction: column;
      }
      .ant-form-item-label label {
        font-size: 16px;
        color: #8b929d;
      }
      .ant-input,
      .ant-input-password {
        border-radius: 8px;
      }
    }

  }
  .login_btn{
    width: 100%;
  }
  ::v-deep .el-form-item__label {
      float: none !important
  }
</style>
