<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { Message, Phone, ChatLineRound, Loading } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import ParticleBackground from '../components/ParticleBackground.vue'
import request from '../utils/request'
import { GITCC_LOGO_URL, PRODUCT_TITLE, PRODUCT_TAGLINE } from '../shellConstants'

const router = useRouter()
const adminInfo = ref(null)
const loading = ref(false)

// 获取管理员信息
const getAdminInfo = async () => {
  try {
    loading.value = true

    const data = await request.get('/user/admin_info')
    console.info('【随机获取管理员信息响应数据】', data)
    
    if (data && data.admin_info) {
      adminInfo.value = data.admin_info
    }
  } catch (error) {
    console.error('【随机获取管理员信息错误】', error)
    ElMessage.error({
      message: '【随机获取管理员信息错误】' + (error?.message || '请稍后重试'),
      duration: 5000,
    })
  } finally {
    loading.value = false
  }
}

// 返回登录页面
const goToLogin = () => {
  router.push('/')
}

// 组件挂载时获取管理员信息
onMounted(() => {
  getAdminInfo()
})
</script>

<template>
  <div class="hud-auth">
    <aside class="hud-auth-brand" aria-labelledby="forgot-brand-title">
      <div class="hud-auth-brand-inner">
        <img :src="GITCC_LOGO_URL" :alt="PRODUCT_TITLE" class="brand-logo" width="56" height="56" />
        <h2 id="forgot-brand-title">
          账户安全 · <span class="accent">{{ PRODUCT_TITLE }}</span>
        </h2>
        <p class="brand-lead">{{ PRODUCT_TAGLINE }}</p>
        <ul>
          <li>密码重置由管理员在业务侧完成</li>
          <li>请准备用户名或注册邮箱以便核验</li>
          <li>本页仅展示联系信息，不提交新密码</li>
        </ul>
      </div>
    </aside>

    <div class="hud-auth-panel">
      <div class="hud-auth-particles" aria-hidden="true">
        <ParticleBackground />
      </div>

      <div class="hud-auth-form-card">
        <div class="form-head">
          <h1 class="form-title">忘记密码</h1>
          <p class="form-sub">通过系统登记的管理员渠道申请重置</p>
        </div>

        <div class="contact-info">
          <div class="info-icon">
            <el-icon :size="56">
              <ChatLineRound />
            </el-icon>
          </div>
          <p class="info-title">请联系管理员重置密码</p>
          <p class="info-desc">如需重置密码，请通过以下方式联系系统管理员：</p>

          <div v-if="loading" class="loading-container">
            <el-icon class="is-loading" :size="24">
              <Loading />
            </el-icon>
            <span>正在加载管理员信息...</span>
          </div>

          <div v-else-if="!adminInfo" class="no-admin-info">
            <p>暂无可用的管理员联系信息，请稍后再试</p>
          </div>

          <div v-else class="admin-contact-group">
            <div class="admin-title">
              {{ adminInfo.role === 'ADMIN' ? '管理员' : '开发人员' }}: {{ adminInfo.last_name }}{{ adminInfo.first_name }}
            </div>

            <div v-if="adminInfo.email" class="contact-item">
              <el-icon>
                <Message />
              </el-icon>
              <span>邮箱：{{ adminInfo.email }}</span>
            </div>

            <div v-if="adminInfo.phone" class="contact-item">
              <el-icon>
                <Phone />
              </el-icon>
              <span>联系电话：{{ adminInfo.phone }}</span>
            </div>
          </div>

          <p class="contact-note">联系时请提供您的用户名/注册邮箱，以便管理员核实您的身份</p>

          <el-button type="primary" class="back-btn" @click="goToLogin">返回登录</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.brand-logo {
  border-radius: 14px;
  margin-bottom: 18px;
  box-shadow: 0 0 0 1px rgba(79, 70, 229, 0.35);
}
.brand-lead {
  margin: 0 0 8px;
  font-size: 0.95rem;
  color: var(--muted);
  line-height: 1.55;
  max-width: 36ch;
}
.hud-auth-particles {
  position: absolute;
  inset: 0;
  z-index: 0;
  pointer-events: none;
  overflow: hidden;
}
.hud-auth-particles :deep(canvas) {
  pointer-events: none;
}
.form-head {
  margin-bottom: 18px;
}
.form-title {
  margin: 0;
  font-size: 1.35rem;
  font-weight: 800;
  letter-spacing: -0.02em;
  color: var(--foreground);
}
.form-sub {
  margin: 6px 0 0;
  font-size: 0.88rem;
  color: var(--muted);
}
.contact-info {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  color: var(--foreground);
}
.info-icon {
  margin-bottom: 14px;
  color: color-mix(in srgb, var(--primary) 75%, var(--foreground));
}
.info-title {
  font-size: 1.05rem;
  font-weight: 600;
  margin-bottom: 10px;
}
.info-desc {
  font-size: 0.88rem;
  margin-bottom: 18px;
  color: var(--muted);
  max-width: 42ch;
  line-height: 1.5;
}
.contact-item {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
  background: color-mix(in srgb, var(--foreground) 4%, transparent);
  padding: 12px 16px;
  border-radius: 10px;
  width: 100%;
  max-width: 400px;
  border: 1px solid var(--border);
  transition: box-shadow 0.2s, background 0.2s;
}
.contact-item:hover {
  background: color-mix(in srgb, var(--primary) 6%, transparent);
  box-shadow: 0 0 0 1px color-mix(in srgb, var(--primary) 22%, transparent);
}
.contact-item .el-icon {
  margin-right: 10px;
  color: var(--muted);
}
.contact-note {
  font-size: 0.82rem;
  margin: 14px 0 18px;
  color: var(--muted);
  font-style: italic;
  max-width: 44ch;
  line-height: 1.5;
}
.back-btn {
  width: 100%;
  max-width: 320px;
  height: 42px;
  font-weight: 600;
  border-radius: 10px;
  background: linear-gradient(180deg, color-mix(in srgb, var(--primary) 100%, #000), var(--primary));
  border-color: color-mix(in srgb, var(--primary) 85%, #000);
  box-shadow: 0 8px 22px color-mix(in srgb, var(--primary) 28%, transparent);
}
.back-btn:active {
  transform: translateY(1px) scale(0.98);
  filter: brightness(0.96);
}
.loading-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin: 16px 0;
  color: var(--muted);
}
.loading-container .el-icon {
  margin-bottom: 10px;
}
.no-admin-info {
  background: color-mix(in srgb, var(--foreground) 4%, transparent);
  padding: 14px;
  border-radius: 10px;
  margin: 12px 0;
  width: 100%;
  max-width: 400px;
  text-align: center;
  color: var(--muted);
  border: 1px dashed var(--border);
}
.admin-contact-group {
  width: 100%;
  max-width: 400px;
  margin-bottom: 8px;
  background: color-mix(in srgb, var(--foreground) 3%, transparent);
  border-radius: 12px;
  padding: 14px 16px;
  border: 1px solid var(--border);
}
.admin-title {
  font-weight: 600;
  margin-bottom: 10px;
  color: var(--foreground);
  font-size: 0.95rem;
  text-align: left;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--border);
}
@media (prefers-reduced-motion: reduce) {
  .back-btn:active {
    transform: none;
  }
  .contact-item:hover {
    box-shadow: none;
  }
}
</style>