<script setup>
import { reactive, ref } from "vue";
import loginApi from "@/api/login";
import { useUserStore } from "@/store";
import { useRouter, useRoute } from "vue-router";
import packageJson from "../../package.json";
import commonApi from "@/views/v1/api/common";

const router = useRouter();
const route = useRoute();
const loading = ref(false);
const title = ref("");
const captcha = ref(null);

let isDevelop = import.meta.env.VITE_APP_ENV === "development";

const form = reactive({
  username: isDevelop ? "admin" : "",
  password: isDevelop ? "123456" : "",
  code: "",
  uuid: "",
});

const userStore = useUserStore();
const redirect = route.query.redirect ? route.query.redirect : "/";

// 刷新验证码
const refreshCaptcha = () => {
  form.code = "";
  form.uuid = "";
  loginApi.getCaptch().then((res) => {
    if (res.code === 200) {
      captcha.value = res.data.image;
      form.uuid = res.data.uuid;
    }
  });
};

// 初始化验证码
refreshCaptcha();

// 获取系统配置
const getSystemConfig = async () => {
  try {
    const { data } = await commonApi.getSystemConfigApi({
      group_code: "site_config",
      key: "site_name",
    });
    title.value = data.value || "科技管理系统";
  } catch (error) {
    title.value = "科技管理系统";
  }
};

getSystemConfig();

// 登录提交
const handleSubmit = async ({ values, errors }) => {
  if (loading.value) return;

  if (!errors) {
    loading.value = true;
    const result = await userStore.login(form);

    if (!result) {
      loading.value = false;
      refreshCaptcha();
      return;
    }

    router.push(redirect);
    loading.value = false;
  }
};
</script>

<template>
  <div class="login-container">
    <!-- 背景装饰 -->
    <div class="decoration decoration-1"></div>
    <div class="decoration decoration-2"></div>
    <div class="shape shape-1"></div>
    <div class="shape shape-2"></div>
    <div class="shape shape-3"></div>

    <div class="container">
      <!-- 右侧登录区 -->
      <div class="login-section">
        <div class="login-form">
          <div class="logo">
            <h1>系统登录</h1>
            <div>欢迎回来，请登录您的账号</div>
          </div>

          <a-form :model="form" @submit="handleSubmit" layout="vertical">
            <a-form-item
              field="username"
              :hide-label="true"
              :rules="[{ required: true, message: '请输入用户名或邮箱' }]"
            >
              <a-input
                v-model="form.username"
                placeholder="用户名或邮箱"
                size="large"
                allow-clear
              >
                <template #prefix>
                  <icon-user />
                </template>
              </a-input>
            </a-form-item>

            <a-form-item
              field="password"
              :hide-label="true"
              :rules="[{ required: true, message: '请输入密码' }]"
            >
              <a-input-password
                v-model="form.password"
                placeholder="密码"
                size="large"
                allow-clear
              >
                <template #prefix>
                  <icon-lock />
                </template>
              </a-input-password>
            </a-form-item>

            <a-form-item
              field="code"
              :hide-label="true"
              :rules="[
                {
                  required: true,
                  match: /^[a-zA-Z0-9]{4}$/,
                  message: '请输入4位验证码',
                },
              ]"
            >
              <a-input
                v-model="form.code"
                placeholder="请输入验证码"
                size="large"
                allow-clear
              >
                <template #prefix>
                  <icon-safe />
                </template>
                <template #append>
                  <img
                    :src="captcha"
                    class="captcha-image"
                    @click="refreshCaptcha"
                  />
                </template>
              </a-input>
            </a-form-item>

            <a-form-item :hide-label="true">
              <a-button
                html-type="submit"
                type="primary"
                size="large"
                long
                :loading="loading"
                class="login-btn"
              >
                登 录
              </a-button>
            </a-form-item>
          </a-form>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="less">
.login-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #6c5ce7 0%, #8e44ad 100%);
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
  position: relative;
  overflow: hidden;
}

.container {
  width: 100%;
  max-width: 500px;
  background: #fff;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.15);
  border-radius: 12px;
  overflow: hidden;
  display: flex;
  position: relative;
  z-index: 2;
}

.image-section {
  flex: 1;
  background: linear-gradient(
    135deg,
    rgba(108, 92, 231, 0.9) 0%,
    rgba(142, 68, 173, 0.85) 100%
  );
  color: white;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 40px;
  position: relative;
  overflow: hidden;
}

.image-content {
  z-index: 2;
  text-align: center;
  max-width: 80%;
}

.image-content h2 {
  font-size: 2.5rem;
  font-weight: 300;
  margin-bottom: 30px;
  letter-spacing: 2px;
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
}

.image-content p {
  font-size: 1.1rem;
  line-height: 1.6;
  opacity: 0.9;
  font-weight: 300;
  max-width: 400px;
  margin: 0 auto 30px;
}

.features {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 20px;
  margin-top: 40px;
}

.feature {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 10px 0;
  font-size: 0.95rem;

  :deep(.arco-icon) {
    font-size: 1.2rem;
    color: rgba(255, 255, 255, 0.9);
  }
}

.graphic-element {
  margin: 40px 0;
  display: flex;
  justify-content: center;
  gap: 15px;
}

.circle {
  width: 70px;
  height: 70px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.15);
  display: flex;
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(5px);
  border: 1px solid rgba(255, 255, 255, 0.2);

  :deep(.arco-icon) {
    font-size: 28px;
    opacity: 0.9;
  }
}

.login-section {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 40px;
  background: #fff;
}

.login-form {
  width: 100%;
  max-width: 400px;
}

.logo {
  text-align: center;
  margin-bottom: 50px;

  h1 {
    font-size: 2.2rem;
    font-weight: 400;
    color: #6c5ce7;
    letter-spacing: 2px;
    margin-bottom: 10px;
  }

  p {
    color: #95a5a6;
    margin-top: 8px;
    font-size: 0.95rem;
    font-weight: 300;
  }
}

:deep(.arco-input-wrapper) {
  border: none;
  border-bottom: 1px solid #e0e0e0;
  border-radius: 0;
  background: transparent;
  transition: all 0.3s ease;

  &:hover,
  &.arco-input-focus {
    border-color: #6c5ce7;
    background: transparent;
  }
}

:deep(.arco-input) {
  background: transparent;
}

:deep(.arco-input-wrapper .arco-input-prefix) {
  color: #95a5a6;

  .arco-icon {
    font-size: 18px;
  }
}

:deep(.arco-input-wrapper.arco-input-focus .arco-input-prefix) {
  color: #6c5ce7;
}

.captcha-image {
  height: 36px;
  cursor: pointer;
  transition: all 0.3s ease;
  border-radius: 4px;

  &:hover {
    opacity: 0.8;
  }
}

:deep(.arco-input-append) {
  padding: 0 !important;
}

.login-btn {
  margin-top: 10px;
  background: #6c5ce7;
  border: none;
  border-radius: 6px;
  font-weight: 500;
  letter-spacing: 1px;
  box-shadow: 0 4px 10px rgba(108, 92, 231, 0.25);
  transition: all 0.3s ease;

  &:hover {
    background: #5d4ae0;
    transform: translateY(-2px);
    box-shadow: 0 7px 15px rgba(108, 92, 231, 0.35);
  }
}

:deep(.arco-form-item) {
  margin-bottom: 25px;
}

// 装饰元素
.decoration {
  position: absolute;
  opacity: 0.1;
  z-index: 1;
}

.decoration-1 {
  top: -80px;
  right: -80px;
  width: 300px;
  height: 300px;
  border-radius: 50%;
  background: white;
}

.decoration-2 {
  bottom: -100px;
  left: -100px;
  width: 350px;
  height: 350px;
  border-radius: 50%;
  background: white;
}

.shape {
  position: absolute;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(5px);
  border: 1px solid rgba(255, 255, 255, 0.1);
  z-index: 1;
}

.shape-1 {
  width: 80px;
  height: 80px;
  border-radius: 20px;
  top: 20%;
  left: 15%;
  transform: rotate(45deg);
  animation: float 3s ease-in-out infinite;
}

.shape-2 {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  bottom: 25%;
  right: 20%;
  animation: float 4s ease-in-out infinite;
}

.shape-3 {
  width: 60px;
  height: 60px;
  top: 60%;
  left: 25%;
  transform: rotate(30deg);
  animation: float 3.5s ease-in-out infinite;
}

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

// 响应式设计
@media (max-width: 900px) {
  .container {
    flex-direction: column;
    max-width: 500px;
    min-height: auto;
  }

  .image-section {
    padding: 30px;
    display: none;
  }

  .login-section {
    padding: 50px 30px;
  }
}
</style>
