<template>
  <div class="internal-login">
    <section class="internal-login__brand">
      <div class="internal-login__brand-main">
        <img src="/logo.png" class="internal-login__logo" alt="医梦AI" />
        <div>
          <h1>OKR 绩效管理系统</h1>
          <p>医梦AI 内部绩效运营工作台</p>
          <svg class="medical-pulse" viewBox="0 0 220 24" aria-hidden="true" focusable="false">
            <path d="M2 12h72l8-8 10 16 12-16 8 8h106" />
          </svg>
        </div>
      </div>
      <svg class="molecule-mark" viewBox="0 0 180 150" aria-hidden="true" focusable="false">
        <path d="M48 44 92 28 134 58 116 112 62 112 48 44Z" />
        <circle cx="48" cy="44" r="7" />
        <circle cx="92" cy="28" r="7" />
        <circle cx="134" cy="58" r="7" />
        <circle cx="116" cy="112" r="7" />
        <circle cx="62" cy="112" r="7" />
      </svg>
    </section>

    <section class="internal-login__panel med-panel">
      <div class="login-panel__header">
        <span class="login-panel__eyebrow">Internal Access</span>
        <h2>账号登录</h2>
      </div>
      <el-form :model="form" :rules="rules" ref="formRef" @submit.prevent="handleSubmit" class="login-form">
        <el-form-item prop="username">
          <el-input
            v-model="form.username"
            placeholder="用户名"
            size="large"
            class="custom-input"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            type="password"
            placeholder="密码"
            size="large"
            show-password
            class="custom-input"
          />
        </el-form-item>
        <el-button
          class="login-btn"
          size="large"
          native-type="submit"
          :loading="loading"
        >
          登录
        </el-button>
      </el-form>
      <div class="login-panel__footer">
        <span class="login-help">账号由 HR 管理员创建，如需开通请联系组织管理员</span>
      </div>
    </section>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '../../stores/auth'

const router = useRouter()
const auth = useAuthStore()
const formRef = ref(null)
const loading = ref(false)

const form = reactive({ username: '', password: '' })
const rules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
}

async function handleSubmit() {
  const valid = await formRef.value.validate().catch(() => false)
  if (!valid) return
  loading.value = true
  try {
    await auth.login(form.username, form.password)
    router.push('/')
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.internal-login {
  min-height: 100vh;
  display: grid;
  grid-template-columns: minmax(320px, 460px) 420px;
  align-items: center;
  justify-content: center;
  gap: 56px;
  padding: 40px;
  background: var(--color-bg);
}

.internal-login__brand {
  position: relative;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 20px;
  min-height: 180px;
}

.internal-login__brand-main {
  display: flex;
  align-items: flex-start;
  gap: 20px;
  position: relative;
  z-index: 1;
}

.internal-login__logo {
  width: 120px;
  height: 64px;
  object-fit: contain;
  flex-shrink: 0;
}

.internal-login__brand h1 {
  margin: 8px 0 8px;
  color: var(--color-text);
  font-size: var(--font-size-3xl);
  line-height: 1.2;
  letter-spacing: 0;
}

.internal-login__brand p {
  margin: 0;
  color: var(--color-text-secondary);
  font-size: var(--font-size-base);
}

.medical-pulse {
  display: block;
  width: 220px;
  height: 24px;
  margin-top: 18px;
  color: var(--color-accent);
  opacity: 0.65;
}

.medical-pulse path,
.molecule-mark path,
.molecule-mark circle {
  fill: none;
  stroke: currentColor;
  stroke-width: 1.5;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.molecule-mark {
  width: 180px;
  height: 150px;
  color: var(--color-brand);
  opacity: 0.14;
  flex-shrink: 0;
}

.internal-login__panel {
  padding: 28px;
}

.login-panel__header {
  margin-bottom: 22px;
}

.login-panel__eyebrow {
  display: block;
  margin-bottom: 6px;
  color: var(--color-accent-strong);
  font-size: var(--font-size-xs);
  font-weight: 700;
  letter-spacing: 1px;
  text-transform: uppercase;
}

.login-panel__header h2 {
  margin: 0;
  color: var(--color-text);
  font-size: var(--font-size-xl);
  font-weight: 500;
}

.login-form {
  padding: 0;
}

.custom-input :deep(.el-input__wrapper) {
  background: var(--color-surface-subtle);
  border: 1px solid var(--color-border-soft);
  box-shadow: none;
}
.custom-input :deep(.el-input__wrapper:hover) {
  border-color: var(--color-accent);
}
.custom-input :deep(.el-input__wrapper.is-focus) {
  border-color: var(--color-accent);
  box-shadow: 0 0 0 1px var(--color-accent-focus);
}
.custom-input :deep(.el-input__inner) {
  color: var(--color-text);
}
.custom-input :deep(.el-input__inner::placeholder) {
  color: var(--color-text-muted);
}
.login-btn {
  width: 100%;
  height: 44px;
  background: var(--color-brand);
  border: none;
  color: var(--color-white);
  font-size: var(--font-size-base);
  font-weight: 500;
  letter-spacing: 0;
  margin-top: 4px;
}
.login-btn:hover {
  background: var(--color-brand-strong);
}
.login-panel__footer {
  text-align: center;
  padding-top: 14px;
}
.login-help {
  color: var(--color-text-muted);
  font-size: var(--font-size-sm);
  letter-spacing: 0;
}

@media (max-width: 900px) {
  .internal-login {
    grid-template-columns: 1fr;
    gap: 24px;
    padding: 24px;
  }

  .internal-login__brand {
    align-items: center;
  }
}
</style>
