<template>
  <div class="login-page">
    <div class="login-bg">
      <div class="bg-circle circle-1"></div>
      <div class="bg-circle circle-2"></div>
      <div class="bg-circle circle-3"></div>
      <div class="bg-grid"></div>
      <div class="bg-particles">
        <span v-for="n in 20" :key="n" class="particle" :style="getParticleStyle(n)"></span>
      </div>
    </div>
    <div class="login-container">
      <div class="login-card">
        <div class="logo-section">
          <div class="logo">
            <div class="logo-inner">
              <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                <path d="M12 2L2 7l10 5 10-5-10-5z"/>
                <path d="M2 17l10 5 10-5"/>
                <path d="M2 12l10 5 10-5"/>
              </svg>
            </div>
            <div class="logo-ring"></div>
            <div class="logo-ring ring-2"></div>
          </div>
          <h1 class="title">爱维宠物医院</h1>
          <p class="subtitle">智能管理平台</p>
        </div>
        
        <t-form :data="form" @submit="onSubmit" class="login-form">
          <t-form-item>
            <t-input 
              v-model="form.account" 
              placeholder="用户名 / 手机号 / 邮箱"
              size="large"
              class="input-field"
            >
              <template #prefix-icon>
                <t-icon name="user" />
              </template>
            </t-input>
          </t-form-item>
          
          <t-form-item>
            <t-input 
              v-model="form.password" 
              type="password" 
              placeholder="请输入密码"
              size="large"
              class="input-field"
            >
              <template #prefix-icon>
                <t-icon name="lock-on" />
              </template>
            </t-input>
          </t-form-item>
          
          <div class="form-options">
            <t-checkbox v-model="rememberMe" class="remember-checkbox">
              <span class="checkbox-label">记住我</span>
            </t-checkbox>
            <a href="#" class="forgot-link">忘记密码?</a>
          </div>
          
          <t-form-item>
            <t-button 
              theme="primary" 
              type="submit" 
              block 
              size="large"
              :loading="loading"
              class="login-btn"
            >
              <span class="btn-text">登 录</span>
              <t-icon name="arrow-right" class="btn-icon" />
            </t-button>
          </t-form-item>
        </t-form>
        
        <div class="footer">
          <p>还没有账号? <router-link to="/register" class="register-link">立即注册</router-link></p>
        </div>
      </div>
      
      <div class="login-illustration">
        <div class="illustration-bg"></div>
        <div class="illustration-content">
          <div class="floating-icons">
            <div class="icon icon-1">🐕</div>
            <div class="icon icon-2">🐈</div>
            <div class="icon icon-3">🏥</div>
            <div class="icon icon-4">💉</div>
            <div class="icon icon-5">🦴</div>
            <div class="icon icon-6">🐾</div>
          </div>
          <div class="content-wrapper">
            <div class="badge">✨ 专业 · 贴心 · 高效</div>
            <h2>专业宠物医疗<br>用心呵护每一刻</h2>
            <p>为爱宠提供最优质的医疗服务<br>让每一个生命都得到最好的照顾</p>
            <div class="feature-list">
              <div class="feature-item">
                <t-icon name="check-circle" class="feature-icon" />
                <span>在线预约</span>
              </div>
              <div class="feature-item">
                <t-icon name="check-circle" class="feature-icon" />
                <span>电子病历</span>
              </div>
              <div class="feature-item">
                <t-icon name="check-circle" class="feature-icon" />
                <span>智能提醒</span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue';
import { useRouter } from 'vue-router';
import { MessagePlugin } from 'tdesign-vue-next';
import { api } from '../api';
import { useAuthStore } from '../store/auth';

const router = useRouter();
const auth = useAuthStore();
const loading = ref(false);
const rememberMe = ref(false);

const form = reactive({
  account: '',
  password: '',
});

const getParticleStyle = (n: number) => {
  return {
    left: `${Math.random() * 100}%`,
    top: `${Math.random() * 100}%`,
    animationDelay: `${Math.random() * 5}s`,
    animationDuration: `${3 + Math.random() * 4}s`,
    width: `${2 + Math.random() * 4}px`,
    height: `${2 + Math.random() * 4}px`,
  };
};

const onSubmit = async () => {
  if (!form.account || !form.password) {
    MessagePlugin.warning('请输入账号与密码');
    return;
  }
  loading.value = true;
  try {
    const res = await api.login(form);
    if (res.code === 0) {
      auth.setAuth(res.data.token, {
        userId: res.data.userId,
        username: res.data.username,
        role: res.data.role,
      });
      MessagePlugin.success('登录成功');
      let redirectPath;
      if (res.data.role === 'CUSTOMER') {
        redirectPath = '/welcome';
      } else if (res.data.role === 'DOCTOR') {
        redirectPath = '/admin/welcome';
      } else {
        redirectPath = '/admin/dashboard';
      }
      router.push(redirectPath);
    } else {
      MessagePlugin.error(res.message || '登录失败');
    }
  } catch {
    MessagePlugin.error('登录失败，请检查网络');
  } finally {
    loading.value = false;
  }
};
</script>

<style scoped>
.login-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #0d9488 0%, #14b8a6 100%);
  position: relative;
  overflow: hidden;
}

.login-bg {
  position: absolute;
  inset: 0;
  overflow: hidden;
}

.bg-circle {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.08);
  animation: float 20s infinite ease-in-out;
  filter: blur(40px);
}

.circle-1 {
  width: 500px;
  height: 500px;
  top: -150px;
  right: -100px;
  animation-delay: 0s;
}

.circle-2 {
  width: 400px;
  height: 400px;
  bottom: -100px;
  left: -100px;
  animation-delay: -7s;
}

.circle-3 {
  width: 300px;
  height: 300px;
  top: 40%;
  left: 20%;
  animation-delay: -14s;
}

.bg-grid {
  position: absolute;
  inset: 0;
  background-image: 
    linear-gradient(rgba(255, 255, 255, 0.03) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255, 255, 255, 0.03) 1px, transparent 1px);
  background-size: 50px 50px;
  animation: gridMove 20s linear infinite;
}

@keyframes gridMove {
  0% { transform: translate(0, 0); }
  100% { transform: translate(50px, 50px); }
}

.bg-particles {
  position: absolute;
  inset: 0;
}

.particle {
  position: absolute;
  background: rgba(255, 255, 255, 0.5);
  border-radius: 50%;
  animation: particleFloat linear infinite;
}

@keyframes particleFloat {
  0% {
    transform: translateY(100vh) scale(0);
    opacity: 0;
  }
  10% {
    opacity: 1;
  }
  90% {
    opacity: 1;
  }
  100% {
    transform: translateY(-100vh) scale(1);
    opacity: 0;
  }
}

@keyframes float {
  0%, 100% { transform: translate(0, 0) scale(1); }
  33% { transform: translate(30px, -30px) scale(1.1); }
  66% { transform: translate(-20px, 20px) scale(0.9); }
}

.login-container {
  display: flex;
  width: 1000px;
  max-width: 95%;
  background: rgba(255, 255, 255, 0.98);
  border-radius: 32px;
  box-shadow: 
    0 25px 50px -12px rgba(0, 0, 0, 0.25),
    0 0 0 1px rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(20px);
  overflow: hidden;
  animation: slideUp 0.8s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative;
  z-index: 1;
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(40px) scale(0.98);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

.login-card {
  flex: 1;
  padding: 56px 48px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.logo-section {
  text-align: center;
  margin-bottom: 40px;
}

.logo {
  width: 80px;
  height: 80px;
  margin: 0 auto 20px;
  position: relative;
}

.logo-inner {
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, #0d9488, #14b8a6);
  border-radius: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  box-shadow: 0 15px 35px rgba(102, 126, 234, 0.4);
  position: relative;
  z-index: 2;
  animation: logoPulse 2s ease-in-out infinite;
}

@keyframes logoPulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.05); }
}

.logo svg {
  width: 40px;
  height: 40px;
}

.logo-ring {
  position: absolute;
  inset: -8px;
  border: 2px solid rgba(102, 126, 234, 0.2);
  border-radius: 28px;
  animation: ringPulse 2s ease-out infinite;
}

.logo-ring.ring-2 {
  animation-delay: 0.5s;
}

@keyframes ringPulse {
  0% { transform: scale(1); opacity: 1; }
  100% { transform: scale(1.3); opacity: 0; }
}

.title {
  font-size: 32px;
  font-weight: 800;
  background: linear-gradient(135deg, #1e293b, #475569);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  margin: 0 0 8px;
  letter-spacing: -0.5px;
}

.subtitle {
  font-size: 15px;
  color: #64748b;
  margin: 0;
  font-weight: 500;
}

.login-form :deep(.t-input) {
  border-radius: 14px;
  height: 52px;
  border: 2px solid #e2e8f0;
  transition: all 0.3s ease;
}

.login-form :deep(.t-input:hover) {
  border-color: #cbd5e1;
}

.login-form :deep(.t-input:focus) {
  border-color: #0d9488;
  box-shadow: 0 0 0 4px rgba(102, 126, 234, 0.1);
}

.login-form :deep(.t-input__inner) {
  padding-left: 44px !important;
  font-size: 15px;
}

.input-field :deep(.t-input__prefix-icon) {
  color: #94a3b8;
  font-size: 20px;
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 8px 0 28px;
}

.remember-checkbox :deep(.t-checkbox__label) {
  font-size: 14px;
  color: #475569;
}

.checkbox-label {
  font-weight: 500;
}

.forgot-link {
  color: #0d9488;
  text-decoration: none;
  font-size: 14px;
  font-weight: 600;
  transition: all 0.3s ease;
  position: relative;
}

.forgot-link::after {
  content: '';
  position: absolute;
  bottom: -2px;
  left: 0;
  width: 0;
  height: 2px;
  background: linear-gradient(90deg, #0d9488, #14b8a6);
  transition: width 0.3s ease;
}

.forgot-link:hover {
  color: #14b8a6;
}

.forgot-link:hover::after {
  width: 100%;
}

.login-btn {
  height: 52px !important;
  border-radius: 14px !important;
  font-size: 16px !important;
  font-weight: 700 !important;
  background: linear-gradient(135deg, #0d9488, #14b8a6) !important;
  border: none !important;
  box-shadow: 0 8px 20px rgba(102, 126, 234, 0.4) !important;
  transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1) !important;
  position: relative;
  overflow: hidden;
}

.login-btn::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255,255,255,0.3), transparent);
  transition: left 0.5s;
}

.login-btn:hover {
  transform: translateY(-2px) !important;
  box-shadow: 0 12px 28px rgba(102, 126, 234, 0.5) !important;
}

.login-btn:hover::before {
  left: 100%;
}

.btn-text {
  margin-right: 8px;
}

.btn-icon {
  transition: transform 0.3s ease;
}

.login-btn:hover .btn-icon {
  transform: translateX(4px);
}

.footer {
  text-align: center;
  font-size: 14px;
  color: #64748b;
}

.register-link {
  color: #0d9488;
  text-decoration: none;
  font-weight: 700;
  transition: all 0.3s ease;
  position: relative;
}

.register-link::after {
  content: '';
  position: absolute;
  bottom: -2px;
  left: 0;
  width: 0;
  height: 2px;
  background: linear-gradient(90deg, #0d9488, #14b8a6);
  transition: width 0.3s ease;
}

.register-link:hover {
  color: #14b8a6;
}

.register-link:hover::after {
  width: 100%;
}

.login-illustration {
  width: 420px;
  background: linear-gradient(135deg, #0d9488, #14b8a6);
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  position: relative;
  overflow: hidden;
}

.illustration-bg {
  position: absolute;
  inset: 0;
  background: 
    radial-gradient(circle at 30% 20%, rgba(255,255,255,0.1) 0%, transparent 50%),
    radial-gradient(circle at 70% 80%, rgba(255,255,255,0.1) 0%, transparent 50%);
}

.illustration-content {
  text-align: center;
  padding: 48px;
  position: relative;
  z-index: 1;
}

.floating-icons {
  position: relative;
  height: 140px;
  margin-bottom: 32px;
}

.icon {
  position: absolute;
  font-size: 36px;
  animation: iconFloat 3s ease-in-out infinite;
  filter: drop-shadow(0 4px 8px rgba(0,0,0,0.2));
}

.icon-1 { left: 5%; top: 0; animation-delay: 0s; }
.icon-2 { right: 5%; top: 10%; animation-delay: 0.4s; }
.icon-3 { left: 50%; top: 30%; transform: translateX(-50%); animation-delay: 0.8s; }
.icon-4 { right: 15%; bottom: 10%; animation-delay: 1.2s; }
.icon-5 { left: 15%; bottom: 5%; animation-delay: 1.6s; }
.icon-6 { right: 30%; top: 5%; animation-delay: 2s; }

@keyframes iconFloat {
  0%, 100% { transform: translateY(0) rotate(0deg); }
  50% { transform: translateY(-20px) rotate(5deg); }
}

.icon-3 {
  font-size: 48px;
}

.content-wrapper {
  position: relative;
}

.badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 20px;
  font-size: 13px;
  font-weight: 600;
  margin-bottom: 24px;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.illustration-content h2 {
  font-size: 28px;
  font-weight: 700;
  margin: 0 0 16px;
  line-height: 1.4;
  text-shadow: 0 2px 10px rgba(0,0,0,0.1);
}

.illustration-content p {
  font-size: 15px;
  opacity: 0.95;
  margin: 0 0 32px;
  line-height: 1.8;
}

.feature-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
  align-items: flex-start;
  text-align: left;
  padding: 0 20px;
}

.feature-item {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 14px;
  font-weight: 500;
  padding: 8px 16px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 10px;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.feature-icon {
  color: #4ade80;
  font-size: 18px;
}

@media (max-width: 900px) {
  .login-illustration {
    display: none;
  }
  
  .login-card {
    padding: 40px 32px;
  }
}

@media (max-width: 480px) {
  .login-card {
    padding: 32px 24px;
  }
  
  .title {
    font-size: 26px;
  }
}
</style>
