<template>
  <div class="login-container">
    <div class="login-box">
      <h2 style="color: white">登录</h2>
      <form @submit.prevent="login">
        <div class="input-group">
          <i class="fas fa-user"></i>
          <input type="text" v-model="username" placeholder="用户名" required />
        </div>
        <div class="input-group">
          <i class="fas fa-lock"></i>
          <input type="password" v-model="password" placeholder="密码" required />
        </div>
        <div class="input-group captcha-group">
          <i class="fas fa-shield-alt"></i>
          <input type="text" v-model="captcha" placeholder="验证码" required />
          <!-- 显示验证码图片 -->
          <img :src="captchaUrl" @click="refreshCaptcha" alt="验证码" class="captcha-image" style="margin-top: 5px"/>
        </div>
        <button type="submit" class="login-button">登录</button>
      </form>
      <p>没有账户？<router-link to="/register">注册</router-link></p>
    </div>
  </div>
</template>


<script>
import axios from 'axios';

export default {
  data() {
    return {
      username: '',
      password: '',
      captcha: '', // 存储用户输入的验证码
      captchaUrl: '' // 验证码图片的 URL
    };
  },
  methods: {
    fetchCaptcha() {
      fetch('http://localhost:8081/captchaImage')
          .then(response => response.json()) // 将响应解析为 JSON
          .then(data => {
            if (data.code === 200) {
              // 将 Base64 编码的图像转换为数据 URL
              this.captchaUrl = `data:image/jpeg;base64,${data.img}`;
            } else {
              console.error('获取验证码失败:', data.msg);
            }
          })
          .catch(error => {
            console.error('获取验证码时出现错误:', error);
          });
    },
    async login() {
      try {
        const response = await axios.post('http://localhost:8081/login', {
          username: this.username,
          password: this.password,
          captcha: this.captcha // 将验证码传递给后端
        });
        // 处理成功登录的逻辑，比如存储 token
        console.log('登录成功', response.data);
      } catch (error) {
        console.error('登录失败', error);
        // 登录失败时可以选择刷新验证码
        this.refreshCaptcha();
      }
    },
    // 刷新验证码
    refreshCaptcha() {
      this.fetchCaptcha();
    }
  },
  mounted() {
    this.fetchCaptcha(); // 组件挂载时获取验证码
    // 加载页面时获取初始验证码
    this.refreshCaptcha();

  }
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #2e3a4e;
}

.login-box {
  width: 350px;
  padding: 20px;
  background-color: #3c4d63;
  border-radius: 8px;
  box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.2);
  text-align: center;
}

.input-group {
  margin-bottom: 15px;
  position: relative;
}

.input-group i {
  position: absolute;
  top: 50%;
  left: 10px;
  transform: translateY(-50%);
  color: #aaa;
}

.input-group input {
  width: 100%;
  padding: 10px 10px 10px 30px;
  border: none;
  border-radius: 4px;
  background-color: #465675;
  color: #fff;
}

.captcha-image {
  height: 40px;
  cursor: pointer;
  margin-left: 10px;
  vertical-align: middle;
}

.login-button {
  width: 100%;
  padding: 10px;
  border: none;
  border-radius: 4px;
  background-color: #007bff;
  color: #fff;
  font-size: 16px;
  cursor: pointer;
}

.login-button:hover {
  background-color: #0056b3;
}
</style>
