<script setup>
import { ref } from 'vue';
import { User, Lock } from '@element-plus/icons-vue';
import { ElMessage } from 'element-plus';
import { getApiBaseUrl } from '../config';

const username = ref('');
const password = ref('');
const emit = defineEmits(['login-success']);
const loading = ref(false);

const login = async () => {
  if (!username.value || !password.value) {
    ElMessage.warning('请输入用户名和密码');
    return;
  }
  
  loading.value = true;
  
  try {
    // 调用后端登录API
    const response = await fetch(`${getApiBaseUrl()}/token`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
      },
      body: new URLSearchParams({
        username: username.value,
        password: password.value,
      }),
    });
    
    const data = await response.json();
    
    if (response.ok) {
      // 保存token到localStorage
      localStorage.setItem('token', data.access_token);
      // 保存用户名到localStorage，用于显示用户信息
      localStorage.setItem('username', username.value);
      // 保存登录状态
      localStorage.setItem('isLoggedIn', 'true');
      // 登录成功
      ElMessage.success('登录成功');
      emit('login-success');
    } else {
      // 登录失败
      ElMessage.error(data.detail || '用户名或密码错误');
    }
  } catch (error) {
    console.error('登录请求出错:', error);
    ElMessage.error('登录请求失败，请稍后再试');
  } finally {
    loading.value = false;
  }
};
</script>

<template>
  <div class="login-container">
    <div class="login-left">
      <img src="../assets/loginBackground.png" alt="Background" class="login-background" />
    </div>
    <div class="login-right">
      <div class="login-header">
        <img src="../assets/logo1.png" alt="北京理工大学" class="bit-logo" />
        <h1 class="system-title">智慧科研评估系统</h1>
      </div>
      <div class="login-form">
        <div class="form-item">
          <el-input 
            v-model="username" 
            placeholder="用户名" 
            prefix-icon="User" 
            size="large"
          />
        </div>
        <div class="form-item">
          <el-input 
            v-model="password" 
            type="password" 
            placeholder="密码" 
            prefix-icon="Lock" 
            size="large"
          />
        </div>
        <el-button 
          type="primary" 
          @click="login" 
          class="login-button" 
          size="large"
          :loading="loading"
        >登录</el-button>
        <div class="forgot-password">
          忘记密码请联系系统管理员
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.login-container {
  display: flex;
  width: 100%;
  height: 100vh;
  background-color: #fff;
}

.login-left {
  flex: 1;
  background: linear-gradient(to bottom right, #e6f7ff, #f0f9ff);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.login-background {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.login-right {
  width: 500px;
  padding: 40px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.login-header {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 40px;
}

.bit-logo {
  width: 80px;
  height: 80px;
  margin-bottom: 20px;
}

.system-title {
  font-size: 24px;
  color: #333;
  font-weight: bold;
  text-align: center;
}

.login-form {
  width: 100%;
}

.form-item {
  margin-bottom: 20px;
}

.login-button {
  width: 100%;
  margin-top: 10px;
}

.forgot-password {
  text-align: center;
  margin-top: 15px;
  color: #909399;
  font-size: 14px;
}
</style>