<template>
  <div class="login-container">
    <div class="login-header">葛洲坝集团交通投资有限公司投资决策分析系统</div>
    <div class="login-form-wrapper">
      <div class="company-logo"></div>
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        label-width="70px"
        class="login-form"
        labelPosition="top"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            :prefix-icon="User"
            v-model="loginForm.username"
            placeholder="请输入用户名"
            prefix-icon="el-icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            prefix-icon="Lock"
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            class="login-btn"
            :loading="loading"
            @click="handleLogin"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref } from "vue";
import { useRouter } from "vue-router";
import { User, Lock } from "@element-plus/icons-vue";

const router = useRouter();
const loginFormRef = ref();
const loading = ref(false);
const captchaSrc = ref("https://picsum.photos/120/40"); // 模拟验证码图片

// 登录表单数据
const loginForm = ref({
  username: "admin",
  password: "",
  captcha: "",
});

// 表单验证规则
const loginRules = ref({
  username: [
    { required: true, message: "请输入用户名", trigger: "blur" },
    { min: 3, max: 20, message: "长度在 3 到 20 个字符", trigger: "blur" },
  ],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    { min: 6, max: 20, message: "长度在 6 到 20 个字符", trigger: "blur" },
  ],
});

// 处理登录
const handleLogin = async () => {
  try {
    await loginFormRef.value?.validate();
    loading.value = true;
    setTimeout(() => {
      // 登录成功后设置token
      const mockToken = "mock-jwt-token";
      localStorage.setItem("token", mockToken);
      loading.value = false;
      // 跳转到首页
      router.push("/");
    }, 1000);
  } catch (error) {
    console.error("登录失败:", error);
    loading.value = false;
  }
};
</script>

<style scoped lang="less">
.login-container {
  width: 100%;
  height: 100vh;
  background-image: url("@/assets/images/login.png");
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  padding-top: 15px;
}

.login-header {
  color: #fff;
  font-size: 24px;
  font-weight: bold;
  padding-left: 20px;
  margin: 0 20px;
  border-radius: 5px;
  display: flex;
  justify-content: left;
  align-items: center;
  height: 50px;
  background: linear-gradient(
    to right,
    rgba(0, 0, 0, 0.3),
    rgba(0, 0, 0, 0.6),
    rgba(0, 0, 0, 0.3)
  );
}

.login-form-wrapper {
  background-color: rgba(0, 0, 0, 0.8);
  position: absolute;
  top: 50%;
  right: 1%;
  transform: translateY(-50%);
  padding: 15px;
  box-sizing: border-box;
  width: 350px;
  height: 350px;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.5);
  border-radius: 8px;
  :deep(.el-form-item) {
    margin-bottom: 10px;
    .el-form-item__content {
      .el-input {
        height: 48px;
        border-radius: 4px;
        .el-input__wrapper {
          background: rgba(255, 255, 255, 0.9);
        }
        .el-input__inner {
          color: #333;
        }
      }
      .el-form-item__label {
        color: #fff;
      }
    }
  }
  :deep(.el-form-item__label) {
    color: #fff;
  }
}

.company-logo {
  width: 100%;
  height: 30px;
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 30px;
  background-image: url(/src/assets/images/logo.png);
  background-size: 100% 80%;
  background-position: center;
  background-repeat: no-repeat;
}

.login-form {
  .login-btn {
    width: 100%;
    height: 30px;
    border-radius: 4px;
    background-color: #1890ff;
    margin-top: 18px;
  }
  .forgot-password {
    color: #fff;
    float: right;
    margin-top: 10px;
  }
}

.captcha-image {
  position: absolute;
  right: 0;
  top: 0;
  cursor: pointer;
  img {
    width: 120px;
    height: 40px;
    border-radius: 4px;
  }
}

:deep(.el-form-item__content) {
  position: relative;
  &.el-form-item__content--with-captcha {
    padding-right: 130px;
  }
}

:deep(.el-input__inner) {
  color: #333;
}

:deep(.el-input__prefix-inner) {
  color: #909399;
}
.login-form {
  :deep(.el-form-item) {
    .el-form-item__content {
      .el-input {
        height: 30px;
      }
    }
  }
  :deep(.el-form-item--label-top) {
    .el-form-item__label {
      line-height: 15px;
    }
  }
}
</style>
