<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" @submit.prevent="handleSubmit">
        <div class="login-panel-title">用户登录</div>
        <label class="login-field">
          <span>账号</span>
          <input v-model.trim="username" autocomplete="username" autofocus placeholder="请输入账号" />
        </label>
        <label class="login-field">
          <span>密码</span>
          <input v-model="password" autocomplete="current-password" placeholder="请输入密码" type="password" />
        </label>
        <div v-if="errorMessage" class="login-error">{{ 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 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 = error?.message || "登录失败，请稍后再试。"
  } finally {
    submitting.value = false
  }
}
</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) 360px;
  gap: 56px;
  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: 42px;
  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: 360px;
  padding: 30px 28px 28px;
  border: 1px solid rgba(48, 220, 255, 0.34);
  background: rgba(3, 14, 32, 0.82);
  box-shadow: 0 24px 70px rgba(0, 0, 0, 0.34), inset 0 0 28px rgba(48, 220, 255, 0.08);
  box-sizing: border-box;
}

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

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

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

.login-field input {
  width: 100%;
  height: 42px;
  padding: 0 12px;
  border: 1px solid rgba(48, 220, 255, 0.26);
  outline: none;
  background: rgba(0, 10, 24, 0.62);
  color: #fff;
  font-size: 15px;
  box-sizing: border-box;
}

.login-field input:focus {
  border-color: rgba(48, 220, 255, 0.72);
  box-shadow: 0 0 0 2px rgba(48, 220, 255, 0.12);
}

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

.login-submit {
  width: 100%;
  height: 44px;
  border: 1px solid rgba(48, 220, 255, 0.52);
  background: linear-gradient(90deg, rgba(24, 140, 196, 0.92), rgba(37, 204, 218, 0.92));
  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>
