<script>
import { login, removeToken, getOuterLoginUrl } from '@/utils/login-helper.ts'
import { Message } from 'element-ui'

export default {
  data() {
    return {
      loginForm: {
        account: '',
        password: ''
      },
      loading: false
    }
  },
  methods: {
    doLogin() {
      if (this.loginForm.account === '' || this.loginForm.password === '' || this.loading) {
        return
      }
      this.loading = true
      removeToken()
      login(this.loginForm)
        .then(() => {
          Message.success('登录成功')
          setTimeout(() => {
            this.$emit('loginSuccess')
          }, 500)
          this.loading = false
        })
        .catch(error => {
          this.loading = false
        })
    },
    handleInput(e) {
      this.$forceUpdate()
    },
    // 中软登录
    goLogin() {
      window.location.href = getOuterLoginUrl()
    }
  }
}

</script>

<template>
  <section>
    <div class="form-wrap">
      <h2 class="text-center mb-32px">登录</h2>
      <el-form :model="loginForm" @submit.native.prevent label-width="60px">
        <el-form-item label="用户名">
          <el-input
            v-model="loginForm.account"
            placeholder="请输入用户名"
            @keydown.enter.native="doLogin"
            @input="handleInput()"
            autofocus
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input
          type="password"
          class="mb-32px"
          v-model="loginForm.password"
          placeholder="请输入密码"
          @keydown.enter.native="doLogin"
          clearable
        ></el-input>
        </el-form-item>
      </el-form>
      <div style="padding-left: 60px;">
        <el-button type="primary" size="small" class="d-block w-100 mb-12px" :loading="loading" @click="doLogin">登录</el-button>
      </div>
      <div class="d-flex flex-end">
        <el-button type="text" @click="goLogin">中软登录</el-button>
      </div>
    </div>
  </section>
</template>

<style lang="scss" scoped>
section {
  display: flex;
  width: 100%;
  height: 100%;
  overflow: hidden;
  justify-content: center;
  align-items: center;
  text-align: center;
  .form-wrap {
    margin-top: -60px;
    width: 400px;
    height: 228px;
    padding: 40px;
    border-radius: 8px;
    background-color: #fff;
    filter: drop-shadow(0px 2px 8px rgba(33, 76, 217, 0.20));
  }
}
</style>
