<script setup>
import { login, getLoginUrl } from '@/utils/login-helper.ts'

const emit = defineEmits(['loginSuccess'])

const dialogVisible = ref(window.location.hostname === 'localhost')

const defautUser = {
  "account": "test-kl",
  "password": "test@123"
}

const loginForm = reactive({
  account: '',
  password: ''
})

const loading = ref(false)
function doLogin() {
  if (loginForm.account === '' || loginForm.password === '' || loading.value) {
    return
  }
  loading.value = true
  login(loginForm)
    .then(() => {
      emit('loginSuccess')
      dialogVisible.value = false
      loading.value = false
    })
    .catch(error => {
      loading.value = false
    })
}

function setDefault() {
  loginForm.account = defautUser.account
  loginForm.password = defautUser.password
}

// 中软登录
function goLogin() {
  window.location.href = getLoginUrl()
}
</script>

<template>
  <el-dialog
    title="登录"
    width="400px"
    :visible.sync="dialogVisible"
    :close-on-click-modal="false"
    :show-close="false"
  >
    <el-form @submit.native.prevent label-width="60px">
      <!-- <p>用户名</p> -->
      <el-form-item label="用户名">
        <el-input
          v-model="loginForm.account"
          placeholder="请输入用户名"
          @keydown.enter.native="doLogin"
          autofocus
          clearable
        ></el-input>
      </el-form-item>
      <!-- <p>密码</p> -->
      <el-form-item label="密码">
        <el-input
        type="password"
        class="__mt-16px"
        v-model="loginForm.password"
        placeholder="请输入密码"
        @keydown.enter.native="doLogin"
        clearable
      ></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <!-- <el-button @click="dialogVisible = false">取消</el-button> -->
      <el-button type="" @click="goLogin">单点登录</el-button>
      <el-button type="" @click="setDefault">默认账号</el-button>
      <el-button type="primary" :loading="loading" @click="doLogin">登录</el-button>
    </span>
</el-dialog>
</template>

<style lang="scss" scoped>
</style>
