<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 message = useMessage()
const route = useRoute()
const router = useRouter()

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

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

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

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)
  }
}

// 获取用户信息
const userInfo = ref({})
async function getUserInfo() {
  try {
    const res = await request.get(
      `/system/user/selectUserById?id=${params.value.userId}`,
    )
    if (res.code === 200) {
      userInfo.value = res.data
    }
  }
  catch (error) {
    console.log(error)
  }
}

// 获取关注粉丝点赞
const selectUserInfo = ref({})
async function getAttention() {
  try {
    const res = await request.get(`/attention/selectUserInfo?userId=${params.value.userId}`)
    if (res.code === 200) {
      selectUserInfo.value = res.data
    }
  }
  catch (err) {
    console.log(err)
  }
}

async function handleJoin() {
  try {
    const data = {
      communityId: params.value.communityId,
      tenantId: params.value.tenantId,
    }
    const res = await request.post(`/community/join`, data)
    if (res.code === 200) {
      message.success('加入成功')
      setTimeout(() => {
        router.push(`/planet-detail?communityId=${params.value.communityId}&tenantId=${params.value.tenantId}`)
      }, 1000)
    }
  }
  catch (err) {
    console.log(err)
  }
}
// 获取关注
// 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-[30px]">
        <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 v-for="(item, index) in communityDetail.avatarList" :key="index" :src="item" 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"
                    @click="handleJoin"
                  >
                    立即加入
                  </div>
                </div>
              </div>
            </div>
          </client-only>
          <div class="max-w-[1140px] mx-auto bg-white rounded-lg p-4">
            <div class="text-[20px] pb-4">
              星球概况
            </div>
            <div class="flex gap-4 bg-[#F9FBFF] rounded-lg p-2">
              <div class="w-[80px] h-[80px] rounded">
                <img class="rounded-full" :src="userInfo.avatar" alt="星球概况">
              </div>
              <div class="flex flex-col justify-between py-2">
                <div class="flex gap-4">
                  <div class="text-[20px] font-medium text-[#192029]">
                    {{ userInfo.nickName }}
                  </div>
                  <!-- <div>
                关注
              </div> -->
                </div>
                <div class="flex gap-4">
                  <div>{{ selectUserInfo.bean }}粉丝</div>
                  <div>{{ selectUserInfo.attention }}关注</div>
                  <div>{{ Number(selectUserInfo.imageLikeNum) + Number(selectUserInfo.modelLikeNum) }}获赞</div>
                </div>
              </div>
            </div>
            <div class="text-[16px] text-[#4a5563] my-5">
              {{ communityDetail.description }}
            </div>
            <div>
              <div class="font-bold text-[20px]">
                付费通知
              </div>
              <div class="bg-[#F9FBFF] rounded-lg p-4 mt-2">
                <div>1、付费后，你可以使用当前付款的帐号在有效期内查看、参与内容互动，向星主、群成员提问。 </div>
                <div>2、本星球由星主自行创建，加入前请确认风险，平台不提供相关保证。若发现违反法律法规的星球，请勿加入，并投诉。</div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
