<template>
  <div class="login-page">
    <div class="system-name">SAAS 管理平台</div>
    <div class="login-view">
      <h2>登录</h2>
      <el-form ref="loginForm" :model="loginForm" class="login-form">
        <el-form-item
          prop="account"
          :rules="[
            { required: true, message: '请输入用户名' },
          ]"
        >
          <el-input
            prefix-icon="el-icon-user"
            v-model="loginForm.account"
            placeholder="User Name"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item
          prop="password"
          :rules="[
            { required: true, message: '请输入密码' },
          ]"
        >
          <el-input
            :type="showPwd ? 'text' : 'password'"
            prefix-icon="el-icon-lock"
            v-model="loginForm.password"
            placeholder="Password"
            @keyup.enter.native="login"
            clearable
          >
            <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 { getLoginState } from '@/common/api/login'
import { setToken, setUser } from '@/utils/auth'
import { mapMutations } from 'vuex'

export default {
  name: 'LoginPage',
  data() {
    return {
      loginForm: {
        account: '',
        password: '',
      },
      showPwd: false,
    }
  },
  mounted() {
    // this.loginForm = {}
  },
  methods: {
    ...mapMutations(['setUserInfo']),
    async login() {
      try {
        await this.$refs.loginForm.validate()
      } catch {
        return
      }
      const l = this.$loading({
        background: 'rgba(0, 0, 0, 0.3)',
      })
      try {
        const res = await getLoginState(this.loginForm)
        if (res.code === 200) {
          setToken(res.data.token)
          setUser(res.data.saasUser)
          this.setUserInfo(res.data.saasUser)
          this.$router.push('/saas/home')
        }
      } catch (e) {
        console.error(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>
