<script setup lang="ts">
import { ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { defaultRouteFor, login } from '../auth'

const route = useRoute()
const router = useRouter()
const username = ref('')
const password = ref('')
const busy = ref(false)
const error = ref('')

async function submit() {
  if (busy.value) return
  busy.value = true
  error.value = ''
  try {
    const user = await login(username.value, password.value)
    const requested = typeof route.query.redirect === 'string' && route.query.redirect.startsWith('/')
      ? route.query.redirect
      : defaultRouteFor(user)
    const normalUserCanOpen = requested === '/demand-map' || requested === '/video-discovery'
    await router.replace(user.role === 'admin' || normalUserCanOpen ? requested : defaultRouteFor(user))
  } catch (cause) {
    error.value = cause instanceof Error ? cause.message : '登录失败，请稍后重试'
  } finally {
    busy.value = false
  }
}
</script>

<template>
  <main class="login-page">
    <section class="login-brand">
      <div class="brand-lockup">
        <span>S</span>
        <div>
          <strong>SupplyAgent</strong>
          <small>CONTENT INTELLIGENCE</small>
        </div>
      </div>
      <div class="brand-message">
        <span>AI-NATIVE CONTENT SUPPLY</span>
        <h1>让内容需求转化为<br />可执行的供给方向</h1>
        <p>连接需求信号、智能决策与内容生产，形成清晰、可靠、可追溯的供给链路。</p>
      </div>
      <div class="brand-grid" aria-hidden="true">
        <i v-for="index in 24" :key="index" />
      </div>
    </section>

    <section class="login-panel">
      <form class="login-card" @submit.prevent="submit">
        <header>
          <span>WELCOME BACK</span>
          <h2>登录 SupplyAgent</h2>
          <p>使用管理员分配的系统账号继续。</p>
        </header>

        <label>
          <span>用户名</span>
          <input
            v-model.trim="username"
            name="username"
            autocomplete="username"
            minlength="3"
            maxlength="64"
            placeholder="请输入用户名"
            required
            autofocus
          />
        </label>

        <label>
          <span>密码</span>
          <input
            v-model="password"
            type="password"
            name="password"
            autocomplete="current-password"
            maxlength="128"
            placeholder="请输入密码"
            required
          />
        </label>

        <p v-if="error" class="login-error" role="alert">{{ error }}</p>

        <button type="submit" :disabled="busy">
          <span v-if="busy" class="spinner" />
          {{ busy ? '正在登录…' : '登录系统' }}
        </button>

        <footer>登录遇到问题，请联系系统管理员</footer>
      </form>
    </section>
  </main>
</template>

<style scoped>
.login-page {
  display: grid;
  min-height: 100vh;
  grid-template-columns: minmax(420px, 1.08fr) minmax(420px, .92fr);
  background: #f7f8fb;
}
.login-brand {
  position: relative;
  display: flex;
  min-height: 100vh;
  padding: 46px 58px;
  flex-direction: column;
  overflow: hidden;
  background:
    radial-gradient(circle at 74% 24%, rgba(117, 117, 224, .34), transparent 27%),
    linear-gradient(145deg, #22234f 0%, #333476 48%, #595bc2 100%);
  color: #fff;
}
.brand-lockup { display: flex; align-items: center; gap: 13px; position: relative; z-index: 2; }
.brand-lockup > span {
  display: grid; width: 42px; height: 42px; place-items: center; border-radius: 13px;
  background: rgba(255,255,255,.16); border: 1px solid rgba(255,255,255,.25);
  font-size: 20px; font-weight: 800; box-shadow: 0 16px 36px rgba(14,15,51,.24);
}
.brand-lockup strong, .brand-lockup small { display: block; }
.brand-lockup strong { font-size: 17px; }
.brand-lockup small { margin-top: 3px; color: #c4c5f0; font-size: 8px; letter-spacing: .18em; }
.brand-message { position: relative; z-index: 2; margin: auto 0; max-width: 620px; }
.brand-message > span { color: #bfc0f2; font-size: 11px; font-weight: 800; letter-spacing: .18em; }
.brand-message h1 { margin: 20px 0; font-size: clamp(38px, 4vw, 62px); line-height: 1.16; letter-spacing: -.045em; }
.brand-message p { max-width: 520px; margin: 0; color: #d7d8f4; font-size: 15px; line-height: 1.9; }
.brand-grid {
  position: absolute; right: -75px; bottom: -80px; display: grid; width: 430px; height: 360px;
  grid-template-columns: repeat(6, 1fr); gap: 12px; transform: rotate(-8deg); opacity: .34;
}
.brand-grid i { border: 1px solid rgba(255,255,255,.28); border-radius: 14px; }
.login-panel { display: grid; min-height: 100vh; padding: 48px; place-items: center; }
.login-card { width: min(100%, 400px); }
.login-card header { margin-bottom: 38px; }
.login-card header > span { color: #6768ce; font-size: 10px; font-weight: 800; letter-spacing: .18em; }
.login-card h2 { margin: 10px 0 8px; color: #252b3b; font-size: 30px; letter-spacing: -.035em; }
.login-card header p { margin: 0; color: #838a98; font-size: 13px; }
.login-card label { display: grid; gap: 8px; margin-top: 20px; }
.login-card label span { color: #4e5667; font-size: 12px; font-weight: 700; }
.login-card input {
  height: 48px; padding: 0 15px; border: 1px solid #d8dce5; border-radius: 10px;
  outline: none; background: #fff; color: #252b3b; font: inherit; transition: .16s;
}
.login-card input:focus { border-color: #6a6bd5; box-shadow: 0 0 0 3px rgba(106,107,213,.12); }
.login-card button {
  display: flex; width: 100%; height: 48px; margin-top: 24px; align-items: center;
  justify-content: center; gap: 9px; border: 0; border-radius: 10px; background: #5b5cc9;
  color: #fff; font-size: 13px; font-weight: 700; cursor: pointer;
  box-shadow: 0 12px 28px rgba(74,75,175,.24);
}
.login-card button:hover:not(:disabled) { background: #5051bd; }
.login-card button:disabled { cursor: wait; opacity: .72; }
.login-error {
  margin: 16px 0 -6px; padding: 10px 12px; border: 1px solid #f2c7ce; border-radius: 8px;
  background: #fff4f5; color: #b4233b; font-size: 12px;
}
.login-card footer { margin-top: 22px; color: #9aa0ac; font-size: 11px; text-align: center; }
.spinner { width: 15px; height: 15px; border: 2px solid rgba(255,255,255,.35); border-top-color: #fff; border-radius: 50%; animation: spin .7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (max-width: 860px) {
  .login-page { display: block; }
  .login-brand { min-height: 240px; padding: 28px; }
  .brand-message { margin: auto 0 10px; }
  .brand-message > span, .brand-message p { display: none; }
  .brand-message h1 { margin: 30px 0 0; font-size: 32px; }
  .login-panel { min-height: calc(100vh - 240px); padding: 36px 24px; }
}
</style>
