<template>
  <div
    class="relative min-h-screen overflow-hidden bg-black bg-cover bg-center bg-no-repeat pb-[calc(4rem+env(safe-area-inset-bottom))] font-sans text-white md:flex md:flex-col"
  >
    <!-- Full Width Header -->
    <div class="h-[60px] md:h-[125px]">
      <div class="fixed top-[20px] z-50 w-full md:top-[45px]">
        <div class="container">
          <header
            class="lucid-glass-bar flex h-[40px] items-center justify-between rounded-[90px] pr-[5px] pl-5 transition-all duration-300 md:h-[60px] md:pr-[10px]"
          >
            <router-link to="/" class="flex items-center gap-2">
              <!-- Desktop Logo -->
              <Logo alt="Hi快VPN" class="h-[18px] w-auto md:ml-8 md:h-[29px]" />
              <span class="ml-3 text-2xl font-black">高能合伙人</span>
            </router-link>
            <div v-if="isLoggedIn" class="flex items-center">
              <router-link
                to="/user-center"
                class="flex size-[30px] items-center justify-center rounded-full bg-[#78788029] text-xl font-bold text-white shadow-lg transition hover:scale-105 md:size-[40px] md:text-3xl"
              >
                {{ userLetter }}
              </router-link>
            </div>
            <button
              v-else
              @click="openLoginModal"
              class="flex h-[30px] cursor-pointer items-center justify-center rounded-full bg-[#78788029] px-6 text-sm font-bold backdrop-blur-md transition hover:brightness-110 md:h-[40px] md:w-[220px] md:text-xl"
            >
              代理后台登录/注册
            </button>
          </header>
        </div>
      </div>
    </div>

    <!-- Main Content Container -->
    <div class="container mx-auto flex flex-col">
      <main class="pt-10">
        <!--    module1    -->
        <div class="mb-[80px] flex">
          <div class="flex flex-col items-center">
            <div class="mb-[20px] ml-[42px] md:ml-[17px]">
              <h2 class="mb-2 text-2xl font-black md:text-8xl">
                <Logo class="h-[34px] md:h-[43px]" />
              </h2>
              <p class="font-600 text-3xl">网在我在, 网快我快</p>
            </div>
            <img src="./Group%20133.png" alt="" class="mt-[52px] h-[287px] w-[182px]" />
          </div>
          <!-- video           -->
        </div>
        <!-- modules2       -->
        <div class="mb-[80px] w-full rounded-[40px] bg-[#ADFF5B] p-8 pb-2 text-black">
          <div class="text-center text-4xl font-black">“超强产品力，全面秒杀同级app”</div>
          <div class="mt-2 text-center">为高能合伙人提供强力的的产品背书，每个点拿出去都能打。</div>
          <div class="el-style-scrollbar mt-8 flex gap-2 overflow-x-auto pb-4">
            <img
              :src="image"
              alt=""
              v-for="(image, index) in modules2Image"
              class="h-[432px] w-[200px] shrink-0 rounded-[30px] md:h-[600px] md:w-[277.5px]"
              :key="index"
            />
          </div>
        </div>
        <!-- modules3       -->
        <div class="mb-[40px] w-full rounded-[40px] pb-2 text-black">
          <div class="text-center text-4xl font-black">
            <Group215Icon class="mx-auto" />
          </div>
          <div class="text-center text-[#999999]">给您的客户提供专业视角的建议</div>
          <div class="mt-[20px] flex justify-center">
            <div class="size-[600px] rounded-[40px] border-8 border-[#ADFF5B]"></div>
          </div>
        </div>
      </main>
    </div>
    <div class="container flex flex-col items-center justify-center">
      <div class="mb-1 h-[20px] w-[352px] md:ml-[17px]">
        <img src="./image-1.png" alt="image" />
      </div>
      <div class="text-center text-[10px] leading-[14px] font-[300] md:ml-[17px] md:text-left">
        <span class="font-[600]">Hi快VPN™</span> &copy; All rights reserved.<br />
        <router-link to="/terms" class="underline">Terms of Service</router-link>
        <router-link to="/privacy" class="ml-2 underline">Privacy Policy</router-link>
      </div>
    </div>
    <LoginFormModal ref="loginModalRef" />
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, watch, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useLocalStorage } from '@vueuse/core'
import LoginFormModal from './components/LoginFormModal.vue'
import Logo from './logo.svg?component'
import Group215Icon from './modules3/Group 215.svg?component'
import request from '@/utils/request'
import Frame98 from './modules2/Frame 98.png'
import Frame99 from './modules2/Frame 99.png'
import Frame100 from './modules2/Frame 100.png'
import Frame101 from './modules2/Frame 101.png'
import Frame104 from './modules2/Frame 104.png'
import Frame105 from './modules2/Frame 105.png'

const modules2Image = [Frame98, Frame99, Frame100, Frame101, Frame104, Frame105]

const route = useRoute()
const router = useRouter()
const token = useLocalStorage('Authorization', '')
const userEmail = useLocalStorage('UserEmail', '')

const isLoggedIn = computed(() => !!token.value)
const userLetter = computed(() => {
  if (!userEmail.value) return '?'
  return userEmail.value.charAt(0).toUpperCase()
})

const fetchUserInfo = async () => {
  if (!token.value) return

  try {
    const res = (await request.get('/api/v1/public/user/info')) as any
    const emailInfo = res.auth_methods?.find((item: any) => item.auth_type === 'email')
    if (emailInfo) {
      userEmail.value = emailInfo.auth_identifier
    }
  } catch (error: any) {
    console.error('Failed to fetch user info:', error)
    if (error?.code === 401 || error?.status === 401) {
      token.value = ''
      userEmail.value = ''
    }
  }
}

onMounted(() => {
  fetchUserInfo()
  if (route.query.login === 'true') {
    openLoginModal()
    router.replace({ query: { ...route.query, login: undefined } })
  }
})

watch(
  () => route.query.login,
  (newVal) => {
    if (newVal === 'true') {
      openLoginModal()
      router.replace({ query: { ...route.query, login: undefined } })
    }
  },
)

const loginModalRef = ref<InstanceType<typeof LoginFormModal> | null>(null)

const openLoginModal = () => {
  loginModalRef.value?.show()
}
</script>

<style scoped></style>
