<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <h1>锡林郭勒盟智慧养殖平台</h1>
        <p>数字化管理系统</p>
      </div>
      
      <a-form
        :model="loginForm"
        :rules="rules"
        @finish="handleLogin"
        class="login-form"
        layout="vertical"
      >
        <a-form-item name="username" label="用户名">
          <a-input
            v-model:value="loginForm.username"
            placeholder="请输入用户名"
            size="large"
            :prefix="renderIcon('user')"
          >
          </a-input>
        </a-form-item>
        
        <a-form-item name="password" label="密码">
          <a-input-password
            v-model:value="loginForm.password"
            placeholder="请输入密码"
            size="large"
            :prefix="renderIcon('lock')"
          />
        </a-form-item>
        
        <a-form-item>
          <a-checkbox v-model:checked="loginForm.remember">
            记住登录状态
          </a-checkbox>
        </a-form-item>
        
        <a-form-item>
          <a-button
            type="primary"
            html-type="submit"
            size="large"
            :loading="loading"
            block
          >
            登录
          </a-button>
        </a-form-item>
      </a-form>
      
      <div class="demo-accounts">
        <h4>演示账户</h4>
        <div class="account-list">
          <div
            v-for="account in demoAccounts"
            :key="account.username"
            @click="setDemoAccount(account)"
            class="account-item"
          >
            <span class="username">{{ account.username }}</span>
            <span class="role">{{ account.role }}</span>
          </div>
        </div>
      </div>
    </div>
    
    <div class="login-footer">
      <p>&copy; 2024 锡林郭勒盟智慧养殖平台. All rights reserved.</p>
    </div>
  </div>
</template>

<script setup>
import { ref, h, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { message } from 'ant-design-vue';
import { UserOutlined, LockOutlined } from '@ant-design/icons-vue';
import { useAuthStore } from '../store/auth.js';

const router = useRouter();
const authStore = useAuthStore();

// 表单数据
const loginForm = ref({
  username: '',
  password: '',
  remember: false,
});

// 加载状态
const loading = ref(false);

// 表单验证规则
const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, message: '用户名至少3个字符', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码至少6个字符', trigger: 'blur' },
  ],
};

// 演示账户
const demoAccounts = [
  { username: 'admin', password: '123456', role: '系统管理员' },
  { username: 'farmer001', password: '123456', role: '养殖户' },
  { username: 'banker001', password: '123456', role: '银行职员' },
  { username: 'insurer001', password: '123456', role: '保险员' },
  { username: 'inspector001', password: '123456', role: '政府检查员' },
  { username: 'trader001', password: '123456', role: '交易员' },
];

// 渲染图标
const renderIcon = (type) => {
  const icons = {
    user: UserOutlined,
    lock: LockOutlined,
  };
  return h(icons[type]);
};

// 设置演示账户
const setDemoAccount = (account) => {
  loginForm.value.username = account.username;
  loginForm.value.password = account.password;
  message.info(`已填入${account.role}演示账户信息`);
};

// 处理登录
const handleLogin = async () => {
  loading.value = true;
  
  try {
    const result = await authStore.login(loginForm.value);
    
    if (result.success) {
      message.success('登录成功！');
      
      // 跳转到首页
      router.push('/');
    } else {
      message.error(result.message || '登录失败');
    }
  } catch (error) {
    console.error('登录错误:', error);
    message.error('登录失败，请稍后重试');
  } finally {
    loading.value = false;
  }
};

// 组件挂载时检查是否已登录
onMounted(() => {
  if (authStore.isAuthenticated) {
    router.push('/');
  }
});
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  padding: 20px;
}

.login-box {
  background: rgba(255, 255, 255, 0.95);
  padding: 40px;
  border-radius: 12px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
  width: 100%;
  max-width: 420px;
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h1 {
  color: #2c3e50;
  font-size: 24px;
  font-weight: bold;
  margin-bottom: 8px;
}

.login-header p {
  color: #7f8c8d;
  font-size: 14px;
  margin: 0;
}

.login-form {
  margin-bottom: 30px;
}

.demo-accounts {
  border-top: 1px solid #eee;
  padding-top: 20px;
}

.demo-accounts h4 {
  color: #34495e;
  font-size: 14px;
  margin-bottom: 12px;
  text-align: center;
}

.account-list {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.account-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 8px;
  background: #f8f9fa;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.2s;
  border: 1px solid transparent;
}

.account-item:hover {
  background: #e3f2fd;
  border-color: #2196f3;
  transform: translateY(-1px);
}

.account-item .username {
  font-size: 12px;
  font-weight: bold;
  color: #2c3e50;
}

.account-item .role {
  font-size: 10px;
  color: #7f8c8d;
  margin-top: 2px;
}

.login-footer {
  position: absolute;
  bottom: 20px;
  width: 100%;
  text-align: center;
}

.login-footer p {
  color: rgba(255, 255, 255, 0.8);
  font-size: 12px;
  margin: 0;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .login-box {
    padding: 30px 20px;
    margin: 10px;
  }
  
  .login-header h1 {
    font-size: 20px;
  }
  
  .account-list {
    grid-template-columns: 1fr;
  }
}
</style>