<script setup lang="ts">
import planetBg from '@/assets/img/planetBg.png'
import { NConfigProvider, NImage, NMessageProvider } from 'naive-ui'
import { onMounted, ref, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'

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

// 从 history state 中获取参数
const params = ref({
  communityId: '',
  tenantId: '',
})

onMounted(() => {
  const state = history.state
  if (state && state.communityId && state.tenantId) {
    params.value = {
      communityId: state.communityId,
      tenantId: state.tenantId,
    }
  }
  else {
    // 如果没有 state，回退到 URL 参数
    params.value = {
      communityId: route.query.communityId as string,
      tenantId: route.query.tenantId as string,
    }
  }
  getCommunityDetail()
})

const userStore = useUserStore()
definePageMeta({
  layout: 'planet',
})

// 获取用户点赞/粉丝/关注数量
interface SelectUserInfo {
  likeCount: number
  bean: number
  download: number
  attention: number
}
const selectUserInfo = ref<SelectUserInfo>({
  likeCount: 0,
  bean: 0,
  download: 0,
  attention: 0,
})

const communityDetail = ref({})
async function getCommunityDetail() {
  try {
    const res = await request.get(`/community/detail?communityId=${params.value.communityId}&tenantId=${params.value.tenantId}`)
    if (res.code === 200) {
      communityDetail.value = res.data
    }
  }
  catch (error) {
    console.error(error)
  }
}

// 获取关注
async function getAttention() {
  try {
    const res = await request.get('/attention/selectUserInfo')
    if (res.code === 200) {
      selectUserInfo.value = res.data
    }
  }
  catch (err) {
    console.error(err)
  }
}
getAttention()
</script>

<template>
  <div class="min-h-screen bg-[#F7F8FA]">
    <div class="relative">
      <!-- 背景图片区域 -->
      <div class="h-[300px] w-full bg-[#E8F3FF]">
        <img :src="planetBg" alt="背景图片" class="w-full h-full object-cover">
      </div>

      <!-- 主要内容区域 -->
      <div class="absolute inset-x-0 top-[80px]">
        <div class="max-w-[1140px] mx-auto">
          <!-- 用户基本信息 -->
          <client-only>
            <div class="flex items-center gap-4 mb-6">
              <img :src="communityDetail.imageUrl" class="w-[224px] h-[224px] rounded" alt="头像">
              <div class="flex flex-col justify-between h-[224px] w-full">
                <div class="flex flex-col gap-3">
                  <div class="text-[20px] font-medium text-[#192029]">
                    {{ communityDetail.communityName }}
                  </div>
                  <div class="text-[14px] text-[#878d95]">
                    最新更新时间: {{ communityDetail.lastUpdateTime }}
                  </div>
                  <div class="text-[14px] flex gap-8">
                    <div class="flex items-center gap-3">
                      <img :src="userStore.userInfo?.avatar" class="w-[20px] h-[20px] rounded-full" alt="头像">
                      <div>
                        成员数量: {{ communityDetail.userNum }}
                      </div>
                    </div>
                    <div>
                      累计更新内容: {{ communityDetail.updateNum }}
                    </div>
                    <div>
                      星球创建天数: {{ communityDetail.createDay }}
                    </div>
                  </div>
                </div>

                <div class="flex justify-between p-2 rounded bg-white  items-center">
                  <div class="flex items-center gap-2 text-[#fc4141]">
                    <span class="font-bold text-[28px]">{{ communityDetail.price ? communityDetail.price : '免费' }}</span>  <span v-if="communityDetail.price" class="text-[16px]">积分</span>
                  </div>
                  <div
                    class="text-white text-[14px] flex items-center justify-center cursor-pointer w-[128px] h-[40px] bg-[linear-gradient(207deg,#3BEDFF_0%,#328AFE_100%)] rounded-lg"
                  >
                    立即加入
                  </div>
                </div>
              </div>
            </div>
          </client-only>
        </div>
      </div>

      <div class="max-w-[1140px] mx-auto">
        <div class="text-[20px] py-4">
          星球概况
        </div>
        <div class="flex gap-4">
          <div class="w-[100px] h-[100px] rounded">
            <!-- <img src="@/assets/img/planet-overview.png" alt="星球概况"> -->
          </div>
          <div class="flex flex-col justify-between">
            <div class="flex gap-4">
              <div>
                name
              </div>
              <div>
                关注
              </div>
            </div>
            <div class="flex gap-4">
              <div>1123粉丝</div>
              <div>1123关注</div>
              <div>1123获赞</div>
            </div>
          </div>
        </div>
        <div />
      </div>
    </div>
  </div>
</template>
