<!-- 智能仓储管理系统登录页面 - 深色科技风格 -->

<template>
  <div class="login-page">
    <!-- 背景层 -->
    <div class="bg-layer" :style="{ backgroundImage: `url(${bgImg})` }" />

    <!-- 主内容区域 -->
    <div class="login-content">
      <!-- 左侧信息区 -->
      <div class="login-left">
        <div class="system-info">
          <h1 class="system-title">智能仓储管理系统</h1>
          <p class="system-subtitle">Intelligent Warehouse Management System</p>
          <div class="feature-list">
            <div class="feature-item">
              <i class="fas fa-check-circle" />
              <span>高效的库存管理</span>
            </div>
            <div class="feature-item">
              <i class="fas fa-check-circle" />
              <span>智能化物料追踪</span>
            </div>
            <div class="feature-item">
              <i class="fas fa-check-circle" />
              <span>实时数据分析</span>
            </div>
          </div>
        </div>
      </div>

      <!-- 右侧登录表单 -->
      <div class="login-right">
        <div class="login-form-wrapper">
          <div class="form-header">
            <div class="form-icon">
              <i class="fas fa-user-circle" />
            </div>
            <h2 class="form-title">欢迎登录</h2>
            <p class="form-subtitle">请输入您的账号信息</p>
          </div>

          <div class="form-content">
            <!-- 用户名输入框 -->
            <div class="form-item">
              <label class="input-label">用户名</label>
              <div class="input-wrapper">
                <i class="fas fa-user input-icon" />
                <input
                  v-model="username" type="text" placeholder="请输入用户名" class="dark-input"
                  @keyup.enter="handleLogin"
                />
              </div>
            </div>

            <!-- 密码输入框 -->
            <div class="form-item">
              <label class="input-label">密码</label>
              <div class="input-wrapper">
                <i class="fas fa-lock input-icon" />
                <input
                  v-model="password" :type="showPassword ? 'text' : 'password'" placeholder="请输入密码"
                  class="dark-input" @keyup.enter="handleLogin"
                />
                <i
                  :class="showPassword ? 'fas fa-eye-slash' : 'fas fa-eye'" class="password-toggle"
                  @click="showPassword = !showPassword"
                />
              </div>
            </div>

            <!-- 登录按钮 -->
            <button class="login-btn" :disabled="loading" @click="handleLogin">
              {{ loading ? '登录中...' : '登 录' }}
            </button>
            <button class="fingerprint-btn" @click="handleFingerprintLogin">
              <i class="fas fa-fingerprint" />
              指纹登录
            </button>
          </div>
        </div>
      </div>
    </div>

    <!-- Loading -->
    <div v-if="loading" class="loading-overlay">
      <div class="loading-dots">
        <div class="dot" />
        <div class="dot" />
        <div class="dot" />
      </div>
      <span class="loading-text">加载中...</span>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { loginApi, queryRemindTime } from '../api/login.js';
import { getFormattedDateTime } from '../common/Common.js';
import { showNotify } from 'vant';

// 图片资源
import bgImg from '../assets/images/bj.png';

const router = useRouter();
const route = useRoute();

// 表单数据
const username = ref('');
const password = ref('');
const loading = ref(false);
const showPassword = ref(false);

onMounted(() => {
    // 检查URL参数中是否包含isOut=true，如果有则存储到localStorage
    const isOutParam = route.query.isOut;
    if (isOutParam !== undefined) {
        const isOutValue = String(isOutParam) === 'true' ? 'true' : 'false';
        if (isOutValue == 'true') {
            localStorage.setItem('isOut', isOutValue);
        } else {
            localStorage.setItem('isOut', isOutValue);
        }
    } else {
        localStorage.setItem('isOut', 'false');
    }
});

// 登录处理函数
const handleLogin = async () => {
    if (!username.value || !password.value) {
    // message.error('请输入完整的登录信息');
        showNotify({ type: 'danger', message: '请输入完整的登录信息' });
        return;
    }

    loading.value = true;
    try {
        const params = {
            username: username.value,
            password: password.value,
            accountDateTime: getFormattedDateTime(),
            languageId: 'zh-CN',
        };
        const formData = new FormData();
        formData.append('userName', params.username);
        formData.append('password', params.password);
        formData.append('accountDateTime', params.accountDateTime);
        formData.append('languageId', params.languageId);
        const res = await loginApi(formData);
        if (res.errorCode === 0) {
            if (res.data) {
                localStorage.setItem('#token', res.data.token);
                localStorage.setItem('#accountId', res.data.accountId);
                localStorage.setItem('#LoginInfo', JSON.stringify(res.data));
            }
            // message.success('欢迎登录，' + res.data.userName);
            showNotify({ type: 'success', message: '欢迎登录，' + res.data.userName });
            const redirectUrl = window.location.href.split('redirectUrl=')[1];

            if (redirectUrl) {
                window.location = decodeURIComponent(redirectUrl);
            } else {
                router.push('/home');
            }
            checkPasswordReminder();
        } else {
            showNotify({ type: 'danger', message: res.errorMessage });
        }
    } catch (error) {
        showNotify({ type: 'danger', message: '登录API调用失败' });
    } finally {
        loading.value = false;
    }
};

const checkPasswordReminder = async () => {
    try {
        const res = await queryRemindTime();
        if (res.errorCode === 0) {
            if (res.data === true) {
                setTimeout(() => {
                    showNotify({ type: 'danger', message: res.errorMessage });
                }, 4000);
            }
        } else {
            showNotify({ type: 'danger', message: res.errorMessage });
        }
    } catch (error) {
        console.error('提示修改密码Api调用失败', error);
        showNotify({ type: 'danger', message: '提示修改密码Api调用失败' });
    }
};

const handleFingerprintLogin = () => {
    const isOut = localStorage.getItem('isOut') === 'true';
    if (isOut) {
        router.push({ path: '/fingerprint-login', query: { isOut: 'true' } });
    } else {
        router.push('/fingerprint-login');
    }
};

// 忘记密码处理函数
const handleForgotPassword = () => {
    showNotify({ type: 'danger', message: '请联系管理员重置密码' });
};
</script>

<style scoped>
/* 登录页面 - 深色科技风格 */
.login-page {
  width: 100%;
  min-height: 100vh;
  position: relative;
  font-family: 'Microsoft YaHei', sans-serif;
  color: #fff;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

/* 背景层 */
.bg-layer {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: #041c3d;
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  z-index: 0;
}

/* 背景动画网格 */
.login-page::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-image:
    linear-gradient(rgba(30, 144, 255, 0.03) 1px, transparent 1px),
    linear-gradient(90deg, rgba(30, 144, 255, 0.03) 1px, transparent 1px);
  background-size: 50px 50px;
  z-index: 1;
  pointer-events: none;
}

/* 主内容区域 */
.login-content {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding-top: 40px;
  padding-bottom: 40px;
  padding-left: 20px;
  padding-right: 20px;
  position: relative;
  z-index: 10;
}

/* 左侧信息区 */
.login-left {
  flex: 1;
  max-width: 500px;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 40px;
  padding-right: 40px;
  color: white;
}

.system-info {
  animation: fadeInLeft 0.8s ease-out;
}

.system-title {
  font-size: 42px;
  font-weight: 700;
  margin-bottom: 16px;
  line-height: 1.2;
  color: #fff;
  text-shadow: 0 0 20px rgba(30, 144, 255, 0.5);
}

.system-subtitle {
  font-size: 18px;
  opacity: 0.9;
  margin-bottom: 40px;
  font-weight: 300;
  color: #7ec8ff;
}

.feature-list {
  display: flex;
  flex-direction: column;
}

.feature-list>* {
  margin-bottom: 20px;
}

.feature-list>*:last-child {
  margin-bottom: 0;
}

.feature-item {
  display: flex;
  align-items: center;
  font-size: 16px;
  opacity: 0.95;
}

.feature-item>* {
  margin-right: 12px;
}

.feature-item>*:last-child {
  margin-right: 0;
}

.feature-item i {
  font-size: 20px;
  color: #00bfff;
}

/* 右侧登录表单 */
.login-right {
  flex: 0 0 450px;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 20px;
  padding-right: 20px;
}

.login-form-wrapper {
  background: linear-gradient(135deg, rgba(9, 45, 82, 0.95) 0%, rgba(5, 30, 60, 0.95) 100%);
  border-radius: 16px;
  padding-top: 48px;
  padding-bottom: 48px;
  padding-left: 40px;
  padding-right: 40px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5), 0 0 30px rgba(30, 144, 255, 0.2);
  animation: fadeInRight 0.8s ease-out;
  border: 1px solid rgba(30, 144, 255, 0.3);
}

.form-header {
  text-align: center;
  margin-bottom: 32px;
}

.form-icon {
  width: 80px;
  height: 80px;
  margin: 0 auto 16px;
  background: linear-gradient(135deg, #1e90ff 0%, #00bfff 100%);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 0 30px rgba(30, 144, 255, 0.4);
}

.form-icon i {
  font-size: 40px;
  color: #fff;
}

.form-title {
  font-size: 28px;
  font-weight: 700;
  color: #fff;
  margin-bottom: 8px;
}

.form-subtitle {
  font-size: 14px;
  color: #7ec8ff;
}

.form-content {
  display: flex;
  flex-direction: column;
}

.form-content>* {
  margin-bottom: 20px;
}

.form-content>*:last-child {
  margin-bottom: 0;
}

.form-item {
  margin-bottom: 1.5rem;
}

.input-label {
  display: block;
  font-size: 14px;
  color: #7ec8ff;
  margin-bottom: 8px;
  font-weight: 500;
}

.input-wrapper {
  position: relative;
  display: flex;
  align-items: center;
  background: rgba(4, 28, 61, 0.8);
  border: 1px solid rgba(30, 144, 255, 0.3);
  border-radius: 8px;
  padding: 12px 16px;
  transition: all 0.3s ease;
}

.input-wrapper:focus-within {
  border-color: #1e90ff;
  box-shadow: 0 0 15px rgba(30, 144, 255, 0.3);
}

.input-icon {
  color: #00bfff;
  font-size: 16px;
  margin-right: 12px;
}

.dark-input {
  flex: 1;
  border: none;
  outline: none;
  background: transparent;
  font-size: 15px;
  color: #fff;
}

.dark-input::placeholder {
  color: rgba(126, 200, 255, 0.5);
}

.password-toggle {
  color: #7ec8ff;
  cursor: pointer;
  transition: color 0.3s;
  margin-left: 8px;
}

.password-toggle:hover {
  color: #00bfff;
}

/* 登录按钮 */
.login-btn {
  width: 100%;
  height: 48px;
  font-size: 16px;
  font-weight: 600;
  background: linear-gradient(90deg, #1e90ff 0%, #00bfff 100%);
  border: none;
  border-radius: 8px;
  transition: all 0.3s ease;
  color: white;
  cursor: pointer;
  margin-top: 8px;
}

.login-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 20px rgba(30, 144, 255, 0.5);
}

.login-btn:disabled {
  opacity: 0.7;
  cursor: not-allowed;
  transform: none;
}

/* 指纹登录按钮 */
.fingerprint-btn {
  width: 100%;
  height: 48px;
  font-size: 16px;
  font-weight: 500;
  background: transparent;
  border: 1px solid rgba(30, 144, 255, 0.5);
  border-radius: 8px;
  transition: all 0.3s ease;
  color: #7ec8ff;
  cursor: pointer;
  margin-top: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.fingerprint-btn:hover {
  background: rgba(30, 144, 255, 0.1);
  border-color: #1e90ff;
  color: #00bfff;
}

.fingerprint-btn i {
  font-size: 20px;
}

.form-footer {
  text-align: center;
  margin-top: 8px;
}

.forgot-link {
  color: #7ec8ff;
  font-size: 14px;
  text-decoration: none;
  transition: color 0.3s ease;
}

.forgot-link:hover {
  color: #00bfff;
  text-decoration: underline;
}

/* 底部版权 */
.login-footer {
  text-align: center;
  padding: 20px;
  color: white;
  font-size: 14px;
  opacity: 0.8;
  position: relative;
  z-index: 1;
}

/* 动画效果 */
@keyframes fadeInLeft {
  from {
    opacity: 0;
    transform: translateX(-30px);
  }

  to {
    opacity: 1;
    transform: translateX(0);
  }
}

@keyframes fadeInRight {
  from {
    opacity: 0;
    transform: translateX(30px);
  }

  to {
    opacity: 1;
    transform: translateX(0);
  }
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .login-left {
    display: none;
  }

  .login-right {
    flex: 0 0 100%;
    max-width: 450px;
  }
}

@media (max-width: 640px) {
  .login-form-wrapper {
    padding: 32px 24px;
    background: linear-gradient(135deg, rgba(9, 45, 82, 0.98) 0%, rgba(5, 30, 60, 0.98) 100%);
  }

  .form-title {
    font-size: 24px;
  }

  .login-right {
    padding: 0 16px;
  }

  .form-icon {
    width: 60px;
    height: 60px;
  }

  .form-icon i {
    font-size: 30px;
  }
}

/* 特性列表文字颜色 */
.feature-item span {
  color: rgba(255, 255, 255, 0.9);
}
</style>
