<template>
  <LoginView
    :company-name="LOGIN_BRAND_NAME"
    :submitting="loginSubmitting"
    :error-message="loginError"
    @login="submitLogin"
    @recover-password="handleRecoverPassword"
    @sso-login="handleSsoLogin"
  />
</template>

<script setup>
import { useRoute, useRouter } from 'vue-router'

import { useSystemState } from '../composables/useSystemState.js'
import LoginView from './LoginView.vue'

const route = useRoute()
const router = useRouter()
const {
  handleLogin,
  handleRecoverPassword,
  handleSsoLogin,
  loginError,
  loginSubmitting,
  resolveEntryRoute
} = useSystemState()

const LOGIN_BRAND_NAME = '易财费控'

async function submitLogin(credentials) {
  const passed = await handleLogin(credentials)

  if (!passed) {
    return
  }

  const redirect = typeof route.query.redirect === 'string' ? route.query.redirect : ''

  if (redirect.startsWith('/app/')) {
    router.replace(redirect)
    return
  }

  router.replace(resolveEntryRoute())
}
</script>
