<template>
  <div class="container">
    <div class="banner">
      <div class="banner-inner">
        <div class="banner-logo">
          <img :src="logoSrc" alt="Logo" class="logo-img" />
          <span class="app-name">湖南韫珠环保科技有限公司</span>
        </div>
        <div class="banner-title">
          <div class="banner-title-text">能耗管理平台</div>
          <div class="banner-sub-title">专业的企业级能源数字化管理平台</div>
        </div>
        <div class="banner-image">
           <!-- 装饰性图形 -->
           <svg width="300" height="300" viewBox="0 0 300 300" fill="none" xmlns="http://www.w3.org/2000/svg" style="opacity: 0.8;">
              <circle cx="150" cy="150" r="120" stroke="white" stroke-width="2" stroke-dasharray="10 10" opacity="0.3"/>
              <circle cx="150" cy="150" r="90" stroke="white" stroke-width="2" opacity="0.5"/>
              <circle cx="150" cy="150" r="60" fill="white" fill-opacity="0.1"/>
              <path d="M150 30V150L230 230" stroke="white" stroke-width="2" stroke-linecap="round"/>
           </svg>
        </div>
      </div>
    </div>
    <div class="content">
      <div class="content-inner">
        <div class="login-form-wrapper">
          <div class="login-header">
            <div class="login-title">欢迎登录</div>
            <div class="login-sub-title">湖南韫珠环保科技有限公司能耗管理平台</div>
          </div>
          <a-form :model="form" :rules="rules" ref="formRef" layout="vertical" class="login-form" size="large">
            <a-form-item field="username" hide-label>
              <a-input 
                v-model="form.username" 
                placeholder="请输入用户名" 
                allow-clear
              >
                <template #prefix>
                  <icon-user />
                </template>
              </a-input>
            </a-form-item>
            <a-form-item field="password" hide-label>
              <a-input-password 
                v-model="form.password" 
                placeholder="请输入密码" 
                allow-clear
                @keyup.enter="handleLogin"
              >
                <template #prefix>
                  <icon-lock />
                </template>
              </a-input-password>
            </a-form-item>
            <div class="login-form-actions">
              <a-checkbox v-model="rememberMe">记住密码</a-checkbox>
              <a-link>忘记密码？</a-link>
            </div>
            <a-form-item hide-label style="margin-top: 20px;">
              <a-button type="primary" long @click="handleLogin" :loading="loading">
                登录
              </a-button>
            </a-form-item>
          </a-form>
        </div>
      </div>
      <div class="footer">
        <span>&copy; {{ new Date().getFullYear() }} 湖南韫珠环保科技有限公司. All Rights Reserved.</span>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue';
import { useRouter } from 'vue-router';
import { Message } from '@arco-design/web-vue';
import { IconUser, IconLock } from '@arco-design/web-vue/es/icon';
import { useUserStore } from '../stores/user';
import logoSrc from '../logo.png';

const router = useRouter();
const userStore = useUserStore();
const formRef = ref();
const loading = ref(false);
const rememberMe = ref(false);

const form = reactive({
  username: '',
  password: ''
});

const rules = {
  username: [{ required: true, message: '请输入用户名' }],
  password: [{ required: true, message: '请输入密码' }]
};

const handleLogin = async () => {
  if (!formRef.value) return;
  
  const errors = await formRef.value.validate();
  if (!errors) {
    loading.value = true;
    try {
      await userStore.login(form);
      Message.success('登录成功');
      router.push('/');
    } catch (error) {
      Message.error('登录失败，请检查账号密码');
    } finally {
      loading.value = false;
    }
  }
};
</script>

<style scoped>
.container {
  display: flex;
  height: 100vh;
  width: 100%;
  overflow: hidden;
}

.banner {
  width: 550px;
  background: linear-gradient(163.85deg, #1d2129 0%, #00308F 100%);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  position: relative;
  overflow: hidden;
}

.banner-inner {
  display: flex;
  flex-direction: column;
  align-items: center;
  color: #fff;
  z-index: 1;
}

.banner-logo {
  display: flex;
  align-items: center;
  margin-bottom: 24px;
}

.logo-img {
  width: 48px;
  height: 48px;
  object-fit: contain;
}

.app-name {
  font-size: 24px;
  font-weight: 600;
  margin-left: 12px;
  letter-spacing: 1px;
}

.banner-title {
  text-align: center;
  margin-bottom: 40px;
}

.banner-title-text {
  font-size: 32px;
  font-weight: bold;
  margin-bottom: 12px;
}

.banner-sub-title {
  font-size: 16px;
  opacity: 0.8;
}

.banner-image {
  margin-top: 20px;
  animation: float 6s ease-in-out infinite;
}

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

.content {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  position: relative;
  background-color: var(--color-bg-1);
}

.content-inner {
  width: 100%;
  max-width: 480px;
  padding: 0 20px;
}

.login-form-wrapper {
  width: 100%;
}

.login-header {
  margin-bottom: 32px;
}

.login-title {
  font-size: 24px;
  font-weight: 500;
  color: var(--color-text-1);
  margin-bottom: 8px;
}

.login-sub-title {
  color: var(--color-text-3);
  font-size: 16px;
}

.login-form-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.footer {
  position: absolute;
  bottom: 20px;
  width: 100%;
  text-align: center;
  color: var(--color-text-3);
  font-size: 12px;
}

/* Responsive */
@media (max-width: 992px) {
  .banner {
    display: none;
  }
}
</style>
