<!--  -->
<template>
  <div class="login">
    <div class="con">
      <div class="left">
        <p>河南博物院</p>
        <p>文物展示平台后台</p>
      </div>
      <div class="right" @keyup.enter="login">
        <p>欢迎登录</p>
        <div class="input">
          <el-form
            :model="ruleForm"
            :rules="rules"
            ref="ruleForm"
            class="demo-ruleForm"
          >
            <el-form-item prop="userName">
              <el-input
                v-model="ruleForm.userName"
                placeholder="账号"
              ></el-input>
            </el-form-item>
            <el-form-item prop="passWord">
              <el-input
                show-password
                v-model="ruleForm.passWord"
                placeholder="密码"
              ></el-input>
            </el-form-item>
          </el-form>
        </div>
        <div class="btn" @click="login">登 录</div>
      </div>
    </div>
  </div>
</template>

<script>
import { encodeStr } from '../utils/pass'
import { Base64 } from 'js-base64'
import { userLogin } from '@/apis/login'
export default {
  name: 'login',
  components: {},
  data () {
    // 这里存放数据
    return {
      ruleForm: {
        userName: '',
        passWord: ''
      },
      rules: {
        userName: [{ required: true, message: '不能为空', trigger: 'blur' }],
        passWord: [{ required: true, message: '不能为空', trigger: 'blur' }]
      }
    }
  },
  // 监听属性 类似于data概念
  computed: {},
  // 监控data中的数据变化
  watch: {},
  // 方法集合
  methods: {
    async login () {
      try {
        await this.$refs.ruleForm.validate()
        const data = {
          userName: this.ruleForm.userName,
          passWord: encodeStr(Base64.encode(this.ruleForm.passWord))
        }
        const res = await userLogin(data)
        if (res.code === 0) {
          localStorage.setItem('HNBWY_token', res.data.token)
          localStorage.setItem('HNBWY_userName', res.data.user.userName)
          this.$router.push('/layout/tab2')
          this.$message.success('登录成功')
        } else this.$message.warning(res.msg)
      } catch (error) {
        console.log(error)
      }
    }
  },
  // 生命周期 - 创建完成（可以访问当前this实例）
  created () {},
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted () {},
  beforeCreate () {}, // 生命周期 - 创建之前
  beforeMount () {}, // 生命周期 - 挂载之前
  beforeUpdate () {}, // 生命周期 - 更新之前
  updated () {}, // 生命周期 - 更新之后
  beforeDestroy () {}, // 生命周期 - 销毁之前
  destroyed () {}, // 生命周期 - 销毁完成
  activated () {} // 如果页面有keep-alive缓存功能，这个函数会触发
}
</script>
<style lang='less' scoped>
.login {
  width: 100vw;
  height: 100vh;
  background: url("../assets/img/bg.jpg");
  background-size: 100% 100%;
  .con {
    display: flex;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 700px;
    height: 400px;
    .left {
      display: flex;
      flex-direction: column;
      justify-content: center;
      width: 350px;
      height: 100%;
      background-color: rgba(58, 128, 210, 0.8);
      & > p {
        width: 80%;
        margin: 0 auto;
        margin-bottom: 30px;
        color: #fff;
        font-weight: 700;
        text-align: center;
        font-size: 30px;
      }
    }
    .right {
      width: 350px;
      background-color: rgba(255, 255, 255, 0.8);
      & > p {
        font-size: 24px;
        text-align: center;
        margin-top: 50px;
      }
      .input {
        width: 90%;
        margin: 50px auto;
      }
      .btn {
        cursor: pointer;
        width: 90%;
        height: 40px;
        line-height: 40px;
        text-align: center;
        margin: 50px auto 0;
        background-color: #3a80d2;
        border-radius: 8px;
        color: #fff;
      }
    }
  }
}
</style>
