<template>
  <div class="login-page">
    <div class="system-name">SaaS 管理平台</div>
    <div class="login-view">
      <h2>登录</h2>
      <el-form :model="loginForm" class="login-form">
        <el-form-item prop="userName">
          <el-input
            prefix-icon="el-icon-user"
            v-model="loginForm.userName"
            placeholder="User Name"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            :type="showPwd ? 'text' : 'password'"
            prefix-icon="el-icon-lock"
            v-model="loginForm.password"
            placeholder="Password"
            @keyup.enter.native="login"
          >
            <i
              @click="showPwd = !showPwd"
              slot="suffix"
              :class="{
                eyes: true,
                open: !showPwd,
                close: showPwd,
              }"
            ></i>
          </el-input>
        </el-form-item>
        <el-form-item class="operate">
          <el-button type="primary" @click="login">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import { login } from '@/common/api/login'
import { setToken } from '@/utils/auth'
export default {
  name: 'LoginPage',
  data() {
    return {
      loginForm: {
        userName: '',
        password: '',
      },
      showPwd: false,
    }
  },
  mounted() {
    this.loginForm = {}
  },
  methods: {
    async login() {
      if (!this.loginForm.userName || !this.loginForm.password) {
        this.$message.error('请输入用户名或密码')
        return
      }
      const l = this.$loading({
        background: 'rgba(0, 0, 0, 0.3)',
      })
      try {
        const res = await login(this.loginForm)
        if (res.code === 200) {
          setToken(res.data.token)
          this.$router.push('/saas/manage')
        }
      } catch (e) {
        console.log(e)
      } finally {
        l.close()
      }
    },
  },
}
</script>
<style lang="scss" scoped>
.login-page {
  height: 100%;
  background: url(../assets/background.png) center / cover
    no-repeat;
  .background-image {
    width: 100%;
    height: 100%;
  }
}

.system-name {
  position: absolute;
  color: #fff;
  top: 10%;
  left: 5%;
  font-size: 50px;
  font-weight: bold;
  font-family: fangsong;
  font-style: italic;
}

.login-view {
  padding: 18px;
  background: hsla(0, 0%, 100%, 0.4);
  position: absolute;
  top: 50%;
  left: 50%;
  height: 300px;
  width: 400px;
  border-radius: 6px;
  transform: translate(-50%, -50%);
  box-sizing: border-box;
  background-position: 100% 0, 50%;
  background-repeat: no-repeat;
  background-size: 528px auto, contain;
}

h2 {
  text-align: center;
}

.operate {
  text-align: center;
}

.login-form {
  padding: 20px;
}

.eyes {
  display: inline-block;
  height: 14px;
  width: 18px;
  margin-top: 11px;
  margin-right: 5px;
  cursor: pointer;
}
.eyes.open {
  background: url('../assets/login/eyes-open.png') no-repeat center /
    cover;
}
.eyes.close {
  background: url('../assets/login/eyes-closed.png') no-repeat center /
    cover;
}

.el-button--primary {
  padding-right: 100px;
  padding-left: 100px;
}
</style>
