<template>
  <div class="login-container">
    <el-card class="login-card">
      <template #header>
        <div class="card-header">
          <span>环境监督系统</span>
        </div>
      </template>
      <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" class="login-form" label-position="top" autocomplete="off" @submit.prevent="handleLogin">
        <el-form-item label="邮箱" prop="email" class="form-item-tight">
          <el-input v-model="loginForm.email" placeholder="请输入邮箱" size="large" :prefix-icon="User" clearable autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password" class="form-item-tight">
          <el-input type="password" v-model="loginForm.password" placeholder="请输入密码" size="large" :prefix-icon="Lock" show-password autocomplete="new-password"></el-input>
        </el-form-item>
        <el-form-item class="login-btn-item">
          <el-button type="primary" @click="handleLogin" :loading="loading" class="login-button" size="large">登 录</el-button>
        </el-form-item>
        <div class="extra-links">
          <el-link type="info" @click="$router.push('/register')">注册账号</el-link>
          <el-link type="info" @click="$router.push('/forgot-password')">找回密码</el-link>
        </div>

        <el-form-item class="guest-btn-item">
          <el-button @click="handleGuestAccess" class="guest-button" size="large" :icon="Promotion">作为游客提交反馈</el-button>
        </el-form-item>

      </el-form>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue';
import { useRouter } from 'vue-router';
import type { FormInstance, FormRules } from 'element-plus';
import { ElMessage } from 'element-plus';
import { useAuthStore } from '@/stores/auth';
import { User, Lock, Promotion } from '@element-plus/icons-vue';

const router = useRouter();
const authStore = useAuthStore();
const loginFormRef = ref<FormInstance>();
const loading = ref(false);

const loginForm = reactive({
  email: '',
  password: '',
});

const loginRules = reactive<FormRules>({
  email: [
    { required: true, message: '请输入邮箱地址', trigger: 'blur' },
    { type: 'email', message: '请输入有效的邮箱地址', trigger: ['blur', 'change'] }
  ],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
});

const handleLogin = async () => {
  if (!loginFormRef.value) return;
  await loginFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true;
      try {
        await authStore.login(loginForm);
        ElMessage.success('登录成功，正在跳转...');
      } catch (error) {
        ElMessage.error('登录失败，请检查您的邮箱和密码。');
        console.error(error);
      } finally {
        loading.value = false;
      }
    }
  });
};

const handleGuestAccess = () => {
  router.push('/public-feedback');
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  width: 100vw;
  overflow: hidden; /* Prevent scrolling */
  background-image: url('/login-bg.avif');
  background-size: cover;
  background-position: center;
}

.login-card {
  position: relative;
  width: 480px;
  border-radius: 25px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
  animation: fadeIn 0.7s ease-in-out;
  background: rgba(245, 245, 245, 0.85); /* Light warm gray */
  backdrop-filter: blur(15px);
  border: 1px solid rgba(255, 255, 255, 0.5);
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(-20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.card-header {
  text-align: center;
  font-size: 26px;
  font-weight: 600;
  color: #004d40; /* Teal Gold color */
  padding: 25px 0 0 0; /* Removed bottom padding */
  border-bottom: 1px solid rgba(0,77,64,0.2);
}

.login-form {
  padding: 10px 35px 20px 35px; /* Minimal top padding */
}

.form-item-tight {
  margin-bottom: 18px;
}

.login-button {
  width: 100%;
  border: none;
  background: #004d40; /* Teal Gold color */
  color: white;
  letter-spacing: 5px;
  padding-left: 10px; /* to compensate letter-spacing */
  transition: all 0.3s ease;
  box-shadow: 0 4px 15px rgba(0, 77, 64, 0.4);
  border-radius: 10px;
}

.login-button:hover {
  background: #00695c;
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(0, 77, 64, 0.5);
}

.login-btn-item {
  margin-top: 25px;
  margin-bottom: 0;
}

.el-input__wrapper {
  transition: all 0.3s ease;
}

.el-input__wrapper:hover {
  box-shadow: 0 0 0 1px var(--el-color-primary) inset !important;
}

.el-input__wrapper.is-focus {
  box-shadow: 0 0 0 1px var(--el-color-primary) inset !important;
}

.extra-links {
  display: flex;
  justify-content: space-between;
  margin-top: 20px;
  padding: 0 10px;
}

.divider-text {
  color: #888;
  font-size: 14px;
}

.guest-btn-item {
  margin-top: 15px;
}

.guest-button {
  width: 100%;
  border-color: #004d40;
  color: #004d40;
}

.guest-button:hover {
  background-color: #e0f2f1;
  border-color: #00695c;
  color: #00695c;
}

.extra-links .el-link {
  color: #bbb;
  font-size: 15px; /* Increased font size */
}
.extra-links .el-link:hover {
  color: #fff;
}
</style>

<style>
/* This is a global style override to achieve the desired hover/focus effect */
.el-input__wrapper {
  transition: all 0.3s ease !important;
}

.el-input__wrapper:hover {
  box-shadow: 0 0 0 1px var(--el-color-primary, #409eff) inset !important;
}

.el-input__wrapper.is-focus {
   box-shadow: 0 0 0 1px var(--el-color-primary, #409eff) inset !important;
}

.el-form-item__label {
  color: #555 !important;
  font-weight: 500 !important;
  letter-spacing: 0.5px !important;
  font-size: 15px !important;
}

.el-input__inner {
  color: #333 !important;
  font-size: 15px !important; /* Matched font size to label */
}

.el-input__icon {
  font-size: 18px !important; /* Icon size can be slightly larger */
}

.el-input__wrapper {
  background-color: rgba(255, 255, 255, 0.9) !important;
  border-radius: 10px !important;
}
</style> 
