<template>
  <div class="login-screen">
    <div class="login-shell">
      <div class="login-brand">
        <span>红原县数字畜牧业监管平台</span>
        <small>Hongyuan Digital Animal Husbandry Supervision Platform</small>
      </div>

      <form class="login-panel" aria-label="平台用户登录" @submit.prevent="handleSubmit">
        <div class="login-panel-kicker">SCREEN 2.0 · V2.2</div>
        <div class="login-panel-title">用户登录</div>
        <p class="login-panel-intro">使用平台账号进入红原县数字畜牧业监管大屏</p>
        <label class="login-field">
          <span>账号</span>
          <input v-model.trim="username" autocomplete="username" autofocus placeholder="请输入平台账号" />
        </label>
        <label class="login-field login-password-field">
          <span>密码</span>
          <span class="login-password-control">
            <input
              v-model="password"
              autocomplete="current-password"
              placeholder="请输入密码"
              :type="passwordVisible ? 'text' : 'password'"
              @keydown="updateCapsLockState"
              @keyup="updateCapsLockState"
              @blur="capsLockOn = false"
            />
            <button
              class="login-password-toggle"
              type="button"
              :aria-label="passwordVisible ? '隐藏密码' : '显示密码'"
              :aria-pressed="passwordVisible"
              @click="passwordVisible = !passwordVisible"
            >
              {{ passwordVisible ? "隐藏" : "显示" }}
            </button>
          </span>
        </label>
        <div class="login-field-assist" aria-live="polite">
          <span v-if="capsLockOn" class="login-caps-lock">大写锁定已开启</span>
          <span v-else>账号由平台管理员统一分配</span>
        </div>
        <div v-if="errorMessage" class="login-error" role="alert">{{ errorMessage }}</div>
        <button class="login-submit" :disabled="submitting" type="submit">
          {{ submitting ? "登录中" : "登录" }}
        </button>
      </form>
    </div>
  </div>
</template>

<script setup>
import { computed, ref } from "vue"
import { useRoute, useRouter } from "vue-router"
import { login } from "@/services/auth"

const route = useRoute()
const router = useRouter()
const username = ref("")
const password = ref("")
const passwordVisible = ref(false)
const capsLockOn = ref(false)
const submitting = ref(false)
const errorMessage = ref("")
const redirectPath = computed(() => {
  const redirect = typeof route.query.redirect === "string" ? decodeURIComponent(route.query.redirect) : "/platform"
  return redirect.startsWith("/login") ? "/platform" : redirect || "/platform"
})

async function handleSubmit() {
  errorMessage.value = ""
  if (!username.value || !password.value) {
    errorMessage.value = "请输入账号和密码。"
    return
  }
  submitting.value = true
  try {
    await login(username.value, password.value)
    router.replace(redirectPath.value)
  } catch (error) {
    errorMessage.value = toFriendlyLoginError(error)
  } finally {
    submitting.value = false
  }
}

function updateCapsLockState(event) {
  capsLockOn.value = Boolean(event?.getModifierState?.("CapsLock"))
}

function toFriendlyLoginError(error) {
  const message = String(error?.message || "").trim()
  if (/401|403|账号|密码|unauthor/i.test(message)) return "账号或密码不正确，请核对后重试。"
  if (/network|fetch|timeout|连接|服务|502|503|504/i.test(message)) return "登录服务暂时无法连接，请稍后重试。"
  return "暂时无法登录，请稍后重试。"
}
</script>

<style lang="scss" scoped>
.login-screen {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  background:
    linear-gradient(180deg, rgba(4, 16, 38, 0.1), rgba(2, 8, 22, 0.62)),
    url("@/assets/images/bg.png") center / cover no-repeat,
    #05142f;
  color: #fff;
}

.login-screen::before {
  content: "";
  position: absolute;
  inset: 0;
  background:
    linear-gradient(90deg, rgba(0, 0, 0, 0.46), transparent 42%, rgba(0, 0, 0, 0.35)),
    radial-gradient(circle at 52% 46%, rgba(48, 220, 255, 0.16), transparent 32%);
}

.login-shell {
  position: relative;
  z-index: 1;
  width: min(980px, calc(100vw - 48px));
  display: grid;
  grid-template-columns: minmax(0, 1fr) 388px;
  gap: 64px;
  align-items: center;
}

.login-brand {
  display: flex;
  flex-direction: column;
  gap: 14px;
  min-width: 0;
  text-shadow: 0 0 20px rgba(48, 220, 255, 0.36);
}

.login-brand span {
  font-size: 44px;
  line-height: 1.2;
  font-weight: 700;
}

.login-brand small {
  color: rgba(196, 243, 254, 0.72);
  font-size: 13px;
  letter-spacing: 0;
}

.login-panel {
  width: 388px;
  padding: 30px 30px 30px;
  border: 1px solid rgba(91, 224, 244, 0.46);
  background: linear-gradient(160deg, rgba(5, 31, 46, 0.94), rgba(2, 17, 29, 0.92));
  box-shadow: 0 24px 70px rgba(0, 0, 0, 0.4), inset 0 0 0 1px rgba(126, 251, 246, 0.06);
  box-sizing: border-box;
}

.login-panel-kicker {
  margin-bottom: 8px;
  color: rgba(126, 251, 246, 0.82);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 1.8px;
  text-align: center;
}

.login-panel-title {
  margin-bottom: 6px;
  color: #ffffff;
  font-size: 22px;
  font-weight: 700;
  text-align: center;
}

.login-panel-intro {
  margin: 0 0 24px;
  color: rgba(211, 241, 247, 0.68);
  font-size: 12px;
  line-height: 20px;
  text-align: center;
}

.login-field {
  display: block;
  margin-bottom: 18px;
}

.login-password-control {
  position: relative;
  display: block;
  margin: 0 !important;
}

.login-field span {
  display: block;
  margin-bottom: 8px;
  color: rgba(196, 243, 254, 0.78);
  font-size: 13px;
}

.login-field input {
  width: 100%;
  height: 44px;
  padding: 0 13px;
  border: 1px solid rgba(91, 224, 244, 0.3);
  outline: none;
  background: rgba(0, 10, 24, 0.62);
  color: #fff;
  font-size: 15px;
  box-sizing: border-box;
}

.login-password-field input {
  padding-right: 64px;
}

.login-field input:focus {
  border-color: rgba(126, 251, 246, 0.78);
  box-shadow: 0 0 0 2px rgba(126, 251, 246, 0.12);
}

.login-field input:-webkit-autofill,
.login-field input:-webkit-autofill:hover,
.login-field input:-webkit-autofill:focus {
  -webkit-text-fill-color: #ffffff;
  caret-color: #ffffff;
  box-shadow: 0 0 0 1000px #061c2b inset, 0 0 0 2px rgba(126, 251, 246, 0.12);
  transition: background-color 9999s ease-out;
}

.login-password-toggle {
  position: absolute;
  right: 4px;
  top: 50%;
  width: 54px;
  height: 34px;
  padding: 0;
  border: 0;
  background: transparent;
  color: rgba(126, 251, 246, 0.82);
  cursor: pointer;
  font-size: 12px;
  transform: translateY(-50%);
}

.login-password-toggle:focus-visible,
.login-submit:focus-visible {
  outline: 2px solid #7efbf6;
  outline-offset: 2px;
}

.login-field-assist {
  min-height: 20px;
  margin: -8px 0 8px;
  color: rgba(211, 241, 247, 0.58);
  font-size: 12px;
  line-height: 20px;
}

.login-caps-lock {
  color: #ffd76c;
}

.login-error {
  min-height: 20px;
  margin: -4px 0 12px;
  color: #ffcf8a;
  font-size: 13px;
  line-height: 20px;
}

.login-submit {
  width: 100%;
  height: 46px;
  border: 1px solid rgba(48, 220, 255, 0.52);
  background: linear-gradient(90deg, rgba(22, 122, 153, 0.96), rgba(42, 196, 205, 0.96));
  color: #fff;
  cursor: pointer;
  font-size: 16px;
  font-weight: 700;
}

.login-submit:disabled {
  cursor: not-allowed;
  opacity: 0.68;
}

@media (max-width: 820px) {
  .login-shell {
    grid-template-columns: 1fr;
    gap: 28px;
  }

  .login-brand span {
    font-size: 30px;
  }

  .login-panel {
    width: 100%;
  }
}
</style>
