<template>
  <div class="login-wrapper">
    <div class="decorations">
      <span class="dot"></span>
      <span class="dot"></span>
      <span class="dot"></span>
      <span class="dot"></span>
      <span class="dot"></span>
      <span class="dot"></span>
      <span class="dot"></span>
      <span class="dot"></span>
      <span class="dot"></span>
      <span class="dot"></span>
      <span class="dot"></span>
      <span class="dot"></span>
      <span class="line line-1"></span>
      <span class="line line-2"></span>
      <span class="line line-3"></span>
    </div>
    <div class="login-main">
      <!-- 研究所标识 -->
      <div class="institute-logo">
        <img src="/resource/img/institute_logo.png" alt="七〇四研究所" />
      </div>

      <!-- 系统标题 -->
      <div class="system-title">
        <img class="system-logo" src="/resource/img/logo.png" alt="Logo" />
        <span class="system-name">事项督办系统</span>
      </div>

      <!-- 单点登录卡片 -->
      <div class="login-card">
        <template v-if="loading">
          <div class="loading-spinner"></div>
          <div class="card-title">正在单点登录</div>
          <div class="card-desc">正在验证身份，请稍候...</div>
        </template>
        <template v-else>
          <div class="card-icon error-icon">!</div>
          <div class="card-title">登录失败</div>
          <div class="card-desc">{{ errorMsg }}</div>
          <button class="retry-btn" @click="handleRetry">重试</button>
        </template>
      </div>
    </div>

    <!-- 底部版权 -->
    <div class="footer-text">
      中国船舶集团有限公司第七〇四研究所 数智化中心&copy;2026
    </div>
  </div>
</template>

<script lang="ts" setup>
  import { ref, onMounted } from 'vue';
  import { useRouter } from 'vue-router';
  import { useUserStore } from '/@/store/modules/user';

  const router = useRouter();
  const userStore = useUserStore();
  const loading = ref(true);
  const errorMsg = ref('');

  async function doAutoLogin() {
    loading.value = true;
    errorMsg.value = '';
    try {
      await userStore.autoLogin(true);
    } catch (error: any) {
      loading.value = false;
      errorMsg.value = error?.message || '单点登录失败，请稍后重试';
    }
  }

  function handleRetry() {
    doAutoLogin();
  }


  onMounted(() => {
    doAutoLogin();
  });
</script>

<style lang="less" scoped>
  .login-wrapper {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    min-height: 100vh;
    background: #fff;
    overflow: hidden;

    &::before,
    &::after {
      content: '';
      position: absolute;
      border-radius: 50%;
      opacity: 0.6;
      pointer-events: none;
    }

    &::before {
      width: 600px;
      height: 600px;
      bottom: -280px;
      left: -120px;
      background: radial-gradient(circle, rgba(59, 130, 246, 0.12), transparent 70%);
    }

    &::after {
      width: 500px;
      height: 500px;
      bottom: -200px;
      right: -100px;
      background: radial-gradient(circle, rgba(16, 185, 129, 0.10), transparent 70%);
    }
  }

  .decorations {
    position: absolute;
    inset: 0;
    pointer-events: none;
    z-index: 0;

    .dot {
      position: absolute;
      border-radius: 50%;
      opacity: 0.55;
    }

    .dot:nth-child(1) { width: 18px; height: 18px; bottom: 22%; left: 12%; background: #3b82f6; opacity: 0.35; }
    .dot:nth-child(2) { width: 12px; height: 12px; bottom: 32%; right: 15%; background: #f59e0b; opacity: 0.45; }
    .dot:nth-child(3) { width: 24px; height: 24px; bottom: 15%; left: 30%; background: #10b981; opacity: 0.3; }
    .dot:nth-child(4) { width: 10px; height: 10px; bottom: 28%; right: 28%; background: #8b5cf6; opacity: 0.5; }
    .dot:nth-child(5) { width: 16px; height: 16px; bottom: 18%; left: 55%; background: #ec4899; opacity: 0.35; }
    .dot:nth-child(6) { width: 20px; height: 20px; bottom: 10%; right: 10%; background: #06b6d4; opacity: 0.4; }
    .dot:nth-child(7) { width: 8px; height: 8px; bottom: 35%; left: 20%; background: #f97316; opacity: 0.5; }
    .dot:nth-child(8) { width: 14px; height: 14px; bottom: 8%; left: 45%; background: #6366f1; opacity: 0.4; }
    .dot:nth-child(9) { width: 20px; height: 20px; top: 8%; left: 10%; background: #3b82f6; opacity: 0.25; }
    .dot:nth-child(10) { width: 10px; height: 10px; top: 14%; right: 18%; background: #ec4899; opacity: 0.35; }
    .dot:nth-child(11) { width: 16px; height: 16px; top: 5%; right: 8%; background: #10b981; opacity: 0.3; }
    .dot:nth-child(12) { width: 8px; height: 8px; top: 18%; left: 25%; background: #f59e0b; opacity: 0.4; }

    .line {
      position: absolute;
      pointer-events: none;
      border-radius: 2px;
      opacity: 0.15;
    }

    .line-1 { top: 12%; right: 5%; width: 120px; height: 2px; background: #3b82f6; transform: rotate(-15deg); }
    .line-2 { top: 20%; left: 8%; width: 80px; height: 2px; background: #ec4899; transform: rotate(10deg); }
    .line-3 { top: 6%; right: 15%; width: 60px; height: 2px; background: #10b981; transform: rotate(-8deg); }
  }

  .login-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 40px 24px;
    z-index: 1;
  }

  .institute-logo {
    margin-bottom: 48px;

    img {
      width: 400px;
      height: auto;
      display: block;
    }
  }

  .system-title {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 48px;

    .system-logo {
      width: 72px;
      height: 72px;
    }

    .system-name {
      font-size: 28px;
      font-weight: 600;
      color: #1a1a1a;
      letter-spacing: 2px;
    }
  }

  .login-card {
    width: 100%;
    max-width: 360px;
    padding: 40px 32px;
    background: #fafbfc;
    border-radius: 12px;
    text-align: center;
  }

  .loading-spinner {
    width: 40px;
    height: 40px;
    margin: 0 auto 20px;
    border: 3px solid #e5e7eb;
    border-top-color: #1a6dd4;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
  }

  @keyframes spin {
    to { transform: rotate(360deg); }
  }

  .card-icon {
    width: 48px;
    height: 48px;
    margin: 0 auto 16px;
    border-radius: 50%;
    font-size: 24px;
    font-weight: 700;
    line-height: 48px;
  }

  .error-icon {
    background: #fee2e2;
    color: #ef4444;
  }

  .card-title {
    font-size: 18px;
    font-weight: 600;
    color: #1a1a1a;
    margin-bottom: 8px;
  }

  .card-desc {
    font-size: 14px;
    color: rgba(0, 0, 0, 0.45);
    margin-bottom: 24px;
  }

  .retry-btn {
    width: 100%;
    height: 44px;
    border: none;
    border-radius: 8px;
    background: #1a6dd4;
    color: #fff;
    font-size: 15px;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.2s;
    margin-bottom: 12px;

    &:hover {
      background: #155ab0;
    }
  }

  .login-link {
    display: inline-block;
    font-size: 14px;
    color: #1a6dd4;
    cursor: pointer;

    &:hover {
      color: #155ab0;
      text-decoration: underline;
    }
  }

  .footer-text {
    position: relative;
    z-index: 1;
    text-align: center;
    font-size: 13px;
    color: rgba(0, 0, 0, 0.65);
    padding: 16px 0;
  }
</style>
