<!-- 登录页面 -->
<template>
  <div class="login-page flex w-full h-screen">
    <LoginLeftView />

    <div class="relative flex-1">
      <AuthTopBar />

      <div class="auth-right-wrap">
        <div class="form">
          <h3 class="title">{{ $t('login.title') }}</h3>
          <p class="sub-title">{{ $t('login.subTitle') }}</p>
          <ElForm
            ref="formRef"
            :model="formData"
            :rules="rules"
            :key="formKey"
            @keyup.enter="handleSubmit"
            style="margin-top: 25px"
          >
            <ElFormItem v-if="isDev" prop="account">
              <ElSelect v-model="formData.account" @change="setupAccount">
                <ElOption
                  v-for="account in accounts"
                  :key="account.key"
                  :label="account.label"
                  :value="account.key"
                >
                  <span>{{ account.label }}</span>
                </ElOption>
              </ElSelect>
            </ElFormItem>
            <ElFormItem prop="username">
              <ElInput
                class="custom-height"
                :placeholder="$t('login.placeholder.username')"
                v-model.trim="formData.username"
              />
            </ElFormItem>
            <ElFormItem prop="password">
              <ElInput
                class="custom-height"
                :placeholder="$t('login.placeholder.password')"
                v-model.trim="formData.password"
                type="password"
                autocomplete="off"
                show-password
              />
            </ElFormItem>

            <div class="flex-cb mt-2 text-sm">
              <ElCheckbox v-model="formData.rememberPassword">{{
                $t('login.rememberPwd')
              }}</ElCheckbox>
              <RouterLink class="text-theme" :to="{ name: 'ForgetPassword' }">{{
                $t('login.forgetPwd')
              }}</RouterLink>
            </div>

            <div style="margin-top: 30px">
              <ElButton
                class="w-full custom-height"
                type="primary"
                @click="handleSubmit"
                :loading="loading"
                v-ripple
              >
                {{ $t('login.btnText') }}
              </ElButton>
            </div>

            <template v-if="ssoProviders.length">
              <ElDivider>企业身份登录</ElDivider>
              <div class="grid gap-2">
                <ElButton
                  v-for="provider in ssoProviders"
                  :key="provider.code"
                  class="w-full"
                  @click="startSSO(provider.code)"
                >
                  使用 {{ provider.display_name }} 登录
                </ElButton>
              </div>
            </template>

            <div class="mt-5 text-sm text-[#a9c2e6]">
              <span>{{ $t('login.noAccount') }}</span>
              <RouterLink class="text-theme" :to="{ name: 'Register' }">{{
                $t('login.register')
              }}</RouterLink>
            </div>
          </ElForm>
        </div>

        <!-- 安全徽标 -->
        <div class="security-badge">
          <span class="sb-dot"></span>
          <span>{{ $t('login.securityBadge') }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
  import AppConfig from '@/config'
  import { useUserStore } from '@/store/modules/user'
  import { useI18n } from 'vue-i18n'
  import { HttpError } from '@/utils/http/error'
  import { exchangeSSOCode, fetchLogin, fetchSSOProviders, fetchTOTPLogin, SSOProvider } from '@/api/auth'
  import { ElMessageBox, ElNotification, type FormInstance, type FormRules } from 'element-plus'

  defineOptions({ name: 'Login' })

  const { t, locale } = useI18n()
  const formKey = ref(0)

  // 监听语言切换，重置表单
  watch(locale, () => {
    formKey.value++
  })

  type AccountKey = 'super' | 'admin' | 'user'

  export interface Account {
    key: AccountKey
    label: string
    userName: string
    password: string
    roles: string[]
  }

  // Built-in demo accounts are for local development only. Shipping known
  // passwords in a production bundle turns them into a backdoor, so the
  // selector is only populated when Vite runs in DEV mode.
  const isDev = import.meta.env.DEV
  const accounts = computed<Account[]>(() => {
    if (!isDev) return []
    return [
      {
        key: 'super',
        label: t('login.roles.super'),
        userName: 'Super',
        password: '123456',
        roles: ['R_SUPER']
      },
      {
        key: 'admin',
        label: t('login.roles.admin'),
        userName: 'Admin',
        password: '123456',
        roles: ['R_ADMIN']
      },
      {
        key: 'user',
        label: t('login.roles.user'),
        userName: 'User',
        password: '123456',
        roles: ['R_USER']
      }
    ]
  })

  const userStore = useUserStore()
  const router = useRouter()
  const route = useRoute()

  const systemName = AppConfig.systemInfo.name
  const formRef = ref<FormInstance>()

  const formData = reactive({
    account: '',
    username: '',
    password: '',
    rememberPassword: true
  })

  const rules = computed<FormRules>(() => ({
    username: [{ required: true, message: t('login.placeholder.username'), trigger: 'blur' }],
    password: [{ required: true, message: t('login.placeholder.password'), trigger: 'blur' }]
  }))

  const loading = ref(false)
  const ssoProviders = ref<SSOProvider[]>([])

  onMounted(async () => {
    if (isDev) {
      setupAccount('super')
    }
    try {
      ssoProviders.value = await fetchSSOProviders()
      const ssoCode = route.query.sso_code as string
      if (ssoCode) {
        loading.value = true
        const result = await exchangeSSOCode(ssoCode)
        if (!result.token) throw new Error('SSO exchange returned no token')
        userStore.setToken(result.token, result.refreshToken || '')
        userStore.setLoginStatus(true)
        await router.replace('/')
      }
    } finally {
      loading.value = false
    }
  })

  function startSSO(code: string) {
    window.location.assign(`/api/v1/portal/sso/${encodeURIComponent(code)}/start`)
  }

  // 设置账号
  const setupAccount = (key: AccountKey) => {
    const selectedAccount = accounts.value.find((account: Account) => account.key === key)
    formData.account = key
    formData.username = selectedAccount?.userName ?? ''
    formData.password = selectedAccount?.password ?? ''
  }

  // 登录
  const handleSubmit = async () => {
    if (!formRef.value) return

    try {
      // 表单验证
      const valid = await formRef.value.validate()
      if (!valid) return

      loading.value = true

      // 登录请求
      const { username, password } = formData

	  let loginResult = await fetchLogin({
        userName: username,
        password
      })

	  if (loginResult.require_totp) {
		if (!loginResult.temp_token) throw new Error('Login failed - no TOTP challenge token')
		const { value } = await ElMessageBox.prompt(
		  '请输入身份验证器中的 6 位动态验证码，或输入备用码',
		  '两步验证',
		  {
			confirmButtonText: '验证',
			cancelButtonText: '取消',
			inputPattern: /^(\d{6}|[A-Za-z2-9]{4}-?[A-Za-z2-9]{4})$/,
			inputErrorMessage: '请输入 6 位动态验证码或 8 位备用码'
		  }
		)
		const factor = value.trim()
		loginResult = await fetchTOTPLogin({
		  temp_token: loginResult.temp_token,
		  ...( /^\d{6}$/.test(factor) ? { code: factor } : { backup_code: factor } )
		})
	  }

	  const { token, refreshToken } = loginResult

      // 验证token
      if (!token) {
        throw new Error('Login failed - no token received')
      }

      // 存储 token 和登录状态
	  userStore.setToken(token, refreshToken || '')
      userStore.setLoginStatus(true)

      // 登录成功处理
      showLoginSuccessNotice()

      // 获取 redirect 参数，如果存在则跳转到指定页面，否则跳转到首页。
      // 只允许站内相对路径，杜绝 //host、外部协议等畸形/钓鱼落点。
      const redirect = route.query.redirect as string
      const isSafeRedirect = typeof redirect === 'string' && redirect.startsWith('/') && !redirect.startsWith('//') && !redirect.includes(':')
      router.push(isSafeRedirect ? redirect : '/')
    } catch (error) {
      // 处理 HttpError
      if (error instanceof HttpError) {
        // console.log(error.code)
      } else {
        // 处理非 HttpError
        // ElMessage.error('登录失败，请稍后重试')
        console.error('[Login] Unexpected error:', error)
      }
    } finally {
      loading.value = false
    }
  }

  // 登录成功提示
  const showLoginSuccessNotice = () => {
    setTimeout(() => {
      ElNotification({
        title: t('login.success.title'),
        type: 'success',
        duration: 2500,
        zIndex: 10000,
        message: `${t('login.success.message')}, ${systemName}!`
      })
    }, 1000)
  }
</script>

<style scoped>
  @import './style.css';
</style>

<style lang="scss" scoped>
  :deep(.el-select__wrapper) {
    height: 40px !important;
  }
</style>

<style lang="scss">
  // 语枢网关登录页：深色 HUD 背景与 Element Plus 控件（登录页恒定深色，不随主题变化）
  .login-page {
    background:
      radial-gradient(900px 520px at 100% -20%, rgb(0 229 255 / 10%), transparent 60%),
      #050b18;

    // 输入框
    .el-input__wrapper,
    .el-select__wrapper {
      background-color: rgb(5 15 35 / 80%) !important;
      border-radius: 8px;
      box-shadow: 0 0 0 1px rgb(0 229 255 / 22%) inset !important;
      transition: box-shadow 0.25s;
    }

    .el-input__wrapper.is-focus,
    .el-select__wrapper.is-focused {
      box-shadow:
        0 0 0 1px #00e5ff inset,
        0 0 14px rgb(0 229 255 / 35%) !important;
    }

    .el-input__inner,
    .el-select__placeholder,
    .el-select__selected-item {
      color: #eaf6ff !important;
      caret-color: #00e5ff;
    }

    .el-input__inner::placeholder {
      color: rgb(160 190 225 / 50%);
    }

    // 复选框
    .el-checkbox__label {
      color: #cfe4ff;
    }

    // 登录按钮（青色渐变发光）
    .el-button--primary {
      color: #03141f;
      font-weight: 600;
      letter-spacing: 2px;
      background-image: linear-gradient(120deg, #00e5ff, #2f80ff);
      border: none;
      box-shadow: 0 6px 22px rgb(0 229 255 / 35%);

      &:hover,
      &:focus {
        color: #03141f;
        background-image: linear-gradient(120deg, #1be9ff, #4d93ff);
        box-shadow: 0 8px 30px rgb(0 229 255 / 50%);
      }
    }

    // SSO 登录按钮
    .el-button:not(.el-button--primary) {
      color: #cfe4ff;
      background: rgb(0 229 255 / 6%);
      border: 1px solid rgb(0 229 255 / 22%);
    }

    .el-divider {
      border-color: rgb(0 229 255 / 18%);

      .el-divider__text {
        color: #a9c2e6;
        background: transparent;
      }
    }
  }
</style>
